1 //------------------------------------------------------------------------------
2 // SetCountTester.cpp
3 //
4 //------------------------------------------------------------------------------
5
6 // Standard Includes -----------------------------------------------------------
7 #include <algorithm>
8 #include <stdio.h>
9
10 // System Includes -------------------------------------------------------------
11 #include <Application.h>
12 #include <Handler.h>
13 #include <Looper.h>
14 #include <Message.h>
15 #include <MessageRunner.h>
16 #include <Messenger.h>
17 #include <OS.h>
18
19 // Project Includes ------------------------------------------------------------
20 #include <TestShell.h>
21 #include <TestUtils.h>
22 #include <cppunit/TestAssert.h>
23
24 // Local Includes --------------------------------------------------------------
25 #include "MessageRunnerTestHelpers.h"
26 #include "SetCountTester.h"
27
28 // Local Defines ---------------------------------------------------------------
29
30 // Globals ---------------------------------------------------------------------
31
32 //------------------------------------------------------------------------------
33
34
35 using namespace std;
36
37
38 static const char *kTesterSignature
39 = "application/x-vnd.obos-messagerunner-setcount-test";
40
41 static const bigtime_t kMinTimeInterval = 50000;
42
43 // check_message_runner_info
44 static
45 void
check_message_runner_info(const BMessageRunner & runner,status_t error,bigtime_t interval=0,int32 count=0)46 check_message_runner_info(const BMessageRunner &runner, status_t error,
47 bigtime_t interval = 0, int32 count = 0)
48 {
49 bigtime_t runnerInterval = 0;
50 int32 runnerCount = 0;
51 CHK(runner.GetInfo(&runnerInterval, &runnerCount) == error);
52 if (error == B_OK) {
53 CHK(runnerInterval == interval);
54 CHK(runnerCount == count);
55 }
56 }
57
58 /*
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.
64 */
SetCount1()65 void SetCountTester::SetCount1()
66 {
67 MessageRunnerTestApp app(kTesterSignature);
68 BMessenger target;
69 BMessage message(MSG_RUNNER_MESSAGE);
70 bigtime_t interval = 100000;
71 int32 count = 0;
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);
79 }
80
81 /*
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.
88 */
SetCount2()89 void SetCountTester::SetCount2()
90 {
91 MessageRunnerTestApp app(kTesterSignature);
92 BMessenger target;
93 BMessage message(MSG_RUNNER_MESSAGE);
94 bigtime_t interval = 50000;
95 int32 count = 1;
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);
100 // set new count
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);
105 }
106
107 /*
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.
115 */
SetCount3()116 void SetCountTester::SetCount3()
117 {
118 MessageRunnerTestApp app(kTesterSignature);
119 MessageRunnerTestLooper *looper = app.TestLooper();
120 BMessenger target(looper);
121 BMessage message(MSG_RUNNER_MESSAGE);
122 bigtime_t interval = 50000;
123 int32 count = 5;
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);
132 // set new count
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);
142 }
143
144 /*
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.
152 */
SetCount4()153 void SetCountTester::SetCount4()
154 {
155 MessageRunnerTestApp app(kTesterSignature);
156 MessageRunnerTestLooper *looper = app.TestLooper();
157 BMessenger target(looper);
158 BMessage message(MSG_RUNNER_MESSAGE);
159 bigtime_t interval = 50000;
160 int32 count = 5;
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);
169 // set new count
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);
178 }
179
180 /*
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.
188 */
SetCount5()189 void SetCountTester::SetCount5()
190 {
191 MessageRunnerTestApp app(kTesterSignature);
192 MessageRunnerTestLooper *looper = app.TestLooper();
193 BMessenger target(looper);
194 BMessage message(MSG_RUNNER_MESSAGE);
195 bigtime_t interval = 50000;
196 int32 count = -1;
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);
205 // set new count
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);
214 }
215
216 /*
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 Haiku: GetInfo() should return B_BAD_VALUE.
226 */
SetCount6()227 void SetCountTester::SetCount6()
228 {
229 MessageRunnerTestApp app(kTesterSignature);
230 MessageRunnerTestLooper *looper = app.TestLooper();
231 BMessenger target(looper);
232 BMessage message(MSG_RUNNER_MESSAGE);
233 bigtime_t interval = 70000;
234 int32 count = 5;
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);
243 // set new count
244 bigtime_t newCount = 0;
245 CHK(runner.SetCount(newCount) == B_OK);
246 CHK(runner.InitCheck() == B_OK);
247 startTime += 1 * interval;
248 #ifdef TEST_R5
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);
253 #else
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);
258 #endif
259 }
260
261 /*
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.
269 */
SetCount7()270 void SetCountTester::SetCount7()
271 {
272 MessageRunnerTestApp app(kTesterSignature);
273 MessageRunnerTestLooper *looper = app.TestLooper();
274 BMessenger target(looper);
275 BMessage message(MSG_RUNNER_MESSAGE);
276 bigtime_t interval = 70000;
277 int32 count = 5;
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);
286 // set new count
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);
296 }
297
298
Suite()299 Test* SetCountTester::Suite()
300 {
301 TestSuite* SuiteOfTests = new TestSuite;
302
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);
310
311 return SuiteOfTests;
312 }
313
314