FIRAppTest.m 39 KB

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