xref: /haiku/src/tests/kits/app/bmessagerunner/SetIntervalTester.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 //------------------------------------------------------------------------------
2 //	SetIntervalTester.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 #include <stdio.h>
8 
9 // System Includes -------------------------------------------------------------
10 #include <Application.h>
11 #include <Handler.h>
12 #include <Looper.h>
13 #include <Message.h>
14 #include <MessageRunner.h>
15 #include <Messenger.h>
16 #include <OS.h>
17 
18 // Project Includes ------------------------------------------------------------
19 #include <TestShell.h>
20 #include <TestUtils.h>
21 #include <cppunit/TestAssert.h>
22 
23 // Local Includes --------------------------------------------------------------
24 #include "MessageRunnerTestHelpers.h"
25 #include "SetIntervalTester.h"
26 
27 // Local Defines ---------------------------------------------------------------
28 
29 // Globals ---------------------------------------------------------------------
30 
31 //------------------------------------------------------------------------------
32 
33 static const char *kTesterSignature
34 	= "application/x-vnd.obos-messagerunner-setinterval-test";
35 
36 static const bigtime_t kMinTimeInterval = 50000;
37 
38 // check_message_runner_info
39 static
40 void
41 check_message_runner_info(const BMessageRunner &runner, status_t error,
42 						  bigtime_t interval = 0, int32 count = 0)
43 {
44 	bigtime_t runnerInterval = 0;
45 	int32 runnerCount = 0;
46 	CHK(runner.GetInfo(&runnerInterval, &runnerCount) == error);
47 	if (error == B_OK) {
48 		CHK(runnerInterval == interval);
49 		CHK(runnerCount == count);
50 	}
51 }
52 
53 /*
54 	status_t SetInterval(bigtime_t interval)
55 	@case 1			object is not properly initialized, interval > 0
56 	@results		Should return B_BAD_VALUE.
57 					InitCheck() should return B_ERROR.
58 					GetInfo() should return B_BAD_VALUE.
59  */
60 void SetIntervalTester::SetInterval1()
61 {
62 	MessageRunnerTestApp app(kTesterSignature);
63 	BMessenger target;
64 	BMessage message(MSG_RUNNER_MESSAGE);
65 	bigtime_t interval = 100000;
66 	int32 count = 0;
67 	BMessageRunner runner(target, &message, interval, count);
68 	CHK(runner.InitCheck() == B_ERROR);
69 	check_message_runner_info(runner, B_BAD_VALUE);
70 	bigtime_t newInterval = 100000;
71 	CHK(runner.SetInterval(newInterval) == B_BAD_VALUE);
72 	CHK(runner.InitCheck() == B_ERROR);
73 	check_message_runner_info(runner, B_BAD_VALUE);
74 }
75 
76 /*
77 	status_t SetInterval(bigtime_t interval)
78 	@case 2			object was properly initialized, but has already delivered
79 					all its messages and thus became unusable, interval > 0
80 	@results		Should return B_BAD_VALUE.
81 					InitCheck() should return B_OK.
82 					GetInfo() should return B_BAD_VALUE.
83  */
84 void SetIntervalTester::SetInterval2()
85 {
86 	MessageRunnerTestApp app(kTesterSignature);
87 	BMessenger target;
88 	BMessage message(MSG_RUNNER_MESSAGE);
89 	bigtime_t interval = 50000;
90 	int32 count = 1;
91 	BMessageRunner runner(target, &message, interval, count);
92 	CHK(runner.InitCheck() == B_OK);
93 	check_message_runner_info(runner, B_OK, interval, count);
94 	snooze(count * interval + 10000);
95 	// set new interval
96 	bigtime_t newInterval = 100000;
97 	CHK(runner.SetInterval(newInterval) == B_BAD_VALUE);
98 	CHK(runner.InitCheck() == B_OK);
99 	check_message_runner_info(runner, B_BAD_VALUE);
100 }
101 
102 /*
103 	status_t SetInterval(bigtime_t interval)
104 	@case 3			object is properly initialized and has still one message
105 					to deliver, interval > 0
106 	@results		Should return B_OK.
107 					InitCheck() should return B_OK.
108 					GetInfo() should return B_OK and the new interval.
109 					The timer is reset. The last message arives after the time
110 					specified by interval has passed.
111  */
112 void SetIntervalTester::SetInterval3()
113 {
114 	MessageRunnerTestApp app(kTesterSignature);
115 	MessageRunnerTestLooper *looper = app.TestLooper();
116 	BMessenger target(looper);
117 	BMessage message(MSG_RUNNER_MESSAGE);
118 	bigtime_t interval = 50000;
119 	int32 count = 5;
120 	BMessageRunner runner(target, &message, interval, count);
121 	bigtime_t startTime = system_time();
122 	CHK(runner.InitCheck() == B_OK);
123 	interval = max(interval, kMinTimeInterval);
124 	check_message_runner_info(runner, B_OK, interval, count);
125 	snooze((count - 1) * interval + 10000);
126 	CHK(looper->CheckMessages(startTime, interval, count - 1));
127 	CHK(app.CountReplies() == count - 1);
128 	// set new interval
129 	bigtime_t newInterval = 100000;
130 	CHK(runner.SetInterval(newInterval) == B_OK);
131 	CHK(runner.InitCheck() == B_OK);
132 	check_message_runner_info(runner, B_OK, newInterval, 1);
133 	startTime = system_time();
134 	snooze(2 * newInterval + 10000);
135 	CHK(looper->CheckMessages(count - 1, startTime, newInterval, 1));
136 	CHK(app.CountReplies() == count);
137 }
138 
139 /*
140 	status_t SetInterval(bigtime_t interval)
141 	@case 4			object is properly initialized and has still some messages
142 					to deliver, interval > 0
143 	@results		Should return B_OK.
144 					InitCheck() should return B_OK.
145 					GetInfo() should return B_OK and the new interval.
146 					The timer is reset. The messages start to arive after
147 					the time specified by interval.
148  */
149 void SetIntervalTester::SetInterval4()
150 {
151 	MessageRunnerTestApp app(kTesterSignature);
152 	MessageRunnerTestLooper *looper = app.TestLooper();
153 	BMessenger target(looper);
154 	BMessage message(MSG_RUNNER_MESSAGE);
155 	bigtime_t interval = 50000;
156 	int32 count = 5;
157 	BMessageRunner runner(target, &message, interval, count);
158 	bigtime_t startTime = system_time();
159 	CHK(runner.InitCheck() == B_OK);
160 	interval = max(interval, kMinTimeInterval);
161 	check_message_runner_info(runner, B_OK, interval, count);
162 	snooze(1 * interval + 10000);
163 	CHK(looper->CheckMessages(startTime, interval, 1));
164 	CHK(app.CountReplies() == 1);
165 	// set new interval
166 	bigtime_t newInterval = 70000;
167 	CHK(runner.SetInterval(newInterval) == B_OK);
168 	CHK(runner.InitCheck() == B_OK);
169 	check_message_runner_info(runner, B_OK, newInterval, count - 1);
170 	startTime = system_time();
171 	snooze(count * newInterval + 10000);
172 	CHK(looper->CheckMessages(1, startTime, newInterval, count - 1));
173 	CHK(app.CountReplies() == count);
174 }
175 
176 /*
177 	status_t SetInterval(bigtime_t interval)
178 	@case 5			object is properly initialized and has still an unlimited
179 					number of messages to deliver, interval > 0
180 	@results		Should return B_OK.
181 					InitCheck() should return B_OK.
182 					GetInfo() should return B_OK and the new interval.
183 					The timer is reset. The messages start to arive after
184 					the time specified by interval.
185  */
186 void SetIntervalTester::SetInterval5()
187 {
188 	MessageRunnerTestApp app(kTesterSignature);
189 	MessageRunnerTestLooper *looper = app.TestLooper();
190 	BMessenger target(looper);
191 	BMessage message(MSG_RUNNER_MESSAGE);
192 	bigtime_t interval = 50000;
193 	int32 count = -1;
194 	BMessageRunner runner(target, &message, interval, count);
195 	bigtime_t startTime = system_time();
196 	CHK(runner.InitCheck() == B_OK);
197 	interval = max(interval, kMinTimeInterval);
198 	check_message_runner_info(runner, B_OK, interval, count);
199 	snooze(1 * interval + 10000);
200 	CHK(looper->CheckMessages(startTime, interval, 1));
201 	CHK(app.CountReplies() == 1);
202 	// set new interval
203 	bigtime_t newInterval = 70000;
204 	CHK(runner.SetInterval(newInterval) == B_OK);
205 	CHK(runner.InitCheck() == B_OK);
206 	check_message_runner_info(runner, B_OK, newInterval, -1);
207 	startTime = system_time();
208 	int32 checkCount = 5;
209 	snooze(checkCount * newInterval + 10000);
210 	CHK(looper->CheckMessages(1, startTime, newInterval, checkCount));
211 	CHK(app.CountReplies() == checkCount + 1);
212 }
213 
214 /*
215 	status_t SetInterval(bigtime_t interval)
216 	@case 6			object is properly initialized and has still some messages
217 					to deliver, interval == 0
218 	@results		Should return B_OK.
219 					InitCheck() should return B_OK.
220 					R5: GetInfo() should return B_BAD_VALUE.
221 						All messages are delivered, but at weird times.
222 					OBOS: GetInfo() should return B_OK and the minimal
223 						  interval. The timer is reset. The messages start to
224 						  arive after the time specified by the minimal
225 						  interval.
226  */
227 void SetIntervalTester::SetInterval6()
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 interval
244 	bigtime_t newInterval = 0;
245 	CHK(runner.SetInterval(newInterval) == B_OK);
246 	newInterval = max(newInterval, kMinTimeInterval);
247 	CHK(runner.InitCheck() == B_OK);
248 #ifdef TEST_R5
249 	check_message_runner_info(runner, B_BAD_VALUE);
250 	snooze(count * interval + 10000);
251 #else
252 	check_message_runner_info(runner, B_OK, newInterval, count - 1);
253 	startTime = system_time();
254 	snooze(count * newInterval + 10000);
255 	CHK(looper->CheckMessages(1, startTime, newInterval, count - 1));
256 #endif
257 	CHK(app.CountReplies() == count);
258 }
259 
260 /*
261 	status_t SetInterval(bigtime_t interval)
262 	@case 7			object is properly initialized and has still some messages
263 					to deliver, interval < 0
264 	@results		Should return B_OK.
265 					InitCheck() should return B_OK.
266 					R5: GetInfo() should return B_BAD_VALUE.
267 						All messages are delivered, but at weird times.
268 					OBOS: GetInfo() should return B_OK and the minimal
269 						  interval. The timer is reset. The messages start to
270 						  arive after the time specified by the minimal
271 						  interval.
272  */
273 void SetIntervalTester::SetInterval7()
274 {
275 	MessageRunnerTestApp app(kTesterSignature);
276 	MessageRunnerTestLooper *looper = app.TestLooper();
277 	BMessenger target(looper);
278 	BMessage message(MSG_RUNNER_MESSAGE);
279 	bigtime_t interval = 70000;
280 	int32 count = 5;
281 	BMessageRunner runner(target, &message, interval, count);
282 	bigtime_t startTime = system_time();
283 	CHK(runner.InitCheck() == B_OK);
284 	interval = max(interval, kMinTimeInterval);
285 	check_message_runner_info(runner, B_OK, interval, count);
286 	snooze(1 * interval + 10000);
287 	CHK(looper->CheckMessages(startTime, interval, 1));
288 	CHK(app.CountReplies() == 1);
289 	// set new interval
290 	bigtime_t newInterval = -1;
291 	CHK(runner.SetInterval(newInterval) == B_OK);
292 	newInterval = max(newInterval, kMinTimeInterval);
293 	CHK(runner.InitCheck() == B_OK);
294 #ifdef TEST_R5
295 	check_message_runner_info(runner, B_BAD_VALUE);
296 	snooze(count * interval + 10000);
297 #else
298 	check_message_runner_info(runner, B_OK, newInterval, count - 1);
299 	startTime = system_time();
300 	snooze(count * newInterval + 10000);
301 	CHK(looper->CheckMessages(1, startTime, newInterval, count - 1));
302 #endif
303 	CHK(app.CountReplies() == count);
304 }
305 
306 
307 Test* SetIntervalTester::Suite()
308 {
309 	TestSuite* SuiteOfTests = new TestSuite;
310 
311 	ADD_TEST4(BMessageRunner, SuiteOfTests, SetIntervalTester, SetInterval1);
312 	ADD_TEST4(BMessageRunner, SuiteOfTests, SetIntervalTester, SetInterval2);
313 	ADD_TEST4(BMessageRunner, SuiteOfTests, SetIntervalTester, SetInterval3);
314 	ADD_TEST4(BMessageRunner, SuiteOfTests, SetIntervalTester, SetInterval4);
315 	ADD_TEST4(BMessageRunner, SuiteOfTests, SetIntervalTester, SetInterval5);
316 	ADD_TEST4(BMessageRunner, SuiteOfTests, SetIntervalTester, SetInterval6);
317 	ADD_TEST4(BMessageRunner, SuiteOfTests, SetIntervalTester, SetInterval7);
318 
319 	return SuiteOfTests;
320 }
321 
322