19bd2271cSDarkWyrm //------------------------------------------------------------------------------
29bd2271cSDarkWyrm // ReadWriteTester.cpp
39bd2271cSDarkWyrm //
49bd2271cSDarkWyrm //------------------------------------------------------------------------------
59bd2271cSDarkWyrm
69bd2271cSDarkWyrm // Standard Includes -----------------------------------------------------------
79bd2271cSDarkWyrm #include <string.h>
89bd2271cSDarkWyrm
99bd2271cSDarkWyrm // System Includes -------------------------------------------------------------
109bd2271cSDarkWyrm #include <Application.h>
119bd2271cSDarkWyrm #include <Clipboard.h>
129bd2271cSDarkWyrm
139bd2271cSDarkWyrm #define CHK CPPUNIT_ASSERT
149bd2271cSDarkWyrm
15*9ed278d6SIngo Weinhold #include <TestShell.h>
16*9ed278d6SIngo Weinhold
179bd2271cSDarkWyrm // Project Includes ------------------------------------------------------------
189bd2271cSDarkWyrm
199bd2271cSDarkWyrm // Local Includes --------------------------------------------------------------
209bd2271cSDarkWyrm #include "ReadWriteTester.h"
219bd2271cSDarkWyrm
229bd2271cSDarkWyrm // Local Defines ---------------------------------------------------------------
239bd2271cSDarkWyrm
249bd2271cSDarkWyrm // Globals ---------------------------------------------------------------------
259bd2271cSDarkWyrm
269bd2271cSDarkWyrm //------------------------------------------------------------------------------
279bd2271cSDarkWyrm
289bd2271cSDarkWyrm /*
299bd2271cSDarkWyrm status_t Clear()
309bd2271cSDarkWyrm @case 1
319bd2271cSDarkWyrm @results Clear() returns B_ERROR
329bd2271cSDarkWyrm */
Clear1()339bd2271cSDarkWyrm void ReadWriteTester::Clear1()
349bd2271cSDarkWyrm {
35394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
369bd2271cSDarkWyrm BClipboard clip("Clear1");
379bd2271cSDarkWyrm
38394cc79eSshadow303 #ifdef TEST_R5
39394cc79eSshadow303 CHK(false);
40394cc79eSshadow303 #endif
41*9ed278d6SIngo Weinhold CPPUNIT_ASSERT_DEBUGGER(clip.Clear());
429bd2271cSDarkWyrm }
439bd2271cSDarkWyrm
449bd2271cSDarkWyrm /*
459bd2271cSDarkWyrm status_t Clear()
46c399a55fSshadow303 @case 2
479bd2271cSDarkWyrm @results Clear() returns B_OK
489bd2271cSDarkWyrm data message is empty
499bd2271cSDarkWyrm */
Clear2()509bd2271cSDarkWyrm void ReadWriteTester::Clear2()
519bd2271cSDarkWyrm {
52394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
539bd2271cSDarkWyrm BClipboard clip("Clear2");
549bd2271cSDarkWyrm BMessage *data;
559bd2271cSDarkWyrm
569bd2271cSDarkWyrm if ( clip.Lock() )
579bd2271cSDarkWyrm {
589bd2271cSDarkWyrm CHK(clip.Clear() == B_OK);
599bd2271cSDarkWyrm if ( (data = clip.Data()) )
609bd2271cSDarkWyrm CHK(data->IsEmpty());
619bd2271cSDarkWyrm }
629bd2271cSDarkWyrm }
639bd2271cSDarkWyrm
649bd2271cSDarkWyrm /*
659bd2271cSDarkWyrm status_t Revert()
669bd2271cSDarkWyrm @case 1
679bd2271cSDarkWyrm @results Revert() returns B_ERROR
689bd2271cSDarkWyrm */
Revert1()699bd2271cSDarkWyrm void ReadWriteTester::Revert1()
709bd2271cSDarkWyrm {
71394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
729bd2271cSDarkWyrm BClipboard clip("Revert1");
739bd2271cSDarkWyrm
74394cc79eSshadow303 #ifdef TEST_R5
75394cc79eSshadow303 CHK(false);
76394cc79eSshadow303 #endif
77*9ed278d6SIngo Weinhold CPPUNIT_ASSERT_DEBUGGER(clip.Revert());
789bd2271cSDarkWyrm }
799bd2271cSDarkWyrm
809bd2271cSDarkWyrm /*
819bd2271cSDarkWyrm status_t Revert()
829bd2271cSDarkWyrm @case 2
839bd2271cSDarkWyrm @results Revert() returns B_OK
849bd2271cSDarkWyrm data message is reset
859bd2271cSDarkWyrm */
Revert2()869bd2271cSDarkWyrm void ReadWriteTester::Revert2()
879bd2271cSDarkWyrm {
88394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
899bd2271cSDarkWyrm BClipboard clip("Revert2");
909bd2271cSDarkWyrm BMessage *data;
919bd2271cSDarkWyrm char *str;
929bd2271cSDarkWyrm ssize_t size;
939bd2271cSDarkWyrm
949bd2271cSDarkWyrm if ( clip.Lock() )
959bd2271cSDarkWyrm {
969bd2271cSDarkWyrm clip.Clear();
979bd2271cSDarkWyrm if ( (data = clip.Data()) )
989bd2271cSDarkWyrm {
999bd2271cSDarkWyrm data->AddData("text/plain",B_MIME_TYPE, "Revert2", 8);
1009bd2271cSDarkWyrm clip.Commit();
1019bd2271cSDarkWyrm }
1029bd2271cSDarkWyrm clip.Unlock();
1039bd2271cSDarkWyrm }
1049bd2271cSDarkWyrm
1059bd2271cSDarkWyrm if ( clip.Lock() )
1069bd2271cSDarkWyrm {
1079bd2271cSDarkWyrm clip.Clear();
1089bd2271cSDarkWyrm if ( (data = clip.Data()) )
1099bd2271cSDarkWyrm {
1109bd2271cSDarkWyrm data->AddData("text/plain",B_MIME_TYPE, "Foo", 4);
1119bd2271cSDarkWyrm }
1129bd2271cSDarkWyrm CHK(clip.Revert() == B_OK);
1139bd2271cSDarkWyrm if ( (data = clip.Data()) )
1149bd2271cSDarkWyrm {
1159bd2271cSDarkWyrm data->FindData("text/plain",B_MIME_TYPE, (const void **)&str, &size);
1169bd2271cSDarkWyrm CHK(strcmp(str,"Revert2") == 0);
1179bd2271cSDarkWyrm }
1189bd2271cSDarkWyrm clip.Unlock();
1199bd2271cSDarkWyrm }
1209bd2271cSDarkWyrm }
1219bd2271cSDarkWyrm
1229bd2271cSDarkWyrm /*
1239bd2271cSDarkWyrm status_t Commit()
1249bd2271cSDarkWyrm @case 1
1259bd2271cSDarkWyrm @results Commit() returns B_ERROR
1269bd2271cSDarkWyrm */
Commit1()1279bd2271cSDarkWyrm void ReadWriteTester::Commit1()
1289bd2271cSDarkWyrm {
129394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
1309bd2271cSDarkWyrm BClipboard clip("Commit1");
1319bd2271cSDarkWyrm
132394cc79eSshadow303 #ifdef TEST_R5
133394cc79eSshadow303 CHK(false);
134394cc79eSshadow303 #endif
135*9ed278d6SIngo Weinhold CPPUNIT_ASSERT_DEBUGGER(clip.Commit());
1369bd2271cSDarkWyrm }
1379bd2271cSDarkWyrm
1389bd2271cSDarkWyrm /*
1399bd2271cSDarkWyrm status_t Commit()
1409bd2271cSDarkWyrm @case 2
1419bd2271cSDarkWyrm @results Commit() returns B_OK
1429bd2271cSDarkWyrm data is uploaded from system
1439bd2271cSDarkWyrm */
Commit2()1449bd2271cSDarkWyrm void ReadWriteTester::Commit2()
1459bd2271cSDarkWyrm {
146394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
1479bd2271cSDarkWyrm BClipboard clipA("Commit2");
1489bd2271cSDarkWyrm BClipboard clipB("Commit2");
1499bd2271cSDarkWyrm BMessage *data;
1509bd2271cSDarkWyrm char *str;
1519bd2271cSDarkWyrm ssize_t size;
1529bd2271cSDarkWyrm
1539bd2271cSDarkWyrm if ( clipA.Lock() )
1549bd2271cSDarkWyrm {
1559bd2271cSDarkWyrm clipA.Clear();
1569bd2271cSDarkWyrm if ( (data = clipA.Data()) )
1579bd2271cSDarkWyrm {
1589bd2271cSDarkWyrm data->AddData("text/plain",B_MIME_TYPE, "Commit2", 8);
1599bd2271cSDarkWyrm CHK(clipA.Commit() == B_OK);
1609bd2271cSDarkWyrm }
1619bd2271cSDarkWyrm clipA.Unlock();
1629bd2271cSDarkWyrm }
1639bd2271cSDarkWyrm if ( clipB.Lock() )
1649bd2271cSDarkWyrm {
1659bd2271cSDarkWyrm if ( (data = clipB.Data()) )
1669bd2271cSDarkWyrm {
1679bd2271cSDarkWyrm data->FindData("text/plain",B_MIME_TYPE, (const void **)&str, &size);
1689bd2271cSDarkWyrm CHK(strcmp(str,"Commit2") == 0);
1699bd2271cSDarkWyrm }
1709bd2271cSDarkWyrm clipB.Unlock();
1719bd2271cSDarkWyrm }
1729bd2271cSDarkWyrm }
1739bd2271cSDarkWyrm
1749bd2271cSDarkWyrm /*
1759bd2271cSDarkWyrm BMessage *Data()
1769bd2271cSDarkWyrm @case 1
1779bd2271cSDarkWyrm @results Data() returns NULL
1789bd2271cSDarkWyrm */
Data1()1799bd2271cSDarkWyrm void ReadWriteTester::Data1()
1809bd2271cSDarkWyrm {
181394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
1829bd2271cSDarkWyrm BClipboard clip("Data1");
1839bd2271cSDarkWyrm
184394cc79eSshadow303 #ifdef TEST_R5
185394cc79eSshadow303 CHK(false);
186394cc79eSshadow303 #endif
187*9ed278d6SIngo Weinhold CPPUNIT_ASSERT_DEBUGGER(clip.Data());
1889bd2271cSDarkWyrm }
1899bd2271cSDarkWyrm
1909bd2271cSDarkWyrm /*
1919bd2271cSDarkWyrm BMessage *Data()
1929bd2271cSDarkWyrm @case 2
1939bd2271cSDarkWyrm @results Data() returns a message with correct data
1949bd2271cSDarkWyrm */
Data2()1959bd2271cSDarkWyrm void ReadWriteTester::Data2()
1969bd2271cSDarkWyrm {
197394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
1989bd2271cSDarkWyrm BClipboard clip("Data2");
1999bd2271cSDarkWyrm BMessage *data;
2009bd2271cSDarkWyrm char *str;
2019bd2271cSDarkWyrm ssize_t size;
2029bd2271cSDarkWyrm
2039bd2271cSDarkWyrm if ( clip.Lock() )
2049bd2271cSDarkWyrm {
2059bd2271cSDarkWyrm clip.Clear();
2069bd2271cSDarkWyrm data = clip.Data();
2079bd2271cSDarkWyrm CHK(data);
2089bd2271cSDarkWyrm data->AddData("text/plain",B_MIME_TYPE, "Data2", 6);
2099bd2271cSDarkWyrm clip.Commit();
2109bd2271cSDarkWyrm clip.Unlock();
2119bd2271cSDarkWyrm }
2129bd2271cSDarkWyrm if ( clip.Lock() )
2139bd2271cSDarkWyrm {
2149bd2271cSDarkWyrm data = clip.Data();
2159bd2271cSDarkWyrm CHK(data);
2169bd2271cSDarkWyrm data->FindData("text/plain",B_MIME_TYPE, (const void **)&str, &size);
2179bd2271cSDarkWyrm CHK(strcmp(str,"Data2") == 0);
2189bd2271cSDarkWyrm clip.Unlock();
2199bd2271cSDarkWyrm }
2209bd2271cSDarkWyrm }
2219bd2271cSDarkWyrm
2229bd2271cSDarkWyrm /*
2239bd2271cSDarkWyrm BMessenger DataSource()
2249bd2271cSDarkWyrm @case 1
2259bd2271cSDarkWyrm @results DataSource returns and invalid BMessenger
2269bd2271cSDarkWyrm */
DataSource1()2279bd2271cSDarkWyrm void ReadWriteTester::DataSource1()
2289bd2271cSDarkWyrm {
229394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
2309bd2271cSDarkWyrm BClipboard clip("DataSource1");
2319bd2271cSDarkWyrm
2329bd2271cSDarkWyrm CHK(!clip.DataSource().IsValid());
2339bd2271cSDarkWyrm }
2349bd2271cSDarkWyrm
2359bd2271cSDarkWyrm /*
2369bd2271cSDarkWyrm BMessenger DataSource()
2379bd2271cSDarkWyrm @case 2
2389bd2271cSDarkWyrm @results DataSource returns an invalid BMessenger
2399bd2271cSDarkWyrm */
DataSource2()2409bd2271cSDarkWyrm void ReadWriteTester::DataSource2()
2419bd2271cSDarkWyrm {
242394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
2439bd2271cSDarkWyrm BClipboard clipA("DataSource2");
2449bd2271cSDarkWyrm BClipboard clipB("DataSource2");
2459bd2271cSDarkWyrm BMessage *data;
2469bd2271cSDarkWyrm
2479bd2271cSDarkWyrm if ( clipA.Lock() )
2489bd2271cSDarkWyrm {
2499bd2271cSDarkWyrm clipA.Clear();
2509bd2271cSDarkWyrm if ( (data = clipA.Data()) )
2519bd2271cSDarkWyrm {
2529bd2271cSDarkWyrm data->AddData("text/plain",B_MIME_TYPE, "DataSource2", 12);
2539bd2271cSDarkWyrm clipA.Commit();
2549bd2271cSDarkWyrm }
2559bd2271cSDarkWyrm clipA.Unlock();
2569bd2271cSDarkWyrm }
2579bd2271cSDarkWyrm CHK(!clipB.DataSource().IsValid());
2589bd2271cSDarkWyrm }
2599bd2271cSDarkWyrm
2609bd2271cSDarkWyrm /*
2619bd2271cSDarkWyrm BMessenger DataSource()
2629bd2271cSDarkWyrm @case 3
2639bd2271cSDarkWyrm @results DataSource returns a valid BMessenger
2649bd2271cSDarkWyrm */
DataSource3()2659bd2271cSDarkWyrm void ReadWriteTester::DataSource3()
2669bd2271cSDarkWyrm {
267394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
2689bd2271cSDarkWyrm BClipboard clipA("DataSource3");
2699bd2271cSDarkWyrm BClipboard clipB("DataSource3");
2709bd2271cSDarkWyrm BMessage *data;
2719bd2271cSDarkWyrm
2729bd2271cSDarkWyrm if ( clipA.Lock() )
2739bd2271cSDarkWyrm {
2749bd2271cSDarkWyrm clipA.Clear();
2759bd2271cSDarkWyrm if ( (data = clipA.Data()) )
2769bd2271cSDarkWyrm {
2779bd2271cSDarkWyrm data->AddData("text/plain",B_MIME_TYPE, "DataSource3", 12);
2789bd2271cSDarkWyrm clipA.Commit();
2799bd2271cSDarkWyrm }
2809bd2271cSDarkWyrm clipA.Unlock();
2819bd2271cSDarkWyrm }
2829bd2271cSDarkWyrm if ( clipB.Lock() )
2839bd2271cSDarkWyrm {
2849bd2271cSDarkWyrm CHK(clipB.DataSource().IsValid());
2859bd2271cSDarkWyrm CHK(clipB.DataSource() == be_app_messenger);
2869bd2271cSDarkWyrm }
2879bd2271cSDarkWyrm }
2889bd2271cSDarkWyrm
2899bd2271cSDarkWyrm /*
2909bd2271cSDarkWyrm status_t StartWatching(BMessenger target)
2919bd2271cSDarkWyrm @case 1
2929bd2271cSDarkWyrm @results return B_OK, target is notified when clipboard is written
2939bd2271cSDarkWyrm */
StartWatching1()2949bd2271cSDarkWyrm void ReadWriteTester::StartWatching1()
2959bd2271cSDarkWyrm {
296394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
2979bd2271cSDarkWyrm BClipboard clip("StartWatching1");
2989bd2271cSDarkWyrm BMessage *data;
2999bd2271cSDarkWyrm
300c399a55fSshadow303 BLooper *looper = new BLooper();
3019bd2271cSDarkWyrm RWHandler handler;
302c399a55fSshadow303 looper->AddHandler(&handler);
303c399a55fSshadow303 looper->Run();
3049bd2271cSDarkWyrm BMessenger target(&handler);
3059bd2271cSDarkWyrm CHK(clip.StartWatching(target) == B_OK);
3069bd2271cSDarkWyrm if ( clip.Lock() )
3079bd2271cSDarkWyrm {
3089bd2271cSDarkWyrm clip.Clear();
3099bd2271cSDarkWyrm data = clip.Data();
3109bd2271cSDarkWyrm data->AddData("text/plain",B_MIME_TYPE, "StartWatching1", 15);
3119bd2271cSDarkWyrm clip.Commit();
3129bd2271cSDarkWyrm clip.Unlock();
3139bd2271cSDarkWyrm }
314c399a55fSshadow303 snooze(100000);
315c399a55fSshadow303 looper->Lock();
316c399a55fSshadow303 looper->Quit();
3179bd2271cSDarkWyrm CHK(handler.ClipboardModified());
3189bd2271cSDarkWyrm clip.StopWatching(target);
3199bd2271cSDarkWyrm }
3209bd2271cSDarkWyrm
3219bd2271cSDarkWyrm /*
3229bd2271cSDarkWyrm status_t StopWatching(BMessenger target)
3239bd2271cSDarkWyrm @case 1
3249bd2271cSDarkWyrm @results return B_OK
3259bd2271cSDarkWyrm */
StopWatching1()3269bd2271cSDarkWyrm void ReadWriteTester::StopWatching1()
3279bd2271cSDarkWyrm {
328394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
3299bd2271cSDarkWyrm BClipboard clip("StopWatching1");
3309bd2271cSDarkWyrm if ( clip.StartWatching(be_app_messenger) == B_OK )
3319bd2271cSDarkWyrm {
3329bd2271cSDarkWyrm CHK(clip.StopWatching(be_app_messenger) == B_OK);
3339bd2271cSDarkWyrm }
3349bd2271cSDarkWyrm }
3359bd2271cSDarkWyrm
3369bd2271cSDarkWyrm /*
3379bd2271cSDarkWyrm status_t StopWatching(BMessenger target)
3389bd2271cSDarkWyrm @case 2
3399bd2271cSDarkWyrm @results return B_BAD_VALUE
3409bd2271cSDarkWyrm */
StopWatching2()3419bd2271cSDarkWyrm void ReadWriteTester::StopWatching2()
3429bd2271cSDarkWyrm {
343394cc79eSshadow303 BApplication app("application/x-vnd.clipboardtest");
3449bd2271cSDarkWyrm BClipboard clip("StopWatching2");
3459bd2271cSDarkWyrm CHK(clip.StopWatching(be_app_messenger) == B_BAD_VALUE);
3469bd2271cSDarkWyrm }
3479bd2271cSDarkWyrm
Suite()3489bd2271cSDarkWyrm Test* ReadWriteTester::Suite()
3499bd2271cSDarkWyrm {
3509bd2271cSDarkWyrm TestSuite* SuiteOfTests = new TestSuite;
3519bd2271cSDarkWyrm
3529bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Clear1);
3539bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Clear2);
3549bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Revert1);
3559bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Revert2);
3569bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Commit1);
3579bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Commit2);
3589bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Data1);
3599bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, Data2);
3609bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, DataSource1);
3619bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, DataSource2);
3629bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, DataSource3);
3639bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, StartWatching1);
3649bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, StopWatching1);
3659bd2271cSDarkWyrm ADD_TEST4(BClipboard, SuiteOfTests, ReadWriteTester, StopWatching2);
3669bd2271cSDarkWyrm
3679bd2271cSDarkWyrm return SuiteOfTests;
3689bd2271cSDarkWyrm }
3699bd2271cSDarkWyrm
3709bd2271cSDarkWyrm // RWHandler
3719bd2271cSDarkWyrm
3729bd2271cSDarkWyrm // constructor
RWHandler()3739bd2271cSDarkWyrm RWHandler::RWHandler()
3749bd2271cSDarkWyrm : BHandler()
3759bd2271cSDarkWyrm {
3769bd2271cSDarkWyrm fClipboardModified = false;
3779bd2271cSDarkWyrm }
3789bd2271cSDarkWyrm
3799bd2271cSDarkWyrm // MessageReceived
3809bd2271cSDarkWyrm void
MessageReceived(BMessage * message)3819bd2271cSDarkWyrm RWHandler::MessageReceived(BMessage *message)
3829bd2271cSDarkWyrm {
3839bd2271cSDarkWyrm switch (message->what) {
3849bd2271cSDarkWyrm case B_CLIPBOARD_CHANGED:
3859bd2271cSDarkWyrm fClipboardModified = true;
3869bd2271cSDarkWyrm break;
3879bd2271cSDarkWyrm default:
3889bd2271cSDarkWyrm BHandler::MessageReceived(message);
3899bd2271cSDarkWyrm break;
3909bd2271cSDarkWyrm }
3919bd2271cSDarkWyrm }
3929bd2271cSDarkWyrm
393736e86f9Sejakowatz // ClipboardModified
394736e86f9Sejakowatz bool
ClipboardModified()395736e86f9Sejakowatz RWHandler::ClipboardModified()
396736e86f9Sejakowatz {
397736e86f9Sejakowatz return fClipboardModified;
398736e86f9Sejakowatz }
3999bd2271cSDarkWyrm
400