InitiatorTests.swift 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. import XCTest
  15. @testable import FirebaseSessions
  16. class InitiatorTests: XCTestCase {
  17. // 2021-11-01 @ 00:00:00 (EST)
  18. let date = Date(timeIntervalSince1970: 1_635_739_200)
  19. func test_beginListening_initiatesColdStart() throws {
  20. let initiator = SessionInitiator()
  21. var initiateCalled = false
  22. initiator.beginListening {
  23. initiateCalled = true
  24. }
  25. assert(initiateCalled)
  26. }
  27. func test_appForegrounded_initiatesNewSession() throws {
  28. // Given
  29. var pausedClock = date
  30. let initiator = SessionInitiator(currentTimeProvider: { pausedClock })
  31. var sessionCount = 0
  32. initiator.beginListening {
  33. sessionCount += 1
  34. }
  35. assert(sessionCount == 1)
  36. // When
  37. // Background, advance time by 30 minutes + 1 second, then foreground
  38. initiator.appBackgrounded()
  39. pausedClock.addTimeInterval(30 * 60 + 1)
  40. initiator.appForegrounded()
  41. // Then
  42. // Session count increases because time spent in background > 30 minutes
  43. assert(sessionCount == 2)
  44. // When
  45. // Background, advance time by exactly 30 minutes, then foreground
  46. initiator.appBackgrounded()
  47. pausedClock.addTimeInterval(30 * 60)
  48. initiator.appForegrounded()
  49. // Then
  50. // Session count doesn't increase because time spent in background <= 30 minutes
  51. assert(sessionCount == 2)
  52. }
  53. }