001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov
019: * @version $Revision$
020: */package org.apache.harmony.awt.wtk;
021:
022: import junit.framework.TestCase;
023:
024: public class ShutdownWatchdogTest extends TestCase {
025:
026: private ShutdownWatchdog wd;
027:
028: public static void main(String[] args) {
029: junit.textui.TestRunner.run(ShutdownWatchdogTest.class);
030: }
031:
032: @Override
033: protected void setUp() throws Exception {
034: super .setUp();
035: wd = new ShutdownWatchdog();
036: }
037:
038: public void testSetWindowListEmptyTrue() {
039: intro();
040: wd.setWindowListEmpty(true);
041: sleep();
042: assertFalse(isShutdownThreadRunning());
043: outro();
044: }
045:
046: public void testSetWindowListEmptyFalse() {
047: intro();
048: wd.setWindowListEmpty(false);
049: assertTrue(isShutdownThreadRunning());
050: outro();
051: }
052:
053: public void testSetAwtQueueEmptyTrue() {
054: intro();
055: wd.setAwtQueueEmpty(true);
056: sleep();
057: assertFalse(isShutdownThreadRunning());
058: outro();
059: }
060:
061: public void testSetAwtQueueEmptyFalse() {
062: intro();
063: wd.setAwtQueueEmpty(false);
064: assertTrue(isShutdownThreadRunning());
065: outro();
066: }
067:
068: public void testSetNativeQueueEmptyTrue() {
069: intro();
070: wd.setNativeQueueEmpty(true);
071: sleep();
072: assertFalse(isShutdownThreadRunning());
073: outro();
074: }
075:
076: public void testSetNativeQueueEmptyFalse() {
077: intro();
078: wd.setNativeQueueEmpty(false);
079: assertTrue(isShutdownThreadRunning());
080: outro();
081: }
082:
083: public void testStartThenForceShutdown() {
084: intro();
085: outro();
086: }
087:
088: private void intro() {
089: assertFalse(isShutdownThreadRunning());
090: wd.start();
091: assertTrue(isShutdownThreadRunning());
092: }
093:
094: private void outro() {
095: wd.forceShutdown();
096: sleep();
097: assertFalse(isShutdownThreadRunning());
098: }
099:
100: private boolean isShutdownThreadRunning() {
101: ThreadGroup g = Thread.currentThread().getThreadGroup();
102: Thread[] threads = new Thread[g.activeCount() + 1];
103: while (true) {
104: int actualCount = g.enumerate(threads);
105: if (actualCount < threads.length) {
106: break;
107: }
108: threads = new Thread[actualCount + 1];
109: }
110: for (Thread element : threads) {
111: if ((element != null)
112: && element.getName().equals("AWT-Shutdown")) {
113: return true;
114: }
115: }
116: return false;
117: }
118:
119: private void sleep() {
120: try {
121: Thread.sleep(100);
122: } catch (InterruptedException e) {
123: e.printStackTrace();
124: }
125: }
126: }
|