FirebaseSessionsTests+BaseBehaviors.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import XCTest
  16. #if SWIFT_PACKAGE
  17. import FirebaseSessionsObjC
  18. #endif // SWIFT_PACKAGE
  19. @testable import FirebaseSessions
  20. final class FirebaseSessionsTestsBase_BaseBehaviors: FirebaseSessionsTestsBase {
  21. // MARK: - Test Settings & Sampling
  22. func test_settingsDisabled_doesNotLogSessionEventButDoesFetchSettings() {
  23. runSessionsSDK(
  24. subscriberSDKs: [
  25. mockPerformanceSubscriber,
  26. ], preSessionsInit: { _ in
  27. self.mockSettings.sessionsEnabled = false
  28. }, postSessionsInit: {
  29. sessions.register(subscriber: self.mockPerformanceSubscriber)
  30. }, postLogEvent: { result, subscriberSDKs in
  31. // Make sure we failed with the correct error
  32. self.assertFailure(result: result, expectedError: .DisabledViaSettingsError)
  33. // We must still fetch settings because otherwise we would
  34. // never fetch them again when we disabled the SDK via settings
  35. XCTAssertTrue(self.mockSettings.updateSettingsCalled)
  36. // Make sure we didn't log any events
  37. XCTAssertNil(self.mockCoordinator.loggedEvent)
  38. }
  39. )
  40. }
  41. func test_sessionSampled_doesNotLogSessionEventButDoesFetchSettings() {
  42. runSessionsSDK(
  43. subscriberSDKs: [
  44. mockPerformanceSubscriber,
  45. ], preSessionsInit: { _ in
  46. self.mockSettings.samplingRate = 0.0
  47. }, postSessionsInit: {
  48. sessions.register(subscriber: self.mockPerformanceSubscriber)
  49. }, postLogEvent: { result, subscriberSDKs in
  50. // Make sure we failed with the correct error
  51. self.assertFailure(result: result, expectedError: .SessionSamplingError)
  52. // We must still fetch settings because otherwise we could
  53. // get stuck with a sampling rate that samples all events.
  54. XCTAssertTrue(self.mockSettings.updateSettingsCalled)
  55. // Make sure we didn't log any events
  56. XCTAssertNil(self.mockCoordinator.loggedEvent)
  57. }
  58. )
  59. }
  60. // MARK: - Test Multiple Initiation
  61. // This test is failing on CI for watchOS only. I can't reproduce it locally
  62. // which may be due to the CI machine running x86 simulators. Disabling
  63. // this test for now.
  64. #if !os(watchOS)
  65. // This test ensures that if we go into the background for longer than
  66. // the Session Timeout, we log another event when we come to the foreground.
  67. //
  68. // We wanted to make sure that since we've introduced promises,
  69. // once the promise has been fulfilled, that .then'ing on the promise
  70. // in future initiations still results in a log
  71. func test_multipleInitiations_logsSessionEventEachInitiation() {
  72. var loggedCount = 0
  73. var lastLoggedSessionID = ""
  74. let loggedTwiceExpectation = expectation(description: "Sessions SDK logged events twice")
  75. runSessionsSDK(
  76. subscriberSDKs: [
  77. mockPerformanceSubscriber,
  78. ], preSessionsInit: { _ in
  79. }, postSessionsInit: {
  80. sessions.register(subscriber: self.mockPerformanceSubscriber)
  81. }, postLogEvent: { result, subscriberSDKs in
  82. // Make sure we log the event
  83. self.assertSuccess(result: result)
  84. // Make sure we logged an event and fetched settings
  85. XCTAssertTrue(self.mockSettings.updateSettingsCalled)
  86. XCTAssertNotNil(self.mockCoordinator.loggedEvent)
  87. // Make sure the Session ID changes between initiations
  88. XCTAssertNotEqual(lastLoggedSessionID, self.sessions.currentSessionDetails.sessionId)
  89. // Make sure the session ID logged to the coordinator has the
  90. // same Session ID as the currentSessionDetails passed to Subscribers
  91. assertEqualProtoString(
  92. self.mockCoordinator.loggedEvent?.proto.session_data.session_id,
  93. expected: self.sessions.currentSessionDetails.sessionId!, fieldName: "session_id"
  94. )
  95. lastLoggedSessionID = self.sessions.currentSessionDetails.sessionId!
  96. loggedCount += 1
  97. if loggedCount <= 1 {
  98. // The first time we log an event, put the app in the background,
  99. // travel forward in time loger than the Session Timeout, and
  100. // then bring the app to the foreground to generate another session.
  101. //
  102. // This postLogEvent callback will be called again after this
  103. self.postBackgroundedNotification()
  104. self.pausedClock.addTimeInterval(30 * 60 + 1)
  105. self.postForegroundedNotification()
  106. } else {
  107. loggedTwiceExpectation.fulfill()
  108. }
  109. }
  110. )
  111. wait(for: [loggedTwiceExpectation], timeout: 3)
  112. // Make sure we logged 2 events
  113. XCTAssertEqual(loggedCount, 2)
  114. }
  115. #endif // !os(watchOS)
  116. }