FIRAnalyticsConfigurationTest.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2018 Google
  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. */
  16. #import "FIRTestCase.h"
  17. #import <FirebaseCore/FIRAnalyticsConfiguration+Internal.h>
  18. #import <FirebaseCore/FIRAnalyticsConfiguration.h>
  19. @interface FIRAnalyticsConfigurationTest : FIRTestCase
  20. /// An observer for NSNotificationCenter.
  21. @property(nonatomic, strong) id observerMock;
  22. @property(nonatomic, strong) NSNotificationCenter *notificationCenter;
  23. @end
  24. @implementation FIRAnalyticsConfigurationTest
  25. - (void)setUp {
  26. [super setUp];
  27. _observerMock = OCMObserverMock();
  28. _notificationCenter = [NSNotificationCenter defaultCenter];
  29. }
  30. - (void)tearDown {
  31. _observerMock = nil;
  32. _notificationCenter = nil;
  33. [super tearDown];
  34. }
  35. /// Test access to the shared instance.
  36. - (void)testSharedInstance {
  37. FIRAnalyticsConfiguration *analyticsConfig = [FIRAnalyticsConfiguration sharedInstance];
  38. XCTAssertNotNil(analyticsConfig);
  39. }
  40. /// Test that setting the minimum session interval on the singleton fires a notification.
  41. - (void)testMinimumSessionIntervalNotification {
  42. // Pick a value to set as the session interval and verify it's in the userInfo dictionary of the
  43. // posted notification.
  44. NSNumber *sessionInterval = @2601;
  45. // Set up the expectation for the notification.
  46. FIRAnalyticsConfiguration *config = [FIRAnalyticsConfiguration sharedInstance];
  47. NSString *notificationName = kFIRAnalyticsConfigurationSetMinimumSessionIntervalNotification;
  48. [self expectNotificationForObserver:self.observerMock
  49. notificationName:notificationName
  50. object:config
  51. userInfo:@{notificationName : sessionInterval}];
  52. // Trigger the notification.
  53. [config setMinimumSessionInterval:[sessionInterval integerValue]];
  54. // Verify the observer mock.
  55. OCMVerifyAll(self.observerMock);
  56. }
  57. /// Test that setting the minimum session timeout interval on the singleton fires a notification.
  58. - (void)testSessionTimeoutIntervalNotification {
  59. // Pick a value to set as the timeout interval and verify it's in the userInfo dictionary of the
  60. // posted notification.
  61. NSNumber *timeoutInterval = @1000;
  62. // Set up the expectation for the notification.
  63. FIRAnalyticsConfiguration *config = [FIRAnalyticsConfiguration sharedInstance];
  64. NSString *notificationName = kFIRAnalyticsConfigurationSetSessionTimeoutIntervalNotification;
  65. [self expectNotificationForObserver:self.observerMock
  66. notificationName:notificationName
  67. object:config
  68. userInfo:@{notificationName : timeoutInterval}];
  69. // Trigger the notification.
  70. [config setSessionTimeoutInterval:[timeoutInterval integerValue]];
  71. /// Verify the observer mock.
  72. OCMVerifyAll(self.observerMock);
  73. }
  74. - (void)testSettingAnalyticsCollectionEnabled {
  75. // Test setting to enabled. The ordering matters for these notifications.
  76. FIRAnalyticsConfiguration *config = [FIRAnalyticsConfiguration sharedInstance];
  77. NSString *notificationName = kFIRAnalyticsConfigurationSetEnabledNotification;
  78. [self.notificationCenter addMockObserver:self.observerMock name:notificationName object:config];
  79. [self.observerMock setExpectationOrderMatters:YES];
  80. [[self.observerMock expect] notificationWithName:notificationName
  81. object:config
  82. userInfo:@{
  83. notificationName : @YES
  84. }];
  85. // Test setting to enabled.
  86. [config setAnalyticsCollectionEnabled:YES];
  87. // Expect the second notification.
  88. [[self.observerMock expect] notificationWithName:notificationName
  89. object:config
  90. userInfo:@{
  91. notificationName : @NO
  92. }];
  93. // Test setting to disabled.
  94. [config setAnalyticsCollectionEnabled:NO];
  95. OCMVerifyAll(self.observerMock);
  96. }
  97. - (void)testSettingAnalyticsCollectionPersistence {
  98. id userDefaultsMock = OCMPartialMock([NSUserDefaults standardUserDefaults]);
  99. FIRAnalyticsConfiguration *config = [FIRAnalyticsConfiguration sharedInstance];
  100. // Test that defaults are written to when persistence is enabled.
  101. [config setAnalyticsCollectionEnabled:YES persistSetting:YES];
  102. OCMVerify([userDefaultsMock setObject:[NSNumber numberWithInteger:kFIRAnalyticsEnabledStateSetYes]
  103. forKey:kFIRAPersistedConfigMeasurementEnabledStateKey]);
  104. [config setAnalyticsCollectionEnabled:NO persistSetting:YES];
  105. OCMVerify([userDefaultsMock setObject:[NSNumber numberWithInteger:kFIRAnalyticsEnabledStateSetNo]
  106. forKey:kFIRAPersistedConfigMeasurementEnabledStateKey]);
  107. // Test that defaults are not written to when persistence is disabled.
  108. [config setAnalyticsCollectionEnabled:YES persistSetting:NO];
  109. OCMReject([userDefaultsMock setObject:OCMOCK_ANY
  110. forKey:kFIRAPersistedConfigMeasurementEnabledStateKey]);
  111. [config setAnalyticsCollectionEnabled:NO persistSetting:NO];
  112. OCMReject([userDefaultsMock setObject:OCMOCK_ANY
  113. forKey:kFIRAPersistedConfigMeasurementEnabledStateKey]);
  114. [userDefaultsMock stopMocking];
  115. }
  116. #pragma mark - Private Test Helpers
  117. - (void)expectNotificationForObserver:(id)observer
  118. notificationName:(NSNotificationName)name
  119. object:(nullable id)object
  120. userInfo:(nullable NSDictionary *)userInfo {
  121. [self.notificationCenter addMockObserver:self.observerMock name:name object:object];
  122. [[observer expect] notificationWithName:name object:object userInfo:userInfo];
  123. }
  124. @end