FIROptionsTest.m 28 KB

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