FIROptionsTest.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // Copyright 2017 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 "FirebaseCore/Tests/Unit/FIRTestCase.h"
  15. #import "FirebaseCore/Extension/FIRAppInternal.h"
  16. #import "FirebaseCore/Sources/FIRBundleUtil.h"
  17. #import "FirebaseCore/Sources/FIROptionsInternal.h"
  18. #import "FirebaseCore/Sources/Public/FirebaseCore/FIRVersion.h"
  19. #import "SharedTestUtilities/FIROptionsMock.h"
  20. extern NSString *const kFIRIsMeasurementEnabled;
  21. extern NSString *const kFIRIsAnalyticsCollectionEnabled;
  22. extern NSString *const kFIRIsAnalyticsCollectionDeactivated;
  23. extern NSString *const kFIRLibraryVersionID;
  24. @interface FIROptions (Test)
  25. - (nullable NSDictionary *)analyticsOptionsDictionaryWithInfoDictionary:
  26. (nullable NSDictionary *)infoDictionary;
  27. @end
  28. @interface FIROptionsTest : FIRTestCase
  29. @end
  30. @implementation FIROptionsTest
  31. - (void)setUp {
  32. [super setUp];
  33. [FIROptions resetDefaultOptions];
  34. }
  35. - (void)testInit {
  36. [FIROptionsMock mockFIROptions];
  37. NSDictionary *optionsDictionary = [FIROptions defaultOptionsDictionary];
  38. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  39. [self assertOptionsMatchDefaults:options andProjectID:YES];
  40. XCTAssertTrue(options.usingOptionsFromDefaultPlist);
  41. }
  42. - (void)testDefaultOptionsDictionaryWithNilFilePath {
  43. id mockBundleUtil = OCMClassMock([FIRBundleUtil class]);
  44. OCMStub([mockBundleUtil optionsDictionaryPathWithResourceName:kServiceInfoFileName
  45. andFileType:kServiceInfoFileType
  46. inBundles:[FIRBundleUtil relevantBundles]])
  47. .andReturn(nil);
  48. XCTAssertNil([FIROptions defaultOptionsDictionary]);
  49. }
  50. - (void)testDefaultOptionsDictionaryWithInvalidSourceFile {
  51. id mockBundleUtil = OCMClassMock([FIRBundleUtil class]);
  52. OCMStub([mockBundleUtil optionsDictionaryPathWithResourceName:kServiceInfoFileName
  53. andFileType:kServiceInfoFileType
  54. inBundles:[FIRBundleUtil relevantBundles]])
  55. .andReturn(@"invalid.plist");
  56. XCTAssertNil([FIROptions defaultOptionsDictionary]);
  57. }
  58. - (void)testDefaultOptions {
  59. [FIROptionsMock mockFIROptions];
  60. FIROptions *options = [FIROptions defaultOptions];
  61. [self assertOptionsMatchDefaults:options andProjectID:YES];
  62. XCTAssertTrue(options.usingOptionsFromDefaultPlist);
  63. }
  64. #ifndef SWIFT_PACKAGE
  65. // Another strategy is needed for these tests to pass with SWIFT_PACKAGE, since the mocking
  66. // prevents the file path traversals.
  67. - (void)testDefaultOptionsAreInitializedOnce {
  68. id mockBundleUtil = OCMClassMock([FIRBundleUtil class]);
  69. OCMExpect([mockBundleUtil optionsDictionaryPathWithResourceName:kServiceInfoFileName
  70. andFileType:kServiceInfoFileType
  71. inBundles:[FIRBundleUtil relevantBundles]])
  72. .andReturn([self validGoogleServicesInfoPlistPath]);
  73. XCTAssertNotNil([FIROptions defaultOptions]);
  74. OCMVerifyAll(mockBundleUtil);
  75. OCMReject([mockBundleUtil optionsDictionaryPathWithResourceName:OCMOCK_ANY
  76. andFileType:OCMOCK_ANY
  77. inBundles:OCMOCK_ANY]);
  78. XCTAssertNotNil([FIROptions defaultOptions]);
  79. OCMVerifyAll(mockBundleUtil);
  80. }
  81. - (void)testDefaultOptionsDictionaryIsInitializedOnce {
  82. id mockBundleUtil = OCMClassMock([FIRBundleUtil class]);
  83. OCMExpect([mockBundleUtil optionsDictionaryPathWithResourceName:kServiceInfoFileName
  84. andFileType:kServiceInfoFileType
  85. inBundles:[FIRBundleUtil relevantBundles]])
  86. .andReturn([self validGoogleServicesInfoPlistPath]);
  87. XCTAssertNotNil([FIROptions defaultOptionsDictionary]);
  88. OCMVerifyAll(mockBundleUtil);
  89. OCMReject([mockBundleUtil optionsDictionaryPathWithResourceName:OCMOCK_ANY
  90. andFileType:OCMOCK_ANY
  91. inBundles:OCMOCK_ANY]);
  92. XCTAssertNotNil([FIROptions defaultOptionsDictionary]);
  93. OCMVerifyAll(mockBundleUtil);
  94. }
  95. - (void)testInitWithContentsOfFile {
  96. NSString *filePath = [self validGoogleServicesInfoPlistPath];
  97. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
  98. [self assertOptionsMatchDefaults:options andProjectID:YES];
  99. XCTAssertFalse(options.usingOptionsFromDefaultPlist);
  100. #pragma clang diagnostic push
  101. #pragma clang diagnostic ignored "-Wnonnull"
  102. FIROptions *emptyOptions = [[FIROptions alloc] initWithContentsOfFile:nil];
  103. #pragma clang diagnostic pop
  104. XCTAssertNil(emptyOptions);
  105. FIROptions *invalidOptions = [[FIROptions alloc] initWithContentsOfFile:@"invalid.plist"];
  106. XCTAssertNil(invalidOptions);
  107. }
  108. #endif
  109. - (void)testInitCustomizedOptions {
  110. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  111. GCMSenderID:kGCMSenderID];
  112. options.APIKey = kAPIKey;
  113. options.bundleID = kBundleID;
  114. options.clientID = kClientID;
  115. options.databaseURL = kDatabaseURL;
  116. options.projectID = kProjectID;
  117. options.storageBucket = kStorageBucket;
  118. [self assertOptionsMatchDefaults:options andProjectID:YES];
  119. XCTAssertFalse(options.usingOptionsFromDefaultPlist);
  120. }
  121. - (void)assertOptionsMatchDefaults:(FIROptions *)options andProjectID:(BOOL)matchProjectID {
  122. XCTAssertEqualObjects(options.googleAppID, kGoogleAppID);
  123. XCTAssertEqualObjects(options.APIKey, kAPIKey);
  124. XCTAssertEqualObjects(options.clientID, kClientID);
  125. XCTAssertEqualObjects(options.GCMSenderID, kGCMSenderID);
  126. XCTAssertEqualObjects(options.libraryVersionID, kFIRLibraryVersionID);
  127. XCTAssertEqualObjects(options.databaseURL, kDatabaseURL);
  128. XCTAssertEqualObjects(options.storageBucket, kStorageBucket);
  129. XCTAssertEqualObjects(options.bundleID, kBundleID);
  130. // Custom `matchProjectID` parameter to be removed once the deprecated `FIROptions` constructor is
  131. // removed.
  132. if (matchProjectID) {
  133. XCTAssertEqualObjects(options.projectID, kProjectID);
  134. }
  135. }
  136. - (void)testCopyingProperties {
  137. NSMutableString *mutableString;
  138. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  139. GCMSenderID:kGCMSenderID];
  140. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  141. options.APIKey = mutableString;
  142. [mutableString appendString:@"2"];
  143. XCTAssertEqualObjects(options.APIKey, @"1");
  144. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  145. options.bundleID = mutableString;
  146. [mutableString appendString:@"2"];
  147. XCTAssertEqualObjects(options.bundleID, @"1");
  148. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  149. options.clientID = mutableString;
  150. [mutableString appendString:@"2"];
  151. XCTAssertEqualObjects(options.clientID, @"1");
  152. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  153. options.GCMSenderID = mutableString;
  154. [mutableString appendString:@"2"];
  155. XCTAssertEqualObjects(options.GCMSenderID, @"1");
  156. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  157. options.projectID = mutableString;
  158. [mutableString appendString:@"2"];
  159. XCTAssertEqualObjects(options.projectID, @"1");
  160. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  161. options.googleAppID = mutableString;
  162. [mutableString appendString:@"2"];
  163. XCTAssertEqualObjects(options.googleAppID, @"1");
  164. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  165. options.databaseURL = mutableString;
  166. [mutableString appendString:@"2"];
  167. XCTAssertEqualObjects(options.databaseURL, @"1");
  168. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  169. options.storageBucket = mutableString;
  170. [mutableString appendString:@"2"];
  171. XCTAssertEqualObjects(options.storageBucket, @"1");
  172. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  173. options.appGroupID = mutableString;
  174. [mutableString appendString:@"2"];
  175. XCTAssertEqualObjects(options.appGroupID, @"1");
  176. }
  177. - (void)testAnalyticsConstants {
  178. // The keys are public values and should never change.
  179. XCTAssertEqualObjects(kFIRIsMeasurementEnabled, @"IS_MEASUREMENT_ENABLED");
  180. XCTAssertEqualObjects(kFIRIsAnalyticsCollectionEnabled, @"FIREBASE_ANALYTICS_COLLECTION_ENABLED");
  181. XCTAssertEqualObjects(kFIRIsAnalyticsCollectionDeactivated,
  182. @"FIREBASE_ANALYTICS_COLLECTION_DEACTIVATED");
  183. }
  184. - (void)testAnalyticsOptions {
  185. // No keys anywhere.
  186. NSDictionary *optionsDictionary = nil;
  187. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  188. NSDictionary *mainDictionary = nil;
  189. NSDictionary *expectedAnalyticsOptions = @{};
  190. NSDictionary *analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:nil];
  191. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  192. optionsDictionary = @{};
  193. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  194. mainDictionary = @{};
  195. expectedAnalyticsOptions = @{};
  196. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  197. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  198. // Main has no keys.
  199. optionsDictionary = @{
  200. kFIRIsAnalyticsCollectionDeactivated : @YES,
  201. kFIRIsAnalyticsCollectionEnabled : @YES,
  202. kFIRIsMeasurementEnabled : @YES
  203. };
  204. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  205. mainDictionary = @{};
  206. expectedAnalyticsOptions = optionsDictionary;
  207. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  208. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  209. // Main overrides all the keys.
  210. optionsDictionary = @{
  211. kFIRIsAnalyticsCollectionDeactivated : @YES,
  212. kFIRIsAnalyticsCollectionEnabled : @YES,
  213. kFIRIsMeasurementEnabled : @YES
  214. };
  215. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  216. mainDictionary = @{
  217. kFIRIsAnalyticsCollectionDeactivated : @NO,
  218. kFIRIsAnalyticsCollectionEnabled : @NO,
  219. kFIRIsMeasurementEnabled : @NO
  220. };
  221. expectedAnalyticsOptions = mainDictionary;
  222. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  223. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  224. // Keys exist only in main.
  225. optionsDictionary = @{};
  226. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  227. mainDictionary = @{
  228. kFIRIsAnalyticsCollectionDeactivated : @YES,
  229. kFIRIsAnalyticsCollectionEnabled : @YES,
  230. kFIRIsMeasurementEnabled : @YES
  231. };
  232. expectedAnalyticsOptions = mainDictionary;
  233. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  234. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  235. // Main overrides single keys.
  236. optionsDictionary = @{
  237. kFIRIsAnalyticsCollectionDeactivated : @YES,
  238. kFIRIsAnalyticsCollectionEnabled : @YES,
  239. kFIRIsMeasurementEnabled : @YES
  240. };
  241. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  242. mainDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @NO};
  243. expectedAnalyticsOptions = @{
  244. kFIRIsAnalyticsCollectionDeactivated : @NO, // override
  245. kFIRIsAnalyticsCollectionEnabled : @YES,
  246. kFIRIsMeasurementEnabled : @YES
  247. };
  248. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  249. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  250. optionsDictionary = @{
  251. kFIRIsAnalyticsCollectionDeactivated : @YES,
  252. kFIRIsAnalyticsCollectionEnabled : @YES,
  253. kFIRIsMeasurementEnabled : @YES
  254. };
  255. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  256. mainDictionary = @{kFIRIsAnalyticsCollectionEnabled : @NO};
  257. expectedAnalyticsOptions = @{
  258. kFIRIsAnalyticsCollectionDeactivated : @YES,
  259. kFIRIsAnalyticsCollectionEnabled : @NO, // override
  260. kFIRIsMeasurementEnabled : @YES
  261. };
  262. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  263. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  264. optionsDictionary = @{
  265. kFIRIsAnalyticsCollectionDeactivated : @YES,
  266. kFIRIsAnalyticsCollectionEnabled : @YES,
  267. kFIRIsMeasurementEnabled : @YES
  268. };
  269. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  270. mainDictionary = @{kFIRIsMeasurementEnabled : @NO};
  271. expectedAnalyticsOptions = @{
  272. kFIRIsAnalyticsCollectionDeactivated : @YES,
  273. kFIRIsAnalyticsCollectionEnabled : @YES,
  274. kFIRIsMeasurementEnabled : @NO // override
  275. };
  276. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  277. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  278. }
  279. - (void)testAnalyticsOptions_combinatorial {
  280. // Complete combinatorial test.
  281. // Possible values for the flags in the plist, where NSNull means the flag is not present.
  282. NSArray *values = @[ [NSNull null], @NO, @YES ];
  283. // Sanity checks for the combination generation.
  284. int combinationCount = 0;
  285. NSMutableArray *uniqueMainCombinations = [[NSMutableArray alloc] init];
  286. NSMutableArray *uniqueOptionsCombinations = [[NSMutableArray alloc] init];
  287. // Generate all optout flag combinations for { main plist X GoogleService-info options plist }.
  288. // Options present in the main plist should override options of the same key in the service plist.
  289. for (id mainDeactivated in values) {
  290. for (id mainAnalyticsEnabled in values) {
  291. for (id mainMeasurementEnabled in values) {
  292. for (id optionsDeactivated in values) {
  293. for (id optionsAnalyticsEnabled in values) {
  294. for (id optionsMeasurementEnabled in values) {
  295. @autoreleasepool {
  296. // Fill the GoogleService-info options plist dictionary.
  297. NSMutableDictionary *optionsDictionary = [[NSMutableDictionary alloc] init];
  298. if (![optionsDeactivated isEqual:[NSNull null]]) {
  299. optionsDictionary[kFIRIsAnalyticsCollectionDeactivated] = optionsDeactivated;
  300. }
  301. if (![optionsAnalyticsEnabled isEqual:[NSNull null]]) {
  302. optionsDictionary[kFIRIsAnalyticsCollectionEnabled] = optionsAnalyticsEnabled;
  303. }
  304. if (![optionsMeasurementEnabled isEqual:[NSNull null]]) {
  305. optionsDictionary[kFIRIsMeasurementEnabled] = optionsMeasurementEnabled;
  306. }
  307. FIROptions *options =
  308. [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  309. if (![uniqueOptionsCombinations containsObject:optionsDictionary]) {
  310. [uniqueOptionsCombinations addObject:optionsDictionary];
  311. }
  312. // Fill the main plist dictionary.
  313. NSMutableDictionary *mainDictionary = [[NSMutableDictionary alloc] init];
  314. if (![mainDeactivated isEqual:[NSNull null]]) {
  315. mainDictionary[kFIRIsAnalyticsCollectionDeactivated] = mainDeactivated;
  316. }
  317. if (![mainAnalyticsEnabled isEqual:[NSNull null]]) {
  318. mainDictionary[kFIRIsAnalyticsCollectionEnabled] = mainAnalyticsEnabled;
  319. }
  320. if (![mainMeasurementEnabled isEqual:[NSNull null]]) {
  321. mainDictionary[kFIRIsMeasurementEnabled] = mainMeasurementEnabled;
  322. }
  323. // Add mainDictionary to uniqueMainCombinations if it isn't included yet.
  324. if (![uniqueMainCombinations containsObject:mainDictionary]) {
  325. [uniqueMainCombinations addObject:mainDictionary];
  326. }
  327. // Generate the expected options by adding main values on top of the service options
  328. // values. The main values will replace any existing options values with the same
  329. // key. This is a different way of combining the two sets of flags from the actual
  330. // implementation in FIROptions, with equivalent output.
  331. NSMutableDictionary *expectedAnalyticsOptions =
  332. [[NSMutableDictionary alloc] initWithDictionary:optionsDictionary];
  333. [expectedAnalyticsOptions addEntriesFromDictionary:mainDictionary];
  334. NSDictionary *analyticsOptions =
  335. [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  336. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  337. combinationCount++;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. }
  344. }
  345. // Verify the sanity checks.
  346. XCTAssertEqual(combinationCount, 729); // = 3^6.
  347. XCTAssertEqual(uniqueOptionsCombinations.count, 27);
  348. int optionsSizeCount[4] = {0};
  349. for (NSDictionary *dictionary in uniqueOptionsCombinations) {
  350. optionsSizeCount[dictionary.count]++;
  351. }
  352. XCTAssertEqual(optionsSizeCount[0], 1);
  353. XCTAssertEqual(optionsSizeCount[1], 6);
  354. XCTAssertEqual(optionsSizeCount[2], 12);
  355. XCTAssertEqual(optionsSizeCount[3], 8);
  356. XCTAssertEqual(uniqueMainCombinations.count, 27);
  357. int mainSizeCount[4] = {0};
  358. for (NSDictionary *dictionary in uniqueMainCombinations) {
  359. mainSizeCount[dictionary.count]++;
  360. }
  361. XCTAssertEqual(mainSizeCount[0], 1);
  362. XCTAssertEqual(mainSizeCount[1], 6);
  363. XCTAssertEqual(mainSizeCount[2], 12);
  364. XCTAssertEqual(mainSizeCount[3], 8);
  365. }
  366. - (void)testAnalyticsCollectionGlobalSwitchEnabled {
  367. // Stub the default app, and set the global switch to YES.
  368. id appMock = OCMClassMock([FIRApp class]);
  369. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  370. OCMStub([appMock defaultApp]).andReturn(appMock);
  371. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(YES);
  372. // With no other settings, Analytics collection should default to the app's flag.
  373. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  374. XCTAssertTrue(options.isAnalyticsCollectionEnabled);
  375. XCTAssertTrue(options.isMeasurementEnabled);
  376. [appMock stopMocking];
  377. }
  378. - (void)testAnalyticsCollectionGlobalSwitchDisabled {
  379. // Stub the default app, and set the global switch to NO.
  380. id appMock = OCMClassMock([FIRApp class]);
  381. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  382. OCMStub([appMock defaultApp]).andReturn(appMock);
  383. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(NO);
  384. // With no other settings, Analytics collection should default to the app's flag.
  385. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  386. XCTAssertFalse(options.isAnalyticsCollectionEnabled);
  387. XCTAssertFalse(options.isMeasurementEnabled);
  388. [appMock stopMocking];
  389. }
  390. - (void)testAnalyticsCollectionGlobalSwitchOverrideToDisable {
  391. // Stub the default app, and set the global switch to YES.
  392. id appMock = OCMClassMock([FIRApp class]);
  393. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  394. OCMStub([appMock defaultApp]).andReturn(appMock);
  395. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(YES);
  396. // Test the three Analytics flags that override to disable Analytics collection.
  397. FIROptions *collectionEnabledOptions = [[FIROptions alloc]
  398. initInternalWithOptionsDictionary:@{kFIRIsAnalyticsCollectionEnabled : @NO}];
  399. XCTAssertFalse(collectionEnabledOptions.isAnalyticsCollectionEnabled);
  400. FIROptions *collectionDeactivatedOptions = [[FIROptions alloc]
  401. initInternalWithOptionsDictionary:@{kFIRIsAnalyticsCollectionDeactivated : @YES}];
  402. XCTAssertFalse(collectionDeactivatedOptions.isAnalyticsCollectionEnabled);
  403. FIROptions *measurementEnabledOptions =
  404. [[FIROptions alloc] initInternalWithOptionsDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  405. XCTAssertFalse(measurementEnabledOptions.isAnalyticsCollectionEnabled);
  406. }
  407. - (void)testAnalyticsCollectionGlobalSwitchOverrideToEnable {
  408. // Stub the default app, and set the global switch to YES.
  409. id appMock = OCMClassMock([FIRApp class]);
  410. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  411. OCMStub([appMock defaultApp]).andReturn(appMock);
  412. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(NO);
  413. // Test the two Analytics flags that can override and enable collection.
  414. FIROptions *collectionEnabledOptions = [[FIROptions alloc]
  415. initInternalWithOptionsDictionary:@{kFIRIsAnalyticsCollectionEnabled : @YES}];
  416. XCTAssertTrue(collectionEnabledOptions.isAnalyticsCollectionEnabled);
  417. FIROptions *measurementEnabledOptions =
  418. [[FIROptions alloc] initInternalWithOptionsDictionary:@{kFIRIsMeasurementEnabled : @YES}];
  419. XCTAssertTrue(measurementEnabledOptions.isAnalyticsCollectionEnabled);
  420. }
  421. - (void)testAnalyticsCollectionExplicitlySet {
  422. NSDictionary *optionsDictionary = @{};
  423. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  424. NSDictionary *analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  425. XCTAssertFalse([options isAnalyticsCollectionExplicitlySet]);
  426. // Test deactivation flag.
  427. optionsDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @YES};
  428. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  429. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  430. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  431. // If "deactivated" == NO, that doesn't mean it's explicitly set / enabled so it should be treated
  432. // as if it's not set.
  433. optionsDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @NO};
  434. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  435. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  436. XCTAssertFalse([options isAnalyticsCollectionExplicitlySet]);
  437. // Test the collection enabled flag.
  438. optionsDictionary = @{kFIRIsAnalyticsCollectionEnabled : @YES};
  439. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  440. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  441. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  442. optionsDictionary = @{kFIRIsAnalyticsCollectionEnabled : @NO};
  443. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  444. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  445. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  446. // Test the old measurement flag.
  447. options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  448. analyticsOptions =
  449. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @YES}];
  450. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  451. options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  452. analyticsOptions =
  453. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  454. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  455. // For good measure, a combination of all 3 (even if they conflict).
  456. optionsDictionary =
  457. @{kFIRIsAnalyticsCollectionDeactivated : @YES, kFIRIsAnalyticsCollectionEnabled : @YES};
  458. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  459. analyticsOptions =
  460. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  461. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  462. }
  463. - (void)testModifyingOptionsThrows {
  464. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  465. GCMSenderID:kGCMSenderID];
  466. options.editingLocked = YES;
  467. // Modification to every property should result in an exception.
  468. XCTAssertThrows(options.APIKey = @"should_throw");
  469. XCTAssertThrows(options.bundleID = @"should_throw");
  470. XCTAssertThrows(options.clientID = @"should_throw");
  471. XCTAssertThrows(options.databaseURL = @"should_throw");
  472. XCTAssertThrows(options.GCMSenderID = @"should_throw");
  473. XCTAssertThrows(options.googleAppID = @"should_throw");
  474. XCTAssertThrows(options.projectID = @"should_throw");
  475. XCTAssertThrows(options.storageBucket = @"should_throw");
  476. }
  477. - (void)testVersionFormat {
  478. // `kFIRLibraryVersionID` is `nil` until `libraryVersion` is called on `FIROptions`.
  479. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  480. GCMSenderID:kGCMSenderID];
  481. __unused NSString *libraryVersion = options.libraryVersionID;
  482. options = nil;
  483. NSRegularExpression *sLibraryVersionRegex =
  484. [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{8,}$" options:0 error:NULL];
  485. NSUInteger numberOfMatches =
  486. [sLibraryVersionRegex numberOfMatchesInString:kFIRLibraryVersionID
  487. options:0
  488. range:NSMakeRange(0, kFIRLibraryVersionID.length)];
  489. XCTAssertEqual(numberOfMatches, 1, @"Incorrect library version format.");
  490. }
  491. - (void)testVersionConsistency {
  492. // `kFIRLibraryVersionID` is `nil` until `libraryVersion` is called on `FIROptions`.
  493. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  494. GCMSenderID:kGCMSenderID];
  495. __unused NSString *libraryVersion = options.libraryVersionID;
  496. options = nil;
  497. // Now `kFIRLibraryVersionID` is assigned, test that it is formatted correctly.
  498. const char *versionString = [kFIRLibraryVersionID UTF8String];
  499. int major = (versionString[0] - '0') * 10 + versionString[1] - '0';
  500. int minor = (versionString[2] - '0') * 10 + versionString[3] - '0';
  501. int patch = (versionString[4] - '0') * 10 + versionString[5] - '0';
  502. NSString *str = [NSString stringWithFormat:@"%d.%d.%d", major, minor, patch];
  503. XCTAssertTrue([FIRFirebaseVersion() hasPrefix:str]);
  504. }
  505. // Repeat test with more Objective-C.
  506. - (void)testVersionConsistency2 {
  507. // `kFIRLibraryVersionID` is `nil` until `libraryVersion` is called on `FIROptions`.
  508. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  509. GCMSenderID:kGCMSenderID];
  510. __unused NSString *libraryVersion = options.libraryVersionID;
  511. options = nil;
  512. NSRange major = NSMakeRange(0, 2);
  513. NSRange minor = NSMakeRange(2, 2);
  514. NSRange patch = NSMakeRange(4, 2);
  515. NSString *str = [NSString
  516. stringWithFormat:@"%d.%d.%d", [[kFIRLibraryVersionID substringWithRange:major] intValue],
  517. [[kFIRLibraryVersionID substringWithRange:minor] intValue],
  518. [[kFIRLibraryVersionID substringWithRange:patch] intValue]];
  519. XCTAssertTrue([FIRFirebaseVersion() hasPrefix:str]);
  520. }
  521. #pragma mark - Helpers
  522. - (NSString *)validGoogleServicesInfoPlistPath {
  523. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info"
  524. ofType:@"plist"];
  525. if (filePath == nil) {
  526. // Use bundleForClass to allow GoogleService-Info.plist to be in the test target's bundle.
  527. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  528. filePath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  529. }
  530. XCTAssertNotNil(filePath);
  531. return filePath;
  532. }
  533. @end