FIRAuthLifeCycleTests.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #import <XCTest/XCTest.h>
  17. #import "FirebaseAuth/Interop/FIRAuthInterop.h"
  18. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  19. #import "FirebaseAuth/Sources/Auth/FIRAuth_Internal.h"
  20. #import "FirebaseAuth/Sources/Backend/FIRAuthRequestConfiguration.h"
  21. #import "FirebaseAuth/Tests/Unit/FIRApp+FIRAuthUnitTests.h"
  22. /** @var kFirebaseAppName1
  23. @brief A fake Firebase app name.
  24. */
  25. static NSString *const kFirebaseAppName1 = @"FIREBASE_APP_NAME_1";
  26. /** @var kFirebaseAppName2
  27. @brief Another fake Firebase app name.
  28. */
  29. static NSString *const kFirebaseAppName2 = @"FIREBASE_APP_NAME_2";
  30. /** @var kAPIKey
  31. @brief The fake API key.
  32. */
  33. static NSString *const kAPIKey = @"FAKE_API_KEY";
  34. /** @var kExpectationTimeout
  35. @brief The maximum time waiting for expectations to fulfill.
  36. */
  37. static const NSTimeInterval kExpectationTimeout = 2;
  38. /** @var kWaitInterval
  39. @brief The time waiting for background tasks to finish before continue when necessary.
  40. */
  41. static const NSTimeInterval kWaitInterval = .5;
  42. @interface FIRAuthLifeCycleTests : XCTestCase
  43. @end
  44. @implementation FIRAuthLifeCycleTests
  45. - (void)setUp {
  46. [super setUp];
  47. [FIRApp resetAppForAuthUnitTests];
  48. }
  49. - (void)tearDown {
  50. [super tearDown];
  51. }
  52. /** @fn testSingleton
  53. @brief Verifies the @c auth method behaves like a singleton.
  54. */
  55. - (void)testSingleton {
  56. FIRAuth *auth1 = [FIRAuth auth];
  57. XCTAssertNotNil(auth1);
  58. FIRAuth *auth2 = [FIRAuth auth];
  59. XCTAssertEqual(auth1, auth2);
  60. }
  61. /** @fn testDefaultAuth
  62. @brief Verifies the @c auth method associates with the default Firebase app.
  63. */
  64. - (void)testDefaultAuth {
  65. FIRAuth *auth1 = [FIRAuth auth];
  66. FIRAuth *auth2 = [FIRAuth authWithApp:[FIRApp defaultApp]];
  67. XCTAssertEqual(auth1, auth2);
  68. XCTAssertEqual(auth1.app, [FIRApp defaultApp]);
  69. }
  70. /** @fn testNilAppException
  71. @brief Verifies the @c auth method raises an exception if the default FIRApp is not configured.
  72. */
  73. - (void)testNilAppException {
  74. [FIRApp resetApps];
  75. XCTAssertThrows([FIRAuth auth]);
  76. }
  77. /** @fn testAppAPIkey
  78. @brief Verifies the API key is correctly copied from @c FIRApp to @c FIRAuth .
  79. */
  80. - (void)testAppAPIkey {
  81. FIRAuth *auth = [FIRAuth auth];
  82. XCTAssertEqualObjects(auth.requestConfiguration.APIKey, kAPIKey);
  83. }
  84. /** @fn testAppAssociation
  85. @brief Verifies each @c FIRApp instance associates with a @c FIRAuth .
  86. */
  87. - (void)testAppAssociation {
  88. FIRApp *app1 = [self app1];
  89. FIRAuth *auth1 = [FIRAuth authWithApp:app1];
  90. XCTAssertNotNil(auth1);
  91. XCTAssertEqual(auth1.app, app1);
  92. FIRApp *app2 = [self app2];
  93. FIRAuth *auth2 = [FIRAuth authWithApp:app2];
  94. XCTAssertNotNil(auth2);
  95. XCTAssertEqual(auth2.app, app2);
  96. XCTAssertNotEqual(auth1, auth2);
  97. }
  98. /** @fn testLifeCycle
  99. @brief Verifies the life cycle of @c FIRAuth is the same as its associated @c FIRApp .
  100. */
  101. - (void)testLifeCycle {
  102. __weak FIRApp *app;
  103. __weak FIRAuth *auth;
  104. @autoreleasepool {
  105. FIRApp *app1 = [self app1];
  106. app = app1;
  107. auth = [FIRAuth authWithApp:app1];
  108. // Verify that neither the app nor the auth is released yet, i.e., the app owns the auth
  109. // because nothing else retains the auth.
  110. XCTAssertNotNil(app);
  111. XCTAssertNotNil(auth);
  112. }
  113. [self waitForTimeIntervel:kWaitInterval];
  114. // Verify that both the app and the auth are released upon exit of the autorelease pool,
  115. // i.e., the app is the sole owner of the auth.
  116. XCTAssertNil(app);
  117. XCTAssertNil(auth);
  118. }
  119. /** @fn app1
  120. @brief Creates a Firebase app.
  121. @return A @c FIRApp with some name.
  122. */
  123. - (FIRApp *)app1 {
  124. return [FIRApp appForAuthUnitTestsWithName:kFirebaseAppName1];
  125. }
  126. /** @fn app2
  127. @brief Creates another Firebase app.
  128. @return A @c FIRApp with some other name.
  129. */
  130. - (FIRApp *)app2 {
  131. return [FIRApp appForAuthUnitTestsWithName:kFirebaseAppName2];
  132. }
  133. /** @fn waitForTimeInterval:
  134. @brief Wait for a particular time interval.
  135. @remarks This method also waits for all other pending @c XCTestExpectation instances.
  136. */
  137. - (void)waitForTimeIntervel:(NSTimeInterval)timeInterval {
  138. static dispatch_queue_t queue;
  139. static dispatch_once_t onceToken;
  140. XCTestExpectation *expectation = [self expectationWithDescription:@"waitForTimeIntervel:"];
  141. dispatch_once(&onceToken, ^{
  142. queue = dispatch_queue_create("com.google.FIRAuthUnitTests.waitForTimeIntervel", NULL);
  143. });
  144. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, timeInterval * NSEC_PER_SEC), queue, ^() {
  145. [expectation fulfill];
  146. });
  147. [self waitForExpectationsWithTimeout:timeInterval + kExpectationTimeout handler:nil];
  148. }
  149. @end