FIRAppTest.m 41 KB

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