/GHUnitIOS.framework/Versions/A/Headers/GHAsyncTestCase.h

http://github.com/zwaldowski/BlocksKit · C++ Header · 155 lines · 25 code · 16 blank · 114 comment · 0 complexity · ef6bd6bef66d63b916ef4cc8bf2eea6e MD5 · raw file

  1. //
  2. // GHAsyncTestCase.h
  3. // GHUnit
  4. //
  5. // Created by Gabriel Handford on 4/8/09.
  6. // Copyright 2009. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person
  9. // obtaining a copy of this software and associated documentation
  10. // files (the "Software"), to deal in the Software without
  11. // restriction, including without limitation the rights to use,
  12. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the
  14. // Software is furnished to do so, subject to the following
  15. // conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. // OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. #import "GHTestCase.h"
  30. // Some default statuses to use; Or define and use your own
  31. enum {
  32. kGHUnitWaitStatusUnknown = 0, //!< Unknown wait status
  33. kGHUnitWaitStatusSuccess, //!< Wait status success
  34. kGHUnitWaitStatusFailure, //!< Wait status failure
  35. kGHUnitWaitStatusCancelled //!< Wait status cancelled
  36. };
  37. /*!
  38. Asynchronous test case with wait and notify.
  39. If notify occurs before wait has started (if it was a synchronous call), this test
  40. case will still work.
  41. Be sure to call prepare before the asynchronous method (otherwise an exception will raise).
  42. @code
  43. @interface MyAsyncTest : GHAsyncTestCase { }
  44. @end
  45. @implementation MyAsyncTest
  46. - (void)testSuccess {
  47. [self prepare];
  48. // Do asynchronous task here
  49. [self performSelector:@selector(_succeed) withObject:nil afterDelay:0.1];
  50. [self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
  51. }
  52. - (void)_succeed {
  53. // Notice the forSelector points to the test above. This is so that
  54. // stray notifies don't error or falsely succeed other tests.
  55. // To ignore the check, forSelector can be NULL.
  56. [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testSuccess)];
  57. }
  58. @end
  59. @endcode
  60. */
  61. @interface GHAsyncTestCase : GHTestCase {
  62. NSInteger waitForStatus_;
  63. NSInteger notifiedStatus_;
  64. BOOL prepared_; // Whether prepared was called before waitForStatus:timeout:
  65. NSRecursiveLock *lock_; // Lock to synchronize on
  66. SEL waitSelector_; // The selector we are waiting on
  67. NSArray *_runLoopModes;
  68. }
  69. /*!
  70. Run loop modes to run while waiting;
  71. Defaults to NSDefaultRunLoopMode, NSRunLoopCommonModes, NSConnectionReplyMode
  72. */
  73. @property (retain, nonatomic) NSArray *runLoopModes;
  74. /*!
  75. Prepare before calling the asynchronous method.
  76. */
  77. - (void)prepare;
  78. /*!
  79. Prepare and specify the selector we will use in notify.
  80. @param selector
  81. */
  82. - (void)prepare:(SEL)selector;
  83. /*!
  84. Wait for notification of status or timeout.
  85. Be sure to prepare before calling your asynchronous method.
  86. For example,
  87. @code
  88. - (void)testFoo {
  89. [self prepare];
  90. // Do asynchronous task here
  91. [self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
  92. }
  93. @endcode
  94. @param status kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status
  95. @param timeout Timeout in seconds
  96. */
  97. - (void)waitForStatus:(NSInteger)status timeout:(NSTimeInterval)timeout;
  98. /*!
  99. @deprecated
  100. */
  101. - (void)waitFor:(NSInteger)status timeout:(NSTimeInterval)timeout;
  102. /*!
  103. Wait for timeout to occur.
  104. Fails if we did _NOT_ timeout.
  105. @param timeout
  106. */
  107. - (void)waitForTimeout:(NSTimeInterval)timeout;
  108. /*!
  109. Notify waiting of status for test selector.
  110. @param status Status, for example, kGHUnitWaitStatusSuccess
  111. @param selector If not NULL, then will verify this selector is where we are waiting.
  112. This prevents stray asynchronous callbacks to fail a later test
  113. */
  114. - (void)notify:(NSInteger)status forSelector:(SEL)selector;
  115. /*!
  116. Notify waiting of status for any selector.
  117. @param status Status, for example, kGHUnitWaitStatusSuccess
  118. */
  119. - (void)notify:(NSInteger)status;
  120. /*!
  121. Run the run loops for the specified interval.
  122. @param interval
  123. @author Adapted from Robert Palmer, pauseForTimeout
  124. */
  125. - (void)runForInterval:(NSTimeInterval)interval;
  126. @end