1 //------------------------------------------------------------------------------
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 "SetCountTester.h"
28 // Local Defines ---------------------------------------------------------------
30 // Globals ---------------------------------------------------------------------
32 //------------------------------------------------------------------------------
38 static const char *kTesterSignature
39 = "application/x-vnd.obos-messagerunner-setcount-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 SetCount(int32 count)
60 @case 1 object is not properly initialized, count > 0
61 @results Should return B_BAD_VALUE.
62 InitCheck() should return B_ERROR.
63 GetInfo() should return B_BAD_VALUE.
65 void SetCountTester::SetCount1()
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 int32 newCount
= 100000;
76 CHK(runner
.SetCount(newCount
) == B_BAD_VALUE
);
77 CHK(runner
.InitCheck() == B_ERROR
);
78 check_message_runner_info(runner
, B_BAD_VALUE
);
82 status_t SetCount(int32 count)
83 @case 2 object was properly initialized, but has already delivered
84 all its messages and thus became unusable, count > 0
85 @results Should return B_BAD_VALUE.
86 InitCheck() should return B_OK.
87 GetInfo() should return B_BAD_VALUE.
89 void SetCountTester::SetCount2()
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 int32 newCount
= 100000;
102 CHK(runner
.SetCount(newCount
) == B_BAD_VALUE
);
103 CHK(runner
.InitCheck() == B_OK
);
104 check_message_runner_info(runner
, B_BAD_VALUE
);
108 status_t SetCount(int32 count)
109 @case 3 object is properly initialized and has still one message
110 to deliver, count > 0
111 @results Should return B_OK.
112 InitCheck() should return B_OK.
113 GetInfo() should return B_OK and the new count.
114 The timer is NOT reset. count messages should arrive.
116 void SetCountTester::SetCount3()
118 MessageRunnerTestApp
app(kTesterSignature
);
119 MessageRunnerTestLooper
*looper
= app
.TestLooper();
120 BMessenger
target(looper
);
121 BMessage
message(MSG_RUNNER_MESSAGE
);
122 bigtime_t interval
= 50000;
124 BMessageRunner
runner(target
, &message
, interval
, count
);
125 bigtime_t startTime
= system_time();
126 CHK(runner
.InitCheck() == B_OK
);
127 interval
= max(interval
, kMinTimeInterval
);
128 check_message_runner_info(runner
, B_OK
, interval
, count
);
129 snooze((count
- 1) * interval
+ 10000);
130 CHK(looper
->CheckMessages(startTime
, interval
, count
- 1));
131 CHK(app
.CountReplies() == count
- 1);
133 bigtime_t newCount
= 3;
134 CHK(runner
.SetCount(newCount
) == B_OK
);
135 CHK(runner
.InitCheck() == B_OK
);
136 check_message_runner_info(runner
, B_OK
, interval
, newCount
);
137 // startTime += system_time();
138 startTime
+= (count
- 1) * interval
;
139 snooze((newCount
+ 1) * interval
+ 10000);
140 CHK(looper
->CheckMessages(count
- 1, startTime
, interval
, newCount
));
141 CHK(app
.CountReplies() == count
- 1 + newCount
);
145 status_t SetCount(int32 count)
146 @case 4 object is properly initialized and has still some messages
147 to deliver, count > 0
148 @results Should return B_OK.
149 InitCheck() should return B_OK.
150 GetInfo() should return B_OK and the new count.
151 The timer is NOT reset. count messages should arrive.
153 void SetCountTester::SetCount4()
155 MessageRunnerTestApp
app(kTesterSignature
);
156 MessageRunnerTestLooper
*looper
= app
.TestLooper();
157 BMessenger
target(looper
);
158 BMessage
message(MSG_RUNNER_MESSAGE
);
159 bigtime_t interval
= 50000;
161 BMessageRunner
runner(target
, &message
, interval
, count
);
162 bigtime_t startTime
= system_time();
163 CHK(runner
.InitCheck() == B_OK
);
164 interval
= max(interval
, kMinTimeInterval
);
165 check_message_runner_info(runner
, B_OK
, interval
, count
);
166 snooze(1 * interval
+ 10000);
167 CHK(looper
->CheckMessages(startTime
, interval
, 1));
168 CHK(app
.CountReplies() == 1);
170 bigtime_t newCount
= 3;
171 CHK(runner
.SetCount(newCount
) == B_OK
);
172 CHK(runner
.InitCheck() == B_OK
);
173 check_message_runner_info(runner
, B_OK
, interval
, newCount
);
174 startTime
+= 1 * interval
;
175 snooze((newCount
+ 1) * interval
+ 10000);
176 CHK(looper
->CheckMessages(1, startTime
, interval
, newCount
));
177 CHK(app
.CountReplies() == 1 + newCount
);
181 status_t SetCount(int32 count)
182 @case 5 object is properly initialized and has still an unlimited
183 number of messages to deliver, count > 0
184 @results Should return B_OK.
185 InitCheck() should return B_OK.
186 GetInfo() should return B_OK and the new count.
187 The timer is NOT reset. count messages should arrive.
189 void SetCountTester::SetCount5()
191 MessageRunnerTestApp
app(kTesterSignature
);
192 MessageRunnerTestLooper
*looper
= app
.TestLooper();
193 BMessenger
target(looper
);
194 BMessage
message(MSG_RUNNER_MESSAGE
);
195 bigtime_t interval
= 50000;
197 BMessageRunner
runner(target
, &message
, interval
, count
);
198 bigtime_t startTime
= system_time();
199 CHK(runner
.InitCheck() == B_OK
);
200 interval
= max(interval
, kMinTimeInterval
);
201 check_message_runner_info(runner
, B_OK
, interval
, count
);
202 snooze(1 * interval
+ 10000);
203 CHK(looper
->CheckMessages(startTime
, interval
, 1));
204 CHK(app
.CountReplies() == 1);
206 bigtime_t newCount
= 3;
207 CHK(runner
.SetCount(newCount
) == B_OK
);
208 CHK(runner
.InitCheck() == B_OK
);
209 check_message_runner_info(runner
, B_OK
, interval
, newCount
);
210 startTime
+= 1 * interval
;
211 snooze((newCount
+ 1) * interval
+ 10000);
212 CHK(looper
->CheckMessages(1, startTime
, interval
, newCount
));
213 CHK(app
.CountReplies() == 1 + newCount
);
217 status_t SetCount(int32 count)
218 @case 6 object is properly initialized and has still some messages
219 to deliver, count == 0
220 @results Should return B_OK.
221 InitCheck() should return B_OK.
222 R5: GetInfo() should return B_OK and count 0!
223 The timer is NOT reset and a message arives after the
224 time specified by the interval!
225 OBOS: GetInfo() should return B_BAD_VALUE.
227 void SetCountTester::SetCount6()
229 MessageRunnerTestApp
app(kTesterSignature
);
230 MessageRunnerTestLooper
*looper
= app
.TestLooper();
231 BMessenger
target(looper
);
232 BMessage
message(MSG_RUNNER_MESSAGE
);
233 bigtime_t interval
= 70000;
235 BMessageRunner
runner(target
, &message
, interval
, count
);
236 bigtime_t startTime
= system_time();
237 CHK(runner
.InitCheck() == B_OK
);
238 interval
= max(interval
, kMinTimeInterval
);
239 check_message_runner_info(runner
, B_OK
, interval
, count
);
240 snooze(1 * interval
+ 10000);
241 CHK(looper
->CheckMessages(startTime
, interval
, 1));
242 CHK(app
.CountReplies() == 1);
244 bigtime_t newCount
= 0;
245 CHK(runner
.SetCount(newCount
) == B_OK
);
246 CHK(runner
.InitCheck() == B_OK
);
247 startTime
+= 1 * interval
;
249 check_message_runner_info(runner
, B_OK
, interval
, newCount
);
250 snooze((newCount
+ 1) * interval
+ 10000);
251 CHK(looper
->CheckMessages(1, startTime
, interval
, 1));
252 CHK(app
.CountReplies() == 1 + 1);
254 check_message_runner_info(runner
, B_BAD_VALUE
);
255 snooze(1 * interval
+ 10000);
256 CHK(looper
->CheckMessages(1, startTime
, interval
, 0));
257 CHK(app
.CountReplies() == 1);
262 status_t SetCount(int32 count)
263 @case 7 object is properly initialized and has still some messages
264 to deliver, count < 0
265 @results Should return B_OK.
266 InitCheck() should return B_OK.
267 GetInfo() should return B_OK and the new count.
268 The timer is NOT reset. Unlimited number of messages.
270 void SetCountTester::SetCount7()
272 MessageRunnerTestApp
app(kTesterSignature
);
273 MessageRunnerTestLooper
*looper
= app
.TestLooper();
274 BMessenger
target(looper
);
275 BMessage
message(MSG_RUNNER_MESSAGE
);
276 bigtime_t interval
= 70000;
278 BMessageRunner
runner(target
, &message
, interval
, count
);
279 bigtime_t startTime
= system_time();
280 CHK(runner
.InitCheck() == B_OK
);
281 interval
= max(interval
, kMinTimeInterval
);
282 check_message_runner_info(runner
, B_OK
, interval
, count
);
283 snooze(1 * interval
+ 10000);
284 CHK(looper
->CheckMessages(startTime
, interval
, 1));
285 CHK(app
.CountReplies() == 1);
287 bigtime_t newCount
= -1;
288 CHK(runner
.SetCount(newCount
) == B_OK
);
289 CHK(runner
.InitCheck() == B_OK
);
290 check_message_runner_info(runner
, B_OK
, interval
, newCount
);
291 startTime
+= 1 * interval
;
292 int32 checkCount
= 5;
293 snooze(checkCount
* interval
+ 10000);
294 CHK(looper
->CheckMessages(1, startTime
, interval
, checkCount
));
295 CHK(app
.CountReplies() == 1 + checkCount
);
299 Test
* SetCountTester::Suite()
301 TestSuite
* SuiteOfTests
= new TestSuite
;
303 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetCountTester
, SetCount1
);
304 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetCountTester
, SetCount2
);
305 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetCountTester
, SetCount3
);
306 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetCountTester
, SetCount4
);
307 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetCountTester
, SetCount5
);
308 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetCountTester
, SetCount6
);
309 ADD_TEST4(BMessageRunner
, SuiteOfTests
, SetCountTester
, SetCount7
);