Files
firebase-ios-sdk/Example/InstanceID/Tests/FIRInstanceIDResultTest.m
T

134 lines
4.7 KiB
Objective-C
Raw Normal View History

2019-02-21 10:51:38 -08:00
/*
* Copyright 2019 Google
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <XCTest/XCTest.h>
2019-04-09 15:54:09 -07:00
#import <FirebaseInstanceID/FIRInstanceID.h>
2019-02-21 10:51:38 -08:00
#import <OCMock/OCMock.h>
2019-04-09 15:54:09 -07:00
2019-02-21 10:51:38 -08:00
#import "Firebase/InstanceID/NSError+FIRInstanceID.h"
static NSString *const kFakeIID = @"fE1e1PZJFSQ";
static NSString *const kFakeToken =
@"fE1e1PZJFSQ:APA91bFAOjp1ahBWn9rTlbjArwBEm_"
@"yUTTzK6dhIvLqzqqCSabaa4TQVM0pGTmF6r7tmMHPe6VYiGMHuCwJFgj5v97xl78sUNMLwuPPhoci8z_"
@"QGlCrTbxCFGzEUfvA3fGpGgIVQU2W6";
@interface FIRInstanceID (ExposedForTest)
- (NSString *)cachedTokenIfAvailable;
- (void)defaultTokenWithHandler:(FIRInstanceIDTokenHandler)handler;
2019-04-09 15:54:09 -07:00
- (instancetype)initPrivately;
- (void)start;
2019-02-21 10:51:38 -08:00
@end
@interface FIRInstanceIDResultTest : XCTestCase {
FIRInstanceID *_instanceID;
id _mockInstanceID;
}
@end
@implementation FIRInstanceIDResultTest
- (void)setUp {
[super setUp];
2019-05-28 10:02:30 -04:00
_mockInstanceID = OCMClassMock([FIRInstanceID class]);
2019-02-21 10:51:38 -08:00
}
- (void)tearDown {
[_mockInstanceID stopMocking];
2019-05-28 10:02:30 -04:00
_mockInstanceID = nil;
2019-02-21 10:51:38 -08:00
[super tearDown];
}
- (void)testResultWithFailedIID {
// mocking getting iid failed with error.
OCMStub([_mockInstanceID
getIDWithHandler:([OCMArg invokeBlockWithArgs:[NSNull null],
[NSError errorWithFIRInstanceIDErrorCode:100],
nil])]);
[_instanceID
instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNil(result);
XCTAssertNotNil(error);
XCTAssertEqual(error.code, 100);
}];
}
- (void)testResultWithCacheToken {
// mocking getting iid succeed and a cache token exists.
OCMStub([_mockInstanceID
getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(kFakeToken);
[_instanceID
instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNotNil(result);
XCTAssertNil(error);
XCTAssertEqualObjects(result.instanceID, kFakeIID);
XCTAssertEqualObjects(result.token, kFakeToken);
}];
}
- (void)testResultWithNewToken {
// mocking getting iid succeed and a new token is generated.
OCMStub([_mockInstanceID
getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(nil);
OCMStub([_mockInstanceID
defaultTokenWithHandler:([OCMArg invokeBlockWithArgs:kFakeToken, [NSNull null], nil])]);
[_instanceID
instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNotNil(result);
XCTAssertNil(error);
XCTAssertEqualObjects(result.instanceID, kFakeIID);
XCTAssertEqualObjects(result.token, kFakeToken);
}];
}
- (void)testResultWithFailedFetchingToken {
// mock getting iid succeed and token fails
OCMStub([_mockInstanceID
getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(nil);
OCMStub([_mockInstanceID
defaultTokenWithHandler:([OCMArg
invokeBlockWithArgs:[NSNull null],
[NSError errorWithFIRInstanceIDErrorCode:200],
nil])]);
[_instanceID
instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
XCTAssertNil(result);
XCTAssertNotNil(error);
XCTAssertEqual(error.code, 200);
}];
}
- (void)testResultCanBeCoplied {
// mocking getting iid succeed and a cache token exists.
OCMStub([_mockInstanceID
getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(kFakeToken);
[_instanceID
instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
FIRInstanceIDResult *resultCopy = [result copy];
XCTAssertEqualObjects(resultCopy.instanceID, kFakeIID);
XCTAssertEqualObjects(resultCopy.token, kFakeToken);
}];
}
@end