FBRunLoopSpinnerTests.m 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * Copyright (c) 2015-present, Facebook, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. */
  9. #import <XCTest/XCTest.h>
  10. #import "FBRunLoopSpinner.h"
  11. @interface FBRunLoopSpinnerTests : XCTestCase
  12. @property (nonatomic, strong) FBRunLoopSpinner *spinner;
  13. @end
  14. /**
  15. Non of the test methods should block testing thread.
  16. If they do, that means they are broken
  17. */
  18. @implementation FBRunLoopSpinnerTests
  19. - (void)setUp
  20. {
  21. [super setUp];
  22. self.spinner = [[FBRunLoopSpinner new] timeout:0.1];
  23. }
  24. - (void)testSpinUntilCompletion
  25. {
  26. __block BOOL _didExecuteBlock = NO;
  27. [FBRunLoopSpinner spinUntilCompletion:^(void (^completion)(void)) {
  28. _didExecuteBlock = YES;
  29. completion();
  30. }];
  31. XCTAssertTrue(_didExecuteBlock);
  32. }
  33. - (void)testSpinUntilTrue
  34. {
  35. __block BOOL _didExecuteBlock = NO;
  36. BOOL didSucceed =
  37. [self.spinner spinUntilTrue:^BOOL{
  38. _didExecuteBlock = YES;
  39. return YES;
  40. }];
  41. XCTAssertTrue(didSucceed);
  42. XCTAssertTrue(_didExecuteBlock);
  43. }
  44. - (void)testSpinUntilTrueTimeout
  45. {
  46. NSError *error;
  47. BOOL didSucceed =
  48. [self.spinner spinUntilTrue:^BOOL{
  49. return NO;
  50. } error:&error];
  51. XCTAssertFalse(didSucceed);
  52. XCTAssertNotNil(error);
  53. }
  54. - (void)testSpinUntilTrueTimeoutMessage
  55. {
  56. NSString *expectedMessage = @"Magic message";
  57. NSError *error;
  58. BOOL didSucceed =
  59. [[self.spinner timeoutErrorMessage:expectedMessage]
  60. spinUntilTrue:^BOOL{
  61. return NO;
  62. } error:&error];
  63. XCTAssertFalse(didSucceed);
  64. XCTAssertEqual(error.localizedDescription, expectedMessage);
  65. }
  66. - (void)testSpinUntilNotNil
  67. {
  68. __block id expectedObject = NSObject.new;
  69. NSError *error;
  70. id returnedObject =
  71. [self.spinner spinUntilNotNil:^id{
  72. return expectedObject;
  73. } error:&error];
  74. XCTAssertNil(error);
  75. XCTAssertEqual(returnedObject, expectedObject);
  76. }
  77. - (void)testSpinUntilNotNilTimeout
  78. {
  79. NSError *error;
  80. id element =
  81. [self.spinner spinUntilNotNil:^id{
  82. return nil;
  83. } error:&error];
  84. XCTAssertNil(element);
  85. XCTAssertNotNil(error);
  86. }
  87. @end