80
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.mj.impl.rt.support;
|
|
19 |
|
|
20 |
import java.security.Permission;
|
|
21 |
|
|
22 |
|
|
23 |
import com.nokia.mj.impl.rt.test.UnitTestSuiteCreator;
|
|
24 |
import com.nokia.mj.impl.rt.support.ShutdownListener;
|
|
25 |
import com.nokia.mj.impl.rt.support.ApplicationUtils;
|
|
26 |
import com.nokia.mj.impl.rt.test.ApplicationUtilsImpl;
|
|
27 |
import com.nokia.mj.impl.utils.Uid;
|
|
28 |
|
|
29 |
import j2meunit.framework.Test;
|
|
30 |
import j2meunit.framework.TestCase;
|
|
31 |
import j2meunit.framework.TestMethod;
|
|
32 |
import j2meunit.framework.TestSuite;
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
/**
|
|
37 |
* ApplicationUtils unit tests.
|
|
38 |
*/
|
|
39 |
public class ApplicationUtilsTest extends TestCase implements UnitTestSuiteCreator
|
|
40 |
{
|
|
41 |
|
|
42 |
// Begin j2meunit test framework setup
|
|
43 |
public TestSuite createTestSuite(String[] args)
|
|
44 |
{
|
|
45 |
TestSuite suite = new TestSuite(this.getClass().getName());
|
|
46 |
String testSelector = "0000000000000000";
|
|
47 |
if (args.length > 1 && args[1] != null)
|
|
48 |
{
|
|
49 |
testSelector = args[1] + "0000000000000000";
|
|
50 |
}
|
|
51 |
|
|
52 |
|
|
53 |
if (testSelector.charAt(0) != '0')
|
|
54 |
{
|
|
55 |
suite.addTest(new ApplicationUtilsTest("testShutdowListeners", new TestMethod()
|
|
56 |
{
|
|
57 |
public void run(TestCase tc)
|
|
58 |
{
|
|
59 |
((ApplicationUtilsTest)tc).testShutdowListeners();
|
|
60 |
}
|
|
61 |
}));
|
|
62 |
}
|
|
63 |
|
|
64 |
if (testSelector.charAt(1) != '0')
|
|
65 |
{
|
|
66 |
suite.addTest(new ApplicationUtilsTest("testShutdowListeners 2", new TestMethod()
|
|
67 |
{
|
|
68 |
public void run(TestCase tc)
|
|
69 |
{
|
|
70 |
((ApplicationUtilsTest)tc).testShutdowListeners2();
|
|
71 |
}
|
|
72 |
}));
|
|
73 |
}
|
|
74 |
|
|
75 |
if (testSelector.charAt(2) != '0')
|
|
76 |
{
|
|
77 |
suite.addTest(new ApplicationUtilsTest("testOtherCalls", new TestMethod()
|
|
78 |
{
|
|
79 |
public void run(TestCase tc)
|
|
80 |
{
|
|
81 |
((ApplicationUtilsTest)tc).testOtherCalls();
|
|
82 |
}
|
|
83 |
}));
|
|
84 |
}
|
|
85 |
|
|
86 |
|
|
87 |
if (testSelector.charAt(3) != '0')
|
|
88 |
{
|
|
89 |
suite.addTest(new ApplicationUtilsTest("testEmptyWaiter", new TestMethod()
|
|
90 |
{
|
|
91 |
public void run(TestCase tc)
|
|
92 |
{
|
|
93 |
((ApplicationUtilsTest)tc).testEmptyWaiter();
|
|
94 |
}
|
|
95 |
}));
|
|
96 |
}
|
|
97 |
|
|
98 |
if (testSelector.charAt(4) != '0')
|
|
99 |
{
|
|
100 |
|
|
101 |
suite.addTest(new ApplicationUtilsTest("testWaiter", new TestMethod()
|
|
102 |
{
|
|
103 |
public void run(TestCase tc)
|
|
104 |
{
|
|
105 |
((ApplicationUtilsTest)tc).testWaiter();
|
|
106 |
}
|
|
107 |
}));
|
|
108 |
}
|
|
109 |
|
|
110 |
if (testSelector.charAt(5) != '0')
|
|
111 |
{
|
|
112 |
|
|
113 |
suite.addTest(new ApplicationUtilsTest("testWaiterNoStart", new TestMethod()
|
|
114 |
{
|
|
115 |
public void run(TestCase tc)
|
|
116 |
{
|
|
117 |
((ApplicationUtilsTest)tc).testWaiterNoStart();
|
|
118 |
}
|
|
119 |
}));
|
|
120 |
}
|
|
121 |
|
|
122 |
if (testSelector.charAt(6) != '0')
|
|
123 |
{
|
|
124 |
|
|
125 |
suite.addTest(new ApplicationUtilsTest("testWaiterNotifyBeforeStart", new TestMethod()
|
|
126 |
{
|
|
127 |
public void run(TestCase tc)
|
|
128 |
{
|
|
129 |
((ApplicationUtilsTest)tc).testWaiterNotifyBeforeStart();
|
|
130 |
}
|
|
131 |
}));
|
|
132 |
}
|
|
133 |
|
|
134 |
if (testSelector.charAt(7) != '0')
|
|
135 |
{
|
|
136 |
|
|
137 |
suite.addTest(new ApplicationUtilsTest("testAppUtilsNotExist", new TestMethod()
|
|
138 |
{
|
|
139 |
public void run(TestCase tc)
|
|
140 |
{
|
|
141 |
((ApplicationUtilsTest)tc).testAppUtilsNotExist();
|
|
142 |
}
|
|
143 |
}));
|
|
144 |
}
|
|
145 |
|
|
146 |
return suite;
|
|
147 |
|
|
148 |
}
|
|
149 |
|
|
150 |
public ApplicationUtilsTest()
|
|
151 |
{
|
|
152 |
}
|
|
153 |
|
|
154 |
public ApplicationUtilsTest(String aTestName, TestMethod aTestMethod)
|
|
155 |
{
|
|
156 |
super(aTestName, aTestMethod);
|
|
157 |
}
|
|
158 |
|
|
159 |
// End j2meunit test framework setup
|
|
160 |
|
|
161 |
protected void setUp()
|
|
162 |
{
|
|
163 |
}
|
|
164 |
|
|
165 |
protected void tearDown()
|
|
166 |
{
|
|
167 |
}
|
|
168 |
|
|
169 |
private static class ShutdownListenerImpl implements ShutdownListener
|
|
170 |
{
|
|
171 |
public int mShutDownCallCount = 0;
|
|
172 |
public void shuttingDown()
|
|
173 |
{
|
|
174 |
mShutDownCallCount++;
|
|
175 |
}
|
|
176 |
}
|
|
177 |
// Test cases
|
|
178 |
|
|
179 |
private void testShutdowListeners()
|
|
180 |
{
|
|
181 |
try
|
|
182 |
{
|
|
183 |
final int count = 25;
|
|
184 |
ApplicationUtils appUtils = ApplicationUtils.getInstance();
|
|
185 |
ShutdownListenerImpl[] sls = new ShutdownListenerImpl[count];
|
|
186 |
for (int i = 0; i < count; ++i)
|
|
187 |
{
|
|
188 |
sls[i] = new ShutdownListenerImpl();
|
|
189 |
appUtils.addShutdownListener(sls[i]);
|
|
190 |
}
|
|
191 |
ApplicationUtilsImpl.doShutdownImpl();
|
|
192 |
for (int i = 0; i < count; ++i)
|
|
193 |
{
|
|
194 |
assertTrue(i + " call count incorrect: "+ sls[i].mShutDownCallCount,
|
|
195 |
sls[i].mShutDownCallCount == 1);
|
|
196 |
}
|
|
197 |
|
|
198 |
for (int i = 0; i < count; ++i)
|
|
199 |
{
|
|
200 |
sls[i] = new ShutdownListenerImpl();
|
|
201 |
appUtils.addShutdownListener(sls[i]);
|
|
202 |
}
|
|
203 |
appUtils.removeShutdownListener(sls[count-1]);
|
|
204 |
ApplicationUtilsImpl.doShutdownImpl();
|
|
205 |
for (int i = 0; i < count - 1; ++i)
|
|
206 |
{
|
|
207 |
assertTrue(i + " call count incorrect: "+ sls[i].mShutDownCallCount,
|
|
208 |
sls[i].mShutDownCallCount == 1);
|
|
209 |
}
|
|
210 |
assertTrue((count -1) + " call count incorrect: "+ sls[count -1].mShutDownCallCount,
|
|
211 |
sls[count -1].mShutDownCallCount == 0);
|
|
212 |
}
|
|
213 |
catch (Throwable t)
|
|
214 |
{
|
|
215 |
t.printStackTrace();
|
|
216 |
assertTrue(t.toString(), false);
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
private void testShutdowListeners2()
|
|
221 |
{
|
|
222 |
try
|
|
223 |
{
|
|
224 |
ApplicationUtils appUtils = ApplicationUtils.getInstance();
|
|
225 |
appUtils.removeShutdownListener(new ShutdownListenerImpl());
|
|
226 |
try
|
|
227 |
{
|
|
228 |
appUtils.removeShutdownListener(null);
|
|
229 |
assertTrue("No exception: ", false);
|
|
230 |
}
|
|
231 |
catch (NullPointerException ne)
|
|
232 |
{
|
|
233 |
}
|
|
234 |
}
|
|
235 |
catch (Throwable t)
|
|
236 |
{
|
|
237 |
t.printStackTrace();
|
|
238 |
assertTrue(t.toString(), false);
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
private void checkCallCounts(String info, int ind)
|
|
243 |
{
|
|
244 |
assertTrue("ApplicationUtils." + info + " failed " + ApplicationUtilsImpl.print(),
|
|
245 |
ApplicationUtilsImpl.checkCalls(ind));
|
|
246 |
}
|
|
247 |
|
|
248 |
public class TestPermission extends Permission
|
|
249 |
{
|
|
250 |
|
|
251 |
public TestPermission()
|
|
252 |
{
|
|
253 |
super("TestPermission");
|
|
254 |
}
|
|
255 |
|
|
256 |
public String getActions()
|
|
257 |
{
|
|
258 |
return "TestPermission Actions";
|
|
259 |
}
|
|
260 |
|
|
261 |
public boolean equals(Object obj)
|
|
262 |
{
|
|
263 |
return false;
|
|
264 |
}
|
|
265 |
|
|
266 |
public int hashCode()
|
|
267 |
{
|
|
268 |
return 0;
|
|
269 |
}
|
|
270 |
public boolean implies(Permission permission)
|
|
271 |
{
|
|
272 |
return true;
|
|
273 |
}
|
|
274 |
}
|
|
275 |
|
|
276 |
private void testOtherCalls()
|
|
277 |
{
|
|
278 |
try
|
|
279 |
{
|
|
280 |
ApplicationUtils appUtils = ApplicationUtils.getInstance();
|
|
281 |
appUtils.notifyExitCmd();
|
|
282 |
checkCallCounts("notifyExitCmd()", ApplicationUtilsImpl.NOTIFY_CALL);
|
|
283 |
|
|
284 |
ApplicationUtilsImpl.clear();
|
|
285 |
appUtils.uiDisposed();
|
|
286 |
checkCallCounts("uiDisposed()", ApplicationUtilsImpl.UI_DISPOSED_CALL);
|
|
287 |
|
|
288 |
ApplicationUtilsImpl.clear();
|
|
289 |
appUtils.pauseApplication();
|
|
290 |
checkCallCounts("pauseApplication()", ApplicationUtilsImpl.PAUSE_CALL);
|
|
291 |
|
|
292 |
ApplicationUtilsImpl.clear();
|
|
293 |
appUtils.pauseApplication();
|
|
294 |
checkCallCounts("pauseApplication()", ApplicationUtilsImpl.PAUSE_CALL);
|
|
295 |
|
|
296 |
ApplicationUtilsImpl.clear();
|
|
297 |
appUtils.resumeApplication();
|
|
298 |
checkCallCounts("resumeApplication()", ApplicationUtilsImpl.RESUME_CALL);
|
|
299 |
|
|
300 |
ApplicationUtilsImpl.clear();
|
|
301 |
Permission p1 = new TestPermission();
|
|
302 |
appUtils.checkPermission(p1);
|
|
303 |
assertTrue("ApplicationUtils.checkPermission(Permission) failed ",
|
|
304 |
p1 == ApplicationUtilsImpl.mPermission);
|
|
305 |
checkCallCounts("checkPermission()", ApplicationUtilsImpl.PERMISSION_CALL1);
|
|
306 |
|
|
307 |
Permission p2 = new TestPermission();
|
|
308 |
Uid uid = Uid.createUid("[12345678]");
|
|
309 |
ApplicationUtilsImpl.clear();
|
|
310 |
appUtils.checkPermission(uid, p2);
|
|
311 |
checkCallCounts("checkPermission2()", ApplicationUtilsImpl.PERMISSION_CALL2);
|
|
312 |
assertTrue("ApplicationUtils.checkPermission(Permission2) failed ",
|
|
313 |
p2 == ApplicationUtilsImpl.mPermission);
|
|
314 |
|
|
315 |
assertTrue("ApplicationUtils.checkPermission(Uid) failed ",
|
|
316 |
uid.equals(ApplicationUtilsImpl.mUid));
|
|
317 |
|
|
318 |
|
|
319 |
}
|
|
320 |
catch (Throwable t)
|
|
321 |
{
|
|
322 |
t.printStackTrace();
|
|
323 |
assertTrue(t.toString(), false);
|
|
324 |
}
|
|
325 |
}
|
|
326 |
|
|
327 |
private static class TestWaiter extends Thread
|
|
328 |
{
|
|
329 |
private static TestWaiter[] mTestWaiters;
|
|
330 |
private boolean mWasReleased = false;
|
|
331 |
private Boolean mCanContinue = null;
|
|
332 |
|
|
333 |
private static void createWaiters(int count)
|
|
334 |
{
|
|
335 |
mTestWaiters = new TestWaiter[count];
|
|
336 |
for (int i = 0; i < mTestWaiters.length; ++i)
|
|
337 |
{
|
|
338 |
mTestWaiters[i] = new TestWaiter();
|
|
339 |
mTestWaiters[i].start();
|
|
340 |
}
|
|
341 |
}
|
|
342 |
|
|
343 |
private static boolean check(boolean canContinue)
|
|
344 |
{
|
|
345 |
for (int i = 0; i < mTestWaiters.length; ++i)
|
|
346 |
{
|
|
347 |
if (!mTestWaiters[i].mWasReleased)
|
|
348 |
{
|
|
349 |
System.err.println("ID " + i + ": " + mTestWaiters[i].toString());
|
|
350 |
return false;
|
|
351 |
}
|
|
352 |
if (mTestWaiters[i].mCanContinue == null ||
|
|
353 |
mTestWaiters[i].mCanContinue.booleanValue() != canContinue)
|
|
354 |
{
|
|
355 |
System.err.println("ID " + i + ": " + mTestWaiters[i].toString());
|
|
356 |
return false;
|
|
357 |
}
|
|
358 |
}
|
|
359 |
return true;
|
|
360 |
}
|
|
361 |
|
|
362 |
private static boolean checkWaiting(boolean isWaiting)
|
|
363 |
{
|
|
364 |
for (int i = 0; i < mTestWaiters.length; ++i)
|
|
365 |
{
|
|
366 |
if (mTestWaiters[i].mWasReleased == isWaiting)
|
|
367 |
{
|
|
368 |
System.err.println("ID " + i + ": " + mTestWaiters[i].toString());
|
|
369 |
return false;
|
|
370 |
}
|
|
371 |
}
|
|
372 |
return true;
|
|
373 |
}
|
|
374 |
|
|
375 |
public String toString()
|
|
376 |
{
|
|
377 |
StringBuffer sb = new StringBuffer();
|
|
378 |
sb.append("State: \n");
|
|
379 |
sb.append(" mWasReleased " + mWasReleased);
|
|
380 |
sb.append("\n mCanContinue " + mCanContinue);
|
|
381 |
return sb.toString();
|
|
382 |
}
|
|
383 |
|
|
384 |
public void run()
|
|
385 |
{
|
|
386 |
boolean canContinue = ApplicationUtils.getInstance().waitStart();
|
|
387 |
mCanContinue = new Boolean(canContinue);
|
|
388 |
mWasReleased = true;
|
|
389 |
}
|
|
390 |
}
|
|
391 |
|
|
392 |
private void testEmptyWaiter()
|
|
393 |
{
|
|
394 |
try
|
|
395 |
{
|
|
396 |
ApplicationUtilsImpl.releaseWaiterImpl(true);
|
|
397 |
ApplicationUtilsImpl.releaseWaiterImpl(false);
|
|
398 |
|
|
399 |
}
|
|
400 |
catch (Throwable t)
|
|
401 |
{
|
|
402 |
t.printStackTrace();
|
|
403 |
assertTrue(t.toString(), false);
|
|
404 |
}
|
|
405 |
}
|
|
406 |
|
|
407 |
private void testWaiter()
|
|
408 |
{
|
|
409 |
try
|
|
410 |
{
|
|
411 |
TestWaiter.createWaiters(5);
|
|
412 |
Thread.sleep(100);
|
|
413 |
assertTrue("releaseWaiterImpl(true) not waiting.", TestWaiter.checkWaiting(true));
|
|
414 |
ApplicationUtilsImpl.releaseWaiterImpl(true);
|
|
415 |
Thread.sleep(100);
|
|
416 |
assertTrue("releaseWaiterImpl(true) failed.", TestWaiter.check(true));
|
|
417 |
|
|
418 |
}
|
|
419 |
catch (Throwable t)
|
|
420 |
{
|
|
421 |
t.printStackTrace();
|
|
422 |
assertTrue(t.toString(), false);
|
|
423 |
}
|
|
424 |
}
|
|
425 |
|
|
426 |
private void testWaiterNoStart()
|
|
427 |
{
|
|
428 |
try
|
|
429 |
{
|
|
430 |
TestWaiter.createWaiters(5);
|
|
431 |
Thread.sleep(100);
|
|
432 |
assertTrue("releaseWaiterImpl(true) not waiting.", TestWaiter.checkWaiting(true));
|
|
433 |
ApplicationUtilsImpl.releaseWaiterImpl(false);
|
|
434 |
Thread.sleep(100);
|
|
435 |
assertTrue("releaseWaiterImpl(true) failed.", TestWaiter.check(false));
|
|
436 |
|
|
437 |
}
|
|
438 |
catch (Throwable t)
|
|
439 |
{
|
|
440 |
t.printStackTrace();
|
|
441 |
assertTrue(t.toString(), false);
|
|
442 |
}
|
|
443 |
}
|
|
444 |
|
|
445 |
private void testWaiterNotifyBeforeStart()
|
|
446 |
{
|
|
447 |
try
|
|
448 |
{
|
|
449 |
ApplicationUtilsImpl.releaseWaiterImpl(true);
|
|
450 |
TestWaiter.createWaiters(5);
|
|
451 |
Thread.sleep(100);
|
|
452 |
assertTrue("releaseWaiterImpl(true) not waiting.", TestWaiter.checkWaiting(false));
|
|
453 |
assertTrue("releaseWaiterImpl(true) failed.", TestWaiter.check(true));
|
|
454 |
|
|
455 |
}
|
|
456 |
catch (Throwable t)
|
|
457 |
{
|
|
458 |
t.printStackTrace();
|
|
459 |
assertTrue(t.toString(), false);
|
|
460 |
}
|
|
461 |
}
|
|
462 |
|
|
463 |
private void testAppUtilsNotExist()
|
|
464 |
{
|
|
465 |
try
|
|
466 |
{
|
|
467 |
ApplicationUtilsImpl.releaseWaiterImpl(true);
|
|
468 |
assertTrue("No exception.", false);
|
|
469 |
}
|
|
470 |
catch (Error re)
|
|
471 |
{
|
|
472 |
int ind = re.toString().indexOf("Not able to instantiate class com.nokia.mj.impl.rt.test2.ApplicationUtilsImpl");
|
|
473 |
boolean ok = ind >= 0;
|
|
474 |
if (!ok)
|
|
475 |
{
|
|
476 |
// Accept also java.lang.ExceptionInInitializerError.
|
|
477 |
ok = re.toString().equals("java.lang.ExceptionInInitializerError");
|
|
478 |
}
|
|
479 |
assertTrue(re.toString(), ok);
|
|
480 |
}
|
|
481 |
catch (Throwable t)
|
|
482 |
{
|
|
483 |
t.printStackTrace();
|
|
484 |
assertTrue(t.toString(), false);
|
|
485 |
}
|
|
486 |
|
|
487 |
}
|
|
488 |
|
|
489 |
|
|
490 |
}
|
|
491 |
|