FIRCLSDataCollectionArbiterTest.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2019 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FIRCLSDataCollectionArbiter.h"
  15. #import <XCTest/XCTest.h>
  16. #if __has_include(<FBLPromises/FBLPromises.h>)
  17. #import <FBLPromises/FBLPromises.h>
  18. #else
  19. #import "FBLPromises.h"
  20. #endif
  21. #import "FIRAppFake.h"
  22. #import "FIRCLSUserDefaults.h"
  23. #pragma mark - Tests for FIRCLSDataCollectionArbiter
  24. @interface FIRCLSDataCollectionArbiterTest : XCTestCase
  25. @property(nonatomic, readwrite) FIRAppFake *fakeApp;
  26. @end
  27. @implementation FIRCLSDataCollectionArbiterTest
  28. - (void)setUp {
  29. [super setUp];
  30. self.fakeApp = [[FIRAppFake alloc] init];
  31. [[FIRCLSUserDefaults standardUserDefaults] removeAllObjects];
  32. [[FIRCLSUserDefaults standardUserDefaults] synchronize];
  33. }
  34. - (void)tearDown {
  35. [[FIRCLSUserDefaults standardUserDefaults] removeAllObjects];
  36. [[FIRCLSUserDefaults standardUserDefaults] synchronize];
  37. [super tearDown];
  38. }
  39. // If you do nothing, it should be YES. We should not be turning Fabric
  40. // customers off by default
  41. - (void)testNothingSet {
  42. self.fakeApp.isDefaultCollectionEnabled = YES;
  43. FIRCLSDataCollectionArbiter *arbiter = [self arbiterWithDictionary:@{}];
  44. XCTAssertTrue([arbiter isCrashlyticsCollectionEnabled]);
  45. }
  46. - (void)testOnlyStickyOff {
  47. FIRCLSDataCollectionArbiter *arbiter = [self arbiterWithDictionary:@{}];
  48. [arbiter setCrashlyticsCollectionEnabled:NO];
  49. XCTAssertFalse([arbiter isCrashlyticsCollectionEnabled]);
  50. }
  51. - (void)testOnlyFlagOff {
  52. FIRCLSDataCollectionArbiter *arbiter =
  53. [self arbiterWithDictionary:[self fabricConfigWithDataCollectionValue:NO]];
  54. XCTAssertFalse([arbiter isCrashlyticsCollectionEnabled]);
  55. }
  56. - (void)testOnlyFIRAppOff {
  57. self.fakeApp.isDefaultCollectionEnabled = NO;
  58. FIRCLSDataCollectionArbiter *arbiter = [self arbiterWithDictionary:@{}];
  59. XCTAssertFalse([arbiter isCrashlyticsCollectionEnabled]);
  60. }
  61. - (void)testStickyPrecedent {
  62. FIRCLSDataCollectionArbiter *arbiter =
  63. [self arbiterWithDictionary:[self fabricConfigWithDataCollectionValue:NO]];
  64. self.fakeApp.isDefaultCollectionEnabled = NO;
  65. [arbiter setCrashlyticsCollectionEnabled:YES];
  66. XCTAssertTrue([arbiter isCrashlyticsCollectionEnabled]);
  67. XCTAssertFalse([arbiter isLegacyDataCollectionKeyInPlist]);
  68. }
  69. - (void)testPlistPrecedent {
  70. FIRCLSDataCollectionArbiter *arbiter =
  71. [self arbiterWithDictionary:[self fabricConfigWithDataCollectionValue:YES]];
  72. self.fakeApp.isDefaultCollectionEnabled = NO;
  73. XCTAssertTrue([arbiter isCrashlyticsCollectionEnabled]);
  74. XCTAssertFalse([arbiter isLegacyDataCollectionKeyInPlist]);
  75. }
  76. - (void)testLegacyFlag {
  77. FIRCLSDataCollectionArbiter *arbiter =
  78. [self arbiterWithDictionary:[self fabricConfigWithLegacyDataCollectionValue:YES]];
  79. XCTAssertTrue([arbiter isLegacyDataCollectionKeyInPlist]);
  80. }
  81. - (void)testLegacyAndNewImplementationsAreIndependent {
  82. FIRCLSDataCollectionArbiter *arbiter =
  83. [self arbiterWithDictionary:[self fabricConfigWithLegacyDataCollectionValue:YES]];
  84. self.fakeApp.isDefaultCollectionEnabled = NO;
  85. XCTAssertFalse([arbiter isCrashlyticsCollectionEnabled]);
  86. XCTAssertTrue([arbiter isLegacyDataCollectionKeyInPlist]);
  87. }
  88. - (void)testLegacyAndNewFlagsAreIndependent {
  89. FIRCLSDataCollectionArbiter *arbiter =
  90. [self arbiterWithDictionary:[self fabricConfigWithDataCollectionValue:YES andLegacy:NO]];
  91. self.fakeApp.isDefaultCollectionEnabled = YES;
  92. XCTAssertTrue([arbiter isCrashlyticsCollectionEnabled]);
  93. XCTAssertTrue([arbiter isLegacyDataCollectionKeyInPlist]);
  94. }
  95. - (XCTestExpectation *)expectationForPromise:(FBLPromise *)promise {
  96. XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"promise"];
  97. [promise then:^id _Nullable(id _Nullable value) {
  98. [expectation fulfill];
  99. return nil;
  100. }];
  101. return expectation;
  102. }
  103. - (void)testWaitForCrashlyticsCollectionEnabled {
  104. // true, wait
  105. FIRCLSDataCollectionArbiter *arbiter = [self arbiterWithDictionary:@{}];
  106. [arbiter setCrashlyticsCollectionEnabled:YES];
  107. FBLPromise *promise = [arbiter waitForCrashlyticsCollectionEnabled];
  108. [self waitForExpectations:@[ [self expectationForPromise:promise] ] timeout:1.0];
  109. // false, wait, true
  110. [arbiter setCrashlyticsCollectionEnabled:NO];
  111. promise = [arbiter waitForCrashlyticsCollectionEnabled];
  112. [arbiter setCrashlyticsCollectionEnabled:YES];
  113. [self waitForExpectations:@[ [self expectationForPromise:promise] ] timeout:1.0];
  114. // false, wait, false, true
  115. [arbiter setCrashlyticsCollectionEnabled:NO];
  116. promise = [arbiter waitForCrashlyticsCollectionEnabled];
  117. [arbiter setCrashlyticsCollectionEnabled:NO];
  118. [arbiter setCrashlyticsCollectionEnabled:YES];
  119. [self waitForExpectations:@[ [self expectationForPromise:promise] ] timeout:1.0];
  120. }
  121. #pragma mark - Helper functions
  122. - (FIRCLSDataCollectionArbiter *)arbiterWithDictionary:(NSDictionary *)dict {
  123. id fakeApp = self.fakeApp;
  124. return [[FIRCLSDataCollectionArbiter alloc] initWithApp:fakeApp withAppInfo:dict];
  125. }
  126. - (NSDictionary *)fabricConfigWithDataCollectionValue:(BOOL)enabled {
  127. return @{@"FirebaseCrashlyticsCollectionEnabled" : @(enabled)};
  128. }
  129. - (NSDictionary *)fabricConfigWithLegacyDataCollectionValue:(BOOL)enabled {
  130. return @{@"firebase_crashlytics_collection_enabled" : @(enabled)};
  131. }
  132. - (NSDictionary *)fabricConfigWithDataCollectionValue:(BOOL)enabled andLegacy:(BOOL)legacyEnabled {
  133. return @{
  134. @"FirebaseCrashlyticsCollectionEnabled" : @(enabled),
  135. @"firebase_crashlytics_collection_enabled" : @(legacyEnabled)
  136. };
  137. }
  138. @end