FIRAuthDispatcherTests.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2017 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/Sources/Auth/FIRAuthDispatcher.h"
  18. /** @var kMaxDifferenceBetweenTimeIntervals
  19. @brief The maximum difference between time intervals (in seconds), after which they will be
  20. considered different.
  21. */
  22. static const NSTimeInterval kMaxDifferenceBetweenTimeIntervals = 0.3;
  23. /** @var kTestDelay
  24. @brief Fake time delay before tasks are dispatched.
  25. */
  26. NSTimeInterval kTestDelay = 0.1;
  27. /** @var kExpectationTimeout
  28. @brief The maximum time waiting for expectations to fulfill.
  29. */
  30. static const NSTimeInterval kExpectationTimeout = 2;
  31. id<OS_dispatch_queue> testWorkQueue;
  32. /** @class FIRAuthDispatcherTests
  33. @brief Tests for @c FIRAuthDispatcher.
  34. */
  35. @interface FIRAuthDispatcherTests : XCTestCase
  36. @end
  37. @implementation FIRAuthDispatcherTests
  38. - (void)setUp {
  39. [super setUp];
  40. testWorkQueue = dispatch_queue_create("test.work.queue", NULL);
  41. }
  42. /** @fn testSharedInstance
  43. @brief Tests @c sharedInstance returns the same object.
  44. */
  45. - (void)testSharedInstance {
  46. FIRAuthDispatcher *instance1 = [FIRAuthDispatcher sharedInstance];
  47. FIRAuthDispatcher *instance2 = [FIRAuthDispatcher sharedInstance];
  48. XCTAssertEqual(instance1, instance2);
  49. }
  50. /** @fn testDispatchAfterDelay
  51. @brief Tests @c dispatchAfterDelay indeed dispatches the specified task after the provided
  52. delay.
  53. */
  54. - (void)testDispatchAfterDelay {
  55. FIRAuthDispatcher *dispatcher = [FIRAuthDispatcher sharedInstance];
  56. XCTestExpectation *expectation = [self expectationWithDescription:@"dispatchAfterCallback"];
  57. NSDate *dateBeforeDispatch = [NSDate date];
  58. dispatcher.dispatchAfterImplementation = nil;
  59. [dispatcher dispatchAfterDelay:kTestDelay
  60. queue:testWorkQueue
  61. task:^{
  62. NSTimeInterval timeSinceDispatch =
  63. fabs([dateBeforeDispatch timeIntervalSinceNow]) - kTestDelay;
  64. XCTAssert(timeSinceDispatch < kMaxDifferenceBetweenTimeIntervals);
  65. [expectation fulfill];
  66. }];
  67. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  68. dispatcher = nil;
  69. }
  70. /** @fn testSetDispatchAfterImplementation
  71. @brief Tests taht @c dispatchAfterImplementation indeed configures a custom implementation for
  72. @c dispatchAfterDelay.
  73. */
  74. - (void)testSetDispatchAfterImplementation {
  75. FIRAuthDispatcher *dispatcher = [FIRAuthDispatcher sharedInstance];
  76. XCTestExpectation *expectation1 = [self expectationWithDescription:@"setDispatchTokenCallback"];
  77. [dispatcher setDispatchAfterImplementation:^(
  78. NSTimeInterval delay, id<OS_dispatch_queue> _Nonnull queue, void (^task)(void)) {
  79. XCTAssertEqual(kTestDelay, delay);
  80. XCTAssertEqual(testWorkQueue, queue);
  81. [expectation1 fulfill];
  82. }];
  83. [dispatcher dispatchAfterDelay:kTestDelay
  84. queue:testWorkQueue
  85. task:^{
  86. // Fail to ensure this code is never executed.
  87. XCTFail();
  88. }];
  89. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  90. dispatcher.dispatchAfterImplementation = nil;
  91. }
  92. @end