FirebaseSessionsTestsBase.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. ///
  21. /// This is the parent class for tests that test against the Sessions internal
  22. /// API or test Sessions `init` functionality.
  23. ///
  24. /// Test cases should only go in subclasses because all tests in the parent
  25. /// class will be run by subclasses.
  26. ///
  27. class FirebaseSessionsTestsBase: XCTestCase {
  28. let testAppID = "testAppID"
  29. // Mocks
  30. var mockCoordinator: MockSessionCoordinator!
  31. var mockAppInfo: MockApplicationInfo!
  32. var mockSettings: MockSettingsProtocol!
  33. // Non-mock dependencies
  34. var initiator: SessionInitiator!
  35. var generator: SessionGenerator!
  36. // Class under test
  37. var sessions: Sessions!
  38. // Example subscribers
  39. var mockCrashlyticsSubscriber: MockSubscriber!
  40. var mockPerformanceSubscriber: MockSubscriber!
  41. var allSubscribers: [SessionsSubscriber] {
  42. return [mockCrashlyticsSubscriber, mockPerformanceSubscriber]
  43. }
  44. // Mock controllers
  45. var pausedClock = Date(timeIntervalSince1970: 1_635_739_200)
  46. override func setUp() {
  47. // Reset the subscribers between tests
  48. mockCrashlyticsSubscriber = MockSubscriber(name: SessionsSubscriberName.Crashlytics)
  49. mockPerformanceSubscriber = MockSubscriber(name: SessionsSubscriberName.Performance)
  50. }
  51. /// This function forms the basis for all tests of type `FirebaseSessionsTestsBase`. It's written
  52. /// so that you don't need to setup expectations for each test individually.
  53. ///
  54. /// It has 4 parts:
  55. /// - `subscriberSDKs` tells the test to expect the list of subscriber SDKs
  56. /// - `preSessionsInit` is before Sessions or any Subscriber SDKs start up. This is a good
  57. /// place to mock variables in any dependencies (eg. Settings, or mocking any Subscribers
  58. /// themselves)
  59. /// - `postSessionsInit` is after Sessions has started up, but before logging any events. This
  60. /// is a good place for Subscribers to call register on the Sessions SDK
  61. /// - `postLogEvent` is called whenever an event is logged via the Sessions SDK. This is where
  62. /// most assertions will happen.
  63. @MainActor func runSessionsSDK(subscriberSDKs: [SessionsSubscriber],
  64. preSessionsInit: (MockSettingsProtocol) -> Void,
  65. postSessionsInit: () -> Void,
  66. postLogEvent: @escaping @MainActor (Result<Void,
  67. FirebaseSessionsError>,
  68. [SessionsSubscriber])
  69. -> Void) {
  70. // This class is static, so we need to clear global state
  71. SessionsDependencies.removeAll()
  72. for subscriberSDK in subscriberSDKs {
  73. SessionsDependencies.addDependency(name: subscriberSDK.sessionsSubscriberName)
  74. }
  75. // Setup an expectation so we can wait for loggedEventCallback to be called.
  76. // We need the expectation because the callback is called in the background
  77. let loggedEventExpectation = XCTestExpectation(description: "Called loggedEventCallback")
  78. mockCoordinator = MockSessionCoordinator()
  79. mockAppInfo = MockApplicationInfo()
  80. mockSettings = MockSettingsProtocol()
  81. // Allow tests to configure settings before it is used during
  82. // initialization of other classes
  83. preSessionsInit(mockSettings)
  84. generator = SessionGenerator(collectEvents: Sessions
  85. .shouldCollectEvents(settings: mockSettings))
  86. initiator = SessionInitiator(settings: mockSettings, currentTimeProvider: {
  87. self.pausedClock
  88. })
  89. sessions = Sessions(appID: testAppID,
  90. sessionGenerator: generator,
  91. coordinator: mockCoordinator,
  92. initiator: initiator,
  93. appInfo: mockAppInfo,
  94. settings: mockSettings) { result in
  95. DispatchQueue.main.async {
  96. // Provide the result for tests to test against
  97. postLogEvent(result, subscriberSDKs)
  98. // Fulfil the expectation so the test can continue
  99. loggedEventExpectation.fulfill()
  100. }
  101. }
  102. // Execute test cases after Sessions is initialized. This is a good
  103. // place register Subscriber SDKs
  104. postSessionsInit()
  105. // Wait for the Sessions SDK to log the session before finishing
  106. // the test.
  107. wait(for: [loggedEventExpectation], timeout: 3)
  108. }
  109. func assertSuccess(result: Result<Void, FirebaseSessionsError>) {
  110. switch result {
  111. case .success(()): break
  112. case let .failure(error):
  113. XCTFail("Expected success but got failure with error: \(error)")
  114. }
  115. }
  116. func assertFailure(result: Result<Void, FirebaseSessionsError>,
  117. expectedError: FirebaseSessionsError) {
  118. switch result {
  119. case .success(()):
  120. XCTFail("Expected failure but got success")
  121. case let .failure(error):
  122. XCTAssertEqual(error, expectedError)
  123. }
  124. }
  125. // Do not add tests to this class because they will be run by all subclasses
  126. }