FIRAuthLifeCycleTests.m 5.0 KB

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