FIRCLSDataCollectionArbiterTest.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "Crashlytics/Crashlytics/DataCollection/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 "Crashlytics/Crashlytics/FIRCLSUserDefaults/FIRCLSUserDefaults.h"
  22. #import "Crashlytics/UnitTests/Mocks/FIRAppFake.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. - (void)testNothingSet {
  40. self.fakeApp.isDefaultCollectionEnabled = YES;
  41. FIRCLSDataCollectionArbiter *arbiter = [self arbiterWithDictionary:@{}];
  42. #ifdef CRASHLYTICS_1P
  43. XCTAssertFalse([arbiter isCrashlyticsCollectionEnabled]);
  44. #else
  45. // It should be YES by default for 3P users.
  46. XCTAssertTrue([arbiter isCrashlyticsCollectionEnabled]);
  47. #endif
  48. }
  49. - (void)testOnlyStickyOff {
  50. FIRCLSDataCollectionArbiter *arbiter = [self arbiterWithDictionary:@{}];
  51. [arbiter setCrashlyticsCollectionEnabled:NO];
  52. XCTAssertFalse([arbiter isCrashlyticsCollectionEnabled]);
  53. }
  54. - (void)testOnlyFlagOff {
  55. FIRCLSDataCollectionArbiter *arbiter =
  56. [self arbiterWithDictionary:[self fabricConfigWithDataCollectionValue:NO]];
  57. XCTAssertFalse([arbiter isCrashlyticsCollectionEnabled]);
  58. }
  59. - (void)testOnlyFIRAppOff {
  60. self.fakeApp.isDefaultCollectionEnabled = NO;
  61. FIRCLSDataCollectionArbiter *arbiter = [self arbiterWithDictionary:@{}];
  62. XCTAssertFalse([arbiter isCrashlyticsCollectionEnabled]);
  63. }
  64. - (void)testStickyPrecedent {
  65. FIRCLSDataCollectionArbiter *arbiter =
  66. [self arbiterWithDictionary:[self fabricConfigWithDataCollectionValue:NO]];
  67. self.fakeApp.isDefaultCollectionEnabled = NO;
  68. [arbiter setCrashlyticsCollectionEnabled:YES];
  69. XCTAssertTrue([arbiter isCrashlyticsCollectionEnabled]);
  70. XCTAssertFalse([arbiter isLegacyDataCollectionKeyInPlist]);
  71. }
  72. - (void)testPlistPrecedent {
  73. FIRCLSDataCollectionArbiter *arbiter =
  74. [self arbiterWithDictionary:[self fabricConfigWithDataCollectionValue:YES]];
  75. self.fakeApp.isDefaultCollectionEnabled = NO;
  76. XCTAssertTrue([arbiter isCrashlyticsCollectionEnabled]);
  77. XCTAssertFalse([arbiter isLegacyDataCollectionKeyInPlist]);
  78. }
  79. - (void)testLegacyFlag {
  80. FIRCLSDataCollectionArbiter *arbiter =
  81. [self arbiterWithDictionary:[self fabricConfigWithLegacyDataCollectionValue:YES]];
  82. XCTAssertTrue([arbiter isLegacyDataCollectionKeyInPlist]);
  83. }
  84. - (void)testLegacyAndNewImplementationsAreIndependent {
  85. FIRCLSDataCollectionArbiter *arbiter =
  86. [self arbiterWithDictionary:[self fabricConfigWithLegacyDataCollectionValue:YES]];
  87. self.fakeApp.isDefaultCollectionEnabled = NO;
  88. XCTAssertFalse([arbiter isCrashlyticsCollectionEnabled]);
  89. XCTAssertTrue([arbiter isLegacyDataCollectionKeyInPlist]);
  90. }
  91. - (void)testLegacyAndNewFlagsAreIndependent {
  92. FIRCLSDataCollectionArbiter *arbiter =
  93. [self arbiterWithDictionary:[self fabricConfigWithDataCollectionValue:YES andLegacy:NO]];
  94. self.fakeApp.isDefaultCollectionEnabled = YES;
  95. XCTAssertTrue([arbiter isCrashlyticsCollectionEnabled]);
  96. XCTAssertTrue([arbiter isLegacyDataCollectionKeyInPlist]);
  97. }
  98. - (XCTestExpectation *)expectationForPromise:(FBLPromise *)promise {
  99. XCTestExpectation *expectation = [[XCTestExpectation alloc] initWithDescription:@"promise"];
  100. [promise then:^id _Nullable(id _Nullable value) {
  101. [expectation fulfill];
  102. return nil;
  103. }];
  104. return expectation;
  105. }
  106. - (void)testWaitForCrashlyticsCollectionEnabled {
  107. // true, wait
  108. FIRCLSDataCollectionArbiter *arbiter = [self arbiterWithDictionary:@{}];
  109. [arbiter setCrashlyticsCollectionEnabled:YES];
  110. FBLPromise *promise = [arbiter waitForCrashlyticsCollectionEnabled];
  111. [self waitForExpectations:@[ [self expectationForPromise:promise] ] timeout:1.0];
  112. // false, wait, true
  113. [arbiter setCrashlyticsCollectionEnabled:NO];
  114. promise = [arbiter waitForCrashlyticsCollectionEnabled];
  115. [arbiter setCrashlyticsCollectionEnabled:YES];
  116. [self waitForExpectations:@[ [self expectationForPromise:promise] ] timeout:1.0];
  117. // false, wait, false, true
  118. [arbiter setCrashlyticsCollectionEnabled:NO];
  119. promise = [arbiter waitForCrashlyticsCollectionEnabled];
  120. [arbiter setCrashlyticsCollectionEnabled:NO];
  121. [arbiter setCrashlyticsCollectionEnabled:YES];
  122. [self waitForExpectations:@[ [self expectationForPromise:promise] ] timeout:1.0];
  123. }
  124. #pragma mark - Helper functions
  125. - (FIRCLSDataCollectionArbiter *)arbiterWithDictionary:(NSDictionary *)dict {
  126. id fakeApp = self.fakeApp;
  127. return [[FIRCLSDataCollectionArbiter alloc] initWithApp:fakeApp withAppInfo:dict];
  128. }
  129. - (NSDictionary *)fabricConfigWithDataCollectionValue:(BOOL)enabled {
  130. return @{@"FirebaseCrashlyticsCollectionEnabled" : @(enabled)};
  131. }
  132. - (NSDictionary *)fabricConfigWithLegacyDataCollectionValue:(BOOL)enabled {
  133. return @{@"firebase_crashlytics_collection_enabled" : @(enabled)};
  134. }
  135. - (NSDictionary *)fabricConfigWithDataCollectionValue:(BOOL)enabled andLegacy:(BOOL)legacyEnabled {
  136. return @{
  137. @"FirebaseCrashlyticsCollectionEnabled" : @(enabled),
  138. @"firebase_crashlytics_collection_enabled" : @(legacyEnabled)
  139. };
  140. }
  141. @end