FirebaseSessionsTestsBase.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 themselves)
  58. /// - `postSessionsInit` is after Sessions has started up, but before logging any events. This
  59. /// is a good place for Subscribers to call register on the Sessions SDK
  60. /// - `postLogEvent` is called whenever an event is logged via the Sessions SDK. This is where
  61. /// most assertions will happen.
  62. func runSessionsSDK(subscriberSDKs: [SessionsSubscriber],
  63. preSessionsInit: (MockSettingsProtocol) -> Void,
  64. postSessionsInit: () -> Void,
  65. postLogEvent: @escaping (Result<Void, FirebaseSessionsError>,
  66. [SessionsSubscriber]) -> Void) {
  67. // This class is static, so we need to clear global state
  68. SessionsDependencies.dependencies.removeAll()
  69. for subscriberSDK in subscriberSDKs {
  70. SessionsDependencies.addDependency(name: subscriberSDK.sessionsSubscriberName)
  71. }
  72. // Setup an expectation so we can wait for loggedEventCallback to be called.
  73. // We need the expectation because the callback is called in the background
  74. let loggedEventExpectation = XCTestExpectation(description: "Called loggedEventCallback")
  75. mockCoordinator = MockSessionCoordinator()
  76. mockAppInfo = MockApplicationInfo()
  77. mockSettings = MockSettingsProtocol()
  78. // Allow tests to configure settings before it is used during
  79. // initialization of other classes
  80. preSessionsInit(mockSettings)
  81. generator = SessionGenerator(collectEvents: Sessions
  82. .shouldCollectEvents(settings: mockSettings))
  83. initiator = SessionInitiator(settings: mockSettings, currentTimeProvider: {
  84. self.pausedClock
  85. })
  86. sessions = Sessions(appID: testAppID,
  87. sessionGenerator: generator,
  88. coordinator: mockCoordinator,
  89. initiator: initiator,
  90. appInfo: mockAppInfo,
  91. settings: mockSettings) { result in
  92. // Provide the result for tests to test against
  93. postLogEvent(result, subscriberSDKs)
  94. // Fulfil the expectation so the test can continue
  95. loggedEventExpectation.fulfill()
  96. }
  97. // Execute test cases after Sessions is initialized. This is a good
  98. // place register Subscriber SDKs
  99. postSessionsInit()
  100. // Wait for the Sessions SDK to log the session before finishing
  101. // the test.
  102. wait(for: [loggedEventExpectation], timeout: 3)
  103. }
  104. func assertSuccess(result: Result<Void, FirebaseSessionsError>) {
  105. switch result {
  106. case .success(()): break
  107. case let .failure(error):
  108. XCTFail("Expected success but got failure with error: \(error)")
  109. }
  110. }
  111. func assertFailure(result: Result<Void, FirebaseSessionsError>,
  112. expectedError: FirebaseSessionsError) {
  113. switch result {
  114. case .success(()):
  115. XCTFail("Expected failure but got success")
  116. case let .failure(error):
  117. XCTAssertEqual(error, expectedError)
  118. }
  119. }
  120. // Do not add tests to this class because they will be run by all subclasses
  121. }