FIROptionsTest.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  168. options.appGroupID = mutableString;
  169. [mutableString appendString:@"2"];
  170. XCTAssertEqualObjects(options.appGroupID, @"1");
  171. }
  172. - (void)testCopyWithZone {
  173. // default options
  174. FIROptions *options = [FIROptions defaultOptions];
  175. options.deepLinkURLScheme = kDeepLinkURLScheme;
  176. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  177. FIROptions *newOptions = [options copy];
  178. XCTAssertEqualObjects(newOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  179. [options setDeepLinkURLScheme:kNewDeepLinkURLScheme];
  180. XCTAssertEqualObjects(options.deepLinkURLScheme, kNewDeepLinkURLScheme);
  181. XCTAssertEqualObjects(newOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  182. // customized options
  183. FIROptions *customizedOptions = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  184. GCMSenderID:kGCMSenderID];
  185. customizedOptions.deepLinkURLScheme = kDeepLinkURLScheme;
  186. FIROptions *copyCustomizedOptions = [customizedOptions copy];
  187. [copyCustomizedOptions setDeepLinkURLScheme:kNewDeepLinkURLScheme];
  188. XCTAssertEqualObjects(customizedOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  189. XCTAssertEqualObjects(copyCustomizedOptions.deepLinkURLScheme, kNewDeepLinkURLScheme);
  190. }
  191. - (void)testAnalyticsConstants {
  192. // The keys are public values and should never change.
  193. XCTAssertEqualObjects(kFIRIsMeasurementEnabled, @"IS_MEASUREMENT_ENABLED");
  194. XCTAssertEqualObjects(kFIRIsAnalyticsCollectionEnabled, @"FIREBASE_ANALYTICS_COLLECTION_ENABLED");
  195. XCTAssertEqualObjects(kFIRIsAnalyticsCollectionDeactivated,
  196. @"FIREBASE_ANALYTICS_COLLECTION_DEACTIVATED");
  197. }
  198. - (void)testAnalyticsOptions {
  199. // No keys anywhere.
  200. NSDictionary *optionsDictionary = nil;
  201. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  202. NSDictionary *mainDictionary = nil;
  203. NSDictionary *expectedAnalyticsOptions = @{};
  204. NSDictionary *analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:nil];
  205. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  206. optionsDictionary = @{};
  207. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  208. mainDictionary = @{};
  209. expectedAnalyticsOptions = @{};
  210. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  211. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  212. // Main has no keys.
  213. optionsDictionary = @{
  214. kFIRIsAnalyticsCollectionDeactivated : @YES,
  215. kFIRIsAnalyticsCollectionEnabled : @YES,
  216. kFIRIsMeasurementEnabled : @YES
  217. };
  218. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  219. mainDictionary = @{};
  220. expectedAnalyticsOptions = optionsDictionary;
  221. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  222. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  223. // Main overrides all the keys.
  224. optionsDictionary = @{
  225. kFIRIsAnalyticsCollectionDeactivated : @YES,
  226. kFIRIsAnalyticsCollectionEnabled : @YES,
  227. kFIRIsMeasurementEnabled : @YES
  228. };
  229. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  230. mainDictionary = @{
  231. kFIRIsAnalyticsCollectionDeactivated : @NO,
  232. kFIRIsAnalyticsCollectionEnabled : @NO,
  233. kFIRIsMeasurementEnabled : @NO
  234. };
  235. expectedAnalyticsOptions = mainDictionary;
  236. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  237. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  238. // Keys exist only in main.
  239. optionsDictionary = @{};
  240. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  241. mainDictionary = @{
  242. kFIRIsAnalyticsCollectionDeactivated : @YES,
  243. kFIRIsAnalyticsCollectionEnabled : @YES,
  244. kFIRIsMeasurementEnabled : @YES
  245. };
  246. expectedAnalyticsOptions = mainDictionary;
  247. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  248. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  249. // Main overrides single keys.
  250. optionsDictionary = @{
  251. kFIRIsAnalyticsCollectionDeactivated : @YES,
  252. kFIRIsAnalyticsCollectionEnabled : @YES,
  253. kFIRIsMeasurementEnabled : @YES
  254. };
  255. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  256. mainDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @NO};
  257. expectedAnalyticsOptions = @{
  258. kFIRIsAnalyticsCollectionDeactivated : @NO, // override
  259. kFIRIsAnalyticsCollectionEnabled : @YES,
  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 = @{kFIRIsAnalyticsCollectionEnabled : @NO};
  271. expectedAnalyticsOptions = @{
  272. kFIRIsAnalyticsCollectionDeactivated : @YES,
  273. kFIRIsAnalyticsCollectionEnabled : @NO, // override
  274. kFIRIsMeasurementEnabled : @YES
  275. };
  276. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  277. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  278. optionsDictionary = @{
  279. kFIRIsAnalyticsCollectionDeactivated : @YES,
  280. kFIRIsAnalyticsCollectionEnabled : @YES,
  281. kFIRIsMeasurementEnabled : @YES
  282. };
  283. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  284. mainDictionary = @{kFIRIsMeasurementEnabled : @NO};
  285. expectedAnalyticsOptions = @{
  286. kFIRIsAnalyticsCollectionDeactivated : @YES,
  287. kFIRIsAnalyticsCollectionEnabled : @YES,
  288. kFIRIsMeasurementEnabled : @NO // override
  289. };
  290. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  291. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  292. }
  293. - (void)testAnalyticsOptions_combinatorial {
  294. // Complete combinatorial test.
  295. // Possible values for the flags in the plist, where NSNull means the flag is not present.
  296. NSArray *values = @[ [NSNull null], @NO, @YES ];
  297. // Sanity checks for the combination generation.
  298. int combinationCount = 0;
  299. NSMutableArray *uniqueMainCombinations = [[NSMutableArray alloc] init];
  300. NSMutableArray *uniqueOptionsCombinations = [[NSMutableArray alloc] init];
  301. // Generate all optout flag combinations for { main plist X GoogleService-info options plist }.
  302. // Options present in the main plist should override options of the same key in the service plist.
  303. for (id mainDeactivated in values) {
  304. for (id mainAnalyticsEnabled in values) {
  305. for (id mainMeasurementEnabled in values) {
  306. for (id optionsDeactivated in values) {
  307. for (id optionsAnalyticsEnabled in values) {
  308. for (id optionsMeasurementEnabled in values) {
  309. @autoreleasepool {
  310. // Fill the GoogleService-info options plist dictionary.
  311. NSMutableDictionary *optionsDictionary = [[NSMutableDictionary alloc] init];
  312. if (![optionsDeactivated isEqual:[NSNull null]]) {
  313. optionsDictionary[kFIRIsAnalyticsCollectionDeactivated] = optionsDeactivated;
  314. }
  315. if (![optionsAnalyticsEnabled isEqual:[NSNull null]]) {
  316. optionsDictionary[kFIRIsAnalyticsCollectionEnabled] = optionsAnalyticsEnabled;
  317. }
  318. if (![optionsMeasurementEnabled isEqual:[NSNull null]]) {
  319. optionsDictionary[kFIRIsMeasurementEnabled] = optionsMeasurementEnabled;
  320. }
  321. FIROptions *options =
  322. [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  323. if (![uniqueOptionsCombinations containsObject:optionsDictionary]) {
  324. [uniqueOptionsCombinations addObject:optionsDictionary];
  325. }
  326. // Fill the main plist dictionary.
  327. NSMutableDictionary *mainDictionary = [[NSMutableDictionary alloc] init];
  328. if (![mainDeactivated isEqual:[NSNull null]]) {
  329. mainDictionary[kFIRIsAnalyticsCollectionDeactivated] = mainDeactivated;
  330. }
  331. if (![mainAnalyticsEnabled isEqual:[NSNull null]]) {
  332. mainDictionary[kFIRIsAnalyticsCollectionEnabled] = mainAnalyticsEnabled;
  333. }
  334. if (![mainMeasurementEnabled isEqual:[NSNull null]]) {
  335. mainDictionary[kFIRIsMeasurementEnabled] = mainMeasurementEnabled;
  336. }
  337. // Add mainDictionary to uniqueMainCombinations if it isn't included yet.
  338. if (![uniqueMainCombinations containsObject:mainDictionary]) {
  339. [uniqueMainCombinations addObject:mainDictionary];
  340. }
  341. // Generate the expected options by adding main values on top of the service options
  342. // values. The main values will replace any existing options values with the same
  343. // key. This is a different way of combining the two sets of flags from the actual
  344. // implementation in FIROptions, with equivalent output.
  345. NSMutableDictionary *expectedAnalyticsOptions =
  346. [[NSMutableDictionary alloc] initWithDictionary:optionsDictionary];
  347. [expectedAnalyticsOptions addEntriesFromDictionary:mainDictionary];
  348. NSDictionary *analyticsOptions =
  349. [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  350. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  351. combinationCount++;
  352. }
  353. }
  354. }
  355. }
  356. }
  357. }
  358. }
  359. // Verify the sanity checks.
  360. XCTAssertEqual(combinationCount, 729); // = 3^6.
  361. XCTAssertEqual(uniqueOptionsCombinations.count, 27);
  362. int optionsSizeCount[4] = {0};
  363. for (NSDictionary *dictionary in uniqueOptionsCombinations) {
  364. optionsSizeCount[dictionary.count]++;
  365. }
  366. XCTAssertEqual(optionsSizeCount[0], 1);
  367. XCTAssertEqual(optionsSizeCount[1], 6);
  368. XCTAssertEqual(optionsSizeCount[2], 12);
  369. XCTAssertEqual(optionsSizeCount[3], 8);
  370. XCTAssertEqual(uniqueMainCombinations.count, 27);
  371. int mainSizeCount[4] = {0};
  372. for (NSDictionary *dictionary in uniqueMainCombinations) {
  373. mainSizeCount[dictionary.count]++;
  374. }
  375. XCTAssertEqual(mainSizeCount[0], 1);
  376. XCTAssertEqual(mainSizeCount[1], 6);
  377. XCTAssertEqual(mainSizeCount[2], 12);
  378. XCTAssertEqual(mainSizeCount[3], 8);
  379. }
  380. - (void)testAnalyticsCollectionGlobalSwitchEnabled {
  381. // Stub the default app, and set the global switch to YES.
  382. id appMock = OCMClassMock([FIRApp class]);
  383. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  384. OCMStub([appMock defaultApp]).andReturn(appMock);
  385. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(YES);
  386. // With no other settings, Analytics collection should default to the app's flag.
  387. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  388. XCTAssertTrue(options.isAnalyticsCollectionEnabled);
  389. XCTAssertTrue(options.isMeasurementEnabled);
  390. [appMock stopMocking];
  391. }
  392. - (void)testAnalyticsCollectionGlobalSwitchDisabled {
  393. // Stub the default app, and set the global switch to NO.
  394. id appMock = OCMClassMock([FIRApp class]);
  395. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  396. OCMStub([appMock defaultApp]).andReturn(appMock);
  397. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(NO);
  398. // With no other settings, Analytics collection should default to the app's flag.
  399. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  400. XCTAssertFalse(options.isAnalyticsCollectionEnabled);
  401. XCTAssertFalse(options.isMeasurementEnabled);
  402. [appMock stopMocking];
  403. }
  404. - (void)testAnalyticsCollectionGlobalSwitchOverrideToDisable {
  405. // Stub the default app, and set the global switch to YES.
  406. id appMock = OCMClassMock([FIRApp class]);
  407. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  408. OCMStub([appMock defaultApp]).andReturn(appMock);
  409. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(YES);
  410. // Test the three Analytics flags that override to disable Analytics collection.
  411. FIROptions *collectionEnabledOptions = [[FIROptions alloc]
  412. initInternalWithOptionsDictionary:@{kFIRIsAnalyticsCollectionEnabled : @NO}];
  413. XCTAssertFalse(collectionEnabledOptions.isAnalyticsCollectionEnabled);
  414. FIROptions *collectionDeactivatedOptions = [[FIROptions alloc]
  415. initInternalWithOptionsDictionary:@{kFIRIsAnalyticsCollectionDeactivated : @YES}];
  416. XCTAssertFalse(collectionDeactivatedOptions.isAnalyticsCollectionEnabled);
  417. FIROptions *measurementEnabledOptions =
  418. [[FIROptions alloc] initInternalWithOptionsDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  419. XCTAssertFalse(measurementEnabledOptions.isAnalyticsCollectionEnabled);
  420. }
  421. - (void)testAnalyticsCollectionGlobalSwitchOverrideToEnable {
  422. // Stub the default app, and set the global switch to YES.
  423. id appMock = OCMClassMock([FIRApp class]);
  424. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  425. OCMStub([appMock defaultApp]).andReturn(appMock);
  426. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(NO);
  427. // Test the two Analytics flags that can override and enable collection.
  428. FIROptions *collectionEnabledOptions = [[FIROptions alloc]
  429. initInternalWithOptionsDictionary:@{kFIRIsAnalyticsCollectionEnabled : @YES}];
  430. XCTAssertTrue(collectionEnabledOptions.isAnalyticsCollectionEnabled);
  431. FIROptions *measurementEnabledOptions =
  432. [[FIROptions alloc] initInternalWithOptionsDictionary:@{kFIRIsMeasurementEnabled : @YES}];
  433. XCTAssertTrue(measurementEnabledOptions.isAnalyticsCollectionEnabled);
  434. }
  435. - (void)testAnalyticsCollectionExplicitlySet {
  436. NSDictionary *optionsDictionary = @{};
  437. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  438. NSDictionary *analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  439. XCTAssertFalse([options isAnalyticsCollectionExplicitlySet]);
  440. // Test deactivation flag.
  441. optionsDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @YES};
  442. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  443. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  444. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  445. // If "deactivated" == NO, that doesn't mean it's explicitly set / enabled so it should be treated
  446. // as if it's not set.
  447. optionsDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @NO};
  448. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  449. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  450. XCTAssertFalse([options isAnalyticsCollectionExplicitlySet]);
  451. // Test the collection enabled flag.
  452. optionsDictionary = @{kFIRIsAnalyticsCollectionEnabled : @YES};
  453. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  454. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  455. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  456. optionsDictionary = @{kFIRIsAnalyticsCollectionEnabled : @NO};
  457. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  458. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  459. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  460. // Test the old measurement flag.
  461. options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  462. analyticsOptions =
  463. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @YES}];
  464. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  465. options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  466. analyticsOptions =
  467. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  468. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  469. // For good measure, a combination of all 3 (even if they conflict).
  470. optionsDictionary =
  471. @{kFIRIsAnalyticsCollectionDeactivated : @YES, kFIRIsAnalyticsCollectionEnabled : @YES};
  472. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  473. analyticsOptions =
  474. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  475. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  476. }
  477. - (void)testModifyingOptionsThrows {
  478. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  479. GCMSenderID:kGCMSenderID];
  480. options.editingLocked = YES;
  481. // Modification to every property should result in an exception.
  482. XCTAssertThrows(options.androidClientID = @"should_throw");
  483. XCTAssertThrows(options.APIKey = @"should_throw");
  484. XCTAssertThrows(options.bundleID = @"should_throw");
  485. XCTAssertThrows(options.clientID = @"should_throw");
  486. XCTAssertThrows(options.databaseURL = @"should_throw");
  487. XCTAssertThrows(options.deepLinkURLScheme = @"should_throw");
  488. XCTAssertThrows(options.GCMSenderID = @"should_throw");
  489. XCTAssertThrows(options.googleAppID = @"should_throw");
  490. XCTAssertThrows(options.projectID = @"should_throw");
  491. XCTAssertThrows(options.storageBucket = @"should_throw");
  492. XCTAssertThrows(options.trackingID = @"should_throw");
  493. }
  494. - (void)testVersionFormat {
  495. NSRegularExpression *sLibraryVersionRegex =
  496. [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{8,}$" options:0 error:NULL];
  497. NSUInteger numberOfMatches =
  498. [sLibraryVersionRegex numberOfMatchesInString:kFIRLibraryVersionID
  499. options:0
  500. range:NSMakeRange(0, kFIRLibraryVersionID.length)];
  501. XCTAssertEqual(numberOfMatches, 1, @"Incorrect library version format.");
  502. }
  503. // TODO: The version test will break when the Firebase major version hits 10.
  504. - (void)testVersionConsistency {
  505. const char *versionString = [kFIRLibraryVersionID UTF8String];
  506. int major = versionString[0] - '0';
  507. int minor = (versionString[1] - '0') * 10 + versionString[2] - '0';
  508. int patch = (versionString[3] - '0') * 10 + versionString[4] - '0';
  509. NSString *str = [NSString stringWithFormat:@"%d.%d.%d", major, minor, patch];
  510. XCTAssertEqualObjects(str, [NSString stringWithUTF8String:(const char *)FIRCoreVersionString]);
  511. }
  512. // Repeat test with more Objective-C.
  513. // TODO: The version test will break when the Firebase major version hits 10.
  514. - (void)testVersionConsistency2 {
  515. NSRange major = NSMakeRange(0, 1);
  516. NSRange minor = NSMakeRange(1, 2);
  517. NSRange patch = NSMakeRange(3, 2);
  518. NSString *str =
  519. [NSString stringWithFormat:@"%@.%d.%d", [kFIRLibraryVersionID substringWithRange:major],
  520. [[kFIRLibraryVersionID substringWithRange:minor] intValue],
  521. [[kFIRLibraryVersionID substringWithRange:patch] intValue]];
  522. XCTAssertEqualObjects(str, [NSString stringWithUTF8String:(const char *)FIRCoreVersionString]);
  523. }
  524. @end