FIRAppTest.m 42 KB

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