1 //------------------------------------------------------------------------------ 2 // ReadWriteTester.cpp 3 // 4 //------------------------------------------------------------------------------ 5 6 // Standard Includes ----------------------------------------------------------- 7 #include <string.h> 8 9 // System Includes ------------------------------------------------------------- 10 #include <Application.h> 11 #include <Clipboard.h> 12 13 #define CHK CPPUNIT_ASSERT 14 15 // Project Includes ------------------------------------------------------------ 16 17 // Local Includes -------------------------------------------------------------- 18 #include "ReadWriteTester.h" 19 20 // Local Defines --------------------------------------------------------------- 21 22 // Globals --------------------------------------------------------------------- 23 24 //------------------------------------------------------------------------------ 25 26 /* 27 status_t Clear() 28 @case 1 29 @results Clear() returns B_ERROR 30 */ 31 void ReadWriteTester::Clear1() 32 { 33 BApplication app("application/x-vnd.clipboardtest"); 34 BClipboard clip("Clear1"); 35 36 #ifdef TEST_R5 37 CHK(false); 38 #endif 39 CHK(clip.Clear() == B_ERROR); 40 } 41 42 /* 43 status_t Clear() 44 @case 2 45 @results Clear() returns B_OK 46 data message is empty 47 */ 48 void ReadWriteTester::Clear2() 49 { 50 BApplication app("application/x-vnd.clipboardtest"); 51 BClipboard clip("Clear2"); 52 BMessage *data; 53 54 if ( clip.Lock() ) 55 { 56 CHK(clip.Clear() == B_OK); 57 if ( (data = clip.Data()) ) 58 CHK(data->IsEmpty()); 59 } 60 } 61 62 /* 63 status_t Revert() 64 @case 1 65 @results Revert() returns B_ERROR 66 */ 67 void ReadWriteTester::Revert1() 68 { 69 BApplication app("application/x-vnd.clipboardtest"); 70 BClipboard clip("Revert1"); 71 72 #ifdef TEST_R5 73 CHK(false); 74 #endif 75 CHK(clip.Revert() == B_ERROR); 76 } 77 78 /* 79 status_t Revert() 80 @case 2 81 @results Revert() returns B_OK 82 data message is reset 83 */ 84 void ReadWriteTester::Revert2() 85 { 86 BApplication app("application/x-vnd.clipboardtest"); 87 BClipboard clip("Revert2"); 88 BMessage *data; 89 char *str; 90 ssize_t size; 91 92 if ( clip.Lock() ) 93 { 94 clip.Clear(); 95 if ( (data = clip.Data()) ) 96 { 97 data->AddData("text/plain",B_MIME_TYPE, "Revert2", 8); 98 clip.Commit(); 99 } 100 clip.Unlock(); 101 } 102 103 if ( clip.Lock() ) 104 { 105 clip.Clear(); 106 if ( (data = clip.Data()) ) 107 { 108 data->AddData("text/plain",B_MIME_TYPE, "Foo", 4); 109 } 110 CHK(clip.Revert() == B_OK); 111 if ( (data = clip.Data()) ) 112 { 113 data->FindData("text/plain",B_MIME_TYPE, (const void **)&str, &size); 114 CHK(strcmp(str,"Revert2") == 0); 115 } 116 clip.Unlock(); 117 } 118 } 119 120 /* 121 status_t Commit() 122 @case 1 123 @results Commit() returns B_ERROR 124 */ 125 void ReadWriteTester::Commit1() 126 { 127 BApplication app("application/x-vnd.clipboardtest"); 128 BClipboard clip("Commit1"); 129 130 #ifdef TEST_R5 131 CHK(false); 132 #endif 133 CHK(clip.Commit() == B_ERROR); 134 } 135 136 /* 137 status_t Commit() 138 @case 2 139 @results Commit() returns B_OK 140 data is uploaded from system 141 */ 142 void ReadWriteTester::Commit2() 143 { 144 BApplication app("application/x-vnd.clipboardtest"); 145 BClipboard clipA("Commit2"); 146 BClipboard clipB("Commit2"); 147 BMessage *data; 148 char *str; 149 ssize_t size; 150 151 if ( clipA.Lock() ) 152 { 153 clipA.Clear(); 154 if ( (data = clipA.Data()) ) 155 { 156 data->AddData("text/plain",B_MIME_TYPE, "Commit2", 8); 157 CHK(clipA.Commit() == B_OK); 158 } 159 clipA.Unlock(); 160 } 161 if ( clipB.Lock() ) 162 { 163 if ( (data = clipB.Data()) ) 164 { 165 data->FindData("text/plain",B_MIME_TYPE, (const void **)&str, &size); 166 CHK(strcmp(str,"Commit2") == 0); 167 } 168 clipB.Unlock(); 169 } 170 } 171 172 /* 173 BMessage *Data() 174 @case 1 175 @results Data() returns NULL 176 */ 177 void ReadWriteTester::Data1() 178 { 179 BApplication app("application/x-vnd.clipboardtest"); 180 BClipboard clip("Data1"); 181 182 #ifdef TEST_R5 183 CHK(false); 184 #endif 185 CHK(!clip.Data()); 186 } 187 188 /* 189 BMessage *Data() 190 @case 2 191 @results Data() returns a message with correct data 192 */ 193 void ReadWriteTester::Data2() 194 { 195 BApplication app("application/x-vnd.clipboardtest"); 196 BClipboard clip("Data2"); 197 BMessage *data; 198 char *str; 199 ssize_t size; 200 201 if ( clip.Lock() ) 202 { 203 clip.Clear(); 204 data = clip.Data(); 205 CHK(data); 206 data->AddData("text/plain",B_MIME_TYPE, "Data2", 6); 207 clip.Commit(); 208 clip.Unlock(); 209 } 210 if ( clip.Lock() ) 211 { 212 data = clip.Data(); 213 CHK(data); 214 data->FindData("text/plain",B_MIME_TYPE, (const void **)&str, &size); 215 CHK(strcmp(str,"Data2") == 0); 216 clip.Unlock(); 217 } 218 } 219 220 /* 221 BMessenger DataSource() 222 @case 1 223 @results DataSource returns and invalid BMessenger 224 */ 225 void ReadWriteTester::DataSource1() 226 { 227 BApplication app("application/x-vnd.clipboardtest"); 228 BClipboard clip("DataSource1"); 229 230 CHK(!clip.DataSource().IsValid()); 231 } 232 233 /* 234 BMessenger DataSource() 235 @case 2 236 @results DataSource returns an invalid BMessenger 237 */ 238 void ReadWriteTester::DataSource2() 239 { 240 BApplication app("application/x-vnd.clipboardtest"); 241 BClipboard clipA("DataSource2"); 242 BClipboard clipB("DataSource2"); 243 BMessage *data; 244 245 if ( clipA.Lock() ) 246 { 247 clipA.Clear(); 248 if ( (data = clipA.Data()) ) 249 { 250 data->AddData("text/plain",B_MIME_TYPE, "DataSource2", 12); 251 clipA.Commit(); 252 } 253 clipA.Unlock(); 254 } 255 CHK(!clipB.DataSource().IsValid()); 256 } 257 258 /* 259 BMessenger DataSource() 260 @case 3 261 @results DataSource returns a valid BMessenger 262 */ 263 void ReadWriteTester::DataSource3() 264 { 265 BApplication app("application/x-vnd.clipboardtest"); 266 BClipboard clipA("DataSource3"); 267 BClipboard clipB("DataSource3"); 268 BMessage *data; 269 270 if ( clipA.Lock() ) 271 { 272 clipA.Clear(); 273 if ( (data = clipA.Data()) ) 274 { 275 data->AddData("text/plain",B_MIME_TYPE, "DataSource3", 12); 276 clipA.Commit(); 277 } 278 clipA.Unlock(); 279 } 280 if ( clipB.Lock() ) 281 { 282 CHK(clipB.DataSource().IsValid()); 283 CHK(clipB.DataSource() == be_app_messenger); 284 } 285 } 286 287 /* 288 status_t StartWatching(BMessenger target) 289 @case 1 290 @results return B_OK, target is notified when clipboard is written 291 */ 292 void ReadWriteTester::StartWatching1() 293 { 294 BApplication app("application/x-vnd.clipboardtest"); 295 BClipboard clip("StartWatching1"); 296 BMessage *data; 297 298 BLooper *looper = new BLooper(); 299 RWHandler handler; 300 looper->AddHandler(&handler); 301 looper->Run(); 302 BMessenger target(&handler); 303 CHK(clip.StartWatching(target) == B_OK); 304 if ( clip.Lock() ) 305 { 306 clip.Clear(); 307 data = clip.Data(); 308 data->AddData("text/plain",B_MIME_TYPE, "StartWatching1", 15); 309 clip.Commit(); 310 clip.Unlock(); 311 } 312 snooze(100000); 313 looper->Lock(); 314 looper->Quit(); 315 CHK(handler.ClipboardModified()); 316 clip.StopWatching(target); 317 } 318 319 /* 320 status_t StopWatching(BMessenger target) 321 @case 1 322 @results return B_OK 323 */ 324 void ReadWriteTester::StopWatching1() 325 { 326 BApplication app("application/x-vnd.clipboardtest"); 327 BClipboard clip("StopWatching1"); 328 if ( clip.StartWatching(be_app_messenger) == B_OK ) 329 { 330 CHK(clip.StopWatching(be_app_messenger) == B_OK); 331 } 332 } 333 334 /* 335 status_t StopWatching(BMessenger target) 336 @case 2 337 @results return B_BAD_VALUE 338 */ 339 void ReadWriteTester::StopWatching2() 340 { 341 BApplication app("application/x-vnd.clipboardtest"); 342 BClipboard clip("StopWatching2"); 343 CHK(clip.StopWatching(be_app_messenger) == B_BAD_VALUE); 344 } 345 346 Test* ReadWriteTester::Suite() 347 { 348 TestSuite* SuiteOfTests = new TestSuite; 349 350 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Clear1); 351 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Clear2); 352 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Revert1); 353 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Revert2); 354 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Commit1); 355 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Commit2); 356 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Data1); 357 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Data2); 358 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, DataSource1); 359 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, DataSource2); 360 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, DataSource3); 361 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, StartWatching1); 362 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, StopWatching1); 363 ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, StopWatching2); 364 365 return SuiteOfTests; 366 } 367 368 // RWHandler 369 370 // constructor 371 RWHandler::RWHandler() 372 : BHandler() 373 { 374 fClipboardModified = false; 375 } 376 377 // MessageReceived 378 void 379 RWHandler::MessageReceived(BMessage *message) 380 { 381 switch (message->what) { 382 case B_CLIPBOARD_CHANGED: 383 fClipboardModified = true; 384 break; 385 default: 386 BHandler::MessageReceived(message); 387 break; 388 } 389 } 390 391 // ClipboardModified 392 bool 393 RWHandler::ClipboardModified() 394 { 395 return fClipboardModified; 396 } 397 398