1 19 20 package org.netbeans.core.output2; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Point ; 24 import java.awt.event.ActionEvent ; 25 import javax.swing.JFrame ; 26 import javax.swing.SwingUtilities ; 27 import junit.framework.TestCase; 28 import org.netbeans.core.output2.ui.AbstractOutputPane; 29 import org.netbeans.core.output2.ui.AbstractOutputTab; 30 import org.openide.windows.OutputEvent; 31 import org.openide.windows.OutputListener; 32 33 37 public class OutputWindowTest extends TestCase { 38 39 public OutputWindowTest(String testName) { 40 super(testName); 41 } 42 43 private OutputWindow win; 44 private NbIO io; 45 private OutWriter out = null; 46 JFrame jf = null; 47 NbIOProvider provider = null; 48 49 protected void setUp() throws Exception { 50 jf = new JFrame (); 51 win = new OutputWindow(); 52 OutputWindow.DEFAULT = win; 53 jf.getContentPane().setLayout (new BorderLayout ()); 54 jf.getContentPane().add (win, BorderLayout.CENTER); 55 jf.setBounds (20, 20, 700, 300); 56 provider = new NbIOProvider(); 57 io = (NbIO) provider.getIO ("Test", false); 58 SwingUtilities.invokeAndWait (new Shower()); 59 } 60 61 private final void sleep() { 62 try { 63 Thread.currentThread().sleep(200); 64 SwingUtilities.invokeAndWait (new Runnable () { 65 public void run() { 66 System.currentTimeMillis(); 67 } 68 }); 69 Thread.currentThread().sleep(200); 70 } catch (Exception e) { 71 fail (e.getMessage()); 72 } 73 } 74 75 public class Shower implements Runnable { 76 public void run() { 77 jf.setVisible(true); 78 } 79 } 80 81 public void testTabsShownAndHidden() { 82 System.out.println ("testTabsShownAndHidden"); 83 int n = win.getTabs().length; 84 assertTrue ("Number of tabs should be 1, not " + n, n == 1); 85 NbIO io2 = (NbIO) provider.getIO("Test2", true); 86 sleep(); 87 n = win.getTabs().length; 88 assertTrue ("Number of tabs should be 2, not " + n, n == 2); 89 90 io2.closeInputOutput(); 91 sleep(); 92 93 n = win.getTabs().length; 94 assertTrue ("After closeInputOutput on second tab, number of tabs should be 1, not " + n, n == 1); 95 } 96 97 public void testTabNamesUpdatedCorrectly() throws Exception { 98 System.out.println ("testTabNamesUpdatedCorrectly"); 99 io.select(); 100 for (int i=0; i < 100; i++) { 101 io.getOut().println ("Here is some text we can delete"); 102 } 103 sleep(); 104 sleep(); 105 sleep(); 106 sleep(); 107 io.getOut().flush(); 108 109 assertTrue ("Tab name should be html but is " + win.getDisplayName(), 110 win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0); 111 112 io.getOut().close(); 113 sleep(); 114 sleep(); 115 assertFalse ("Tab name should not be html", win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0); 116 117 final OutputTab tab = (OutputTab) win.getSelectedTab(); 118 119 SwingUtilities.invokeLater (new Runnable () { 121 public void run() { 122 win.getController().postPopupMenu(win, tab, new Point (0,0), tab); 123 win.getController().clearAction.actionPerformed(new ActionEvent (tab, ActionEvent.ACTION_PERFORMED, "clear")); 124 } 125 }); 126 sleep(); 127 sleep(); 128 assertFalse ("Tab name should not be html", win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0); 129 130 io.getOut().reset(); 131 sleep(); 132 io.getOut().println("And here is some more text"); 133 sleep(); 134 135 assertTrue ("Tab name should be html", win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0); 136 137 io.getOut().close(); 138 sleep(); 139 sleep(); 140 assertFalse ("Tab name should not be html", win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0); 141 142 } 143 144 146 170 public void testAbleToRetrieveSameInputOutputInstance() { 171 System.out.println ("testAbleToRetrieveSameInputOutputInstance"); 172 NbIO io2 = (NbIO) provider.getIO("Test2", true); 173 sleep(); 174 175 NbIO io3 = (NbIO) provider.getIO("Test", false); 176 assertSame ("Requesting a tab with a name already in use should return the original InputOutput", io3, io); 177 178 NbIO io4 = (NbIO) provider.getIO("Test2", false); 179 assertSame ("Requesting a second tab with a name already in use should return the same InputOutput", io4, io2); 180 181 NbIO io5 = (NbIO) provider.getIO("Test", true); 182 assertNotSame ("Requesting a new InputOutput with a name matching another tab should not return the other tab", io5, io); 183 } 184 185 public void testIOclosed() { 186 System.out.println ("testIOclosed"); 187 assertFalse ("If a tab is showing, its InputOutput should not say it is closed", io.isClosed()); 188 NbIO io2 = (NbIO) provider.getIO("Test2", true); 189 sleep(); 190 assertFalse ("If a tab is showing, its InputOutput should not say it is closed", io2.isClosed()); 191 192 assertFalse ("Adding another tab should not make the first tab think it has been closed, its InputOutput should not say it is closed", io.isClosed()); 193 194 io2.closeInputOutput(); 195 sleep(); 196 197 assertTrue ("After programmatically closing a tab, its InputOutput should return true from isClosed()", io2.isClosed()); 198 199 io2 = (NbIO) provider.getIO("Test3", true); 200 sleep(); 201 202 NbWriter writer = (NbWriter) io.getOut(); 203 writer.println ("Hello world"); 204 sleep(); 205 OutWriter outwriter = writer.out(); 206 207 assertFalse ("After removing and adding another tab, the last tab added should not say it has been closed", io2.isClosed()); 208 io.select(); 209 sleep(); 210 211 final OutputTab tab = (OutputTab) win.getSelectedTab(); 212 assertSame ("After calling InputOutput.select(), a tab owing the IO it was requested on should be the selected tab", tab.getIO(), io); 213 214 SwingUtilities.invokeLater (new Runnable () { 216 public void run() { 217 win.getController().postPopupMenu(win, tab, new Point (0,0), tab); 218 win.getController().clearAction.actionPerformed(new ActionEvent (tab, ActionEvent.ACTION_PERFORMED, "clear")); 219 } 220 }); 221 sleep(); 222 sleep(); 223 224 io.getOut().println ("Goodbye world"); 225 226 assertSame ("OutputWriter should not be replaced by calling clearing/reset() on it", writer, io.getOut()); 227 assertNotSame ("Underlying OutWriter should be replaced by calling reset() on a used NbWriter", outwriter, writer.out()); 228 } 229 230 231 public void testListenersCleared() throws Exception { 232 235 System.out.println ("testListenersCleared"); 236 io.select(); 237 io.getOut().println ("Helloooooo...."); 238 sleep(); 239 L[] ls = new L[20]; 240 for (int i=0; i < ls.length; i++) { 241 L l = new L(); 242 io.getOut().println ("Hyperlink " + i, l); 243 ls[i] = l; 244 } 245 sleep(); 246 247 for (int i=0; i < ls.length; i++) { 248 ls[i].assertNotCleared("Newly written listeners should be cleared"); 249 } 250 251 sleep(); 252 253 final OutputTab tab = (OutputTab) win.getSelectedTab(); 254 assertNotNull ("Selected tab should not be null", tab); 255 assertSame ("After calling InputOutput.select(), a tab owing the IO it was requested on should be the selected tab", tab.getIO(), io); 256 257 SwingUtilities.invokeLater (new Runnable () { 259 public void run() { 260 win.getController().postPopupMenu(win, tab, new Point (0,0), tab); 261 win.getController().clearAction.actionPerformed(new ActionEvent (tab, ActionEvent.ACTION_PERFORMED, "clear")); 262 } 263 }); 264 sleep(); 265 sleep(); 266 267 for (int i=0; i < ls.length; i++) { 268 ls[i].assertCleared("After invoking the GUI's clear action, listeners should be cleared"); 269 } 270 271 L[] ls2 = new L[20]; 272 for (int i=0; i < ls2.length; i++) { 273 L l = new L(); 274 io.getOut().println ("Second round of hyperlinks " + i, l); 275 ls2[i] = l; 276 } 277 sleep(); 278 279 for (int i=0; i < ls2.length; i++) { 280 ls2[i].assertNotCleared("The second round of listeners were cleared prematurely"); 281 } 282 283 for (int i=0; i < ls.length; i++) { 285 ls[i].assertNotCleared("Already cleared listeners should be unreferenced and should not be cleared a second time"); 287 } 288 289 SwingUtilities.invokeLater (new Runnable () { 291 public void run() { 292 win.getController().postPopupMenu(win, tab, new Point (0,0), tab); 293 win.getController().clearAction.actionPerformed(new ActionEvent (tab, ActionEvent.ACTION_PERFORMED, "clear")); 294 } 295 }); 296 sleep(); 297 sleep(); 298 299 for (int i=0; i < ls2.length; i++) { 300 ls2[i].assertCleared("After invoking the Clear Output action a second time, the newly written listeners were not cleared - " + i); 301 } 302 303 for (int i=0; i < ls.length; i++) { 306 ls[i].assertNotCleared("Already cleared listeners should not be touched by clearing or writing new data"); 308 } 309 310 L[] ls3 = new L[20]; 311 for (int i=0; i < ls3.length; i++) { 312 L l = new L(); 313 io.getOut().println ("Third round of hyperlinks " + i, l); 314 ls3[i] = l; 315 } 316 sleep(); 317 318 for (int i=0; i < ls3.length; i++) { 319 ls3[i].assertNotCleared("Third round of writes with listeners had those listeners prematurely cleared"); 320 } 321 322 io.reset(); 324 sleep(); 325 sleep(); 326 327 for (int i=0; i < ls3.length; i++) { 328 ls3[i].assertCleared("InputOutput.reset() should cause all OutputListeners to be cleared"); 329 } 330 331 io = (NbIO) provider.getIO("Another tab", true); 332 333 L[] ls4 = new L[20]; 334 for (int i=0; i < ls4.length; i++) { 335 L l = new L(); 336 io.getOut().println ("Third round of hyperlinks " + i, l); 337 ls4[i] = l; 338 } 339 sleep(); 340 341 for (int i=0; i < ls4.length; i++) { 342 ls4[i].assertNotCleared("Premature clear"); 343 } 344 345 io.getOut().close(); sleep(); 347 io.closeInputOutput(); 349 sleep(); 350 sleep(); 351 sleep(); 352 sleep(); 353 sleep(); 354 355 for (int i=0; i < ls4.length; i++) { 356 ls4[i].assertCleared("CloseInputOutput should cause all OutputListeners to be cleared"); 357 } 358 } 359 360 361 public class L implements OutputListener { 362 private OutputEvent clearedEvent = null; 363 public void assertCleared(String msg) { 364 assertNotNull (msg, clearedEvent); 365 clearedEvent = null; 366 } 367 368 public void assertNotCleared(String msg) { 369 assertNull (msg, clearedEvent); 370 } 371 372 public void outputLineSelected(OutputEvent ev) { 373 } 374 375 public void outputLineAction(OutputEvent ev) { 376 } 377 378 public void outputLineCleared(OutputEvent ev) { 379 clearedEvent = ev; 380 } 381 } 382 383 } 384 | Popular Tags |