FIROptionsTest.m 30 KB

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