Skip to content

Commit aebfb27

Browse files
FirebaseInstallations: more unit tests to prove correct behaviour (#4612)
* FIS: more unit tests to prove correct behaviour * Fix comments * Fix comments * Add missing return value to `then` block.
1 parent d261cde commit aebfb27

File tree

1 file changed

+92
-5
lines changed

1 file changed

+92
-5
lines changed

FirebaseInstallations/Source/Tests/Unit/FIRInstallationsIDControllerTests.m

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#import <FirebaseCore/FIRAppInternal.h>
2222

2323
#import "FBLPromise+Testing.h"
24+
#import "FBLPromise+Then.h"
2425
#import "FIRInstallationsErrorUtil+Tests.h"
2526
#import "FIRInstallationsItem+Tests.h"
2627

@@ -330,25 +331,71 @@ - (void)testGetInstallationItem_WhenThereIsIIDAndNoFID_ThenFIDIsCreatedAndRegist
330331
OCMVerifyAll(self.mockIIDTokenStore);
331332
}
332333

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 {
334381
// 1. Expect the installation to be requested from the store only once.
335382
FIRInstallationsItem *storedInstallation1 =
336383
[FIRInstallationsItem createRegisteredInstallationItem];
337384
FBLPromise<FIRInstallationsItem *> *pendingStorePromise = [FBLPromise pendingPromise];
338385
OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
339386
.andReturn(pendingStorePromise);
340387

341-
// 3. Request installation n times
388+
// 2. Request installation n times
342389
NSInteger requestCount = 10;
343390
NSMutableArray *installationPromises = [NSMutableArray arrayWithCapacity:requestCount];
344391
for (NSInteger i = 0; i < requestCount; i++) {
345392
[installationPromises addObject:[self.controller getInstallationItem]];
346393
}
347394

348-
// 4. Resolve store promise.
395+
// 3. Resolve store promise.
349396
[pendingStorePromise fulfill:storedInstallation1];
350397

351-
// 5. Wait for operation to be completed and check.
398+
// 4. Wait for operation to be completed and check.
352399
XCTAssert(FBLWaitForPromisesWithTimeout(0.5));
353400

354401
for (FBLPromise<FIRInstallationsItem *> *installationPromise in installationPromises) {
@@ -359,7 +406,7 @@ - (void)testGetInstallationItem_WhenCalledSeveralTimes_OnlyOneOperationIsPerform
359406
OCMVerifyAll(self.mockInstallationsStore);
360407
OCMVerifyAll(self.mockAPIService);
361408

362-
// 6. Check that a new request is performed once previous finished.
409+
// 5. Check that a new request is performed once previous finished.
363410
FIRInstallationsItem *storedInstallation2 =
364411
[FIRInstallationsItem createRegisteredInstallationItem];
365412
OCMExpect([self.mockInstallationsStore installationForAppID:self.appID appName:self.appName])
@@ -375,6 +422,46 @@ - (void)testGetInstallationItem_WhenCalledSeveralTimes_OnlyOneOperationIsPerform
375422
OCMVerifyAll(self.mockAPIService);
376423
}
377424

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+
378465
#pragma mark - Get Auth Token
379466

380467
- (void)testGetAuthToken_WhenValidInstallationExists_ThenItIsReturned {

0 commit comments

Comments
 (0)