FIRCLSDataCollectionArbiterTest.m 5.9 KB

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