FirebaseSessionsTests+Subscribers.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #if SWIFT_PACKAGE
  17. import FirebaseSessionsObjC
  18. #endif // SWIFT_PACKAGE
  19. @testable import FirebaseSessions
  20. final class FirebaseSessionsTestsBase_Subscribers: FirebaseSessionsTestsBase {
  21. // Check that the Session ID that was passed to the Subscriber SDK
  22. // matches the Session ID that the Sessions SDK logged, and ensure
  23. // both are not empty.
  24. @MainActor func assertValidChangedSessionID() {
  25. let expectedSessionID = sessions.currentSessionDetails.sessionId
  26. XCTAssert(expectedSessionID!.count > 0)
  27. for mock in [mockCrashlyticsSubscriber, mockPerformanceSubscriber] {
  28. let mocksChangedSessionID = mock?.sessionThatChanged?.sessionId!
  29. XCTAssert(mocksChangedSessionID!.count > 0)
  30. XCTAssertEqual(expectedSessionID, mocksChangedSessionID)
  31. }
  32. }
  33. // MARK: - Test Subscriber Callbacks
  34. @MainActor func test_registerSubscriber_callsOnSessionChanged() {
  35. runSessionsSDK(
  36. subscriberSDKs: [
  37. mockCrashlyticsSubscriber,
  38. mockPerformanceSubscriber,
  39. ], preSessionsInit: { _ in
  40. // Nothing
  41. }, postSessionsInit: {
  42. // Register the subscribers
  43. sessions.register(subscriber: self.mockPerformanceSubscriber)
  44. sessions.register(subscriber: self.mockCrashlyticsSubscriber)
  45. }, postLogEvent: { result, subscriberSDKs in
  46. // Ensure the subscribers still get a Session ID from their subscription
  47. self.assertValidChangedSessionID()
  48. }
  49. )
  50. }
  51. // Make sure that even if the Sessions SDK is disabled, and data collection
  52. // is disabled, the Sessions SDK still generates Session IDs and provides
  53. // them to Subscribers
  54. @MainActor func test_subscribersDataCollectionDisabled_callsOnSessionChanged() {
  55. runSessionsSDK(
  56. subscriberSDKs: [
  57. mockCrashlyticsSubscriber,
  58. mockPerformanceSubscriber,
  59. ], preSessionsInit: { _ in
  60. // Disable the Sessions SDK in all possible ways
  61. self.mockCrashlyticsSubscriber.isDataCollectionEnabled = false
  62. self.mockCrashlyticsSubscriber.isDataCollectionEnabled = false
  63. self.mockSettings.sessionsEnabled = false
  64. self.mockSettings.samplingRate = 0.0
  65. }, postSessionsInit: {
  66. // Register the subscribers
  67. sessions.register(subscriber: self.mockPerformanceSubscriber)
  68. sessions.register(subscriber: self.mockCrashlyticsSubscriber)
  69. }, postLogEvent: { result, subscriberSDKs in
  70. // Ensure the subscribers still get a Session ID from their subscription
  71. self.assertValidChangedSessionID()
  72. }
  73. )
  74. }
  75. @MainActor func test_noDependencies_doesNotLogSessionEvent() {
  76. runSessionsSDK(
  77. subscriberSDKs: [],
  78. preSessionsInit: { _ in
  79. // Nothing
  80. }, postSessionsInit: {
  81. // Nothing
  82. }, postLogEvent: { result, subscriberSDKs in
  83. // Make sure we didn't do any data collection
  84. self.assertFailure(result: result, expectedError: .NoDependenciesError)
  85. XCTAssertFalse(self.mockSettings.updateSettingsCalled)
  86. XCTAssertNil(self.mockCoordinator.loggedEvent)
  87. }
  88. )
  89. }
  90. @MainActor func test_noSubscribersWithRegistrations_doesNotCrash() {
  91. runSessionsSDK(
  92. subscriberSDKs: [],
  93. preSessionsInit: { _ in
  94. // Nothing
  95. }, postSessionsInit: {
  96. // Register the subscribers even though they didn't
  97. // add themselves as dependencies.
  98. // This case shouldn't happen but if it did we don't want
  99. // to have any unexpected behavior
  100. sessions.register(subscriber: self.mockPerformanceSubscriber)
  101. sessions.register(subscriber: self.mockCrashlyticsSubscriber)
  102. }, postLogEvent: { result, subscriberSDKs in
  103. // Make sure we didn't do any data collection
  104. self.assertFailure(result: result, expectedError: .NoDependenciesError)
  105. XCTAssertFalse(self.mockSettings.updateSettingsCalled)
  106. XCTAssertNil(self.mockCoordinator.loggedEvent)
  107. }
  108. )
  109. }
  110. }