xref: /haiku/src/tests/kits/app/bclipboard/LockTester.cpp (revision 9bd2271c4a30d810ab2b544d811d26618659d299)
1 //------------------------------------------------------------------------------
2 //	LockTester.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 
8 // System Includes -------------------------------------------------------------
9 #include <Clipboard.h>
10 
11 #define CHK	CPPUNIT_ASSERT
12 
13 // Project Includes ------------------------------------------------------------
14 
15 // Local Includes --------------------------------------------------------------
16 #include "LockTester.h"
17 
18 // Local Defines ---------------------------------------------------------------
19 
20 // Globals ---------------------------------------------------------------------
21 
22 //------------------------------------------------------------------------------
23 
24 /*
25 	bool Lock()
26 	@case 1
27 	@results		Lock() returns true
28  */
29 void LockTester::Lock1()
30 {
31   BClipboard clip("Lock1");
32 
33   CHK(clip.Lock());
34 }
35 
36 static int32 LockTest2(void *data)
37 {
38   BClipboard *clip = (BClipboard *)data;
39   clip->Lock();
40   snooze(3000000); //Should be 3 seconds
41   delete clip;
42   return 0;
43 }
44 /*
45 	bool Lock()
46 	@case 2
47 	@results		Lock() returns false
48  */
49 void LockTester::Lock2()
50 {
51   BClipboard *clip = new BClipboard("Lock2");
52 
53   /* This method isn't guaranteed to work, but *should* work.
54      Spawn a thread which locks the clipboard, waits several seconds
55      and then deletes the locked clipboard.  After spawning
56      the thread, the main thread should keep checking if the clipboard
57      is locked.  Once it is locked, the thread tries to lock it and gets
58      blocked.  Once the clipboard is deleted, it should return false.
59      */
60   thread_id thread = spawn_thread(LockTest2,"locktest",B_NORMAL_PRIORITY,clip);
61 
62   while(!clip->IsLocked());
63   CHK(clip->Lock() == false);
64   kill_thread(thread);
65 }
66 
67 /*
68 	bool IsLocked()
69 	@case 1
70 	@results		IsLocked() returns true
71  */
72 void LockTester::IsLocked1()
73 {
74   BClipboard clip("IsLocked1");
75 
76   clip.Lock();
77   CHK(clip.IsLocked());
78 }
79 
80 /*
81 	bool IsLocked()
82 	@case 2
83 	@results		IsLocked() returns false
84  */
85 void LockTester::IsLocked2()
86 {
87   BClipboard clip("IsLocked2");
88 
89   CHK(!clip.IsLocked());
90 }
91 
92 /*
93 	void Unlock()
94 	@case 1
95 	@results		IsLocked() returns false
96  */
97 void LockTester::Unlock1()
98 {
99   BClipboard clip("Unlock1");
100 
101   clip.Lock();
102   CHK(clip.IsLocked());
103   clip.Unlock();
104   CHK(!clip.IsLocked());
105 }
106 
107 Test* LockTester::Suite()
108 {
109 	TestSuite* SuiteOfTests = new TestSuite;
110 
111 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, Lock1);
112 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, Lock2);
113 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, IsLocked1);
114 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, IsLocked2);
115 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, Unlock1);
116 
117 	return SuiteOfTests;
118 }
119 
120 
121 
122