InitiatorTests.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2022 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. @testable import FirebaseSessions
  15. import XCTest
  16. class InitiatorTests: XCTestCase {
  17. // 2021-11-01 @ 00:00:00 (EST)
  18. let date = Date(timeIntervalSince1970: 1_635_739_200)
  19. let validSettings: [String: Any] = [:]
  20. var cache: SettingsCacheClient!
  21. var appInfo: MockApplicationInfo!
  22. var downloader: MockSettingsDownloader!
  23. var remoteSettings: RemoteSettings!
  24. var localOverrideSettings: LocalOverrideSettings!
  25. var sdkDefaultSettings: SDKDefaultSettings!
  26. var sessionSettings: SessionsSettings!
  27. override func setUp() {
  28. super.setUp()
  29. appInfo = MockApplicationInfo()
  30. cache = SettingsCache()
  31. cache.removeCache() // just reinstantiating cache isn't enough because of persistence
  32. downloader = MockSettingsDownloader(successResponse: validSettings)
  33. remoteSettings = RemoteSettings(appInfo: appInfo, downloader: downloader, cache: cache)
  34. remoteSettings.updateSettings(currentTime: Date())
  35. localOverrideSettings = LocalOverrideSettings()
  36. sdkDefaultSettings = SDKDefaultSettings()
  37. sessionSettings = SessionsSettings(
  38. appInfo: appInfo,
  39. installations: MockInstallationsProtocol(),
  40. sdkDefaults: sdkDefaultSettings,
  41. localOverrides: localOverrideSettings,
  42. remoteSettings: remoteSettings
  43. )
  44. }
  45. func test_beginListening_initiatesColdStart() throws {
  46. let initiator = SessionInitiator(settings: sessionSettings)
  47. var initiateCalled = false
  48. initiator.beginListening {
  49. initiateCalled = true
  50. }
  51. XCTAssert(initiateCalled)
  52. }
  53. func test_appForegrounded_initiatesNewSession() async throws {
  54. // Given
  55. var pausedClock = date
  56. let initiator = SessionInitiator(
  57. settings: sessionSettings,
  58. currentTimeProvider: { pausedClock }
  59. )
  60. var sessionCount = 0
  61. initiator.beginListening {
  62. sessionCount += 1
  63. }
  64. XCTAssert(sessionCount == 1)
  65. // When
  66. // Background, advance time by 30 minutes + 1 second, then foreground
  67. await postBackgroundedNotification()
  68. pausedClock.addTimeInterval(30 * 60 + 1)
  69. await postForegroundedNotification()
  70. // Then
  71. // Session count increases because time spent in background > 30 minutes
  72. XCTAssert(sessionCount == 2)
  73. // When
  74. // Background, advance time by exactly 30 minutes, then foreground
  75. await postBackgroundedNotification()
  76. pausedClock.addTimeInterval(30 * 60)
  77. await postForegroundedNotification()
  78. // Then
  79. // Session count doesn't increase because time spent in background <= 30 minutes
  80. XCTAssert(sessionCount == 2)
  81. }
  82. }