FIRAppTest.m 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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/Tests/Unit/FIRTestComponents.h"
  16. #import "FirebaseCore/Sources/FIRAnalyticsConfiguration.h"
  17. #import "FirebaseCore/Sources/Private/FIRAppInternal.h"
  18. #import "FirebaseCore/Sources/Private/FIRCoreDiagnosticsConnector.h"
  19. #import "FirebaseCore/Sources/Private/FIROptionsInternal.h"
  20. #import "GoogleUtilities/Environment/Private/GULAppEnvironmentUtil.h"
  21. NSString *const kFIRTestAppName1 = @"test_app_name_1";
  22. NSString *const kFIRTestAppName2 = @"test-app-name-2";
  23. @interface FIRApp (TestInternal)
  24. + (void)resetApps;
  25. - (instancetype)initInstanceWithName:(NSString *)name options:(FIROptions *)options;
  26. - (BOOL)configureCore;
  27. + (NSError *)errorForInvalidAppID;
  28. - (BOOL)isAppIDValid;
  29. + (NSString *)actualBundleID;
  30. + (NSNumber *)mapFromServiceStringToTypeEnum:(NSString *)serviceString;
  31. + (NSString *)deviceModel;
  32. + (NSString *)installString;
  33. + (NSURL *)filePathURLWithName:(NSString *)fileName;
  34. + (NSString *)stringAtURL:(NSURL *)filePathURL;
  35. + (BOOL)writeString:(NSString *)string toURL:(NSURL *)filePathURL;
  36. + (void)logAppInfo:(NSNotification *)notification;
  37. + (BOOL)validateAppID:(NSString *)appID;
  38. + (BOOL)validateAppIDFormat:(NSString *)appID withVersion:(NSString *)version;
  39. + (BOOL)validateAppIDFingerprint:(NSString *)appID withVersion:(NSString *)version;
  40. + (nullable NSNumber *)readDataCollectionSwitchFromPlist;
  41. + (nullable NSNumber *)readDataCollectionSwitchFromUserDefaultsForApp:(FIRApp *)app;
  42. @end
  43. @interface FIRAppTest : FIRTestCase
  44. @property(nonatomic) id appClassMock;
  45. @property(nonatomic) id observerMock;
  46. @property(nonatomic) id mockCoreDiagnosticsConnector;
  47. @property(nonatomic) NSNotificationCenter *notificationCenter;
  48. @end
  49. @implementation FIRAppTest
  50. - (void)setUp {
  51. [super setUp];
  52. [FIROptions resetDefaultOptions];
  53. [FIRApp resetApps];
  54. _appClassMock = OCMClassMock([FIRApp class]);
  55. _observerMock = OCMObserverMock();
  56. _mockCoreDiagnosticsConnector = OCMClassMock([FIRCoreDiagnosticsConnector class]);
  57. #if SWIFT_PACKAGE
  58. [self mockFIROptions];
  59. #endif
  60. OCMStub(ClassMethod([self.mockCoreDiagnosticsConnector logCoreTelemetryWithOptions:[OCMArg any]]))
  61. .andDo(^(NSInvocation *invocation){
  62. });
  63. // TODO: Remove all usages of defaultCenter in Core, then we can instantiate an instance here to
  64. // inject instead of using defaultCenter.
  65. _notificationCenter = [NSNotificationCenter defaultCenter];
  66. }
  67. - (void)tearDown {
  68. [_appClassMock stopMocking];
  69. [_notificationCenter removeObserver:_observerMock];
  70. _observerMock = nil;
  71. _notificationCenter = nil;
  72. _mockCoreDiagnosticsConnector = nil;
  73. [super tearDown];
  74. }
  75. - (void)testConfigure {
  76. [self registerLibrariesWithClasses:@[
  77. [FIRTestClassCached class], [FIRTestClassEagerCached class]
  78. ]];
  79. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName
  80. isDefaultApp:YES];
  81. [self expectNotificationForObserver:self.observerMock
  82. notificationName:kFIRAppReadyToConfigureSDKNotification
  83. object:[FIRApp class]
  84. userInfo:expectedUserInfo];
  85. XCTAssertNoThrow([FIRApp configure]);
  86. OCMVerifyAll(self.observerMock);
  87. FIRApp *app = [FIRApp defaultApp];
  88. XCTAssertNotNil(app);
  89. XCTAssertEqualObjects(app.name, kFIRDefaultAppName);
  90. XCTAssertEqualObjects(app.options.clientID, kClientID);
  91. XCTAssertTrue([FIRApp allApps].count == 1);
  92. // Check the registered libraries instances available.
  93. XCTAssertNotNil(FIR_COMPONENT(FIRTestProtocolCached, app.container));
  94. XCTAssertNotNil(FIR_COMPONENT(FIRTestProtocolEagerCached, app.container));
  95. XCTAssertNil(FIR_COMPONENT(FIRTestProtocol, app.container));
  96. }
  97. - (void)testConfigureWithNoDefaultOptions {
  98. id optionsClassMock = OCMClassMock([FIROptions class]);
  99. OCMStub([optionsClassMock defaultOptions]).andReturn(nil);
  100. XCTAssertThrows([FIRApp configure]);
  101. }
  102. - (void)testConfigureWithOptions {
  103. #pragma clang diagnostic push
  104. #pragma clang diagnostic ignored "-Wnonnull"
  105. // Test `nil` options.
  106. XCTAssertThrows([FIRApp configureWithOptions:nil]);
  107. #pragma clang diagnostic pop
  108. XCTAssertTrue([FIRApp allApps].count == 0);
  109. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName
  110. isDefaultApp:YES];
  111. [self expectNotificationForObserver:self.observerMock
  112. notificationName:kFIRAppReadyToConfigureSDKNotification
  113. object:[FIRApp class]
  114. userInfo:expectedUserInfo];
  115. // Use a valid instance of options.
  116. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  117. GCMSenderID:kGCMSenderID];
  118. options.clientID = kClientID;
  119. XCTAssertNoThrow([FIRApp configureWithOptions:options]);
  120. OCMVerifyAll(self.observerMock);
  121. // Verify the default app instance is created.
  122. FIRApp *app = [FIRApp defaultApp];
  123. XCTAssertNotNil(app);
  124. XCTAssertEqualObjects(app.name, kFIRDefaultAppName);
  125. XCTAssertEqualObjects(app.options.googleAppID, kGoogleAppID);
  126. XCTAssertEqualObjects(app.options.GCMSenderID, kGCMSenderID);
  127. XCTAssertEqualObjects(app.options.clientID, kClientID);
  128. XCTAssertTrue([FIRApp allApps].count == 1);
  129. }
  130. - (void)testConfigureWithNameAndOptions {
  131. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  132. GCMSenderID:kGCMSenderID];
  133. options.clientID = kClientID;
  134. #pragma clang diagnostic push
  135. #pragma clang diagnostic ignored "-Wnonnull"
  136. XCTAssertThrows([FIRApp configureWithName:nil options:options]);
  137. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:nil]);
  138. #pragma clang diagnostic pop
  139. XCTAssertThrows([FIRApp configureWithName:@"" options:options]);
  140. XCTAssertTrue([FIRApp allApps].count == 0);
  141. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRTestAppName1
  142. isDefaultApp:NO];
  143. [self expectNotificationForObserver:self.observerMock
  144. notificationName:kFIRAppReadyToConfigureSDKNotification
  145. object:[FIRApp class]
  146. userInfo:expectedUserInfo];
  147. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:options]);
  148. OCMVerifyAll(self.observerMock);
  149. XCTAssertTrue([FIRApp allApps].count == 1);
  150. FIRApp *app = [FIRApp appNamed:kFIRTestAppName1];
  151. XCTAssertNotNil(app);
  152. XCTAssertEqualObjects(app.name, kFIRTestAppName1);
  153. XCTAssertEqualObjects(app.options.clientID, kClientID);
  154. // Configure the same app again should throw an exception.
  155. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:options]);
  156. }
  157. - (void)testConfigureWithMultipleApps {
  158. FIROptions *options1 = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  159. GCMSenderID:kGCMSenderID];
  160. options1.deepLinkURLScheme = kDeepLinkURLScheme;
  161. // Set up notification center observer for verifying notifications.
  162. [self.notificationCenter addMockObserver:self.observerMock
  163. name:kFIRAppReadyToConfigureSDKNotification
  164. object:[FIRApp class]];
  165. NSDictionary *expectedUserInfo1 = [self expectedUserInfoWithAppName:kFIRTestAppName1
  166. isDefaultApp:NO];
  167. [[self.observerMock expect] notificationWithName:kFIRAppReadyToConfigureSDKNotification
  168. object:[FIRApp class]
  169. userInfo:expectedUserInfo1];
  170. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:options1]);
  171. XCTAssertTrue([FIRApp allApps].count == 1);
  172. // Configure a different app with valid customized options.
  173. FIROptions *options2 = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  174. GCMSenderID:kGCMSenderID];
  175. options2.bundleID = kBundleID;
  176. options2.APIKey = kCustomizedAPIKey;
  177. NSDictionary *expectedUserInfo2 = [self expectedUserInfoWithAppName:kFIRTestAppName2
  178. isDefaultApp:NO];
  179. [[self.observerMock expect] notificationWithName:kFIRAppReadyToConfigureSDKNotification
  180. object:[FIRApp class]
  181. userInfo:expectedUserInfo2];
  182. [self.observerMock setExpectationOrderMatters:YES];
  183. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName2 options:options2]);
  184. OCMVerifyAll(self.observerMock);
  185. XCTAssertTrue([FIRApp allApps].count == 2);
  186. FIRApp *app = [FIRApp appNamed:kFIRTestAppName2];
  187. XCTAssertNotNil(app);
  188. XCTAssertEqualObjects(app.name, kFIRTestAppName2);
  189. XCTAssertEqualObjects(app.options.googleAppID, kGoogleAppID);
  190. XCTAssertEqualObjects(app.options.APIKey, kCustomizedAPIKey);
  191. }
  192. - (void)testConfigureThrowsAfterConfigured {
  193. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  194. GCMSenderID:kGCMSenderID];
  195. [FIRApp configureWithOptions:options];
  196. XCTAssertNotNil([FIRApp defaultApp]);
  197. // A second configure call should throw, since Firebase is already configured.
  198. XCTAssertThrows([FIRApp configureWithOptions:options]);
  199. // Test the same with a custom named app.
  200. [FIRApp configureWithName:kFIRTestAppName1 options:options];
  201. XCTAssertNotNil([FIRApp appNamed:kFIRTestAppName1]);
  202. // A second configure call should throw, since Firebase is already configured.
  203. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:options]);
  204. }
  205. - (void)testConfigureDefaultAppInExtension {
  206. id environmentMock = OCMClassMock([GULAppEnvironmentUtil class]);
  207. OCMStub([environmentMock isAppExtension]).andReturn(YES);
  208. // Set up the default app like a standard app.
  209. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  210. GCMSenderID:kGCMSenderID];
  211. [FIRApp configureWithOptions:options];
  212. XCTAssertNotNil([FIRApp defaultApp]);
  213. XCTAssertEqual([FIRApp allApps].count, 1);
  214. // Configuring with the same set of options shouldn't throw.
  215. XCTAssertNoThrow([FIRApp configureWithOptions:options]);
  216. // Only 1 app should have been configured still, the default app.
  217. XCTAssertNotNil([FIRApp defaultApp]);
  218. XCTAssertEqual([FIRApp allApps].count, 1);
  219. // Use a set of a different options to call configure again, which should throw.
  220. FIROptions *differentOptions = [[FIROptions alloc] initWithGoogleAppID:@"1:789:ios:789XYZ"
  221. GCMSenderID:kGCMSenderID];
  222. XCTAssertThrows([FIRApp configureWithOptions:differentOptions]);
  223. XCTAssertEqual([FIRApp allApps].count, 1);
  224. // Explicily stop the environmentMock.
  225. [environmentMock stopMocking];
  226. environmentMock = nil;
  227. }
  228. - (void)testConfigureCustomAppInExtension {
  229. id environmentMock = OCMClassMock([GULAppEnvironmentUtil class]);
  230. OCMStub([environmentMock isAppExtension]).andReturn(YES);
  231. // Set up a custom named app like a standard app.
  232. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  233. GCMSenderID:kGCMSenderID];
  234. [FIRApp configureWithName:kFIRTestAppName1 options:options];
  235. XCTAssertNotNil([FIRApp appNamed:kFIRTestAppName1]);
  236. XCTAssertEqual([FIRApp allApps].count, 1);
  237. // Configuring with the same set of options shouldn't throw.
  238. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:options]);
  239. // Only 1 app should have been configured still.
  240. XCTAssertNotNil([FIRApp appNamed:kFIRTestAppName1]);
  241. XCTAssertEqual([FIRApp allApps].count, 1);
  242. // Use a set of a different options to call configure again, which should throw.
  243. FIROptions *differentOptions = [[FIROptions alloc] initWithGoogleAppID:@"1:789:ios:789XYZ"
  244. GCMSenderID:kGCMSenderID];
  245. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:differentOptions]);
  246. XCTAssertEqual([FIRApp allApps].count, 1);
  247. // Explicily stop the environmentMock.
  248. [environmentMock stopMocking];
  249. environmentMock = nil;
  250. }
  251. - (void)testValidName {
  252. XCTAssertNoThrow([FIRApp configureWithName:@"aA1_" options:[FIROptions defaultOptions]]);
  253. XCTAssertNoThrow([FIRApp configureWithName:@"aA1-" options:[FIROptions defaultOptions]]);
  254. XCTAssertNoThrow([FIRApp configureWithName:@"aAē1_" options:[FIROptions defaultOptions]]);
  255. XCTAssertThrows([FIRApp configureWithName:@"aA1%" options:[FIROptions defaultOptions]]);
  256. XCTAssertThrows([FIRApp configureWithName:@"aA1?" options:[FIROptions defaultOptions]]);
  257. XCTAssertThrows([FIRApp configureWithName:@"aA1!" options:[FIROptions defaultOptions]]);
  258. }
  259. - (void)testDefaultApp {
  260. FIRApp *app = [FIRApp defaultApp];
  261. XCTAssertNil(app);
  262. [FIRApp configure];
  263. app = [FIRApp defaultApp];
  264. XCTAssertEqualObjects(app.name, kFIRDefaultAppName);
  265. XCTAssertEqualObjects(app.options.clientID, kClientID);
  266. }
  267. - (void)testAppNamed {
  268. FIRApp *app = [FIRApp appNamed:kFIRTestAppName1];
  269. XCTAssertNil(app);
  270. [FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]];
  271. app = [FIRApp appNamed:kFIRTestAppName1];
  272. XCTAssertEqualObjects(app.name, kFIRTestAppName1);
  273. XCTAssertEqualObjects(app.options.clientID, kClientID);
  274. }
  275. - (void)testDeleteApp {
  276. [self registerLibrariesWithClasses:@[
  277. [FIRTestClassCached class], [FIRTestClassEagerCached class]
  278. ]];
  279. NSString *name = NSStringFromSelector(_cmd);
  280. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  281. GCMSenderID:kGCMSenderID];
  282. [FIRApp configureWithName:name options:options];
  283. FIRApp *app = [FIRApp appNamed:name];
  284. XCTAssertNotNil(app);
  285. XCTAssertTrue([FIRApp allApps].count == 1);
  286. // Check the registered libraries instances available.
  287. XCTAssertNotNil(FIR_COMPONENT(FIRTestProtocolCached, app.container));
  288. XCTAssertNotNil(FIR_COMPONENT(FIRTestProtocolEagerCached, app.container));
  289. XCTAssertNil(FIR_COMPONENT(FIRTestProtocol, app.container));
  290. [self expectNotificationForObserver:self.observerMock
  291. notificationName:kFIRAppDeleteNotification
  292. object:[FIRApp class]
  293. userInfo:[OCMArg any]];
  294. XCTestExpectation *expectation =
  295. [self expectationWithDescription:@"Deleting the app should succeed."];
  296. [app deleteApp:^(BOOL success) {
  297. XCTAssertTrue(success);
  298. [expectation fulfill];
  299. }];
  300. [self waitForExpectations:@[ expectation ] timeout:1];
  301. OCMVerifyAll(self.observerMock);
  302. XCTAssertTrue([FIRApp allApps].count == 0);
  303. // Check no new library instances created after the app delete.
  304. XCTAssertNil(FIR_COMPONENT(FIRTestProtocolCached, app.container));
  305. XCTAssertNil(FIR_COMPONENT(FIRTestProtocolEagerCached, app.container));
  306. }
  307. - (void)testErrorForSubspecConfigurationFailure {
  308. NSError *error = [FIRApp errorForSubspecConfigurationFailureWithDomain:kFirebaseCoreErrorDomain
  309. errorCode:-38
  310. service:@"Auth"
  311. reason:@"some reason"];
  312. XCTAssertNotNil(error);
  313. XCTAssert([error.domain isEqualToString:kFirebaseCoreErrorDomain]);
  314. XCTAssert(error.code == -38);
  315. XCTAssert([error.description containsString:@"Configuration failed for"]);
  316. }
  317. - (void)testOptionsLocking {
  318. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  319. GCMSenderID:kGCMSenderID];
  320. options.projectID = kProjectID;
  321. options.databaseURL = kDatabaseURL;
  322. // Options should not be locked before they are used to configure a `FIRApp`.
  323. XCTAssertFalse(options.isEditingLocked);
  324. // The options returned should be locked after configuring `FIRApp`.
  325. NSString *name = NSStringFromSelector(_cmd);
  326. [FIRApp configureWithName:name options:options];
  327. FIROptions *optionsCopy = [[FIRApp appNamed:name] options];
  328. XCTAssertTrue(optionsCopy.isEditingLocked);
  329. }
  330. #pragma mark - App ID v1
  331. - (void)testAppIDV1 {
  332. // Missing separator between platform:fingerprint.
  333. XCTAssertFalse([FIRApp validateAppID:@"1:1337:iosdeadbeef"]);
  334. // Wrong platform "android".
  335. XCTAssertFalse([FIRApp validateAppID:@"1:1337:android:deadbeef"]);
  336. // The fingerprint, aka 4th field, should only contain hex characters.
  337. XCTAssertFalse([FIRApp validateAppID:@"1:1337:ios:123abcxyz"]);
  338. // The fingerprint, aka 4th field, is not tested in V1, so a bad value shouldn't cause a failure.
  339. XCTAssertTrue([FIRApp validateAppID:@"1:1337:ios:deadbeef"]);
  340. }
  341. #pragma mark - App ID v2
  342. - (void)testAppIDV2 {
  343. // Missing separator between platform:fingerprint.
  344. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios5e18052ab54fbfec"]);
  345. // Unknown versions may contain anything.
  346. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:123abcxyz"]);
  347. XCTAssertTrue([FIRApp validateAppID:@"2:thisdoesn'teven_m:a:t:t:e:r_"]);
  348. // Known good fingerprint.
  349. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:5e18052ab54fbfec"]);
  350. // Unknown fingerprint, not tested so shouldn't cause a failure.
  351. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:deadbeef"]);
  352. }
  353. #pragma mark - App ID other
  354. - (void)testAppIDV3 {
  355. // Currently there is no specification for v3, so we would not expect it to fail.
  356. XCTAssertTrue([FIRApp validateAppID:@"3:1337:ios:deadbeef"]);
  357. }
  358. - (void)testAppIDEmpty {
  359. XCTAssertFalse([FIRApp validateAppID:@""]);
  360. }
  361. - (void)testAppIDValidationTrue {
  362. // Ensure that isAppIDValid matches validateAppID.
  363. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"" GCMSenderID:@""];
  364. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  365. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(YES);
  366. XCTAssertTrue([app isAppIDValid]);
  367. }
  368. - (void)testAppIDValidationFalse {
  369. // Ensure that isAppIDValid matches validateAppID.
  370. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"" GCMSenderID:@""];
  371. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  372. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(NO);
  373. XCTAssertFalse([app isAppIDValid]);
  374. }
  375. - (void)testAppIDPrefix {
  376. // Unknown numeric-character prefixes should pass.
  377. XCTAssertTrue([FIRApp validateAppID:@"0:"]);
  378. XCTAssertTrue([FIRApp validateAppID:@"01:"]);
  379. XCTAssertTrue([FIRApp validateAppID:@"10:"]);
  380. XCTAssertTrue([FIRApp validateAppID:@"010:"]);
  381. XCTAssertTrue([FIRApp validateAppID:@"3:"]);
  382. XCTAssertTrue([FIRApp validateAppID:@"123:"]);
  383. XCTAssertTrue([FIRApp validateAppID:@"999999999:"]);
  384. // Non-numeric prefixes should not pass.
  385. XCTAssertFalse([FIRApp validateAppID:@"a:"]);
  386. XCTAssertFalse([FIRApp validateAppID:@"abcsdf0:"]);
  387. XCTAssertFalse([FIRApp validateAppID:@"0aaaa:"]);
  388. XCTAssertFalse([FIRApp validateAppID:@"0aaaa0450:"]);
  389. XCTAssertFalse([FIRApp validateAppID:@"-1:"]);
  390. XCTAssertFalse([FIRApp validateAppID:@"abcsdf:"]);
  391. XCTAssertFalse([FIRApp validateAppID:@"ABDCF:"]);
  392. XCTAssertFalse([FIRApp validateAppID:@" :"]);
  393. XCTAssertFalse([FIRApp validateAppID:@"1 :"]);
  394. XCTAssertFalse([FIRApp validateAppID:@" 1:"]);
  395. XCTAssertFalse([FIRApp validateAppID:@" 123 :"]);
  396. XCTAssertFalse([FIRApp validateAppID:@"1 23:"]);
  397. XCTAssertFalse([FIRApp validateAppID:@"&($*&%(*$&:"]);
  398. XCTAssertFalse([FIRApp validateAppID:@"abCDSF$%%df:"]);
  399. // Known version prefixes should never pass without the rest of the app ID string present.
  400. XCTAssertFalse([FIRApp validateAppID:@"1:"]);
  401. // Version must include ":".
  402. XCTAssertFalse([FIRApp validateAppID:@"0"]);
  403. XCTAssertFalse([FIRApp validateAppID:@"01"]);
  404. XCTAssertFalse([FIRApp validateAppID:@"10"]);
  405. XCTAssertFalse([FIRApp validateAppID:@"010"]);
  406. XCTAssertFalse([FIRApp validateAppID:@"3"]);
  407. XCTAssertFalse([FIRApp validateAppID:@"123"]);
  408. XCTAssertFalse([FIRApp validateAppID:@"999999999"]);
  409. XCTAssertFalse([FIRApp validateAppID:@"com.google.bundleID"]);
  410. }
  411. - (void)testAppIDFormatInvalid {
  412. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  413. // Some direct tests of the validateAppIDFormat:withVersion: method.
  414. // Sanity checks first.
  415. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  416. NSString *const kGoodVersionV1 = @"1";
  417. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  418. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  419. NSString *const kGoodVersionV2 = @"2";
  420. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  421. // Version mismatch.
  422. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV1]);
  423. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV2]);
  424. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@"999:"]);
  425. // Nil or empty strings.
  426. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:nil]);
  427. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@""]);
  428. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:kGoodVersionV1]);
  429. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:kGoodVersionV1]);
  430. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:nil]);
  431. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:@""]);
  432. // App ID contains only the version prefix.
  433. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodVersionV1 withVersion:kGoodVersionV1]);
  434. // The version is the entire app ID.
  435. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  436. // Versions digits that may make a partial match.
  437. XCTAssertFalse([FIRApp validateAppIDFormat:@"01:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  438. XCTAssertFalse([FIRApp validateAppIDFormat:@"10:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  439. XCTAssertFalse([FIRApp validateAppIDFormat:@"11:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  440. XCTAssertFalse([FIRApp validateAppIDFormat:@"21:1337:ios:5e18052ab54fbfec"
  441. withVersion:kGoodVersionV2]);
  442. XCTAssertFalse([FIRApp validateAppIDFormat:@"22:1337:ios:5e18052ab54fbfec"
  443. withVersion:kGoodVersionV2]);
  444. XCTAssertFalse([FIRApp validateAppIDFormat:@"02:1337:ios:5e18052ab54fbfec"
  445. withVersion:kGoodVersionV2]);
  446. XCTAssertFalse([FIRApp validateAppIDFormat:@"20:1337:ios:5e18052ab54fbfec"
  447. withVersion:kGoodVersionV2]);
  448. // Extra fields.
  449. XCTAssertFalse([FIRApp validateAppIDFormat:@"ab:1:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  450. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:ab:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  451. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ab:ios:deadbeef" withVersion:kGoodVersionV1]);
  452. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:ab:deadbeef" withVersion:kGoodVersionV1]);
  453. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:deadbeef:ab" withVersion:kGoodVersionV1]);
  454. }
  455. - (void)testAppIDFingerprintInvalid {
  456. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  457. // Some direct tests of the validateAppIDFingerprint:withVersion: method.
  458. // Sanity checks first.
  459. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  460. NSString *const kGoodVersionV1 = @"1";
  461. XCTAssertTrue([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  462. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  463. NSString *const kGoodVersionV2 = @"2";
  464. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  465. // Nil or empty strings.
  466. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:nil]);
  467. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@""]);
  468. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:kGoodVersionV1]);
  469. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:kGoodVersionV1]);
  470. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:nil]);
  471. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:@""]);
  472. // App ID contains only the version prefix.
  473. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodVersionV1 withVersion:kGoodVersionV1]);
  474. // The version is the entire app ID.
  475. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  476. }
  477. // Uncomment if you need to measure performance of [FIRApp validateAppID:].
  478. // It is commented because measures are heavily dependent on a build agent configuration,
  479. // so it cannot produce reliable resault on CI
  480. //- (void)testAppIDFingerprintPerfomance {
  481. // [self measureBlock:^{
  482. // for (NSInteger i = 0; i < 100; ++i) {
  483. // [self testAppIDPrefix];
  484. // }
  485. // }];
  486. //}
  487. #pragma mark - Automatic Data Collection Tests
  488. - (void)testGlobalDataCollectionNoFlags {
  489. // Test: No flags set.
  490. NSString *name = NSStringFromSelector(_cmd);
  491. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  492. GCMSenderID:kGCMSenderID];
  493. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  494. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  495. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  496. .andReturn(nil);
  497. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  498. }
  499. - (void)testGlobalDataCollectionPlistSetEnabled {
  500. // Test: Plist set to enabled, no override.
  501. NSString *name = NSStringFromSelector(_cmd);
  502. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  503. GCMSenderID:kGCMSenderID];
  504. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  505. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  506. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  507. .andReturn(nil);
  508. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  509. }
  510. - (void)testGlobalDataCollectionPlistSetDisabled {
  511. // Test: Plist set to disabled, no override.
  512. NSString *name = NSStringFromSelector(_cmd);
  513. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  514. GCMSenderID:kGCMSenderID];
  515. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  516. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  517. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  518. .andReturn(nil);
  519. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  520. }
  521. - (void)testGlobalDataCollectionUserSpecifiedEnabled {
  522. // Test: User specified as enabled, no plist value.
  523. NSString *name = NSStringFromSelector(_cmd);
  524. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  525. GCMSenderID:kGCMSenderID];
  526. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  527. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  528. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  529. .andReturn(@YES);
  530. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  531. }
  532. - (void)testGlobalDataCollectionUserSpecifiedDisabled {
  533. // Test: User specified as disabled, no plist value.
  534. NSString *name = NSStringFromSelector(_cmd);
  535. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  536. GCMSenderID:kGCMSenderID];
  537. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  538. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  539. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  540. .andReturn(@NO);
  541. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  542. }
  543. - (void)testGlobalDataCollectionUserOverriddenEnabled {
  544. // Test: User specified as enabled, with plist set as disabled.
  545. NSString *name = NSStringFromSelector(_cmd);
  546. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  547. GCMSenderID:kGCMSenderID];
  548. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  549. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  550. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  551. .andReturn(@YES);
  552. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  553. }
  554. - (void)testGlobalDataCollectionUserOverriddenDisabled {
  555. // Test: User specified as disabled, with plist set as enabled.
  556. NSString *name = NSStringFromSelector(_cmd);
  557. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  558. GCMSenderID:kGCMSenderID];
  559. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  560. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  561. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  562. .andReturn(@NO);
  563. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  564. }
  565. - (void)testGlobalDataCollectionWriteToDefaults {
  566. id defaultsMock = OCMPartialMock([NSUserDefaults standardUserDefaults]);
  567. NSString *name = NSStringFromSelector(_cmd);
  568. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  569. GCMSenderID:kGCMSenderID];
  570. [FIRApp configureWithName:name options:options];
  571. FIRApp *app = [FIRApp appNamed:name];
  572. app.dataCollectionDefaultEnabled = YES;
  573. NSString *key =
  574. [NSString stringWithFormat:kFIRGlobalAppDataCollectionEnabledDefaultsKeyFormat, app.name];
  575. OCMVerify([defaultsMock setObject:@YES forKey:key]);
  576. app.dataCollectionDefaultEnabled = NO;
  577. OCMVerify([defaultsMock setObject:@NO forKey:key]);
  578. [defaultsMock stopMocking];
  579. }
  580. - (void)testGlobalDataCollectionClearedAfterDelete {
  581. // Configure and disable data collection for the default FIRApp.
  582. NSString *name = NSStringFromSelector(_cmd);
  583. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  584. GCMSenderID:kGCMSenderID];
  585. [FIRApp configureWithName:name options:options];
  586. FIRApp *app = [FIRApp appNamed:name];
  587. app.dataCollectionDefaultEnabled = NO;
  588. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  589. // Delete the app, and verify that the switch was reset.
  590. XCTestExpectation *deleteFinished =
  591. [self expectationWithDescription:@"The app should successfully delete."];
  592. [app deleteApp:^(BOOL success) {
  593. XCTAssertTrue(success);
  594. [deleteFinished fulfill];
  595. }];
  596. // Wait for the delete to complete.
  597. [self waitForExpectations:@[ deleteFinished ] timeout:1];
  598. // Set up an app with the same name again, and check the data collection flag.
  599. [FIRApp configureWithName:name options:options];
  600. XCTAssertTrue([FIRApp appNamed:name].isDataCollectionDefaultEnabled);
  601. }
  602. - (void)testGlobalDataCollectionNoDiagnosticsSent {
  603. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  604. GCMSenderID:kGCMSenderID];
  605. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  606. app.dataCollectionDefaultEnabled = NO;
  607. // Add an observer for the diagnostics notification. Currently no object is sent, but in the
  608. // future that could change so leave it as OCMOCK_ANY.
  609. [self.notificationCenter addMockObserver:self.observerMock
  610. name:kFIRAppDiagnosticsNotification
  611. object:OCMOCK_ANY];
  612. // Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
  613. // collection switch is disabled, the `sendLogs` call should return immediately and not fire a
  614. // notification.
  615. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  616. .andReturn(@NO);
  617. // Ensure configure doesn't fire a notification.
  618. [FIRApp configure];
  619. // The observer mock is strict and will raise an exception when an unexpected notification is
  620. // received.
  621. OCMVerifyAll(self.observerMock);
  622. }
  623. #pragma mark - Analytics Flag Tests
  624. - (void)testAnalyticsSetByGlobalDataCollectionSwitch {
  625. // Test that the global data collection switch triggers setting Analytics when no explicit flag is
  626. // set.
  627. id optionsMock = OCMClassMock([FIROptions class]);
  628. OCMStub([optionsMock isAnalyticsCollectionExplicitlySet]).andReturn(NO);
  629. // We need to use the default app name since Analytics only associates with the default app.
  630. FIRApp *defaultApp = [[FIRApp alloc] initInstanceWithName:kFIRDefaultAppName options:optionsMock];
  631. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  632. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  633. // Ensure Analytics is set after the global flag is set. It needs to
  634. [defaultApp setDataCollectionDefaultEnabled:YES];
  635. OCMVerify([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:NO]);
  636. [defaultApp setDataCollectionDefaultEnabled:NO];
  637. OCMVerify([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:NO]);
  638. }
  639. - (void)testAnalyticsNotSetByGlobalDataCollectionSwitch {
  640. // Test that the global data collection switch doesn't override an explicitly set Analytics flag.
  641. id optionsMock = OCMClassMock([FIROptions class]);
  642. OCMStub([optionsMock isAnalyticsCollectionExplicitlySet]).andReturn(YES);
  643. FIRApp *app = [[FIRApp alloc] initInstanceWithName:@"testAnalyticsNotSet" options:optionsMock];
  644. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  645. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  646. // Reject any changes to Analytics when the data collection changes.
  647. OCMReject([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:YES]);
  648. OCMReject([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:NO]);
  649. [app setDataCollectionDefaultEnabled:YES];
  650. OCMReject([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:YES]);
  651. OCMReject([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:NO]);
  652. [app setDataCollectionDefaultEnabled:NO];
  653. }
  654. #pragma mark - Internal Methods
  655. - (void)testIsDefaultAppConfigured {
  656. // Ensure it's false before anything is configured.
  657. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  658. // Configure it and ensure it's configured.
  659. [FIRApp configure];
  660. XCTAssertTrue([FIRApp isDefaultAppConfigured]);
  661. // Reset the apps and ensure it's not configured anymore.
  662. [FIRApp resetApps];
  663. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  664. }
  665. - (void)testInvalidLibraryName {
  666. [FIRApp registerLibrary:@"Oops>" withVersion:@"1.0.0"];
  667. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"Oops"]);
  668. }
  669. - (void)testInvalidLibraryVersion {
  670. NSString *originalFirebaseUserAgent = [FIRApp firebaseUserAgent];
  671. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0+"];
  672. XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:originalFirebaseUserAgent]);
  673. }
  674. - (void)testSingleLibrary {
  675. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0"];
  676. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0"]);
  677. }
  678. - (void)testMultipleLibraries {
  679. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0"];
  680. [FIRApp registerLibrary:@"ValidName2" withVersion:@"2.0.0"];
  681. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0 ValidName2/2.0.0"]);
  682. }
  683. - (void)testRegisteringConformingLibrary {
  684. Class testClass = [FIRTestClass class];
  685. [FIRApp registerInternalLibrary:testClass withName:@"ValidName" withVersion:@"1.0.0"];
  686. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0"]);
  687. }
  688. - (void)testRegisteringNonConformingLibrary {
  689. XCTAssertThrows([FIRApp registerInternalLibrary:[NSString class]
  690. withName:@"InvalidLibrary"
  691. withVersion:@"1.0.0"]);
  692. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"InvalidLibrary`/1.0.0"]);
  693. }
  694. - (void)testSwiftFlagWithNoSwift {
  695. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"swift/false"]);
  696. }
  697. - (void)testApplePlatformFlag {
  698. // When a Catalyst app is run on macOS then both `TARGET_OS_MACCATALYST` and `TARGET_OS_IOS` are
  699. // `true`.
  700. #if TARGET_OS_MACCATALYST
  701. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  702. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  703. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  704. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  705. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  706. #elif TARGET_OS_IOS
  707. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  708. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  709. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  710. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  711. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  712. #endif // TARGET_OS_MACCATALYST
  713. #if TARGET_OS_TV
  714. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  715. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  716. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  717. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  718. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  719. #endif // TARGET_OS_TV
  720. #if TARGET_OS_OSX
  721. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  722. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  723. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  724. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  725. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  726. #endif // TARGET_OS_OSX
  727. #if TARGET_OS_WATCH
  728. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  729. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  730. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  731. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  732. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  733. #endif // TARGET_OS_WATCH
  734. }
  735. #pragma mark - Core Diagnostics
  736. - (void)testCoreDiagnosticsLoggedWhenFIRAppIsConfigured {
  737. [self expectCoreDiagnosticsDataLogWithOptions:[self appOptions]];
  738. [self createConfiguredAppWithName:NSStringFromSelector(_cmd)];
  739. OCMVerifyAll(self.mockCoreDiagnosticsConnector);
  740. }
  741. - (void)testCoreDiagnosticsLoggedWhenAppDidBecomeActive {
  742. FIRApp *app = [self createConfiguredAppWithName:NSStringFromSelector(_cmd)];
  743. [self expectCoreDiagnosticsDataLogWithOptions:app.options];
  744. [self.notificationCenter postNotificationName:[self appDidBecomeActiveNotificationName]
  745. object:nil];
  746. OCMVerifyAll(self.mockCoreDiagnosticsConnector);
  747. }
  748. #pragma mark - private
  749. - (void)expectNotificationForObserver:(id)observer
  750. notificationName:(NSNotificationName)name
  751. object:(nullable id)object
  752. userInfo:(nullable NSDictionary *)userInfo {
  753. [self.notificationCenter addMockObserver:observer name:name object:object];
  754. [[observer expect] notificationWithName:name object:object userInfo:userInfo];
  755. }
  756. - (NSDictionary<NSString *, NSObject *> *)expectedUserInfoWithAppName:(NSString *)name
  757. isDefaultApp:(BOOL)isDefaultApp {
  758. return @{
  759. kFIRAppNameKey : name,
  760. kFIRAppIsDefaultAppKey : [NSNumber numberWithBool:isDefaultApp],
  761. kFIRGoogleAppIDKey : kGoogleAppID
  762. };
  763. }
  764. - (void)expectCoreDiagnosticsDataLogWithOptions:(nullable FIROptions *)expectedOptions {
  765. [self.mockCoreDiagnosticsConnector stopMocking];
  766. self.mockCoreDiagnosticsConnector = nil;
  767. self.mockCoreDiagnosticsConnector = OCMClassMock([FIRCoreDiagnosticsConnector class]);
  768. OCMExpect(ClassMethod([self.mockCoreDiagnosticsConnector
  769. logCoreTelemetryWithOptions:[OCMArg checkWithBlock:^BOOL(FIROptions *options) {
  770. if (!expectedOptions) {
  771. return YES;
  772. }
  773. return [options.googleAppID isEqualToString:expectedOptions.googleAppID] &&
  774. [options.GCMSenderID isEqualToString:expectedOptions.GCMSenderID];
  775. }]]));
  776. }
  777. - (NSNotificationName)appDidBecomeActiveNotificationName {
  778. #if TARGET_OS_IOS || TARGET_OS_TV
  779. return UIApplicationDidBecomeActiveNotification;
  780. #endif
  781. #if TARGET_OS_OSX
  782. return NSApplicationDidBecomeActiveNotification;
  783. #endif
  784. }
  785. - (FIRApp *)createConfiguredAppWithName:(NSString *)name {
  786. FIROptions *options = [self appOptions];
  787. [FIRApp configureWithName:name options:options];
  788. return [FIRApp appNamed:name];
  789. }
  790. - (FIROptions *)appOptions {
  791. return [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  792. }
  793. - (void)registerLibrariesWithClasses:(NSArray<Class> *)classes {
  794. for (Class klass in classes) {
  795. [FIRApp registerInternalLibrary:klass withName:NSStringFromClass(klass) withVersion:@"1.0"];
  796. }
  797. }
  798. @end