FIRAnalyticsConfigurationTest.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "FirebaseCore/Tests/Unit/FIRTestCase.h"
  17. #import <FirebaseCore/FIRAnalyticsConfiguration.h>
  18. @interface FIRAnalyticsConfigurationTest : FIRTestCase
  19. /// An observer for NSNotificationCenter.
  20. @property(nonatomic, strong) id observerMock;
  21. @property(nonatomic, strong) NSNotificationCenter *notificationCenter;
  22. @end
  23. @implementation FIRAnalyticsConfigurationTest
  24. - (void)setUp {
  25. [super setUp];
  26. _observerMock = OCMObserverMock();
  27. _notificationCenter = [NSNotificationCenter defaultCenter];
  28. }
  29. - (void)tearDown {
  30. _observerMock = nil;
  31. _notificationCenter = nil;
  32. [super tearDown];
  33. }
  34. /// Test access to the shared instance.
  35. - (void)testSharedInstance {
  36. FIRAnalyticsConfiguration *analyticsConfig = [FIRAnalyticsConfiguration sharedInstance];
  37. XCTAssertNotNil(analyticsConfig);
  38. }
  39. - (void)testSettingAnalyticsCollectionPersistence {
  40. id userDefaultsMock = OCMPartialMock([NSUserDefaults standardUserDefaults]);
  41. FIRAnalyticsConfiguration *config = [FIRAnalyticsConfiguration sharedInstance];
  42. // Test that defaults are written to when persistence is enabled.
  43. [config setAnalyticsCollectionEnabled:YES persistSetting:YES];
  44. OCMVerify([userDefaultsMock setObject:[NSNumber numberWithInteger:kFIRAnalyticsEnabledStateSetYes]
  45. forKey:kFIRAPersistedConfigMeasurementEnabledStateKey]);
  46. [config setAnalyticsCollectionEnabled:NO persistSetting:YES];
  47. OCMVerify([userDefaultsMock setObject:[NSNumber numberWithInteger:kFIRAnalyticsEnabledStateSetNo]
  48. forKey:kFIRAPersistedConfigMeasurementEnabledStateKey]);
  49. // Test that defaults are not written to when persistence is disabled.
  50. [config setAnalyticsCollectionEnabled:YES persistSetting:NO];
  51. OCMReject([userDefaultsMock setObject:OCMOCK_ANY
  52. forKey:kFIRAPersistedConfigMeasurementEnabledStateKey]);
  53. [config setAnalyticsCollectionEnabled:NO persistSetting:NO];
  54. OCMReject([userDefaultsMock setObject:OCMOCK_ANY
  55. forKey:kFIRAPersistedConfigMeasurementEnabledStateKey]);
  56. [userDefaultsMock stopMocking];
  57. }
  58. #pragma mark - Private Test Helpers
  59. - (void)expectNotificationForObserver:(id)observer
  60. notificationName:(NSNotificationName)name
  61. object:(nullable id)object
  62. userInfo:(nullable NSDictionary *)userInfo {
  63. [self.notificationCenter addMockObserver:self.observerMock name:name object:object];
  64. [[observer expect] notificationWithName:name object:object userInfo:userInfo];
  65. }
  66. @end