001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.api.progress;
043:
044: import javax.swing.JComponent;
045: import javax.swing.JProgressBar;
046: import javax.swing.SwingUtilities;
047: import javax.swing.Timer;
048: import javax.swing.UIDefaults;
049: import javax.swing.UIManager;
050: import junit.framework.TestCase;
051: import org.netbeans.progress.module.Controller;
052: import org.netbeans.progress.spi.InternalHandle;
053: import org.netbeans.progress.spi.ProgressEvent;
054: import org.netbeans.progress.spi.ProgressUIWorker;
055: import org.openide.util.Cancellable;
056: import org.openide.util.Exceptions;
057:
058: /**
059: *
060: * @author Milos Kleint (mkleint@netbeans.org)
061: */
062: public class ProgressHandleFactoryTest extends TestCase {
063:
064: public ProgressHandleFactoryTest(String testName) {
065: super (testName);
066: }
067:
068: /**
069: * Test of createHandle method, of class org.netbeans.progress.api.ProgressHandleFactory.
070: */
071: public void testCreateHandle() {
072:
073: ProgressHandle handle = ProgressHandleFactory
074: .createHandle("task 1");
075: InternalHandle internal = handle.getInternalHandle();
076: assertEquals("task 1", internal.getDisplayName());
077: assertFalse(internal.isAllowCancel());
078: assertFalse(internal.isCustomPlaced());
079: assertEquals(InternalHandle.STATE_INITIALIZED, internal
080: .getState());
081:
082: handle = ProgressHandleFactory.createHandle("task 2",
083: new TestCancel());
084: internal = handle.getInternalHandle();
085: assertEquals("task 2", internal.getDisplayName());
086: assertTrue(internal.isAllowCancel());
087: assertFalse(internal.isCustomPlaced());
088: assertEquals(InternalHandle.STATE_INITIALIZED, internal
089: .getState());
090:
091: }
092:
093: public void testCustomComponentIsInitialized() {
094: Controller.defaultInstance = new TestController();
095:
096: ProgressHandle handle = ProgressHandleFactory
097: .createHandle("task 1");
098: JComponent component = ProgressHandleFactory
099: .createProgressComponent(handle);
100:
101: handle.start(15);
102: handle.progress(2);
103: waitForTimerFinish();
104:
105: assertEquals(15, ((JProgressBar) component).getMaximum());
106: assertEquals(2, ((JProgressBar) component).getValue());
107:
108: handle = ProgressHandleFactory.createHandle("task 2");
109: component = ProgressHandleFactory
110: .createProgressComponent(handle);
111:
112: handle.start(20);
113: waitForTimerFinish();
114:
115: assertEquals(20, ((JProgressBar) component).getMaximum());
116: assertEquals(0, ((JProgressBar) component).getValue());
117:
118: }
119:
120: private static class AwtBlocker implements Runnable {
121:
122: public AwtBlocker(int blockingTime) {
123: this .blockingTime = blockingTime;
124: }
125:
126: public void run() {
127: UIDefaults uidef = UIManager.getDefaults();
128: synchronized (uidef) {
129: blocking = true;
130: sleep();
131: }
132: }
133:
134: synchronized void sleep() {
135: try {
136: wait(blockingTime);
137: } catch (InterruptedException ex) {
138: }
139: }
140:
141: synchronized void wakeup() {
142: notify();
143: }
144:
145: volatile public boolean blocking = false;
146: private int blockingTime;
147: }
148:
149: /**
150: * Tests if ProgressUIWorkerProvider is created inside awt thread (if not deadlock is possible)
151: */
152: public void testProgressCanBeCreatedOutOfSyncAwt() {
153: Controller.defaultInstance = null;
154: final int blockingTime = 10000;
155: AwtBlocker blocker = new AwtBlocker(blockingTime);
156: long start = System.currentTimeMillis();
157: SwingUtilities.invokeLater(blocker);
158: while (!blocker.blocking) {
159: try {
160: Thread.sleep(10);
161: } catch (InterruptedException ex) {
162: }
163: }
164:
165: ProgressHandle pHandle = ProgressHandleFactory
166: .createHandle("Peforming operation...");
167: pHandle.start();
168: long elapsed = System.currentTimeMillis() - start;
169: assertTrue(
170: "Possible deadlock detected, ProgressUIWorkerProvider is creating UI outside AWT thread.",
171: elapsed < blockingTime);
172: pHandle.finish();
173: blocker.wakeup();
174: }
175:
176: private static class TestCancel implements Cancellable {
177: public boolean cancel() {
178: return true;
179: }
180:
181: }
182:
183: private class TestController extends Controller {
184: public TestController() {
185: super (new ProgressUIWorker() {
186: public void processProgressEvent(ProgressEvent event) {
187: }
188:
189: public void processSelectedProgressEvent(
190: ProgressEvent event) {
191: }
192: });
193: }
194:
195: public Timer getTestTimer() {
196: return timer;
197: }
198: }
199:
200: private void waitForTimerFinish() {
201: TestController tc = (TestController) Controller.defaultInstance;
202: int count = 0;
203: do {
204: if (count > 10) {
205: fail("Takes too much time");
206: }
207: try {
208: count = count + 1;
209: Thread.sleep(300);
210: } catch (InterruptedException exc) {
211: System.out.println("interrupted");
212: }
213: } while (tc.getTestTimer().isRunning());
214:
215: }
216:
217: }
|