xref: /haiku/src/tests/kits/app/bclipboard/ReadWriteTester.cpp (revision 2807c36668a1730dd59bc39de65e0b8f88cd5d0d)
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   char *str;
298   ssize_t size;
299 
300   BLooper *looper = new BLooper();
301   RWHandler handler;
302   looper->AddHandler(&handler);
303   looper->Run();
304   BMessenger target(&handler);
305   CHK(clip.StartWatching(target) == B_OK);
306   if ( clip.Lock() )
307   {
308     clip.Clear();
309     data = clip.Data();
310     data->AddData("text/plain",B_MIME_TYPE, "StartWatching1", 15);
311     clip.Commit();
312     clip.Unlock();
313   }
314   snooze(100000);
315   looper->Lock();
316   looper->Quit();
317   CHK(handler.ClipboardModified());
318   clip.StopWatching(target);
319 }
320 
321 /*
322 	status_t StopWatching(BMessenger target)
323 	@case 1
324 	@results		return B_OK
325  */
326 void ReadWriteTester::StopWatching1()
327 {
328   BApplication app("application/x-vnd.clipboardtest");
329   BClipboard clip("StopWatching1");
330   if ( clip.StartWatching(be_app_messenger) == B_OK )
331   {
332     CHK(clip.StopWatching(be_app_messenger) == B_OK);
333   }
334 }
335 
336 /*
337 	status_t StopWatching(BMessenger target)
338 	@case 2
339 	@results		return B_BAD_VALUE
340  */
341 void ReadWriteTester::StopWatching2()
342 {
343   BApplication app("application/x-vnd.clipboardtest");
344   BClipboard clip("StopWatching2");
345   CHK(clip.StopWatching(be_app_messenger) == B_BAD_VALUE);
346 }
347 
348 Test* ReadWriteTester::Suite()
349 {
350 	TestSuite* SuiteOfTests = new TestSuite;
351 
352 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Clear1);
353 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Clear2);
354 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Revert1);
355 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Revert2);
356 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Commit1);
357 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Commit2);
358 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Data1);
359 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Data2);
360 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, DataSource1);
361 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, DataSource2);
362 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, DataSource3);
363 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, StartWatching1);
364 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, StopWatching1);
365 	ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, StopWatching2);
366 
367 	return SuiteOfTests;
368 }
369 
370 // RWHandler
371 
372 // constructor
373 RWHandler::RWHandler()
374 		 : BHandler()
375 {
376 	fClipboardModified = false;
377 }
378 
379 // MessageReceived
380 void
381 RWHandler::MessageReceived(BMessage *message)
382 {
383 	switch (message->what) {
384 		case B_CLIPBOARD_CHANGED:
385 		  	fClipboardModified = true;
386 			break;
387 		default:
388 			BHandler::MessageReceived(message);
389 			break;
390 	}
391 }
392 
393 // ClipboardModified
394 bool
395 RWHandler::ClipboardModified()
396 {
397 	return fClipboardModified;
398 }
399 
400