FIROptionsTest.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 "FIRTestCase.h"
  15. #import <FirebaseCore/FIRAppInternal.h>
  16. #import <FirebaseCore/FIRBundleUtil.h>
  17. #import <FirebaseCore/FIROptionsInternal.h>
  18. #import <FirebaseCore/FIRVersion.h>
  19. extern NSString *const kFIRIsMeasurementEnabled;
  20. extern NSString *const kFIRIsAnalyticsCollectionEnabled;
  21. extern NSString *const kFIRIsAnalyticsCollectionDeactivated;
  22. extern NSString *const kFIRLibraryVersionID;
  23. @interface FIROptions (Test)
  24. - (nullable NSDictionary *)analyticsOptionsDictionaryWithInfoDictionary:
  25. (nullable NSDictionary *)infoDictionary;
  26. @end
  27. @interface FIROptionsTest : FIRTestCase
  28. @end
  29. @implementation FIROptionsTest
  30. - (void)setUp {
  31. [super setUp];
  32. [FIROptions resetDefaultOptions];
  33. }
  34. - (void)testInit {
  35. NSDictionary *optionsDictionary = [FIROptions defaultOptionsDictionary];
  36. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  37. [self assertOptionsMatchDefaults:options andProjectID:YES];
  38. XCTAssertNil(options.deepLinkURLScheme);
  39. XCTAssertTrue(options.usingOptionsFromDefaultPlist);
  40. options.deepLinkURLScheme = kDeepLinkURLScheme;
  41. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  42. }
  43. - (void)testDefaultOptionsDictionaryWithNilFilePath {
  44. id mockBundleUtil = OCMClassMock([FIRBundleUtil class]);
  45. [OCMStub([mockBundleUtil optionsDictionaryPathWithResourceName:kServiceInfoFileName
  46. andFileType:kServiceInfoFileType
  47. inBundles:[FIRBundleUtil relevantBundles]])
  48. andReturn:nil];
  49. XCTAssertNil([FIROptions defaultOptionsDictionary]);
  50. }
  51. - (void)testDefaultOptionsDictionaryWithInvalidSourceFile {
  52. id mockBundleUtil = OCMClassMock([FIRBundleUtil class]);
  53. [OCMStub([mockBundleUtil optionsDictionaryPathWithResourceName:kServiceInfoFileName
  54. andFileType:kServiceInfoFileType
  55. inBundles:[FIRBundleUtil relevantBundles]])
  56. andReturn:@"invalid.plist"];
  57. XCTAssertNil([FIROptions defaultOptionsDictionary]);
  58. }
  59. - (void)testDefaultOptions {
  60. FIROptions *options = [FIROptions defaultOptions];
  61. [self assertOptionsMatchDefaults:options andProjectID:YES];
  62. XCTAssertNil(options.deepLinkURLScheme);
  63. XCTAssertTrue(options.usingOptionsFromDefaultPlist);
  64. options.deepLinkURLScheme = kDeepLinkURLScheme;
  65. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  66. }
  67. - (void)testInitCustomizedOptions {
  68. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  69. GCMSenderID:kGCMSenderID];
  70. options.APIKey = kAPIKey;
  71. options.bundleID = kBundleID;
  72. options.clientID = kClientID;
  73. options.databaseURL = kDatabaseURL;
  74. options.deepLinkURLScheme = kDeepLinkURLScheme;
  75. options.projectID = kProjectID;
  76. options.storageBucket = kStorageBucket;
  77. options.trackingID = kTrackingID;
  78. [self assertOptionsMatchDefaults:options andProjectID:YES];
  79. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  80. XCTAssertFalse(options.usingOptionsFromDefaultPlist);
  81. }
  82. - (void)testInitWithContentsOfFile {
  83. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info"
  84. ofType:@"plist"];
  85. if (filePath == nil) {
  86. // Use bundleForClass to allow GoogleService-Info.plist to be in the test target's bundle.
  87. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  88. filePath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  89. }
  90. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
  91. [self assertOptionsMatchDefaults:options andProjectID:YES];
  92. XCTAssertNil(options.deepLinkURLScheme);
  93. XCTAssertFalse(options.usingOptionsFromDefaultPlist);
  94. #pragma clang diagnostic push
  95. #pragma clang diagnostic ignored "-Wnonnull"
  96. FIROptions *emptyOptions = [[FIROptions alloc] initWithContentsOfFile:nil];
  97. #pragma clang diagnostic pop
  98. XCTAssertNil(emptyOptions);
  99. FIROptions *invalidOptions = [[FIROptions alloc] initWithContentsOfFile:@"invalid.plist"];
  100. XCTAssertNil(invalidOptions);
  101. }
  102. - (void)assertOptionsMatchDefaults:(FIROptions *)options andProjectID:(BOOL)matchProjectID {
  103. XCTAssertEqualObjects(options.googleAppID, kGoogleAppID);
  104. XCTAssertEqualObjects(options.APIKey, kAPIKey);
  105. XCTAssertEqualObjects(options.clientID, kClientID);
  106. XCTAssertEqualObjects(options.trackingID, kTrackingID);
  107. XCTAssertEqualObjects(options.GCMSenderID, kGCMSenderID);
  108. XCTAssertNil(options.androidClientID);
  109. XCTAssertEqualObjects(options.libraryVersionID, kFIRLibraryVersionID);
  110. XCTAssertEqualObjects(options.databaseURL, kDatabaseURL);
  111. XCTAssertEqualObjects(options.storageBucket, kStorageBucket);
  112. XCTAssertEqualObjects(options.bundleID, kBundleID);
  113. // Custom `matchProjectID` parameter to be removed once the deprecated `FIROptions` constructor is
  114. // removed.
  115. if (matchProjectID) {
  116. XCTAssertEqualObjects(options.projectID, kProjectID);
  117. }
  118. }
  119. - (void)testCopyingProperties {
  120. NSMutableString *mutableString;
  121. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  122. GCMSenderID:kGCMSenderID];
  123. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  124. options.APIKey = mutableString;
  125. [mutableString appendString:@"2"];
  126. XCTAssertEqualObjects(options.APIKey, @"1");
  127. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  128. options.bundleID = mutableString;
  129. [mutableString appendString:@"2"];
  130. XCTAssertEqualObjects(options.bundleID, @"1");
  131. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  132. options.clientID = mutableString;
  133. [mutableString appendString:@"2"];
  134. XCTAssertEqualObjects(options.clientID, @"1");
  135. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  136. options.trackingID = mutableString;
  137. [mutableString appendString:@"2"];
  138. XCTAssertEqualObjects(options.trackingID, @"1");
  139. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  140. options.GCMSenderID = mutableString;
  141. [mutableString appendString:@"2"];
  142. XCTAssertEqualObjects(options.GCMSenderID, @"1");
  143. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  144. options.projectID = mutableString;
  145. [mutableString appendString:@"2"];
  146. XCTAssertEqualObjects(options.projectID, @"1");
  147. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  148. options.androidClientID = mutableString;
  149. [mutableString appendString:@"2"];
  150. XCTAssertEqualObjects(options.androidClientID, @"1");
  151. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  152. options.googleAppID = mutableString;
  153. [mutableString appendString:@"2"];
  154. XCTAssertEqualObjects(options.googleAppID, @"1");
  155. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  156. options.databaseURL = mutableString;
  157. [mutableString appendString:@"2"];
  158. XCTAssertEqualObjects(options.databaseURL, @"1");
  159. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  160. options.deepLinkURLScheme = mutableString;
  161. [mutableString appendString:@"2"];
  162. XCTAssertEqualObjects(options.deepLinkURLScheme, @"1");
  163. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  164. options.storageBucket = mutableString;
  165. [mutableString appendString:@"2"];
  166. XCTAssertEqualObjects(options.storageBucket, @"1");
  167. }
  168. - (void)testCopyWithZone {
  169. // default options
  170. FIROptions *options = [FIROptions defaultOptions];
  171. options.deepLinkURLScheme = kDeepLinkURLScheme;
  172. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  173. FIROptions *newOptions = [options copy];
  174. XCTAssertEqualObjects(newOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  175. [options setDeepLinkURLScheme:kNewDeepLinkURLScheme];
  176. XCTAssertEqualObjects(options.deepLinkURLScheme, kNewDeepLinkURLScheme);
  177. XCTAssertEqualObjects(newOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  178. // customized options
  179. FIROptions *customizedOptions = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  180. GCMSenderID:kGCMSenderID];
  181. customizedOptions.deepLinkURLScheme = kDeepLinkURLScheme;
  182. FIROptions *copyCustomizedOptions = [customizedOptions copy];
  183. [copyCustomizedOptions setDeepLinkURLScheme:kNewDeepLinkURLScheme];
  184. XCTAssertEqualObjects(customizedOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  185. XCTAssertEqualObjects(copyCustomizedOptions.deepLinkURLScheme, kNewDeepLinkURLScheme);
  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 isAnalyticsCollectionExpicitlySet]);
  436. // Test deactivation flag.
  437. optionsDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @YES};
  438. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  439. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  440. XCTAssertTrue([options isAnalyticsCollectionExpicitlySet]);
  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 isAnalyticsCollectionExpicitlySet]);
  447. // Test the collection enabled flag.
  448. optionsDictionary = @{kFIRIsAnalyticsCollectionEnabled : @YES};
  449. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  450. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  451. XCTAssertTrue([options isAnalyticsCollectionExpicitlySet]);
  452. optionsDictionary = @{kFIRIsAnalyticsCollectionEnabled : @NO};
  453. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  454. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  455. XCTAssertTrue([options isAnalyticsCollectionExpicitlySet]);
  456. // Test the old measurement flag.
  457. options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  458. analyticsOptions =
  459. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @YES}];
  460. XCTAssertTrue([options isAnalyticsCollectionExpicitlySet]);
  461. options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  462. analyticsOptions =
  463. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  464. XCTAssertTrue([options isAnalyticsCollectionExpicitlySet]);
  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 isAnalyticsCollectionExpicitlySet]);
  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.androidClientID = @"should_throw");
  479. XCTAssertThrows(options.APIKey = @"should_throw");
  480. XCTAssertThrows(options.bundleID = @"should_throw");
  481. XCTAssertThrows(options.clientID = @"should_throw");
  482. XCTAssertThrows(options.databaseURL = @"should_throw");
  483. XCTAssertThrows(options.deepLinkURLScheme = @"should_throw");
  484. XCTAssertThrows(options.GCMSenderID = @"should_throw");
  485. XCTAssertThrows(options.googleAppID = @"should_throw");
  486. XCTAssertThrows(options.projectID = @"should_throw");
  487. XCTAssertThrows(options.storageBucket = @"should_throw");
  488. XCTAssertThrows(options.trackingID = @"should_throw");
  489. }
  490. - (void)testVersionFormat {
  491. NSRegularExpression *sLibraryVersionRegex =
  492. [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{8,}$" options:0 error:NULL];
  493. NSUInteger numberOfMatches =
  494. [sLibraryVersionRegex numberOfMatchesInString:kFIRLibraryVersionID
  495. options:0
  496. range:NSMakeRange(0, kFIRLibraryVersionID.length)];
  497. XCTAssertEqual(numberOfMatches, 1, @"Incorrect library version format.");
  498. }
  499. // TODO: The version test will break when the Firebase major version hits 10.
  500. - (void)testVersionConsistency {
  501. const char *versionString = [kFIRLibraryVersionID UTF8String];
  502. int major = versionString[0] - '0';
  503. int minor = (versionString[1] - '0') * 10 + versionString[2] - '0';
  504. int patch = (versionString[3] - '0') * 10 + versionString[4] - '0';
  505. NSString *str = [NSString stringWithFormat:@"%d.%d.%d", major, minor, patch];
  506. XCTAssertEqualObjects(str, [NSString stringWithUTF8String:(const char *)FIRCoreVersionString]);
  507. }
  508. // Repeat test with more Objective-C.
  509. // TODO: The version test will break when the Firebase major version hits 10.
  510. - (void)testVersionConsistency2 {
  511. NSRange major = NSMakeRange(0, 1);
  512. NSRange minor = NSMakeRange(1, 2);
  513. NSRange patch = NSMakeRange(3, 2);
  514. NSString *str =
  515. [NSString stringWithFormat:@"%@.%d.%d", [kFIRLibraryVersionID substringWithRange:major],
  516. [[kFIRLibraryVersionID substringWithRange:minor] intValue],
  517. [[kFIRLibraryVersionID substringWithRange:patch] intValue]];
  518. XCTAssertEqualObjects(str, [NSString stringWithUTF8String:(const char *)FIRCoreVersionString]);
  519. }
  520. @end