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