1 19 20 package org.netbeans.api.progress; 21 22 import java.lang.reflect.Method ; 23 import javax.swing.JComponent ; 24 import javax.swing.JFrame ; 25 import javax.swing.JLabel ; 26 import javax.swing.SwingUtilities ; 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 38 public class ProgressHandleTest extends NbTestCase { 39 40 ProgressHandle proghandle; 41 InternalHandle handle; 42 public ProgressHandleTest(String testName) { 43 super(testName); 44 } 45 46 49 protected void setUp() throws Exception { 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 return true; 58 } 59 }); 60 handle = proghandle.getInternalHandle(); 61 } 62 63 66 public void testGetDisplayName() { 67 assertEquals("displayName", handle.getDisplayName()); 68 } 69 70 73 public void testGetState() { 74 assertEquals(InternalHandle.STATE_INITIALIZED, handle.getState()); 75 76 boolean ok = false; 77 try { 78 proghandle.finish(); 80 } catch (IllegalStateException 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 proghandle.start(); 91 } catch (IllegalStateException exc) { 92 ok = true; 93 } 94 assertTrue(ok); 95 handle.requestCancel(); 97 assertEquals(InternalHandle.STATE_REQUEST_STOP, handle.getState()); 98 proghandle.finish(); 99 assertEquals(InternalHandle.STATE_FINISHED, handle.getState()); 100 } 101 102 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 115 public void testIsCustomPlaced() { 116 assertFalse(handle.isCustomPlaced()); 117 JComponent comp = ProgressHandleFactory.createProgressComponent(proghandle); 118 assertTrue(handle.isCustomPlaced()); 119 JLabel main = ProgressHandleFactory.createMainLabelComponent(proghandle); 120 assertTrue(handle.isCustomPlaced()); 121 assertNotNull(main); 122 JLabel detail = ProgressHandleFactory.createDetailLabelComponent(proghandle); 123 assertTrue(handle.isCustomPlaced()); 124 assertNotNull(detail); 125 126 boolean ok = false; 127 try { 128 comp = ProgressHandleFactory.createProgressComponent(proghandle); 130 } catch (IllegalStateException exc) { 131 ok = true; 132 } 133 134 assertTrue(ok); 135 } 136 137 140 public void testCustomPlacedLabels() { 141 assertFalse(handle.isCustomPlaced()); 142 JComponent comp = ProgressHandleFactory.createProgressComponent(proghandle); 143 JLabel main = ProgressHandleFactory.createMainLabelComponent(proghandle); 144 JLabel detail = ProgressHandleFactory.createDetailLabelComponent(proghandle); 145 proghandle.start(); 146 proghandle.setDisplayName("test1"); 147 proghandle.progress("message1"); 148 try { 150 Thread.sleep(300); 151 } catch (InterruptedException exc) { 152 System.out.println("interrupted"); 153 } 154 assertEquals("test1", main.getText()); 155 assertEquals("message1", detail.getText()); 156 } 157 158 159 public void testIfShortOnesGetDiscarded() throws Exception { 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 control.run(); 170 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 exc) { 181 System.out.println("interrupted"); 182 } 183 184 control.run(); 186 assertFalse(control.tobeRestartedDelay == -1); 188 189 h2.start(); 190 h2.progress(""); 191 proghandle.finish(); 192 193 control.run(); 195 assertFalse(control.tobeRestartedDelay == -1); 197 198 try { 199 Thread.sleep(300); 200 } catch (InterruptedException exc) { 201 System.out.println("interrupted"); 202 } 203 h2.finish(); 204 control.run(); 206 assertTrue(control.tobeRestartedDelay == -1); 208 209 } 210 211 public void testIfCustomShortOnesGetDiscarded() throws Exception { 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 control.run(); 224 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 exc) { 235 System.out.println("interrupted"); 236 } 237 control.run(); 239 assertFalse(control.tobeRestartedDelay == -1); 241 242 h2.setInitialDelay(1000); 243 h2.start(); 244 h2.progress(""); 245 proghandle.finish(); 246 247 control.run(); 249 assertFalse(control.tobeRestartedDelay == -1); 251 252 try { 253 Thread.sleep(600); 254 } catch (InterruptedException exc) { 255 System.out.println("interrupted"); 256 } 257 h2.finish(); 258 control.run(); 259 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 { 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 exc) { 300 System.out.println("interrupted"); 301 } 302 proghandle.finish(); 303 assertTrue(ui.pinged); 304 } 305 306 public void testIfCustomLongOnesGetProcessed() throws Exception { 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 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 337 public void testSwitch() { 338 final int WAIT = 1500; 339 340 class MyFrame extends JFrame implements Runnable { 341 342 JComponent component; 343 344 public MyFrame(JComponent 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 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 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 e) { 378 e.printStackTrace(); 379 } 380 try { 381 Method meth = component.getClass().getMethod("isIndeterminate", new Class [0]); 382 Boolean bool = (Boolean )meth.invoke(component, new Object [0]); 383 assertFalse("The progress bar is still indeterminate!", bool.booleanValue()); 384 } catch (Exception 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 |