xref: /haiku/src/tests/kits/app/bclipboard/LockTester.cpp (revision 394cc79e32e656d69c00e79afb18af77f63d1067)
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   CHK(false); // fail until I figure out why it is hanging
52   BClipboard *clip = new BClipboard("Lock2");
53 
54   /* This method isn't guaranteed to work, but *should* work.
55      Spawn a thread which locks the clipboard, waits several seconds
56      and then deletes the locked clipboard.  After spawning
57      the thread, the main thread should keep checking if the clipboard
58      is locked.  Once it is locked, the thread tries to lock it and gets
59      blocked.  Once the clipboard is deleted, it should return false.
60      */
61   thread_id thread = spawn_thread(LockTest2,"locktest",B_NORMAL_PRIORITY,clip);
62 
63   while(!clip->IsLocked());
64   CHK(clip->Lock() == false);
65   kill_thread(thread);
66 }
67 
68 /*
69 	bool IsLocked()
70 	@case 1
71 	@results		IsLocked() returns true
72  */
73 void LockTester::IsLocked1()
74 {
75   BClipboard clip("IsLocked1");
76 
77   clip.Lock();
78   CHK(clip.IsLocked());
79 }
80 
81 /*
82 	bool IsLocked()
83 	@case 2
84 	@results		IsLocked() returns false
85  */
86 void LockTester::IsLocked2()
87 {
88   BClipboard clip("IsLocked2");
89 
90   CHK(!clip.IsLocked());
91 }
92 
93 /*
94 	void Unlock()
95 	@case 1
96 	@results		IsLocked() returns false
97  */
98 void LockTester::Unlock1()
99 {
100   BClipboard clip("Unlock1");
101 
102   clip.Lock();
103   CHK(clip.IsLocked());
104   clip.Unlock();
105   CHK(!clip.IsLocked());
106 }
107 
108 Test* LockTester::Suite()
109 {
110 	TestSuite* SuiteOfTests = new TestSuite;
111 
112 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, Lock1);
113 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, Lock2);
114 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, IsLocked1);
115 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, IsLocked2);
116 	ADD_TEST4(BClipboard, SuiteOfTests, LockTester, Unlock1);
117 
118 	return SuiteOfTests;
119 }
120 
121 
122 
123