SessionCoordinatorTests.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. @testable import FirebaseSessions
  17. class SessionCoordinatorTests: XCTestCase {
  18. var installations = MockInstallationsProtocol()
  19. var time = MockTimeProvider()
  20. var fireLogger = MockGDTLogger()
  21. var appInfo = MockApplicationInfo()
  22. var coordinator: SessionCoordinator!
  23. override func setUp() {
  24. super.setUp()
  25. coordinator = SessionCoordinator(
  26. installations: installations,
  27. fireLogger: fireLogger
  28. )
  29. }
  30. override func tearDown() {
  31. installations.authTokenFinished = false
  32. installations.installationIdFinished = false
  33. }
  34. var defaultSessionInfo: SessionInfo {
  35. return SessionInfo(
  36. sessionId: "test_session_id",
  37. firstSessionId: "test_first_session_id",
  38. dispatchEvents: true,
  39. sessionIndex: 0
  40. )
  41. }
  42. func test_attemptLoggingSessionStart_logsToGDT() throws {
  43. let event = SessionStartEvent(sessionInfo: defaultSessionInfo, appInfo: appInfo, time: time)
  44. var resultSuccess = false
  45. coordinator.attemptLoggingSessionStart(event: event) { result in
  46. switch result {
  47. case .success(()):
  48. resultSuccess = true
  49. case .failure:
  50. resultSuccess = false
  51. }
  52. }
  53. // Make sure we've set the Installation ID
  54. assertEqualProtoString(
  55. event.proto.session_data.firebase_installation_id,
  56. expected: MockInstallationsProtocol.testInstallationId,
  57. fieldName: "installation_id"
  58. )
  59. XCTAssertTrue(installations.authTokenFinished)
  60. XCTAssertTrue(installations.installationIdFinished)
  61. // We should have logged successfully
  62. XCTAssertEqual(fireLogger.loggedEvent, event)
  63. XCTAssert(resultSuccess)
  64. }
  65. func test_attemptLoggingSessionStart_handlesGDTError() throws {
  66. fireLogger.result = .failure(NSError(domain: "TestError", code: -1))
  67. let event = SessionStartEvent(sessionInfo: defaultSessionInfo, appInfo: appInfo, time: time)
  68. // Start success so it must be set to false
  69. var resultSuccess = true
  70. coordinator.attemptLoggingSessionStart(event: event) { result in
  71. switch result {
  72. case .success(()):
  73. resultSuccess = true
  74. case .failure:
  75. resultSuccess = false
  76. }
  77. }
  78. XCTAssertTrue(installations.authTokenFinished)
  79. XCTAssertTrue(installations.installationIdFinished)
  80. // Make sure we've set the Installation ID
  81. assertEqualProtoString(
  82. event.proto.session_data.firebase_installation_id,
  83. expected: MockInstallationsProtocol.testInstallationId,
  84. fieldName: "installation_id"
  85. )
  86. // We should have logged the event, but with a failed result
  87. XCTAssertEqual(fireLogger.loggedEvent, event)
  88. XCTAssertFalse(resultSuccess)
  89. }
  90. func test_attemptLoggingSessionStart_handlesInstallationsError() throws {
  91. installations.result = .failure(NSError(domain: "TestInstallationsError", code: -1))
  92. let event = SessionStartEvent(sessionInfo: defaultSessionInfo, appInfo: appInfo, time: time)
  93. // Start success so it must be set to false
  94. var resultSuccess = true
  95. coordinator.attemptLoggingSessionStart(event: event) { result in
  96. switch result {
  97. case .success(()):
  98. resultSuccess = true
  99. case .failure:
  100. resultSuccess = false
  101. }
  102. }
  103. XCTAssertTrue(installations.authTokenFinished)
  104. XCTAssertTrue(installations.installationIdFinished)
  105. // We should have logged the event, but with a failed result
  106. XCTAssertNotNil(fireLogger.loggedEvent)
  107. XCTAssertFalse(resultSuccess)
  108. }
  109. func test_attemptLoggingSessionStart_handlesGDTAndInstallationsError() throws {
  110. let fireLogError = NSError(domain: "DataTransportError", code: -2)
  111. fireLogger.result = .failure(fireLogError)
  112. installations
  113. .result = .failure(FirebaseSessionsError
  114. .SessionInstallationsError(NSError(domain: "TestInstallationsError", code: -1)))
  115. let event = SessionStartEvent(sessionInfo: defaultSessionInfo, appInfo: appInfo, time: time)
  116. // Start success so it must be set to false
  117. var resultSuccess = true
  118. coordinator.attemptLoggingSessionStart(event: event) { result in
  119. switch result {
  120. case .success(()):
  121. resultSuccess = true
  122. case let .failure(err):
  123. resultSuccess = false
  124. // Result should use the FireLog error if there's an error in both
  125. // Installations and FireLog
  126. XCTAssertEqual(err, FirebaseSessionsError.DataTransportError(fireLogError))
  127. }
  128. }
  129. XCTAssertTrue(installations.authTokenFinished)
  130. XCTAssertTrue(installations.installationIdFinished)
  131. // Make sure we've set the Installation ID to empty because the FIID
  132. // fetch failed
  133. assertEqualProtoString(
  134. event.proto.session_data.firebase_installation_id,
  135. expected: "",
  136. fieldName: "installation_id"
  137. )
  138. // We should have logged the event, but with a failed result
  139. XCTAssertEqual(fireLogger.loggedEvent, event)
  140. XCTAssertFalse(resultSuccess)
  141. }
  142. }