21
21
#import < FirebaseCore/FIRAppInternal.h>
22
22
23
23
#import " FBLPromise+Testing.h"
24
+ #import " FBLPromise+Then.h"
24
25
#import " FIRInstallationsErrorUtil+Tests.h"
25
26
#import " FIRInstallationsItem+Tests.h"
26
27
@@ -330,25 +331,71 @@ - (void)testGetInstallationItem_WhenThereIsIIDAndNoFID_ThenFIDIsCreatedAndRegist
330
331
OCMVerifyAll (self.mockIIDTokenStore );
331
332
}
332
333
333
- - (void )testGetInstallationItem_WhenCalledSeveralTimes_OnlyOneOperationIsPerformed {
334
+ - (void )testGetInstallationItem_WhenCalledWhileRegistering_DoesNotWaitForAPIResponse {
335
+ // 1. Expect the installation to be requested from the store only once.
336
+ FIRInstallationsItem *storedInstallation1 =
337
+ [FIRInstallationsItem createUnregisteredInstallationItem ];
338
+ OCMExpect ([self .mockInstallationsStore installationForAppID: self .appID appName: self .appName])
339
+ .andReturn ([FBLPromise resolvedWith: storedInstallation1]);
340
+
341
+ // 2. Expect registration API request to be sent.
342
+ FBLPromise<FIRInstallationsItem *> *pendingAPIPromise = [FBLPromise pendingPromise ];
343
+ OCMExpect ([self .mockAPIService registerInstallation: storedInstallation1])
344
+ .andReturn (pendingAPIPromise);
345
+
346
+ // 3. Request and wait for 1st FID.
347
+ FBLPromise<FIRInstallationsItem *> *getInstallationPromise1 =
348
+ [self .controller getInstallationItem ];
349
+ XCTestExpectation *getInstallationsExpectation1 =
350
+ [self expectationWithDescription: @" getInstallationsExpectation1" ];
351
+ getInstallationPromise1.then (^id (FIRInstallationsItem *installation) {
352
+ [getInstallationsExpectation1 fulfill ];
353
+ return nil ;
354
+ });
355
+ [self waitForExpectations: @[ getInstallationsExpectation1 ] timeout: 0.5 ];
356
+
357
+ // 4. Request FID 2nd time.
358
+ OCMExpect ([self .mockInstallationsStore installationForAppID: self .appID appName: self .appName])
359
+ .andReturn ([FBLPromise resolvedWith: storedInstallation1]);
360
+
361
+ FBLPromise<FIRInstallationsItem *> *getInstallationPromise2 =
362
+ [self .controller getInstallationItem ];
363
+ XCTestExpectation *getInstallationsExpectation2 =
364
+ [self expectationWithDescription: @" getInstallationsExpectation2" ];
365
+ getInstallationPromise2.then (^id (FIRInstallationsItem *installation) {
366
+ [getInstallationsExpectation2 fulfill ];
367
+ return nil ;
368
+ });
369
+ [self waitForExpectations: @[ getInstallationsExpectation2 ] timeout: 0.5 ];
370
+
371
+ // 5. Resolve API promise.
372
+ [pendingAPIPromise reject: [FIRInstallationsErrorUtil APIErrorWithHTTPCode: 400 ]];
373
+
374
+ // 6. Check
375
+ XCTAssert (FBLWaitForPromisesWithTimeout (0.5 ));
376
+ OCMVerifyAll (self.mockInstallationsStore );
377
+ OCMVerifyAll (self.mockAPIService );
378
+ }
379
+
380
+ - (void )testGetInstallationItem_WhenCalledSeveralTimesWaitingForStore_OnlyOneOperationIsPerformed {
334
381
// 1. Expect the installation to be requested from the store only once.
335
382
FIRInstallationsItem *storedInstallation1 =
336
383
[FIRInstallationsItem createRegisteredInstallationItem ];
337
384
FBLPromise<FIRInstallationsItem *> *pendingStorePromise = [FBLPromise pendingPromise ];
338
385
OCMExpect ([self .mockInstallationsStore installationForAppID: self .appID appName: self .appName])
339
386
.andReturn (pendingStorePromise);
340
387
341
- // 3 . Request installation n times
388
+ // 2 . Request installation n times
342
389
NSInteger requestCount = 10 ;
343
390
NSMutableArray *installationPromises = [NSMutableArray arrayWithCapacity: requestCount];
344
391
for (NSInteger i = 0 ; i < requestCount; i++) {
345
392
[installationPromises addObject: [self .controller getInstallationItem ]];
346
393
}
347
394
348
- // 4 . Resolve store promise.
395
+ // 3 . Resolve store promise.
349
396
[pendingStorePromise fulfill: storedInstallation1];
350
397
351
- // 5 . Wait for operation to be completed and check.
398
+ // 4 . Wait for operation to be completed and check.
352
399
XCTAssert (FBLWaitForPromisesWithTimeout (0.5 ));
353
400
354
401
for (FBLPromise<FIRInstallationsItem *> *installationPromise in installationPromises) {
@@ -359,7 +406,7 @@ - (void)testGetInstallationItem_WhenCalledSeveralTimes_OnlyOneOperationIsPerform
359
406
OCMVerifyAll (self.mockInstallationsStore );
360
407
OCMVerifyAll (self.mockAPIService );
361
408
362
- // 6 . Check that a new request is performed once previous finished.
409
+ // 5 . Check that a new request is performed once previous finished.
363
410
FIRInstallationsItem *storedInstallation2 =
364
411
[FIRInstallationsItem createRegisteredInstallationItem ];
365
412
OCMExpect ([self .mockInstallationsStore installationForAppID: self .appID appName: self .appName])
@@ -375,6 +422,46 @@ - (void)testGetInstallationItem_WhenCalledSeveralTimes_OnlyOneOperationIsPerform
375
422
OCMVerifyAll (self.mockAPIService );
376
423
}
377
424
425
+ - (void )testGetInstallationItem_WhenCalledSeveralTimesWaitingForAPI_OnlyOneAPIRequestIsSent {
426
+ // 1. Expect a single API request.
427
+ FBLPromise<FIRInstallationsItem *> *registerAPIPromise = [FBLPromise pendingPromise ];
428
+ OCMExpect ([self .mockAPIService registerInstallation: [OCMArg any ]]).andReturn (registerAPIPromise);
429
+
430
+ // 2. Request FID multiple times.
431
+ NSInteger requestCount = 10 ;
432
+ for (NSInteger i = 0 ; i < requestCount; i++) {
433
+ XCTestExpectation *getFIDExpectation = [self
434
+ expectationWithDescription: [NSString stringWithFormat: @" getFIDExpectation%ld " , (long )i]];
435
+
436
+ // 2.1. Expect stored FID to be requested.
437
+ FIRInstallationsItem *storedInstallation =
438
+ [FIRInstallationsItem createUnregisteredInstallationItem ];
439
+ OCMExpect ([self .mockInstallationsStore installationForAppID: self .appID appName: self .appName])
440
+ .andReturn ([FBLPromise resolvedWith: storedInstallation]);
441
+
442
+ // 2.2. Expect the FID to be returned.
443
+ FBLPromise<FIRInstallationsItem *> *getFIDPromise = [self .controller getInstallationItem ];
444
+
445
+ [getFIDPromise then: ^id _Nullable (FIRInstallationsItem *_Nullable value) {
446
+ XCTAssertNotNil (value);
447
+ XCTAssertEqualObjects (value.firebaseInstallationID ,
448
+ storedInstallation.firebaseInstallationID );
449
+ [getFIDExpectation fulfill ];
450
+ return nil ;
451
+ }];
452
+
453
+ [self waitForExpectations: @[ getFIDExpectation ] timeout: 0.5 ];
454
+ }
455
+
456
+ // 3. Finish API request.
457
+ [registerAPIPromise reject: [FIRInstallationsErrorUtil APIErrorWithHTTPCode: 400 ]];
458
+
459
+ // 4. Verify mocks
460
+ XCTAssert (FBLWaitForPromisesWithTimeout (0.5 ));
461
+ OCMVerifyAll (self.mockInstallationsStore );
462
+ OCMVerifyAll (self.mockAPIService );
463
+ }
464
+
378
465
#pragma mark - Get Auth Token
379
466
380
467
- (void )testGetAuthToken_WhenValidInstallationExists_ThenItIsReturned {
0 commit comments