SessionCoordinatorTests.swift 4.8 KB

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