1 //------------------------------------------------------------------------------
2 // SetIntervalTester.cpp
4 //------------------------------------------------------------------------------
6 // Standard Includes -----------------------------------------------------------
10 // System Includes -------------------------------------------------------------
11 #include <Application.h>
15 #include <MessageRunner.h>
16 #include <Messenger.h>
19 // Project Includes ------------------------------------------------------------
20 #include <TestShell.h>
21 #include <TestUtils.h>
22 #include <cppunit/TestAssert.h>
24 // Local Includes --------------------------------------------------------------
25 #include "MessageRunnerTestHelpers.h"
26 #include "SetIntervalTester.h"
28 // Local Defines ---------------------------------------------------------------
30 // Globals ---------------------------------------------------------------------
32 //------------------------------------------------------------------------------
38 static const char *kTesterSignature
39 = "application/x-vnd.obos-messagerunner-setinterval-test";
41 static const bigtime_t kMinTimeInterval
= 50000;
43 // check_message_runner_info
46 check_message_runner_info(const BMessageRunner
&runner
, status_t error
,
47 bigtime_t interval
= 0, int32 count
= 0)
49 bigtime_t runnerInterval
= 0;
50 int32 runnerCount
= 0;
51 CHK(runner
.GetInfo(&runnerInterval
, &runnerCount
) == error
);
53 CHK(runnerInterval
== interval
);
54 CHK(runnerCount
== count
);
59 status_t SetInterval(bigtime_t interval)
60 @case 1 object is not properly initialized, interval > 0
61 @results Should return B_BAD_VALUE.
62 InitCheck() should return B_ERROR.
63 GetInfo() should return B_BAD_VALUE.
65 void SetIntervalTester::SetInterval1()
67 MessageRunnerTestApp
app(kTesterSignature
);
69 BMessage
message(MSG_RUNNER_MESSAGE
);
70 bigtime_t interval
= 100000;
72 BMessageRunner
runner(target
, &message
, interval
, count
);
73 CHK(runner
.InitCheck() == B_ERROR
);
74 check_message_runner_info(runner
, B_BAD_VALUE
);
75 bigtime_t newInterval
= 100000;
76 CHK(runner
.SetInterval(newInterval
) == B_BAD_VALUE
);
77 CHK(runner
.InitCheck() == B_ERROR
);
78 check_message_runner_info(runner
, B_BAD_VALUE
);
82 status_t SetInterval(bigtime_t interval)
83 @case 2 object was properly initialized, but has already delivered
84 all its messages and thus became unusable, interval > 0
85 @results Should return B_BAD_VALUE.
86 InitCheck() should return B_OK.
87 GetInfo() should return B_BAD_VALUE.
89 void SetIntervalTester::SetInterval2()
91 MessageRunnerTestApp
app(kTesterSignature
);
93 BMessage
message(MSG_RUNNER_MESSAGE
);
94 bigtime_t interval
= 50000;
96 BMessageRunner
runner(target
, &message
, interval
, count
);
97 CHK(runner
.InitCheck() == B_OK
);
98 check_message_runner_info(runner
, B_OK
, interval
, count
);
99 snooze(count
* interval
+ 10000);
101 bigtime_t newInterval
= 100000;
102 CHK(runner
.SetInterval(newInterval
) == B_BAD_VALUE
);
103 CHK(runner
.InitCheck() == B_OK
);
104 check_message_runner_info(runner
, B_BAD_VALUE
);
108 status_t SetInterval(bigtime_t interval)
109 @case 3 object is properly initialized and has still one message
110 to deliver, interval > 0
111 @results Should return B_OK.
112 InitCheck() should return B_OK.
113 GetInfo() should return B_OK and the new interval.
114 The timer is reset. The last message arives after the time
115 specified by interval has passed.
117 void SetIntervalTester::SetInterval3()
119 MessageRunnerTestApp
app(kTesterSignature
);
120 MessageRunnerTestLooper
*looper
= app
.TestLooper();
121 BMessenger
target(looper
);
122 BMessage
message(MSG_RUNNER_MESSAGE
);
123 bigtime_t interval
= 50000;
125 BMessageRunner
runner(target
, &message
, interval
, count
);
126 bigtime_t startTime
= system_time();
127 CHK(runner
.InitCheck() == B_OK
);
128 interval
= max(interval
, kMinTimeInterval
);
129 check_message_runner_info(runner
, B_OK
, interval
, count
);
130 snooze((count
- 1) * interval
+ 10000);
131 CHK(looper
->CheckMessages(startTime
, interval
, count
- 1));
132 CHK(app
.CountReplies() == count
- 1);
134 bigtime_t newInterval
= 100000;
135 CHK(runner
.SetInterval(newInterval
) == B_OK
);
136 CHK(runner
.InitCheck() == B_OK
);
137 check_message_runner_info(runner
, B_OK
, newInterval
, 1);
138 startTime
= system_time();
139 snooze(2 * newInterval
+ 10000);
140 CHK(looper
->CheckMessages(count
- 1, startTime
, newInterval
, 1));
141 CHK(app
.CountReplies() == count
);
145 status_t SetInterval(bigtime_t interval)
146 @case 4 object is properly initialized and has still some messages
147 to deliver, interval > 0
148 @results Should return B_OK.
149 InitCheck() should return B_OK.
150 GetInfo() should return B_OK and the new interval.
151 The timer is reset. The messages start to arive after
152 the time specified by interval.
154 void SetIntervalTester::SetInterval4()
156 MessageRunnerTestApp
app(kTesterSignature
);
157 MessageRunnerTestLooper
*looper
= app
.TestLooper();
158 BMessenger
target(looper
);
159 BMessage
message(MSG_RUNNER_MESSAGE
);
160 bigtime_t interval
= 50000;
162 BMessageRunner
runner(target
, &message
, interval
, count
);
163 bigtime_t startTime
= system_time();
164 CHK(runner
.InitCheck() == B_OK
);
165 interval
= max(interval
, kMinTimeInterval
);
166 check_message_runner_info(runner
, B_OK
, interval
, count
);
167 snooze(1 * interval
+ 10000);
168 CHK(looper
->CheckMessages(startTime
, interval
, 1));
169 CHK(app
.CountReplies() == 1);
171 bigtime_t newInterval
= 70000;
172 CHK(runner
.SetInterval(newInterval
) == B_OK
);
173 CHK(runner
.InitCheck() == B_OK
);
174 check_message_runner_info(runner
, B_OK
, newInterval
, count
- 1);
175 startTime
= system_time();
176 snooze(count
* newInterval
+ 10000);
177 CHK(looper
->CheckMessages(1, startTime
, newInterval
, count
- 1));
178 CHK(app
.CountReplies() == count
);
182 status_t SetInterval(bigtime_t interval)
183 @case 5 object is properly initialized and has still an unlimited
184 number of messages to deliver, interval > 0
185 @results Should return B_OK.
186 InitCheck() should return B_OK.
187 GetInfo() should return B_OK and the new interval.
188 The timer is reset. The messages start to arive after
189 the time specified by interval.
191 void SetIntervalTester::SetInterval5()
193 MessageRunnerTestApp
app(kTesterSignature
);
194 MessageRunnerTestLooper
*looper
= app
.TestLooper();
195 BMessenger
target(looper
);
196 BMessage
message(MSG_RUNNER_MESSAGE
);
197 bigtime_t interval
= 50000;
199 BMessageRunner
runner(target
, &message
, interval
, count
);
200 bigtime_t startTime
= system_time();
201 CHK(runner
.InitCheck() == B_OK
);
202 interval
= max(interval
, kMinTimeInterval
);
203 check_message_runner_info(runner
, B_OK
, interval
, count
);
204 snooze(1 * interval
+ 10000);
205 CHK(looper
->CheckMessages(startTime
, interval
, 1));
206 CHK(app
.CountReplies() == 1);
208 bigtime_t newInterval
= 70000;
209 CHK(runner
.SetInterval(newInterval
) == B_OK
);
210 CHK(runner
.InitCheck() == B_OK
);
211 check_message_runner_info(runner
, B_OK
, newInterval
, -1);
212 startTime
= system_time();
213 int32 checkCount
= 5;
214 snooze(checkCount
* newInterval
+ 10000);
215 CHK(looper
->CheckMessages(1, startTime
, newInterval
, checkCount
));
216 CHK(app
.CountReplies() == checkCount
+ 1);
220 status_t SetInterval(bigtime_t interval)
221 @case 6 object is properly initialized and has still some messages
222 to deliver, interval == 0
223 @results Should return B_OK.
224 InitCheck() should return B_OK.
225 R5: GetInfo() should return B_BAD_VALUE.
226 All messages are delivered, but at weird times.
227 OBOS: GetInfo() should return B_OK and the minimal
228 interval. The timer is reset. The messages start to
229 arive after the time specified by the minimal
232 void SetIntervalTester::SetInterval6()
234 MessageRunnerTestApp
app(kTesterSignature
);
235 MessageRunnerTestLooper
*looper
= app
.TestLooper();
236 BMessenger
target(looper
);
237 BMessage
message(MSG_RUNNER_MESSAGE
);
238 bigtime_t interval
= 70000;
240 BMessageRunner
runner(target
, &message
, interval
, count
);
241 bigtime_t startTime
= system_time();
242 CHK(runner
.InitCheck() == B_OK
);
243 interval
= max(interval
, kMinTimeInterval
);
244 check_message_runner_info(runner
, B_OK
, interval
, count
);
245 snooze(1 * interval
+ 10000);
246 CHK(looper
->CheckMessages(startTime
, interval
, 1));
247 CHK(app
.CountReplies() == 1);
249 bigtime_t newInterval
= 0;
250 CHK(runner
.SetInterval(newInterval
) == B_OK
);
251 newInterval
= max(newInterval
, kMinTimeInterval
);
252 CHK(runner
.InitCheck() == B_OK
);
254 check_message_runner_info(runner
, B_BAD_VALUE
);
255 snooze(count
* interval
+ 10000);
257 check_message_runner_info(runner
, B_OK
, newInterval
, count
- 1);
258 startTime
= system_time();
259 snooze(count
* newInterval
+ 10000);
260 CHK(looper
->CheckMessages(1, startTime
, newInterval
, count
- 1));
262 CHK(app
.CountReplies() == count
);
266 status_t SetInterval(bigtime_t interval)
267 @case 7 object is properly initialized and has still some messages
268 to deliver, interval < 0
269 @results Should return B_OK.
270 InitCheck() should return B_OK.
271 R5: GetInfo() should return B_BAD_VALUE.
272 All messages are delivered, but at weird times.
273 OBOS: GetInfo() should return B_OK and the minimal
274 interval. The timer is reset. The messages start to
275 arive after the time specified by the minimal
278 void SetIntervalTester::SetInterval7()
280 MessageRunnerTestApp
app(kTesterSignature
);
281 MessageRunnerTestLooper
*looper
= app
.TestLooper();
282 BMessenger
target(looper
);
283 BMessage
message(MSG_RUNNER_MESSAGE
);
284 bigtime_t interval
= 70000;
286 BMessageRunner
runner(target
, &message
, interval
, count
);
287 bigtime_t startTime
= system_time();
288 CHK(runner
.InitCheck() == B_OK
);
289 interval
= max(interval
, kMinTimeInterval
);
290 check_message_runner_info(runner
, B_OK
, interval
, count
);
291 snooze(1 * interval
+ 10000);
292 CHK(looper
->CheckMessages(startTime
, interval
, 1));
293 CHK(app
.CountReplies() == 1);
295 bigtime_t newInterval
= -1;
296 CHK(runner
.SetInterval(newInterval
) == B_OK
);
297 newInterval
= max(newInterval
, kMinTimeInterval
);
298 CHK(runner
.InitCheck() == B_OK
);
300 check_message_runner_info(runner
, B_BAD_VALUE
);
301 snooze(count
* interval
+ 10000);
303 check_message_runner_info(runner
, B_OK
, newInterval
, count
- 1);
304 startTime
= system_time();
305 snooze(count
* newInterval
+ 10000);
306 CHK(looper
->CheckMessages(1, startTime
, newInterval
, count
- 1));
308 CHK(app
.CountReplies() == count
);
312 Test
* SetIntervalTester::Suite()
314 TestSuite
* SuiteOfTests
= new TestSuite
;
316 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetIntervalTester
, SetInterval1
);
317 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetIntervalTester
, SetInterval2
);
318 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetIntervalTester
, SetInterval3
);
319 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetIntervalTester
, SetInterval4
);
320 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetIntervalTester
, SetInterval5
);
321 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetIntervalTester
, SetInterval6
);
322 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetIntervalTester
, SetInterval7
);