FIROptionsTest.m 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseCore/Tests/Unit/FIRTestCase.h"
  15. #import "FirebaseCore/Extension/FIRAppInternal.h"
  16. #import "FirebaseCore/Extension/FIROptionsInternal.h"
  17. #import "FirebaseCore/Sources/FIRBundleUtil.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. [self assertOptionsMatchDefaults:options andProjectID:YES];
  127. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  128. XCTAssertFalse(options.usingOptionsFromDefaultPlist);
  129. }
  130. - (void)assertOptionsMatchDefaults:(FIROptions *)options andProjectID:(BOOL)matchProjectID {
  131. XCTAssertEqualObjects(options.googleAppID, kGoogleAppID);
  132. XCTAssertEqualObjects(options.APIKey, kAPIKey);
  133. XCTAssertEqualObjects(options.clientID, kClientID);
  134. XCTAssertEqualObjects(options.GCMSenderID, kGCMSenderID);
  135. XCTAssertEqualObjects(options.libraryVersionID, kFIRLibraryVersionID);
  136. XCTAssertEqualObjects(options.databaseURL, kDatabaseURL);
  137. XCTAssertEqualObjects(options.storageBucket, kStorageBucket);
  138. XCTAssertEqualObjects(options.bundleID, kBundleID);
  139. // Custom `matchProjectID` parameter to be removed once the deprecated `FIROptions` constructor is
  140. // removed.
  141. if (matchProjectID) {
  142. XCTAssertEqualObjects(options.projectID, kProjectID);
  143. }
  144. }
  145. - (void)testCopyingProperties {
  146. NSMutableString *mutableString;
  147. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  148. GCMSenderID:kGCMSenderID];
  149. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  150. options.APIKey = mutableString;
  151. [mutableString appendString:@"2"];
  152. XCTAssertEqualObjects(options.APIKey, @"1");
  153. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  154. options.bundleID = mutableString;
  155. [mutableString appendString:@"2"];
  156. XCTAssertEqualObjects(options.bundleID, @"1");
  157. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  158. options.clientID = mutableString;
  159. [mutableString appendString:@"2"];
  160. XCTAssertEqualObjects(options.clientID, @"1");
  161. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  162. options.GCMSenderID = mutableString;
  163. [mutableString appendString:@"2"];
  164. XCTAssertEqualObjects(options.GCMSenderID, @"1");
  165. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  166. options.projectID = mutableString;
  167. [mutableString appendString:@"2"];
  168. XCTAssertEqualObjects(options.projectID, @"1");
  169. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  170. options.googleAppID = mutableString;
  171. [mutableString appendString:@"2"];
  172. XCTAssertEqualObjects(options.googleAppID, @"1");
  173. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  174. options.databaseURL = mutableString;
  175. [mutableString appendString:@"2"];
  176. XCTAssertEqualObjects(options.databaseURL, @"1");
  177. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  178. options.deepLinkURLScheme = mutableString;
  179. [mutableString appendString:@"2"];
  180. XCTAssertEqualObjects(options.deepLinkURLScheme, @"1");
  181. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  182. options.storageBucket = mutableString;
  183. [mutableString appendString:@"2"];
  184. XCTAssertEqualObjects(options.storageBucket, @"1");
  185. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  186. options.appGroupID = mutableString;
  187. [mutableString appendString:@"2"];
  188. XCTAssertEqualObjects(options.appGroupID, @"1");
  189. }
  190. - (void)testCopyWithZone {
  191. [FIROptionsMock mockFIROptions];
  192. // default options
  193. FIROptions *options = [FIROptions defaultOptions];
  194. options.deepLinkURLScheme = kDeepLinkURLScheme;
  195. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  196. FIROptions *newOptions = [options copy];
  197. XCTAssertEqualObjects(newOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  198. [options setDeepLinkURLScheme:kNewDeepLinkURLScheme];
  199. XCTAssertEqualObjects(options.deepLinkURLScheme, kNewDeepLinkURLScheme);
  200. XCTAssertEqualObjects(newOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  201. // customized options
  202. FIROptions *customizedOptions = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  203. GCMSenderID:kGCMSenderID];
  204. customizedOptions.deepLinkURLScheme = kDeepLinkURLScheme;
  205. FIROptions *copyCustomizedOptions = [customizedOptions copy];
  206. [copyCustomizedOptions setDeepLinkURLScheme:kNewDeepLinkURLScheme];
  207. XCTAssertEqualObjects(customizedOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  208. XCTAssertEqualObjects(copyCustomizedOptions.deepLinkURLScheme, kNewDeepLinkURLScheme);
  209. }
  210. - (void)testAnalyticsConstants {
  211. // The keys are public values and should never change.
  212. XCTAssertEqualObjects(kFIRIsMeasurementEnabled, @"IS_MEASUREMENT_ENABLED");
  213. XCTAssertEqualObjects(kFIRIsAnalyticsCollectionEnabled, @"FIREBASE_ANALYTICS_COLLECTION_ENABLED");
  214. XCTAssertEqualObjects(kFIRIsAnalyticsCollectionDeactivated,
  215. @"FIREBASE_ANALYTICS_COLLECTION_DEACTIVATED");
  216. }
  217. - (void)testAnalyticsOptions {
  218. // No keys anywhere.
  219. NSDictionary *optionsDictionary = nil;
  220. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  221. NSDictionary *mainDictionary = nil;
  222. NSDictionary *expectedAnalyticsOptions = @{};
  223. NSDictionary *analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:nil];
  224. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  225. optionsDictionary = @{};
  226. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  227. mainDictionary = @{};
  228. expectedAnalyticsOptions = @{};
  229. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  230. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  231. // Main has no keys.
  232. optionsDictionary = @{
  233. kFIRIsAnalyticsCollectionDeactivated : @YES,
  234. kFIRIsAnalyticsCollectionEnabled : @YES,
  235. kFIRIsMeasurementEnabled : @YES
  236. };
  237. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  238. mainDictionary = @{};
  239. expectedAnalyticsOptions = optionsDictionary;
  240. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  241. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  242. // Main overrides all the keys.
  243. optionsDictionary = @{
  244. kFIRIsAnalyticsCollectionDeactivated : @YES,
  245. kFIRIsAnalyticsCollectionEnabled : @YES,
  246. kFIRIsMeasurementEnabled : @YES
  247. };
  248. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  249. mainDictionary = @{
  250. kFIRIsAnalyticsCollectionDeactivated : @NO,
  251. kFIRIsAnalyticsCollectionEnabled : @NO,
  252. kFIRIsMeasurementEnabled : @NO
  253. };
  254. expectedAnalyticsOptions = mainDictionary;
  255. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  256. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  257. // Keys exist only in main.
  258. optionsDictionary = @{};
  259. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  260. mainDictionary = @{
  261. kFIRIsAnalyticsCollectionDeactivated : @YES,
  262. kFIRIsAnalyticsCollectionEnabled : @YES,
  263. kFIRIsMeasurementEnabled : @YES
  264. };
  265. expectedAnalyticsOptions = mainDictionary;
  266. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  267. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  268. // Main overrides single keys.
  269. optionsDictionary = @{
  270. kFIRIsAnalyticsCollectionDeactivated : @YES,
  271. kFIRIsAnalyticsCollectionEnabled : @YES,
  272. kFIRIsMeasurementEnabled : @YES
  273. };
  274. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  275. mainDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @NO};
  276. expectedAnalyticsOptions = @{
  277. kFIRIsAnalyticsCollectionDeactivated : @NO, // override
  278. kFIRIsAnalyticsCollectionEnabled : @YES,
  279. kFIRIsMeasurementEnabled : @YES
  280. };
  281. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  282. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  283. optionsDictionary = @{
  284. kFIRIsAnalyticsCollectionDeactivated : @YES,
  285. kFIRIsAnalyticsCollectionEnabled : @YES,
  286. kFIRIsMeasurementEnabled : @YES
  287. };
  288. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  289. mainDictionary = @{kFIRIsAnalyticsCollectionEnabled : @NO};
  290. expectedAnalyticsOptions = @{
  291. kFIRIsAnalyticsCollectionDeactivated : @YES,
  292. kFIRIsAnalyticsCollectionEnabled : @NO, // override
  293. kFIRIsMeasurementEnabled : @YES
  294. };
  295. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  296. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  297. optionsDictionary = @{
  298. kFIRIsAnalyticsCollectionDeactivated : @YES,
  299. kFIRIsAnalyticsCollectionEnabled : @YES,
  300. kFIRIsMeasurementEnabled : @YES
  301. };
  302. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  303. mainDictionary = @{kFIRIsMeasurementEnabled : @NO};
  304. expectedAnalyticsOptions = @{
  305. kFIRIsAnalyticsCollectionDeactivated : @YES,
  306. kFIRIsAnalyticsCollectionEnabled : @YES,
  307. kFIRIsMeasurementEnabled : @NO // override
  308. };
  309. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  310. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  311. }
  312. - (void)testAnalyticsOptions_combinatorial {
  313. // Complete combinatorial test.
  314. // Possible values for the flags in the plist, where NSNull means the flag is not present.
  315. NSArray *values = @[ [NSNull null], @NO, @YES ];
  316. // Sanity checks for the combination generation.
  317. int combinationCount = 0;
  318. NSMutableArray *uniqueMainCombinations = [[NSMutableArray alloc] init];
  319. NSMutableArray *uniqueOptionsCombinations = [[NSMutableArray alloc] init];
  320. // Generate all optout flag combinations for { main plist X GoogleService-info options plist }.
  321. // Options present in the main plist should override options of the same key in the service plist.
  322. for (id mainDeactivated in values) {
  323. for (id mainAnalyticsEnabled in values) {
  324. for (id mainMeasurementEnabled in values) {
  325. for (id optionsDeactivated in values) {
  326. for (id optionsAnalyticsEnabled in values) {
  327. for (id optionsMeasurementEnabled in values) {
  328. @autoreleasepool {
  329. // Fill the GoogleService-info options plist dictionary.
  330. NSMutableDictionary *optionsDictionary = [[NSMutableDictionary alloc] init];
  331. if (![optionsDeactivated isEqual:[NSNull null]]) {
  332. optionsDictionary[kFIRIsAnalyticsCollectionDeactivated] = optionsDeactivated;
  333. }
  334. if (![optionsAnalyticsEnabled isEqual:[NSNull null]]) {
  335. optionsDictionary[kFIRIsAnalyticsCollectionEnabled] = optionsAnalyticsEnabled;
  336. }
  337. if (![optionsMeasurementEnabled isEqual:[NSNull null]]) {
  338. optionsDictionary[kFIRIsMeasurementEnabled] = optionsMeasurementEnabled;
  339. }
  340. FIROptions *options =
  341. [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  342. if (![uniqueOptionsCombinations containsObject:optionsDictionary]) {
  343. [uniqueOptionsCombinations addObject:optionsDictionary];
  344. }
  345. // Fill the main plist dictionary.
  346. NSMutableDictionary *mainDictionary = [[NSMutableDictionary alloc] init];
  347. if (![mainDeactivated isEqual:[NSNull null]]) {
  348. mainDictionary[kFIRIsAnalyticsCollectionDeactivated] = mainDeactivated;
  349. }
  350. if (![mainAnalyticsEnabled isEqual:[NSNull null]]) {
  351. mainDictionary[kFIRIsAnalyticsCollectionEnabled] = mainAnalyticsEnabled;
  352. }
  353. if (![mainMeasurementEnabled isEqual:[NSNull null]]) {
  354. mainDictionary[kFIRIsMeasurementEnabled] = mainMeasurementEnabled;
  355. }
  356. // Add mainDictionary to uniqueMainCombinations if it isn't included yet.
  357. if (![uniqueMainCombinations containsObject:mainDictionary]) {
  358. [uniqueMainCombinations addObject:mainDictionary];
  359. }
  360. // Generate the expected options by adding main values on top of the service options
  361. // values. The main values will replace any existing options values with the same
  362. // key. This is a different way of combining the two sets of flags from the actual
  363. // implementation in FIROptions, with equivalent output.
  364. NSMutableDictionary *expectedAnalyticsOptions =
  365. [[NSMutableDictionary alloc] initWithDictionary:optionsDictionary];
  366. [expectedAnalyticsOptions addEntriesFromDictionary:mainDictionary];
  367. NSDictionary *analyticsOptions =
  368. [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  369. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  370. combinationCount++;
  371. }
  372. }
  373. }
  374. }
  375. }
  376. }
  377. }
  378. // Verify the sanity checks.
  379. XCTAssertEqual(combinationCount, 729); // = 3^6.
  380. XCTAssertEqual(uniqueOptionsCombinations.count, 27);
  381. int optionsSizeCount[4] = {0};
  382. for (NSDictionary *dictionary in uniqueOptionsCombinations) {
  383. optionsSizeCount[dictionary.count]++;
  384. }
  385. XCTAssertEqual(optionsSizeCount[0], 1);
  386. XCTAssertEqual(optionsSizeCount[1], 6);
  387. XCTAssertEqual(optionsSizeCount[2], 12);
  388. XCTAssertEqual(optionsSizeCount[3], 8);
  389. XCTAssertEqual(uniqueMainCombinations.count, 27);
  390. int mainSizeCount[4] = {0};
  391. for (NSDictionary *dictionary in uniqueMainCombinations) {
  392. mainSizeCount[dictionary.count]++;
  393. }
  394. XCTAssertEqual(mainSizeCount[0], 1);
  395. XCTAssertEqual(mainSizeCount[1], 6);
  396. XCTAssertEqual(mainSizeCount[2], 12);
  397. XCTAssertEqual(mainSizeCount[3], 8);
  398. }
  399. - (void)testAnalyticsCollectionGlobalSwitchEnabled {
  400. // Stub the default app, and set the global switch to YES.
  401. id appMock = OCMClassMock([FIRApp class]);
  402. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  403. OCMStub([appMock defaultApp]).andReturn(appMock);
  404. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(YES);
  405. // With no other settings, Analytics collection should default to the app's flag.
  406. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  407. XCTAssertTrue(options.isAnalyticsCollectionEnabled);
  408. XCTAssertTrue(options.isMeasurementEnabled);
  409. [appMock stopMocking];
  410. }
  411. - (void)testAnalyticsCollectionGlobalSwitchDisabled {
  412. // Stub the default app, and set the global switch to NO.
  413. id appMock = OCMClassMock([FIRApp class]);
  414. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  415. OCMStub([appMock defaultApp]).andReturn(appMock);
  416. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(NO);
  417. // With no other settings, Analytics collection should default to the app's flag.
  418. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  419. XCTAssertFalse(options.isAnalyticsCollectionEnabled);
  420. XCTAssertFalse(options.isMeasurementEnabled);
  421. [appMock stopMocking];
  422. }
  423. - (void)testAnalyticsCollectionGlobalSwitchOverrideToDisable {
  424. // Stub the default app, and set the global switch to YES.
  425. id appMock = OCMClassMock([FIRApp class]);
  426. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  427. OCMStub([appMock defaultApp]).andReturn(appMock);
  428. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(YES);
  429. // Test the three Analytics flags that override to disable Analytics collection.
  430. FIROptions *collectionEnabledOptions = [[FIROptions alloc]
  431. initInternalWithOptionsDictionary:@{kFIRIsAnalyticsCollectionEnabled : @NO}];
  432. XCTAssertFalse(collectionEnabledOptions.isAnalyticsCollectionEnabled);
  433. FIROptions *collectionDeactivatedOptions = [[FIROptions alloc]
  434. initInternalWithOptionsDictionary:@{kFIRIsAnalyticsCollectionDeactivated : @YES}];
  435. XCTAssertFalse(collectionDeactivatedOptions.isAnalyticsCollectionEnabled);
  436. FIROptions *measurementEnabledOptions =
  437. [[FIROptions alloc] initInternalWithOptionsDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  438. XCTAssertFalse(measurementEnabledOptions.isAnalyticsCollectionEnabled);
  439. }
  440. - (void)testAnalyticsCollectionGlobalSwitchOverrideToEnable {
  441. // Stub the default app, and set the global switch to YES.
  442. id appMock = OCMClassMock([FIRApp class]);
  443. OCMStub([appMock isDefaultAppConfigured]).andReturn(YES);
  444. OCMStub([appMock defaultApp]).andReturn(appMock);
  445. OCMStub([appMock isDataCollectionDefaultEnabled]).andReturn(NO);
  446. // Test the two Analytics flags that can override and enable collection.
  447. FIROptions *collectionEnabledOptions = [[FIROptions alloc]
  448. initInternalWithOptionsDictionary:@{kFIRIsAnalyticsCollectionEnabled : @YES}];
  449. XCTAssertTrue(collectionEnabledOptions.isAnalyticsCollectionEnabled);
  450. FIROptions *measurementEnabledOptions =
  451. [[FIROptions alloc] initInternalWithOptionsDictionary:@{kFIRIsMeasurementEnabled : @YES}];
  452. XCTAssertTrue(measurementEnabledOptions.isAnalyticsCollectionEnabled);
  453. }
  454. - (void)testAnalyticsCollectionExplicitlySet {
  455. NSDictionary *optionsDictionary = @{};
  456. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  457. NSDictionary *analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  458. XCTAssertFalse([options isAnalyticsCollectionExplicitlySet]);
  459. // Test deactivation flag.
  460. optionsDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @YES};
  461. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  462. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  463. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  464. // If "deactivated" == NO, that doesn't mean it's explicitly set / enabled so it should be treated
  465. // as if it's not set.
  466. optionsDictionary = @{kFIRIsAnalyticsCollectionDeactivated : @NO};
  467. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  468. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  469. XCTAssertFalse([options isAnalyticsCollectionExplicitlySet]);
  470. // Test the collection enabled flag.
  471. optionsDictionary = @{kFIRIsAnalyticsCollectionEnabled : @YES};
  472. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  473. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  474. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  475. optionsDictionary = @{kFIRIsAnalyticsCollectionEnabled : @NO};
  476. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  477. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:@{}];
  478. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  479. // Test the old measurement flag.
  480. options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  481. analyticsOptions =
  482. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @YES}];
  483. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  484. options = [[FIROptions alloc] initInternalWithOptionsDictionary:@{}];
  485. analyticsOptions =
  486. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  487. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  488. // For good measure, a combination of all 3 (even if they conflict).
  489. optionsDictionary =
  490. @{kFIRIsAnalyticsCollectionDeactivated : @YES, kFIRIsAnalyticsCollectionEnabled : @YES};
  491. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  492. analyticsOptions =
  493. [options analyticsOptionsDictionaryWithInfoDictionary:@{kFIRIsMeasurementEnabled : @NO}];
  494. XCTAssertTrue([options isAnalyticsCollectionExplicitlySet]);
  495. }
  496. - (void)testModifyingOptionsThrows {
  497. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  498. GCMSenderID:kGCMSenderID];
  499. options.editingLocked = YES;
  500. // Modification to every property should result in an exception.
  501. XCTAssertThrows(options.APIKey = @"should_throw");
  502. XCTAssertThrows(options.bundleID = @"should_throw");
  503. XCTAssertThrows(options.clientID = @"should_throw");
  504. XCTAssertThrows(options.databaseURL = @"should_throw");
  505. XCTAssertThrows(options.deepLinkURLScheme = @"should_throw");
  506. XCTAssertThrows(options.GCMSenderID = @"should_throw");
  507. XCTAssertThrows(options.googleAppID = @"should_throw");
  508. XCTAssertThrows(options.projectID = @"should_throw");
  509. XCTAssertThrows(options.storageBucket = @"should_throw");
  510. }
  511. - (void)testVersionFormat {
  512. // `kFIRLibraryVersionID` is `nil` until `libraryVersion` is called on `FIROptions`.
  513. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  514. GCMSenderID:kGCMSenderID];
  515. __unused NSString *libraryVersion = options.libraryVersionID;
  516. options = nil;
  517. NSRegularExpression *sLibraryVersionRegex =
  518. [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{8,}$" options:0 error:NULL];
  519. NSUInteger numberOfMatches =
  520. [sLibraryVersionRegex numberOfMatchesInString:kFIRLibraryVersionID
  521. options:0
  522. range:NSMakeRange(0, kFIRLibraryVersionID.length)];
  523. XCTAssertEqual(numberOfMatches, 1, @"Incorrect library version format.");
  524. }
  525. - (void)testVersionConsistency {
  526. // `kFIRLibraryVersionID` is `nil` until `libraryVersion` is called on `FIROptions`.
  527. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  528. GCMSenderID:kGCMSenderID];
  529. __unused NSString *libraryVersion = options.libraryVersionID;
  530. options = nil;
  531. // Now `kFIRLibraryVersionID` is assigned, test that it is formatted correctly.
  532. const char *versionString = [kFIRLibraryVersionID UTF8String];
  533. int major = (versionString[0] - '0') * 10 + versionString[1] - '0';
  534. int minor = (versionString[2] - '0') * 10 + versionString[3] - '0';
  535. int patch = (versionString[4] - '0') * 10 + versionString[5] - '0';
  536. NSString *str = [NSString stringWithFormat:@"%d.%d.%d", major, minor, patch];
  537. XCTAssertTrue([FIRFirebaseVersion() hasPrefix:str]);
  538. }
  539. // Repeat test with more Objective-C.
  540. - (void)testVersionConsistency2 {
  541. // `kFIRLibraryVersionID` is `nil` until `libraryVersion` is called on `FIROptions`.
  542. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  543. GCMSenderID:kGCMSenderID];
  544. __unused NSString *libraryVersion = options.libraryVersionID;
  545. options = nil;
  546. NSRange major = NSMakeRange(0, 2);
  547. NSRange minor = NSMakeRange(2, 2);
  548. NSRange patch = NSMakeRange(4, 2);
  549. NSString *str = [NSString
  550. stringWithFormat:@"%d.%d.%d", [[kFIRLibraryVersionID substringWithRange:major] intValue],
  551. [[kFIRLibraryVersionID substringWithRange:minor] intValue],
  552. [[kFIRLibraryVersionID substringWithRange:patch] intValue]];
  553. XCTAssertTrue([FIRFirebaseVersion() hasPrefix:str]);
  554. }
  555. #pragma mark - Helpers
  556. - (NSString *)validGoogleServicesInfoPlistPath {
  557. NSString *filePath = [[NSBundle mainBundle] pathForResource:@"GoogleService-Info"
  558. ofType:@"plist"];
  559. if (filePath == nil) {
  560. // Use bundleForClass to allow GoogleService-Info.plist to be in the test target's bundle.
  561. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  562. filePath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  563. }
  564. XCTAssertNotNil(filePath);
  565. return filePath;
  566. }
  567. @end