1 //------------------------------------------------------------------------------ 2 // SetCountTester.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 "SetCountTester.h" 26 27 // Local Defines --------------------------------------------------------------- 28 29 // Globals --------------------------------------------------------------------- 30 31 //------------------------------------------------------------------------------ 32 33 static const char *kTesterSignature 34 = "application/x-vnd.obos-messagerunner-setcount-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 SetCount(int32 count) 55 @case 1 object is not properly initialized, count > 0 56 @results Should return B_BAD_VALUE. 57 InitCheck() should return B_ERROR. 58 GetInfo() should return B_BAD_VALUE. 59 */ 60 void SetCountTester::SetCount1() 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 int32 newCount = 100000; 71 CHK(runner.SetCount(newCount) == B_BAD_VALUE); 72 CHK(runner.InitCheck() == B_ERROR); 73 check_message_runner_info(runner, B_BAD_VALUE); 74 } 75 76 /* 77 status_t SetCount(int32 count) 78 @case 2 object was properly initialized, but has already delivered 79 all its messages and thus became unusable, count > 0 80 @results Should return B_BAD_VALUE. 81 InitCheck() should return B_OK. 82 GetInfo() should return B_BAD_VALUE. 83 */ 84 void SetCountTester::SetCount2() 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 count 96 int32 newCount = 100000; 97 CHK(runner.SetCount(newCount) == B_BAD_VALUE); 98 CHK(runner.InitCheck() == B_OK); 99 check_message_runner_info(runner, B_BAD_VALUE); 100 } 101 102 /* 103 status_t SetCount(int32 count) 104 @case 3 object is properly initialized and has still one message 105 to deliver, count > 0 106 @results Should return B_OK. 107 InitCheck() should return B_OK. 108 GetInfo() should return B_OK and the new count. 109 The timer is NOT reset. count messages should arrive. 110 */ 111 void SetCountTester::SetCount3() 112 { 113 MessageRunnerTestApp app(kTesterSignature); 114 MessageRunnerTestLooper *looper = app.TestLooper(); 115 BMessenger target(looper); 116 BMessage message(MSG_RUNNER_MESSAGE); 117 bigtime_t interval = 50000; 118 int32 count = 5; 119 BMessageRunner runner(target, &message, interval, count); 120 bigtime_t startTime = system_time(); 121 CHK(runner.InitCheck() == B_OK); 122 interval = max(interval, kMinTimeInterval); 123 check_message_runner_info(runner, B_OK, interval, count); 124 snooze((count - 1) * interval + 10000); 125 CHK(looper->CheckMessages(startTime, interval, count - 1)); 126 CHK(app.CountReplies() == count - 1); 127 // set new count 128 bigtime_t newCount = 3; 129 CHK(runner.SetCount(newCount) == B_OK); 130 CHK(runner.InitCheck() == B_OK); 131 check_message_runner_info(runner, B_OK, interval, newCount); 132 // startTime += system_time(); 133 startTime += (count - 1) * interval; 134 snooze((newCount + 1) * interval + 10000); 135 CHK(looper->CheckMessages(count - 1, startTime, interval, newCount)); 136 CHK(app.CountReplies() == count - 1 + newCount); 137 } 138 139 /* 140 status_t SetCount(int32 count) 141 @case 4 object is properly initialized and has still some messages 142 to deliver, count > 0 143 @results Should return B_OK. 144 InitCheck() should return B_OK. 145 GetInfo() should return B_OK and the new count. 146 The timer is NOT reset. count messages should arrive. 147 */ 148 void SetCountTester::SetCount4() 149 { 150 MessageRunnerTestApp app(kTesterSignature); 151 MessageRunnerTestLooper *looper = app.TestLooper(); 152 BMessenger target(looper); 153 BMessage message(MSG_RUNNER_MESSAGE); 154 bigtime_t interval = 50000; 155 int32 count = 5; 156 BMessageRunner runner(target, &message, interval, count); 157 bigtime_t startTime = system_time(); 158 CHK(runner.InitCheck() == B_OK); 159 interval = max(interval, kMinTimeInterval); 160 check_message_runner_info(runner, B_OK, interval, count); 161 snooze(1 * interval + 10000); 162 CHK(looper->CheckMessages(startTime, interval, 1)); 163 CHK(app.CountReplies() == 1); 164 // set new count 165 bigtime_t newCount = 3; 166 CHK(runner.SetCount(newCount) == B_OK); 167 CHK(runner.InitCheck() == B_OK); 168 check_message_runner_info(runner, B_OK, interval, newCount); 169 startTime += 1 * interval; 170 snooze((newCount + 1) * interval + 10000); 171 CHK(looper->CheckMessages(1, startTime, interval, newCount)); 172 CHK(app.CountReplies() == 1 + newCount); 173 } 174 175 /* 176 status_t SetCount(int32 count) 177 @case 5 object is properly initialized and has still an unlimited 178 number of messages to deliver, count > 0 179 @results Should return B_OK. 180 InitCheck() should return B_OK. 181 GetInfo() should return B_OK and the new count. 182 The timer is NOT reset. count messages should arrive. 183 */ 184 void SetCountTester::SetCount5() 185 { 186 MessageRunnerTestApp app(kTesterSignature); 187 MessageRunnerTestLooper *looper = app.TestLooper(); 188 BMessenger target(looper); 189 BMessage message(MSG_RUNNER_MESSAGE); 190 bigtime_t interval = 50000; 191 int32 count = -1; 192 BMessageRunner runner(target, &message, interval, count); 193 bigtime_t startTime = system_time(); 194 CHK(runner.InitCheck() == B_OK); 195 interval = max(interval, kMinTimeInterval); 196 check_message_runner_info(runner, B_OK, interval, count); 197 snooze(1 * interval + 10000); 198 CHK(looper->CheckMessages(startTime, interval, 1)); 199 CHK(app.CountReplies() == 1); 200 // set new count 201 bigtime_t newCount = 3; 202 CHK(runner.SetCount(newCount) == B_OK); 203 CHK(runner.InitCheck() == B_OK); 204 check_message_runner_info(runner, B_OK, interval, newCount); 205 startTime += 1 * interval; 206 snooze((newCount + 1) * interval + 10000); 207 CHK(looper->CheckMessages(1, startTime, interval, newCount)); 208 CHK(app.CountReplies() == 1 + newCount); 209 } 210 211 /* 212 status_t SetCount(int32 count) 213 @case 6 object is properly initialized and has still some messages 214 to deliver, count == 0 215 @results Should return B_OK. 216 InitCheck() should return B_OK. 217 R5: GetInfo() should return B_OK and count 0! 218 The timer is NOT reset and a message arives after the 219 time specified by the interval! 220 OBOS: GetInfo() should return B_BAD_VALUE. 221 */ 222 void SetCountTester::SetCount6() 223 { 224 MessageRunnerTestApp app(kTesterSignature); 225 MessageRunnerTestLooper *looper = app.TestLooper(); 226 BMessenger target(looper); 227 BMessage message(MSG_RUNNER_MESSAGE); 228 bigtime_t interval = 70000; 229 int32 count = 5; 230 BMessageRunner runner(target, &message, interval, count); 231 bigtime_t startTime = system_time(); 232 CHK(runner.InitCheck() == B_OK); 233 interval = max(interval, kMinTimeInterval); 234 check_message_runner_info(runner, B_OK, interval, count); 235 snooze(1 * interval + 10000); 236 CHK(looper->CheckMessages(startTime, interval, 1)); 237 CHK(app.CountReplies() == 1); 238 // set new count 239 bigtime_t newCount = 0; 240 CHK(runner.SetCount(newCount) == B_OK); 241 CHK(runner.InitCheck() == B_OK); 242 startTime += 1 * interval; 243 #ifdef TEST_R5 244 check_message_runner_info(runner, B_OK, interval, newCount); 245 snooze((newCount + 1) * interval + 10000); 246 CHK(looper->CheckMessages(1, startTime, interval, 1)); 247 CHK(app.CountReplies() == 1 + 1); 248 #else 249 check_message_runner_info(runner, B_BAD_VALUE); 250 snooze(1 * interval + 10000); 251 CHK(looper->CheckMessages(1, startTime, interval, 0)); 252 CHK(app.CountReplies() == 1); 253 #endif 254 } 255 256 /* 257 status_t SetCount(int32 count) 258 @case 7 object is properly initialized and has still some messages 259 to deliver, count < 0 260 @results Should return B_OK. 261 InitCheck() should return B_OK. 262 GetInfo() should return B_OK and the new count. 263 The timer is NOT reset. Unlimited number of messages. 264 */ 265 void SetCountTester::SetCount7() 266 { 267 MessageRunnerTestApp app(kTesterSignature); 268 MessageRunnerTestLooper *looper = app.TestLooper(); 269 BMessenger target(looper); 270 BMessage message(MSG_RUNNER_MESSAGE); 271 bigtime_t interval = 70000; 272 int32 count = 5; 273 BMessageRunner runner(target, &message, interval, count); 274 bigtime_t startTime = system_time(); 275 CHK(runner.InitCheck() == B_OK); 276 interval = max(interval, kMinTimeInterval); 277 check_message_runner_info(runner, B_OK, interval, count); 278 snooze(1 * interval + 10000); 279 CHK(looper->CheckMessages(startTime, interval, 1)); 280 CHK(app.CountReplies() == 1); 281 // set new count 282 bigtime_t newCount = -1; 283 CHK(runner.SetCount(newCount) == B_OK); 284 CHK(runner.InitCheck() == B_OK); 285 check_message_runner_info(runner, B_OK, interval, newCount); 286 startTime += 1 * interval; 287 int32 checkCount = 5; 288 snooze(checkCount * interval + 10000); 289 CHK(looper->CheckMessages(1, startTime, interval, checkCount)); 290 CHK(app.CountReplies() == 1 + checkCount); 291 } 292 293 294 Test* SetCountTester::Suite() 295 { 296 TestSuite* SuiteOfTests = new TestSuite; 297 298 ADD_TEST4(BMessageRunner, SuiteOfTests, SetCountTester, SetCount1); 299 ADD_TEST4(BMessageRunner, SuiteOfTests, SetCountTester, SetCount2); 300 ADD_TEST4(BMessageRunner, SuiteOfTests, SetCountTester, SetCount3); 301 ADD_TEST4(BMessageRunner, SuiteOfTests, SetCountTester, SetCount4); 302 ADD_TEST4(BMessageRunner, SuiteOfTests, SetCountTester, SetCount5); 303 ADD_TEST4(BMessageRunner, SuiteOfTests, SetCountTester, SetCount6); 304 ADD_TEST4(BMessageRunner, SuiteOfTests, SetCountTester, SetCount7); 305 306 return SuiteOfTests; 307 } 308 309