FIRAppTest.m 39 KB

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