xref: /haiku/src/tests/kits/app/bhandler/BHandlerTester.cpp (revision e8b9c0e1a69fe9463b06fa1a10105480d38b55ea)
1 //------------------------------------------------------------------------------
2 //	BHandlerTester.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 
8 // System Includes -------------------------------------------------------------
9 #include <Message.h>
10 #include <OS.h>
11 #include <Handler.h>
12 #include <Looper.h>
13 #include <MessageFilter.h>
14 
15 // Project Includes ------------------------------------------------------------
16 
17 // Local Includes --------------------------------------------------------------
18 #include "BHandlerTester.h"
19 #include "LockLooperTestCommon.h"
20 // Local Defines ---------------------------------------------------------------
21 
22 // Globals ---------------------------------------------------------------------
23 
24 //------------------------------------------------------------------------------
25 /**
26 	BHandler(const char* name)
27 	@case			Construct with NULL name
28 	@param	name	NULL
29 	@results		BHandler::Name() should return NULL
30  */
BHandler1()31 void TBHandlerTester::BHandler1()
32 {
33 	BHandler Handler((const char*)NULL);
34 	CPPUNIT_ASSERT(Handler.Name() == NULL);
35 }
36 //------------------------------------------------------------------------------
37 /**
38 	BHandler(const char* name)
39 	@case			Construct with valid name
40 	@param	name	valid string
41 	@results		BHandler::Name() returns name
42  */
BHandler2()43 void TBHandlerTester::BHandler2()
44 {
45 	BHandler Handler("name");
46 	CPPUNIT_ASSERT(string("name") == Handler.Name());
47 }
48 //------------------------------------------------------------------------------
49 /**
50 	BHandler(BMessage* archive)
51 	@case			archive is valid and has field "_name"
52 	@param	archive	valid BMessage pointer
53 	@results		BHandler::Name() returns _name
54  */
BHandler3()55 void TBHandlerTester::BHandler3()
56 {
57 	BMessage Archive;
58 	Archive.AddString("_name", "the name");
59 	BHandler Handler(&Archive);
60 	CPPUNIT_ASSERT(string("the name") == Handler.Name());
61 }
62 //------------------------------------------------------------------------------
63 /**
64 	BHandler(BMessage* archive)
65 	@case			archive is valid, but has no field "_name"
66 	@param	archive	valid BMessage pointer
67 	@results		BHandler::Name() returns NULL
68  */
BHandler4()69 void TBHandlerTester::BHandler4()
70 {
71 	BMessage Archive;
72 	BHandler Handler(&Archive);
73 	CPPUNIT_ASSERT(Handler.Name() == NULL);
74 }
75 //------------------------------------------------------------------------------
76 /**
77 	BHandler(BMessage* archive)
78 	@case			archive is NULL
79 	@param	archive	NULL
80 	@results		BHandler::Name() returns NULL
81 	@note			This test is not enabled against the original implementation
82 					as it doesn't check for a NULL parameter and seg faults.
83  */
84 
BHandler5()85 void TBHandlerTester::BHandler5()
86 {
87 #if !defined(TEST_R5)
88 	BHandler Handler((BMessage*)NULL);
89 	CPPUNIT_ASSERT(Handler.Name() == NULL);
90 #endif
91 }
92 //------------------------------------------------------------------------------
93 /**
94 	Archive(BMessage* data, bool deep = true)
95 	@case			data is NULL, deep is false
96 	@param	data	NULL
97 	@param	deep	false
98 	@results		Returns B_BAD_VALUE
99 	@note			This test is not enabled against the original implementation
100 					as it doesn't check for NULL parameters and seg faults
101  */
Archive1()102 void TBHandlerTester::Archive1()
103 {
104 #if !defined(TEST_R5)
105 	BHandler Handler;
106 	CPPUNIT_ASSERT(Handler.Archive(NULL, false) == B_BAD_VALUE);
107 #endif
108 }
109 //------------------------------------------------------------------------------
110 /**
111 	Archive(BMessage* data, bool deep = true)
112 	@case			data is NULL, deep is true
113 	@param	data	NULL
114 	@param	deep	false
115 	@results		Returns B_BAD_VALUE
116 	@note			This test is not enabled against the original implementation
117 					as it doesn't check for NULL parameters and seg faults
118  */
Archive2()119 void TBHandlerTester::Archive2()
120 {
121 #if !defined(TEST_R5)
122 	BHandler Handler;
123 	CPPUNIT_ASSERT(Handler.Archive(NULL) == B_BAD_VALUE);
124 #endif
125 }
126 //------------------------------------------------------------------------------
127 /**
128 	Archive(BMessage* data, bool deep = true)
129 	@case			data is valid, deep is false
130 	@param	data	valid BMessage pointer
131 	@param	deep	false
132 	@results		Returns B_OK
133 					Resultant archive has string field labeled "_name"
134 					Field "_name" contains the string "a name"
135 					Resultant archive has string field labeled "class"
136 					Field "class" contains the string "BHandler"
137  */
Archive3()138 void TBHandlerTester::Archive3()
139 {
140 	BMessage Archive;
141 	BHandler Handler("a name");
142 	CPPUNIT_ASSERT(Handler.Archive(&Archive, false) == B_OK);
143 
144 	const char* data;
145 	CPPUNIT_ASSERT(Archive.FindString("_name", &data) == B_OK);
146 	CPPUNIT_ASSERT(string("a name") == data);
147 	CPPUNIT_ASSERT(Archive.FindString("class", &data) == B_OK);
148 	CPPUNIT_ASSERT(string("BHandler") == data);
149 }
150 //------------------------------------------------------------------------------
151 /**
152 	Archive(BMessage *data, bool deep = true)
153 	@case			data is valid, deep is true
154 	@param	data	valid BMessage pointer
155 	@param	deep	true
156 	@results		Returns B_OK
157 					Resultant archive has string field labeled "_name"
158 					Field "_name" contains the string "a name"
159 					Resultant archive has string field labeled "class"
160 					Field "class" contains the string "BHandler"
161  */
Archive4()162 void TBHandlerTester::Archive4()
163 {
164 	BMessage Archive;
165 	BHandler Handler("another name");
166 	CPPUNIT_ASSERT(Handler.Archive(&Archive) == B_OK);
167 
168 	const char* data;
169 	CPPUNIT_ASSERT(Archive.FindString("_name", &data) == B_OK);
170 	CPPUNIT_ASSERT(string("another name") == data);
171 	CPPUNIT_ASSERT(Archive.FindString("class", &data) == B_OK);
172 	CPPUNIT_ASSERT(string("BHandler") == data);
173 }
174 //------------------------------------------------------------------------------
175 /**
176 	Instantiate(BMessage* data)
177 	@case			data is NULL
178 	@param	data	NULL
179 	@results
180 	@note			This test is not enabled against the original implementation
181 					as it doesn't check for NULL parameters and seg faults
182  */
Instantiate1()183 void TBHandlerTester::Instantiate1()
184 {
185 #if !defined(TEST_R5)
186 	CPPUNIT_ASSERT(BHandler::Instantiate(NULL) == NULL);
187 	CPPUNIT_ASSERT(errno == B_BAD_VALUE);
188 #endif
189 }
190 //------------------------------------------------------------------------------
191 /**
192 	Instantiate(BMessage* data)
193 	@case			data is valid, has field "_name"
194 	@param	data	Valid BMessage pointer with string field "class" containing
195 					string "BHandler" and with string field "_name" containing
196 					string "a name"
197 	@results		BHandler::Name() returns "a name"
198  */
Instantiate2()199 void TBHandlerTester::Instantiate2()
200 {
201 	BMessage Archive;
202 	Archive.AddString("class", "BHandler");
203 	Archive.AddString("_name", "a name");
204 
205 	BHandler* Handler =
206 		dynamic_cast<BHandler*>(BHandler::Instantiate(&Archive));
207 	CPPUNIT_ASSERT(Handler != NULL);
208 	CPPUNIT_ASSERT(string("a name") == Handler->Name());
209 	CPPUNIT_ASSERT(errno == B_OK);
210 }
211 //------------------------------------------------------------------------------
212 /**
213 	Instantiate(BMessage *data)
214 	@case			data is valid, has no field "_name"
215 	@param	data	valid BMessage pointer with string field "class" containing
216 					string "BHandler"
217 	@results		BHandler::Name() returns NULL
218  */
219 
Instantiate3()220 void TBHandlerTester::Instantiate3()
221 {
222 	BMessage Archive;
223 	Archive.AddString("class", "BHandler");
224 
225 	BHandler* Handler =
226 		dynamic_cast<BHandler*>(BHandler::Instantiate(&Archive));
227 	CPPUNIT_ASSERT(Handler != NULL);
228 	CPPUNIT_ASSERT(Handler->Name() == NULL);
229 	CPPUNIT_ASSERT(errno == B_OK);
230 }
231 //------------------------------------------------------------------------------
232 /**
233 	SetName(const char* name)
234 	Name()
235 	@case			name is NULL
236 	@param	name	NULL
237 	@results		BHandler::Name() returns NULL
238 
239  */
SetName1()240 void TBHandlerTester::SetName1()
241 {
242 	BHandler Handler("a name");
243 	CPPUNIT_ASSERT(string("a name") == Handler.Name());
244 
245 	Handler.SetName(NULL);
246 	CPPUNIT_ASSERT(Handler.Name() == NULL);
247 }
248 //------------------------------------------------------------------------------
249 /**
250 	SetName(const char *name)
251 	Name()
252 	@case			name is valid
253 	@param	name	Valid string pointer
254 	@results		BHandler::Name returns name
255  */
SetName2()256 void TBHandlerTester::SetName2()
257 {
258 	BHandler Handler("a name");
259 	CPPUNIT_ASSERT(string("a name") == Handler.Name());
260 
261 	Handler.SetName("another name");
262 	CPPUNIT_ASSERT(string("another name") == Handler.Name());
263 }
264 //------------------------------------------------------------------------------
265 /**
266 	Perform(perform_code d, void *arg)
267 	@case		feed meaningless data, should return B_NAME_NOT_FOUND
268 	@param	d	N/A
269 	@param	arg	NULL
270 	@results	Returns B_NAME_NOT_FOUND
271  */
Perform1()272 void TBHandlerTester::Perform1()
273 {
274 	BHandler Handler;
275 	CPPUNIT_ASSERT(Handler.Perform(0, NULL) == B_NAME_NOT_FOUND);
276 }
277 //------------------------------------------------------------------------------
278 /**
279 	FilterList();
280 	@case		Default constructed BHandler
281 	@results	FilterList() returns NULL
282  */
FilterList1()283 void TBHandlerTester::FilterList1()
284 {
285 	BHandler Handler;
286 	CPPUNIT_ASSERT(!Handler.FilterList());
287 }
288 //------------------------------------------------------------------------------
Suite()289 Test* TBHandlerTester::Suite()
290 {
291 	TestSuite* SuiteOfTests = new TestSuite;
292 
293 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, BHandler1);
294 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, BHandler2);
295 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, BHandler3);
296 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, BHandler4);
297 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, BHandler5);
298 
299 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, Archive1);
300 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, Archive2);
301 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, Archive3);
302 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, Archive4);
303 
304 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, Instantiate1);
305 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, Instantiate2);
306 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, Instantiate3);
307 
308 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, SetName1);
309 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, SetName2);
310 
311 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, Perform1);
312 
313 	ADD_TEST4(BHandler, SuiteOfTests, TBHandlerTester, FilterList1);
314 
315 	return SuiteOfTests;
316 }
317 //------------------------------------------------------------------------------
318 
319 /*
320  * $Log $
321  *
322  * $Id  $
323  *
324  */
325 
326 
327 
328