FIROptionsTest.m 30 KB

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