xref: /haiku/src/tests/kits/app/broster/GetAppListTester.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 //------------------------------------------------------------------------------
2 //	GetAppListTester.cpp
3 //
4 //------------------------------------------------------------------------------
5 
6 // Standard Includes -----------------------------------------------------------
7 #include <stdio.h>
8 
9 // System Includes -------------------------------------------------------------
10 #include <Message.h>
11 #include <OS.h>
12 #include <Handler.h>
13 #include <List.h>
14 #include <Looper.h>
15 #include <Roster.h>
16 #include <String.h>
17 
18 // Project Includes ------------------------------------------------------------
19 #include <TestShell.h>
20 #include <TestUtils.h>
21 #include <cppunit/TestAssert.h>
22 
23 // Local Includes --------------------------------------------------------------
24 #include "AppRunner.h"
25 #include "GetAppListTester.h"
26 
27 // Local Defines ---------------------------------------------------------------
28 
29 // Globals ---------------------------------------------------------------------
30 
31 //------------------------------------------------------------------------------
32 
33 // contains_list
34 static
35 bool
36 contains_list(const BList &a, const BList &b)
37 {
38 	int32 bCount = b.CountItems();
39 	bool contains = true;
40 	for (int32 i = 0; contains && i < bCount; i++)
41 		contains = a.HasItem(b.ItemAt(i));
42 	return contains;
43 }
44 
45 // check_list
46 static
47 void
48 check_list(const BList &toCheck, const BList &base, const BList &extendedBase,
49 		   const BList &expected)
50 {
51 	// toCheck and extendedBase must have prefix base
52 	int32 baseCount = base.CountItems();
53 	for (int32 i = 0; i < baseCount; i++) {
54 		CHK(base.ItemAt(i) == toCheck.ItemAt(i));
55 		CHK(base.ItemAt(i) == extendedBase.ItemAt(i));
56 	}
57 	// toCheck must have correct size
58 	int32 toCheckCount = toCheck.CountItems();
59 	int32 extendedBaseCount = extendedBase.CountItems();
60 	int32 expectedCount = expected.CountItems();
61 	CHK(toCheckCount == extendedBaseCount + expectedCount);
62 	// toCheck must contain all elements of extendedBase and expected
63 	// (arbitrary order)
64 	BList list(extendedBase);
65 	list.AddList((BList*)&expected);
66 	CHK(contains_list(toCheck, list));
67 	CHK(contains_list(list, toCheck));
68 }
69 
70 // check_list
71 static
72 void
73 check_list(const BList &toCheck, const BList &base, const BList &expected)
74 {
75 	check_list(toCheck, base, base, expected);
76 }
77 
78 // check_list
79 static
80 void
81 check_list(const BList &toCheck, const BList &expected)
82 {
83 	BList base;
84 	check_list(toCheck, base, expected);
85 }
86 
87 
88 /*
89 	void GetAppList(BList *teamIDList) const
90 	@case 1			teamIDList is NULL
91 	@results		Should do nothing.
92 */
93 void GetAppListTester::GetAppListTestA1()
94 {
95 // R5: crashes when passing a NULL BList
96 #ifndef TEST_R5
97 	BRoster roster;
98 	roster.GetAppList(NULL);
99 #endif
100 }
101 
102 /*
103 	void GetAppList(BList *teamIDList) const
104 	@case 2			teamIDList is not NULL and not empty
105 	@results		Should append the team IDs of all running apps to
106 					teamIDList.
107 */
108 void GetAppListTester::GetAppListTestA2()
109 {
110 	// create a list with some dummy entries
111 	BList list;
112 	list.AddItem((void*)-7);
113 	list.AddItem((void*)-42);
114 	// get a list of running applications for reference
115 	BRoster roster;
116 	BList list1(list);
117 	roster.GetAppList(&list1);
118 	// run some apps
119 	AppRunner runner1(true);
120 	AppRunner runner2(true);
121 	AppRunner runner3(true);
122 	CHK(runner1.Run("AppRunTestApp1") == B_OK);
123 	CHK(runner2.Run("AppRunTestApp2") == B_OK);
124 	CHK(runner3.Run("BMessengerTestApp1") == B_OK);
125 	BList expectedApps;
126 	expectedApps.AddItem((void*)runner1.Team());
127 	expectedApps.AddItem((void*)runner2.Team());
128 	expectedApps.AddItem((void*)runner3.Team());
129 	// get a new app list and check it
130 	BList list2(list);
131 	roster.GetAppList(&list2);
132 	check_list(list2, list, list1, expectedApps);
133 	// quit app 1
134 	runner1.WaitFor(true);
135 	expectedApps.RemoveItem((void*)runner1.Team());
136 	BList list3(list);
137 	roster.GetAppList(&list3);
138 	check_list(list3, list, list1, expectedApps);
139 	// quit app 2
140 	runner2.WaitFor(true);
141 	expectedApps.RemoveItem((void*)runner2.Team());
142 	BList list4(list);
143 	roster.GetAppList(&list4);
144 	check_list(list4, list, list1, expectedApps);
145 	// quit app 3
146 	runner3.WaitFor(true);
147 	expectedApps.RemoveItem((void*)runner3.Team());
148 	BList list5(list);
149 	roster.GetAppList(&list5);
150 	check_list(list5, list, list1, expectedApps);
151 }
152 
153 /*
154 	void GetAppList(const char *signature, BList *teamIDList) const
155 	@case 1			signature or teamIDList are NULL
156 	@results		Should do nothing/should not modify teamIDList.
157 */
158 void GetAppListTester::GetAppListTestB1()
159 {
160 // R5: crashes when passing a NULL signature/BList
161 #ifndef TEST_R5
162 	const char *signature = "application/x-vnd.obos-app-run-testapp1";
163 	// create a list with some dummy entries
164 	BList emptyList;
165 	BList list;
166 	list.AddItem((void*)-7);
167 	list.AddItem((void*)-42);
168 	// NULL signature and list
169 	BRoster roster;
170 	roster.GetAppList(NULL, NULL);
171 	// NULL signature
172 	BList list1(list);
173 	roster.GetAppList(NULL, &list1);
174 	check_list(list1, list, list, emptyList);
175 	// NULL list
176 	AppRunner runner(true);
177 	CHK(runner.Run("AppRunTestApp1") == B_OK);
178 	roster.GetAppList(signature, NULL);
179 	runner.WaitFor(true);
180 #endif
181 }
182 
183 /*
184 	void GetAppList(const char *signature, BList *teamIDList) const
185 	@case 2			teamIDList is not NULL and not empty, signature is not
186 					NULL, but no app with this signature is running
187 	@results		Should not modify teamIDList.
188 */
189 void GetAppListTester::GetAppListTestB2()
190 {
191 	const char *signature = "application/x-vnd.obos-does-not-exist";
192 	// create a list with some dummy entries
193 	BList list;
194 	list.AddItem((void*)-7);
195 	list.AddItem((void*)-42);
196 	// get a list of running applications for reference
197 	BRoster roster;
198 	BList list1(list);
199 	roster.GetAppList(signature, &list1);
200 	// run some apps
201 	AppRunner runner1(true);
202 	AppRunner runner2(true);
203 	AppRunner runner3(true);
204 	CHK(runner1.Run("AppRunTestApp1") == B_OK);
205 	CHK(runner2.Run("AppRunTestApp2") == B_OK);
206 	CHK(runner3.Run("BMessengerTestApp1") == B_OK);
207 	BList expectedApps;
208 	// get a new app list and check it
209 	BList list2(list);
210 	roster.GetAppList(signature, &list2);
211 	check_list(list2, list, list1, expectedApps);
212 	// quit app 1
213 	runner1.WaitFor(true);
214 	BList list3(list);
215 	roster.GetAppList(signature, &list3);
216 	check_list(list3, list, list1, expectedApps);
217 	// quit app 2
218 	runner2.WaitFor(true);
219 	BList list4(list);
220 	roster.GetAppList(signature, &list4);
221 	check_list(list4, list, list1, expectedApps);
222 	// quit app 3
223 	runner3.WaitFor(true);
224 	BList list5(list);
225 	roster.GetAppList(signature, &list5);
226 	check_list(list5, list, list1, expectedApps);
227 }
228 
229 /*
230 	void GetAppList(const char *signature, BList *teamIDList) const
231 	@case 3			teamIDList is not NULL and not empty, signature is not
232 					NULL and app(s) with this signature is (are) running
233 	@results		Should append the team IDs of all running apps with the
234 					supplied signature to teamIDList.
235 */
236 void GetAppListTester::GetAppListTestB3()
237 {
238 	const char *signature = "application/x-vnd.obos-app-run-testapp1";
239 	// create a list with some dummy entries
240 	BList list;
241 	list.AddItem((void*)-7);
242 	list.AddItem((void*)-42);
243 	// get a list of running applications for reference
244 	BRoster roster;
245 	BList list1(list);
246 	roster.GetAppList(signature, &list1);
247 	check_list(list1, list);
248 	// run some apps
249 	AppRunner runner1(true);
250 	AppRunner runner2(true);
251 	AppRunner runner3(true);
252 	CHK(runner1.Run("AppRunTestApp1") == B_OK);
253 	CHK(runner2.Run("AppRunTestApp2") == B_OK);
254 	CHK(runner3.Run("BMessengerTestApp1") == B_OK);
255 	BList expectedApps;
256 	expectedApps.AddItem((void*)runner1.Team());
257 	expectedApps.AddItem((void*)runner2.Team());
258 	// get a new app list and check it
259 	BList list2(list);
260 	roster.GetAppList(signature, &list2);
261 	check_list(list2, list, expectedApps);
262 	// quit app 1
263 	runner1.WaitFor(true);
264 	expectedApps.RemoveItem((void*)runner1.Team());
265 	BList list3(list);
266 	roster.GetAppList(signature, &list3);
267 	check_list(list3, list, expectedApps);
268 	// quit app 2
269 	runner2.WaitFor(true);
270 	expectedApps.RemoveItem((void*)runner2.Team());
271 	BList list4(list);
272 	roster.GetAppList(signature, &list4);
273 	check_list(list4, list, expectedApps);
274 	// quit app 3
275 	runner3.WaitFor(true);
276 	BList list5(list);
277 	roster.GetAppList(signature, &list5);
278 	check_list(list5, list, expectedApps);
279 }
280 
281 
282 Test* GetAppListTester::Suite()
283 {
284 	TestSuite* SuiteOfTests = new TestSuite;
285 
286 	ADD_TEST4(BRoster, SuiteOfTests, GetAppListTester, GetAppListTestA1);
287 	ADD_TEST4(BRoster, SuiteOfTests, GetAppListTester, GetAppListTestA2);
288 
289 	ADD_TEST4(BRoster, SuiteOfTests, GetAppListTester, GetAppListTestB1);
290 	ADD_TEST4(BRoster, SuiteOfTests, GetAppListTester, GetAppListTestB2);
291 	ADD_TEST4(BRoster, SuiteOfTests, GetAppListTester, GetAppListTestB3);
292 
293 	return SuiteOfTests;
294 }
295 
296