KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > progress > ProgressHandleTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.progress;
21
22 import java.lang.reflect.Method JavaDoc;
23 import javax.swing.JComponent JavaDoc;
24 import javax.swing.JFrame JavaDoc;
25 import javax.swing.JLabel JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import org.netbeans.junit.NbTestCase;
28 import org.netbeans.progress.module.Controller;
29 import org.netbeans.progress.spi.InternalHandle;
30 import org.netbeans.progress.spi.ProgressUIWorker;
31 import org.netbeans.progress.spi.ProgressEvent;
32 import org.openide.util.Cancellable;
33
34 /**
35  *
36  * @author Milos Kleint (mkleint@netbeans.org)
37  */

38 public class ProgressHandleTest extends NbTestCase {
39     
40     ProgressHandle proghandle;
41     InternalHandle handle;
42     public ProgressHandleTest(String JavaDoc testName) {
43         super(testName);
44     }
45     
46     // TODO add test methods here. The name must begin with 'test'. For example:
47
// public void testHello() {}
48

49     protected void setUp() throws Exception JavaDoc {
50         Controller.defaultInstance = new Controller(new ProgressUIWorker() {
51             public void processProgressEvent(ProgressEvent event) { }
52             public void processSelectedProgressEvent(ProgressEvent event) { }
53         });
54         proghandle = ProgressHandleFactory.createHandle("displayName",new Cancellable() {
55             public boolean cancel() {
56                 // empty
57
return true;
58             }
59         });
60         handle = proghandle.getInternalHandle();
61     }
62
63     /**
64      * Test of getDisplayName method, of class org.netbeans.progress.api.ProgressHandle.
65      */

66     public void testGetDisplayName() {
67         assertEquals("displayName", handle.getDisplayName());
68     }
69
70     /**
71      * Test of getState method, of class org.netbeans.progress.api.ProgressHandle.
72      */

73     public void testGetState() {
74         assertEquals(InternalHandle.STATE_INITIALIZED, handle.getState());
75
76         boolean ok = false;
77         try {
78             // cannot finish a task before starting.
79
proghandle.finish();
80         } catch (IllegalStateException JavaDoc exc) {
81             ok = true;
82         }
83         assertTrue(ok);
84         
85         proghandle.start();
86         assertEquals(InternalHandle.STATE_RUNNING, handle.getState());
87         ok = false;
88         try {
89             // cannot start a task repeatedly.
90
proghandle.start();
91         } catch (IllegalStateException JavaDoc exc) {
92             ok = true;
93         }
94         assertTrue(ok);
95         // package private call, user triggered cancel action.
96
handle.requestCancel();
97         assertEquals(InternalHandle.STATE_REQUEST_STOP, handle.getState());
98         proghandle.finish();
99         assertEquals(InternalHandle.STATE_FINISHED, handle.getState());
100     }
101
102     /**
103      * Test of isAllowCancel method, of class org.netbeans.progress.api.ProgressHandle.
104      */

105     public void testIsAllowCancel() {
106         assertTrue(handle.isAllowCancel());
107         ProgressHandle h2 = ProgressHandleFactory.createHandle("ds2");
108         InternalHandle handle2 = h2.getInternalHandle();
109         assertFalse(handle2.isAllowCancel());
110     }
111
112     /**
113      * Test of isCustomPlaced method, of class org.netbeans.progress.api.ProgressHandle.
114      */

115     public void testIsCustomPlaced() {
116         assertFalse(handle.isCustomPlaced());
117         JComponent JavaDoc comp = ProgressHandleFactory.createProgressComponent(proghandle);
118         assertTrue(handle.isCustomPlaced());
119         JLabel JavaDoc main = ProgressHandleFactory.createMainLabelComponent(proghandle);
120         assertTrue(handle.isCustomPlaced());
121         assertNotNull(main);
122         JLabel JavaDoc detail = ProgressHandleFactory.createDetailLabelComponent(proghandle);
123         assertTrue(handle.isCustomPlaced());
124         assertNotNull(detail);
125                 
126         boolean ok = false;
127         try {
128             // cannot get the custom component multiple times..
129
comp = ProgressHandleFactory.createProgressComponent(proghandle);
130         } catch (IllegalStateException JavaDoc exc) {
131             ok = true;
132         }
133         
134         assertTrue(ok);
135     }
136     
137     /**
138      * Test of custom placed labels of class org.netbeans.progress.api.ProgressHandle.
139      */

140     public void testCustomPlacedLabels() {
141         assertFalse(handle.isCustomPlaced());
142         JComponent JavaDoc comp = ProgressHandleFactory.createProgressComponent(proghandle);
143         JLabel JavaDoc main = ProgressHandleFactory.createMainLabelComponent(proghandle);
144         JLabel JavaDoc detail = ProgressHandleFactory.createDetailLabelComponent(proghandle);
145         proghandle.start();
146         proghandle.setDisplayName("test1");
147         proghandle.progress("message1");
148         // kind of bad to have the wait here to overcome the scheduling..
149
try {
150             Thread.sleep(300);
151         } catch (InterruptedException JavaDoc exc) {
152             System.out.println("interrupted");
153         }
154         assertEquals("test1", main.getText());
155         assertEquals("message1", detail.getText());
156     }
157     
158     
159     // tasks shorter than the InternalHandle.INITIAL_DELAY should be discarded.
160
public void testIfShortOnesGetDiscarded() throws Exception JavaDoc {
161         OneThreadController control = new OneThreadController(new FailingUI());
162         Controller.defaultInstance = control;
163         proghandle = ProgressHandleFactory.createHandle("a1");
164         proghandle.start();
165         proghandle.progress("");
166         proghandle.finish();
167         
168         //simulate timer run
169
control.run();
170         //after running the timer sould be stopped
171
assertTrue(control.tobeRestartedDelay == -1);
172
173         
174         proghandle = ProgressHandleFactory.createHandle("a2");
175         ProgressHandle h2 = ProgressHandleFactory.createHandle("a3");
176         proghandle.start();
177         proghandle.progress("");
178         try {
179             Thread.sleep(300);
180         } catch (InterruptedException JavaDoc exc) {
181             System.out.println("interrupted");
182         }
183         
184         //simulate timer run
185
control.run();
186         // timer should continue
187
assertFalse(control.tobeRestartedDelay == -1);
188         
189         h2.start();
190         h2.progress("");
191         proghandle.finish();
192         
193         //simulate timer run
194
control.run();
195         // timer should be continuing
196
assertFalse(control.tobeRestartedDelay == -1);
197         
198         try {
199             Thread.sleep(300);
200         } catch (InterruptedException JavaDoc exc) {
201             System.out.println("interrupted");
202         }
203         h2.finish();
204         //simulate timer run
205
control.run();
206         // timer should be stopped
207
assertTrue(control.tobeRestartedDelay == -1);
208         
209     }
210     
211     // tasks shorter than the custom init delay should be discarded.
212
public void testIfCustomShortOnesGetDiscarded() throws Exception JavaDoc {
213         System.out.println("testIfCustomShortOnesGetDiscarded");
214         OneThreadController control = new OneThreadController(new FailingUI());
215         Controller.defaultInstance = control;
216         proghandle = ProgressHandleFactory.createHandle("c1");
217         proghandle.setInitialDelay(100);
218         proghandle.start();
219         proghandle.progress("");
220         proghandle.finish();
221         
222         //simulate timer run
223
control.run();
224         //after running the timer sould be stopped
225
assertTrue(control.tobeRestartedDelay == -1);
226         
227         proghandle = ProgressHandleFactory.createHandle("c2");
228         ProgressHandle h2 = ProgressHandleFactory.createHandle("c3");
229         proghandle.setInitialDelay(100);
230         proghandle.start();
231         proghandle.progress("");
232         try {
233             Thread.sleep(50);
234         } catch (InterruptedException JavaDoc exc) {
235             System.out.println("interrupted");
236         }
237         //simulate timer run
238
control.run();
239         // timer should continue
240
assertFalse(control.tobeRestartedDelay == -1);
241         
242         h2.setInitialDelay(1000);
243         h2.start();
244         h2.progress("");
245         proghandle.finish();
246         
247         //simulate timer run
248
control.run();
249         // timer should be continuing
250
assertFalse(control.tobeRestartedDelay == -1);
251         
252         try {
253             Thread.sleep(600);
254         } catch (InterruptedException JavaDoc exc) {
255             System.out.println("interrupted");
256         }
257         h2.finish();
258         control.run();
259         // timer should NOT continue
260
assertTrue(control.tobeRestartedDelay == -1);
261     }
262     
263     private class FailingUI implements ProgressUIWorker {
264             public void processProgressEvent(ProgressEvent event) {
265                 fail("How come we are processing a short one - " + event.getSource().getDisplayName());
266             }
267             public void processSelectedProgressEvent(ProgressEvent event) {
268                 fail("How come we are processing a short one - " + event.getSource().getDisplayName());
269             }
270     }
271     
272     private class OneThreadController extends Controller {
273         
274         public int tobeRestartedDelay = -1;
275         
276         public OneThreadController(ProgressUIWorker comp) {
277             super(comp);
278         }
279         
280         protected void resetTimer(int initialDelay, boolean restart) {
281             timer.setInitialDelay(initialDelay);
282             if (restart) {
283                 tobeRestartedDelay = initialDelay;
284             } else {
285                 tobeRestartedDelay = -1;
286             }
287         }
288     }
289
290   
291     public void testIfLongOnesGetProcessed() throws Exception JavaDoc {
292         assert !SwingUtilities.isEventDispatchThread();
293         PingingUI ui = new PingingUI();
294         Controller.defaultInstance = new Controller(ui);
295         proghandle = ProgressHandleFactory.createHandle("b1");
296         proghandle.start();
297         try {
298             Thread.sleep(800);
299         } catch (InterruptedException JavaDoc exc) {
300             System.out.println("interrupted");
301         }
302         proghandle.finish();
303         assertTrue(ui.pinged);
304     }
305     
306     public void testIfCustomLongOnesGetProcessed() throws Exception JavaDoc {
307         assert !SwingUtilities.isEventDispatchThread();
308         PingingUI ui = new PingingUI();
309         Controller.defaultInstance = new Controller(ui);
310         proghandle = ProgressHandleFactory.createHandle("b1");
311         proghandle.setInitialDelay(100);
312         proghandle.start();
313         try {
314             Thread.sleep(200);
315         } catch (InterruptedException JavaDoc exc) {
316             System.out.println("interrupted");
317         }
318         proghandle.finish();
319
320         assertTrue(ui.pinged);
321     }
322     
323     private class PingingUI implements ProgressUIWorker {
324         public boolean pinged = false;
325             public void processProgressEvent(ProgressEvent event) {
326                 pinged = true;
327             }
328             public void processSelectedProgressEvent(ProgressEvent event) {
329                 pinged = true;
330             }
331     }
332     
333
334 /**
335  * test switching in non-status bar component
336  */

337     public void testSwitch() {
338         final int WAIT = 1500;
339
340 class MyFrame extends JFrame JavaDoc implements Runnable JavaDoc {
341     
342     JComponent JavaDoc component;
343     
344     public MyFrame(JComponent JavaDoc component) {
345         getContentPane().add(component);
346     }
347     
348     public void run() {
349         setVisible(true);
350         setBounds(0, 0, 400, 50);
351     }
352 }
353         
354         assertFalse(SwingUtilities.isEventDispatchThread());
355         ProgressHandle handle = ProgressHandleFactory.createHandle("foo");
356         JComponent JavaDoc component = ProgressHandleFactory.createProgressComponent(handle);
357         
358
359         
360         handle.start();
361         
362         MyFrame frm = new MyFrame(component);
363         SwingUtilities.invokeLater(frm);
364         
365         try {
366             Thread.sleep(WAIT);
367         } catch (InterruptedException JavaDoc e) {
368             e.printStackTrace();
369         }
370         
371         
372         handle.switchToDeterminate(100);
373         handle.progress(50);
374         
375         try {
376             Thread.sleep(WAIT);
377         } catch (InterruptedException JavaDoc e) {
378             e.printStackTrace();
379         }
380         try {
381             Method JavaDoc meth = component.getClass().getMethod("isIndeterminate", new Class JavaDoc[0]);
382             Boolean JavaDoc bool = (Boolean JavaDoc)meth.invoke(component, new Object JavaDoc[0]);
383             assertFalse("The progress bar is still indeterminate!", bool.booleanValue());
384         } catch (Exception JavaDoc ex) {
385             ex.printStackTrace();
386             fail();
387         }
388         handle.finish();
389         frm.setVisible(false);
390         frm.dispose();
391     }
392
393     protected boolean runInEQ() {
394         return false;
395     }
396
397     
398     public void testManyWorkUnits () {
399         int units = 500000;
400         int step = 500;
401         proghandle.start (units * step);
402         for (int i = 200; i < units; i = i+ 50) {
403             proghandle.progress (i * step);
404             assertTrue(handle.getPercentageDone() >= 0);
405         }
406         proghandle.finish ();
407     }
408     
409     
410 }
411
Popular Tags