SessionStartEventTests.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 SessionStartEventTests: XCTestCase {
  18. var identifiers: MockIdentifierProvider!
  19. var time: MockTimeProvider!
  20. var appInfo: MockApplicationInfo!
  21. override func setUp() {
  22. super.setUp()
  23. identifiers = MockIdentifierProvider()
  24. time = MockTimeProvider()
  25. appInfo = MockApplicationInfo()
  26. }
  27. /// This function runs the `testCase` twice, once for the proto object stored in
  28. /// the event, and once after encoding and decoding the proto. This is useful for
  29. /// testing cases where the proto hasn't been encoded correctly.
  30. func testProtoAndDecodedProto(sessionEvent: SessionStartEvent,
  31. testCase: (firebase_appquality_sessions_SessionEvent) -> Void) {
  32. let proto = sessionEvent.proto
  33. testCase(proto)
  34. /// If you are getting failures in this test case, and not the one above, the
  35. /// problem likely lies in encoding the proto
  36. let decodedProto = sessionEvent.encodeDecodeEvent()
  37. testCase(decodedProto)
  38. }
  39. func test_init_setsSessionIDs() {
  40. identifiers.mockAllValidIDs()
  41. let event = SessionStartEvent(identifiers: identifiers, appInfo: appInfo, time: time)
  42. testProtoAndDecodedProto(sessionEvent: event) { proto in
  43. assertEqualProtoString(
  44. proto.session_data.session_id,
  45. expected: MockIdentifierProvider.testSessionID,
  46. fieldName: "session_id"
  47. )
  48. assertEqualProtoString(
  49. proto.session_data.previous_session_id,
  50. expected: MockIdentifierProvider.testPreviousSessionID,
  51. fieldName: "previous_session_id"
  52. )
  53. XCTAssertEqual(proto.session_data.event_timestamp_us, 123)
  54. }
  55. }
  56. func test_init_setsApplicationInfo() {
  57. appInfo.mockAllInfo()
  58. let event = SessionStartEvent(identifiers: identifiers, appInfo: appInfo, time: time)
  59. testProtoAndDecodedProto(sessionEvent: event) { proto in
  60. assertEqualProtoString(
  61. proto.application_info.app_id,
  62. expected: MockApplicationInfo.testAppID,
  63. fieldName: "app_id"
  64. )
  65. assertEqualProtoString(
  66. proto.application_info.session_sdk_version,
  67. expected: MockApplicationInfo.testSDKVersion,
  68. fieldName: "session_sdk_version"
  69. )
  70. assertEqualProtoString(
  71. proto.application_info.apple_app_info.bundle_short_version,
  72. expected: MockApplicationInfo.testBundleID,
  73. fieldName: "bundle_short_version"
  74. )
  75. assertEqualProtoString(
  76. proto.application_info.apple_app_info.mcc_mnc,
  77. expected: MockApplicationInfo.testMCCMNC,
  78. fieldName: "mcc_mnc"
  79. )
  80. // Ensure we convert the test OS name into the enum.
  81. XCTAssertEqual(
  82. proto.application_info.apple_app_info.os_name,
  83. firebase_appquality_sessions_OsName_IOS
  84. )
  85. }
  86. }
  87. func test_setInstallationID_setsInstallationID() {
  88. identifiers.mockAllValidIDs()
  89. let event = SessionStartEvent(identifiers: identifiers, appInfo: appInfo, time: time)
  90. event.setInstallationID(identifiers: identifiers)
  91. testProtoAndDecodedProto(sessionEvent: event) { proto in
  92. assertEqualProtoString(
  93. proto.session_data.firebase_installation_id,
  94. expected: MockIdentifierProvider.testInstallationID,
  95. fieldName: "firebase_installation_id"
  96. )
  97. }
  98. }
  99. func test_convertOSName_convertsCorrectly() {
  100. let expectations: [(given: String, expected: firebase_appquality_sessions_OsName)] = [
  101. ("macos", firebase_appquality_sessions_OsName_MACOS),
  102. ("maccatalyst", firebase_appquality_sessions_OsName_MACCATALYST),
  103. ("ios_on_mac", firebase_appquality_sessions_OsName_IOS_ON_MAC),
  104. ("ios", firebase_appquality_sessions_OsName_IOS),
  105. ("tvos", firebase_appquality_sessions_OsName_TVOS),
  106. ("watchos", firebase_appquality_sessions_OsName_WATCHOS),
  107. ("ipados", firebase_appquality_sessions_OsName_IPADOS),
  108. ("something unknown", firebase_appquality_sessions_OsName_UNKNOWN_OSNAME),
  109. ]
  110. expectations.forEach { (given: String, expected: firebase_appquality_sessions_OsName) in
  111. appInfo.osName = given
  112. let event = SessionStartEvent(identifiers: identifiers, appInfo: appInfo, time: time)
  113. testProtoAndDecodedProto(sessionEvent: event) { proto in
  114. XCTAssertEqual(event.proto.application_info.apple_app_info.os_name, expected)
  115. }
  116. }
  117. }
  118. func test_convertLogEnvironment_convertsCorrectly() {
  119. let expectations: [(given: DevEnvironment, expected: firebase_appquality_sessions_LogEnvironment)] = [
  120. (.prod, firebase_appquality_sessions_LogEnvironment_LOG_ENVIRONMENT_PROD),
  121. (.staging, firebase_appquality_sessions_LogEnvironment_LOG_ENVIRONMENT_STAGING),
  122. (.autopush, firebase_appquality_sessions_LogEnvironment_LOG_ENVIRONMENT_AUTOPUSH),
  123. ]
  124. expectations.forEach { (given: DevEnvironment, expected: firebase_appquality_sessions_LogEnvironment) in
  125. appInfo.environment = given
  126. let event = SessionStartEvent(identifiers: identifiers, appInfo: appInfo, time: time)
  127. XCTAssertEqual(event.proto.application_info.log_environment, expected)
  128. }
  129. }
  130. }