|
| 1 | +/* |
| 2 | + * Copyright 2019 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#import <XCTest/XCTest.h> |
| 18 | + |
| 19 | +#import <FirebaseAuthInterop/FIRAuthInterop.h> |
| 20 | +#import <FirebaseCore/FIRAppInternal.h> |
| 21 | +#import <FirebaseCore/FIRComponent.h> |
| 22 | +#import <FirebaseCore/FIRLibrary.h> |
| 23 | + |
| 24 | +#import "FIRApp+FIRAuthUnitTests.h" |
| 25 | +#import "FIRAuthRequestConfiguration.h" |
| 26 | +#import "FIRAuth_Internal.h" |
| 27 | + |
| 28 | +/** @var kFirebaseAppName1 |
| 29 | + @brief A fake Firebase app name. |
| 30 | + */ |
| 31 | +static NSString *const kFirebaseAppName1 = @"FIREBASE_APP_NAME_1"; |
| 32 | + |
| 33 | +/** @var kFirebaseAppName2 |
| 34 | + @brief Another fake Firebase app name. |
| 35 | + */ |
| 36 | +static NSString *const kFirebaseAppName2 = @"FIREBASE_APP_NAME_2"; |
| 37 | + |
| 38 | +/** @var kAPIKey |
| 39 | + @brief The fake API key. |
| 40 | + */ |
| 41 | +static NSString *const kAPIKey = @"FAKE_API_KEY"; |
| 42 | + |
| 43 | +/** @var kExpectationTimeout |
| 44 | + @brief The maximum time waiting for expectations to fulfill. |
| 45 | + */ |
| 46 | +static const NSTimeInterval kExpectationTimeout = 2; |
| 47 | + |
| 48 | +/** @var kWaitInterval |
| 49 | + @brief The time waiting for background tasks to finish before continue when necessary. |
| 50 | + */ |
| 51 | +static const NSTimeInterval kWaitInterval = .5; |
| 52 | + |
| 53 | +@interface FIRAuthLifeCycleTests : XCTestCase |
| 54 | + |
| 55 | +@end |
| 56 | + |
| 57 | +@implementation FIRAuthLifeCycleTests |
| 58 | + |
| 59 | +- (void)setUp { |
| 60 | + [super setUp]; |
| 61 | + |
| 62 | + [FIRApp resetAppForAuthUnitTests]; |
| 63 | +} |
| 64 | + |
| 65 | +- (void)tearDown { |
| 66 | + [super tearDown]; |
| 67 | +} |
| 68 | + |
| 69 | +/** @fn testSingleton |
| 70 | + @brief Verifies the @c auth method behaves like a singleton. |
| 71 | + */ |
| 72 | +- (void)testSingleton { |
| 73 | + FIRAuth *auth1 = [FIRAuth auth]; |
| 74 | + XCTAssertNotNil(auth1); |
| 75 | + FIRAuth *auth2 = [FIRAuth auth]; |
| 76 | + XCTAssertEqual(auth1, auth2); |
| 77 | +} |
| 78 | + |
| 79 | +/** @fn testDefaultAuth |
| 80 | + @brief Verifies the @c auth method associates with the default Firebase app. |
| 81 | + */ |
| 82 | +- (void)testDefaultAuth { |
| 83 | + FIRAuth *auth1 = [FIRAuth auth]; |
| 84 | + FIRAuth *auth2 = [FIRAuth authWithApp:[FIRApp defaultApp]]; |
| 85 | + XCTAssertEqual(auth1, auth2); |
| 86 | + XCTAssertEqual(auth1.app, [FIRApp defaultApp]); |
| 87 | +} |
| 88 | + |
| 89 | +/** @fn testNilAppException |
| 90 | + @brief Verifies the @c auth method raises an exception if the default FIRApp is not configured. |
| 91 | + */ |
| 92 | +- (void)testNilAppException { |
| 93 | + [FIRApp resetApps]; |
| 94 | + XCTAssertThrows([FIRAuth auth]); |
| 95 | +} |
| 96 | + |
| 97 | +/** @fn testAppAPIkey |
| 98 | + @brief Verifies the API key is correctly copied from @c FIRApp to @c FIRAuth . |
| 99 | + */ |
| 100 | +- (void)testAppAPIkey { |
| 101 | + FIRAuth *auth = [FIRAuth auth]; |
| 102 | + XCTAssertEqualObjects(auth.requestConfiguration.APIKey, kAPIKey); |
| 103 | +} |
| 104 | + |
| 105 | +/** @fn testAppAssociation |
| 106 | + @brief Verifies each @c FIRApp instance associates with a @c FIRAuth . |
| 107 | + */ |
| 108 | +- (void)testAppAssociation { |
| 109 | + FIRApp *app1 = [self app1]; |
| 110 | + FIRAuth *auth1 = [FIRAuth authWithApp:app1]; |
| 111 | + XCTAssertNotNil(auth1); |
| 112 | + XCTAssertEqual(auth1.app, app1); |
| 113 | + |
| 114 | + FIRApp *app2 = [self app2]; |
| 115 | + FIRAuth *auth2 = [FIRAuth authWithApp:app2]; |
| 116 | + XCTAssertNotNil(auth2); |
| 117 | + XCTAssertEqual(auth2.app, app2); |
| 118 | + |
| 119 | + XCTAssertNotEqual(auth1, auth2); |
| 120 | +} |
| 121 | + |
| 122 | +/** @fn testLifeCycle |
| 123 | + @brief Verifies the life cycle of @c FIRAuth is the same as its associated @c FIRApp . |
| 124 | + */ |
| 125 | +- (void)testLifeCycle { |
| 126 | + __weak FIRApp *app; |
| 127 | + __weak FIRAuth *auth; |
| 128 | + @autoreleasepool { |
| 129 | + FIRApp *app1 = [self app1]; |
| 130 | + app = app1; |
| 131 | + auth = [FIRAuth authWithApp:app1]; |
| 132 | + // Verify that neither the app nor the auth is released yet, i.e., the app owns the auth |
| 133 | + // because nothing else retains the auth. |
| 134 | + XCTAssertNotNil(app); |
| 135 | + XCTAssertNotNil(auth); |
| 136 | + } |
| 137 | + [self waitForTimeIntervel:kWaitInterval]; |
| 138 | + // Verify that both the app and the auth are released upon exit of the autorelease pool, |
| 139 | + // i.e., the app is the sole owner of the auth. |
| 140 | + XCTAssertNil(app); |
| 141 | + XCTAssertNil(auth); |
| 142 | +} |
| 143 | + |
| 144 | +/** @fn app1 |
| 145 | + @brief Creates a Firebase app. |
| 146 | + @return A @c FIRApp with some name. |
| 147 | + */ |
| 148 | +- (FIRApp *)app1 { |
| 149 | + return [FIRApp appForAuthUnitTestsWithName:kFirebaseAppName1]; |
| 150 | +} |
| 151 | + |
| 152 | +/** @fn app2 |
| 153 | + @brief Creates another Firebase app. |
| 154 | + @return A @c FIRApp with some other name. |
| 155 | + */ |
| 156 | +- (FIRApp *)app2 { |
| 157 | + return [FIRApp appForAuthUnitTestsWithName:kFirebaseAppName2]; |
| 158 | +} |
| 159 | + |
| 160 | +/** @fn waitForTimeInterval: |
| 161 | + @brief Wait for a particular time interval. |
| 162 | + @remarks This method also waits for all other pending @c XCTestExpectation instances. |
| 163 | + */ |
| 164 | +- (void)waitForTimeIntervel:(NSTimeInterval)timeInterval { |
| 165 | + static dispatch_queue_t queue; |
| 166 | + static dispatch_once_t onceToken; |
| 167 | + XCTestExpectation *expectation = [self expectationWithDescription:@"waitForTimeIntervel:"]; |
| 168 | + dispatch_once(&onceToken, ^{ |
| 169 | + queue = dispatch_queue_create("com.google.FIRAuthUnitTests.waitForTimeIntervel", NULL); |
| 170 | + }); |
| 171 | + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeInterval * NSEC_PER_SEC), queue, ^() { |
| 172 | + [expectation fulfill]; |
| 173 | + }); |
| 174 | + [self waitForExpectationsWithTimeout:timeInterval + kExpectationTimeout handler:nil]; |
| 175 | +} |
| 176 | + |
| 177 | +@end |
0 commit comments