Skip to content
Newer
Older
100644 164 lines (127 sloc) 4.69 KB
e24ecfc @gabriel Asynchronous test case
gabriel authored
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
30 #import "GHTestCase.h"
31
c2aedda @gabriel Appledoc
gabriel authored
32 /*!
33 Common wait statuses to use with waitForStatus:timeout:.
34 */
e24ecfc @gabriel Asynchronous test case
gabriel authored
35 enum {
c2aedda @gabriel Appledoc
gabriel authored
36 kGHUnitWaitStatusUnknown = 0, // Unknown wait status
37 kGHUnitWaitStatusSuccess, // Wait status success
38 kGHUnitWaitStatusFailure, // Wait status failure
39 kGHUnitWaitStatusCancelled // Wait status cancelled
e24ecfc @gabriel Asynchronous test case
gabriel authored
40 };
41
42 /*!
93f8b71 @gabriel Updating documentation (Doxygen)
gabriel authored
43 Asynchronous test case with wait and notify.
44
4aa498b @gabriel Removing stale files; Cleaning up some source;
gabriel authored
45 If notify occurs before wait has started (if it was a synchronous call), this test
46 case will still work.
47
e24ecfc @gabriel Asynchronous test case
gabriel authored
48 Be sure to call prepare before the asynchronous method (otherwise an exception will raise).
49
c2aedda @gabriel Appledoc
gabriel authored
50 @interface MyAsyncTest : GHAsyncTestCase { }
51 @end
52
53 @implementation MyAsyncTest
54
55 - (void)testSuccess {
56 // Prepare for asynchronous call
57 [self prepare];
58
59 // Do asynchronous task here
60 [self performSelector:@selector(_succeed) withObject:nil afterDelay:0.1];
61
62 // Wait for notify
63 [self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
64 }
65
66 - (void)_succeed {
67 // Notify the wait. Notice the forSelector points to the test above.
68 // This is so that stray notifies don't error or falsely succeed other tests.
69 // To ignore the check, forSelector can be NULL.
70 [self notify:kGHUnitWaitStatusSuccess forSelector:@selector(testSuccess)];
71 }
72
73 @end
74
e24ecfc @gabriel Asynchronous test case
gabriel authored
75 */
76 @interface GHAsyncTestCase : GHTestCase {
77
e771172 @gabriel Tabs to spaces:
gabriel authored
78 NSInteger waitForStatus_;
79 NSInteger notifiedStatus_;
80
81 BOOL prepared_; // Whether prepared was called before waitForStatus:timeout:
82 NSRecursiveLock *lock_; // Lock to synchronize on
83 SEL waitSelector_; // The selector we are waiting on
84
4aa498b @gabriel Removing stale files; Cleaning up some source;
gabriel authored
85 NSArray *_runLoopModes;
e24ecfc @gabriel Asynchronous test case
gabriel authored
86 }
87
4aa498b @gabriel Removing stale files; Cleaning up some source;
gabriel authored
88 /*!
89 Run loop modes to run while waiting;
90 Defaults to NSDefaultRunLoopMode, NSRunLoopCommonModes, NSConnectionReplyMode
91 */
0530b1a @gabriel Converting to ARC. Fixing build issues.
gabriel authored
92 @property (strong, nonatomic) NSArray *runLoopModes;
65d7259 @gabriel Adding run loops modes property
gabriel authored
93
e24ecfc @gabriel Asynchronous test case
gabriel authored
94 /*!
93f8b71 @gabriel Updating documentation (Doxygen)
gabriel authored
95 Prepare before calling the asynchronous method.
e24ecfc @gabriel Asynchronous test case
gabriel authored
96 */
97 - (void)prepare;
98
99 /*!
100 Prepare and specify the selector we will use in notify.
c2aedda @gabriel Appledoc
gabriel authored
101
102 @param selector Selector
e24ecfc @gabriel Asynchronous test case
gabriel authored
103 */
104 - (void)prepare:(SEL)selector;
105
106 /*!
107 Wait for notification of status or timeout.
108
93f8b71 @gabriel Updating documentation (Doxygen)
gabriel authored
109 Be sure to prepare before calling your asynchronous method.
e24ecfc @gabriel Asynchronous test case
gabriel authored
110 For example,
111
c2aedda @gabriel Appledoc
gabriel authored
112 - (void)testFoo {
113 [self prepare];
114
115 // Do asynchronous task here
116
117 [self waitForStatus:kGHUnitWaitStatusSuccess timeout:1.0];
118 }
e24ecfc @gabriel Asynchronous test case
gabriel authored
119
120 @param status kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status
121 @param timeout Timeout in seconds
122 */
0e379ee @gabriel 0.3.9
gabriel authored
123 - (void)waitForStatus:(NSInteger)status timeout:(NSTimeInterval)timeout;
124
f626583 @gabriel Updating documentation for Doxygen
gabriel authored
125 /*!
c2aedda @gabriel Appledoc
gabriel authored
126 @param status kGHUnitWaitStatusSuccess, kGHUnitWaitStatusFailure or custom status
127 @param timeout Timeout in seconds
128 @deprecated Use waitForTimeout:
f626583 @gabriel Updating documentation for Doxygen
gabriel authored
129 */
e24ecfc @gabriel Asynchronous test case
gabriel authored
130 - (void)waitFor:(NSInteger)status timeout:(NSTimeInterval)timeout;
131
132 /*!
ca55904 @gabriel Adding waitForTimeout:
gabriel authored
133 Wait for timeout to occur.
134 Fails if we did _NOT_ timeout.
c2aedda @gabriel Appledoc
gabriel authored
135
136 @param timeout Timeout
ca55904 @gabriel Adding waitForTimeout:
gabriel authored
137 */
138 - (void)waitForTimeout:(NSTimeInterval)timeout;
139
140 /*!
0e379ee @gabriel 0.3.9
gabriel authored
141 Notify waiting of status for test selector.
c2aedda @gabriel Appledoc
gabriel authored
142
0e379ee @gabriel 0.3.9
gabriel authored
143 @param status Status, for example, kGHUnitWaitStatusSuccess
c2aedda @gabriel Appledoc
gabriel authored
144 @param selector If not NULL, then will verify this selector is where we are waiting. This prevents stray asynchronous callbacks to fail a later test.
e24ecfc @gabriel Asynchronous test case
gabriel authored
145 */
146 - (void)notify:(NSInteger)status forSelector:(SEL)selector;
147
0e379ee @gabriel 0.3.9
gabriel authored
148 /*!
149 Notify waiting of status for any selector.
c2aedda @gabriel Appledoc
gabriel authored
150
0e379ee @gabriel 0.3.9
gabriel authored
151 @param status Status, for example, kGHUnitWaitStatusSuccess
152 */
153 - (void)notify:(NSInteger)status;
154
cac475f @gabriel Run for interval (Robert Palmer)
gabriel authored
155 /*!
c2aedda @gabriel Appledoc
gabriel authored
156 Run the run loops for the specified interval.
157
158 @param interval Interval
cac475f @gabriel Run for interval (Robert Palmer)
gabriel authored
159 @author Adapted from Robert Palmer, pauseForTimeout
160 */
161 - (void)runForInterval:(NSTimeInterval)interval;
162
e24ecfc @gabriel Asynchronous test case
gabriel authored
163 @end
Something went wrong with that request. Please try again.