Clover coverage report - JFCUnit Test Coverage
Coverage timestamp: Mon Dec 20 2004 23:38:10 MST
file stats: LOC: 826   Methods: 26
NCLOC: 515   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TestHelperT.java 97.1% 99.2% 96.2% 98.9%
coverage coverage
 1   
 package junit.extensions.jfcunit;
 2   
 
 3   
 import java.util.List;
 4   
 
 5   
 import java.awt.Component;
 6   
 import java.awt.FlowLayout;
 7   
 import java.awt.Frame;
 8   
 import java.awt.Point;
 9   
 import java.awt.Toolkit;
 10   
 import java.awt.event.WindowAdapter;
 11   
 import java.awt.event.WindowEvent;
 12   
 import javax.swing.JButton;
 13   
 import javax.swing.JDialog;
 14   
 import javax.swing.JFileChooser;
 15   
 import javax.swing.JFrame;
 16   
 import javax.swing.JLabel;
 17   
 import javax.swing.JMenu;
 18   
 import javax.swing.JMenuItem;
 19   
 import javax.swing.JOptionPane;
 20   
 import javax.swing.JScrollPane;
 21   
 import javax.swing.JTabbedPane;
 22   
 import javax.swing.JTextField;
 23   
 import javax.swing.JToggleButton;
 24   
 import javax.swing.JToolBar;
 25   
 import javax.swing.SwingUtilities;
 26   
 import javax.swing.filechooser.FileSystemView;
 27   
 
 28   
 import org.apache.regexp.RESyntaxException;
 29   
 import junit.extensions.jfcunit.finder.AbstractButtonFinder;
 30   
 import junit.extensions.jfcunit.finder.ComponentFinder;
 31   
 import junit.extensions.jfcunit.finder.Finder;
 32   
 import junit.extensions.jfcunit.finder.NamedComponentFinder;
 33   
 import junit.swingui.TestRunner;
 34   
 import java.lang.reflect.InvocationTargetException;
 35   
 import junit.extensions.jfcunit.finder.FrameFinder;
 36   
 import junit.extensions.jfcunit.finder.DialogFinder;
 37   
 import junit.extensions.jfcunit.finder.JFileChooserFinder;
 38   
 
 39   
 /**
 40   
  * Title:        TestHelperT
 41   
  * Description:  Unit tests for the {@link TestHelper} class and it's methods.
 42   
  *               Designed to work with JUnit v3.7 or later.
 43   
  *
 44   
  * @author Greg Houston
 45   
  * @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
 46   
  */
 47   
 public class TestHelperT extends AbstractTestCase {
 48   
 
 49   
   /**
 50   
    * String litteral for something.
 51   
    */
 52   
     private static final String SOMETHING = "something";
 53   
     /**
 54   
      * Default constructor that accepts a String.
 55   
      *
 56   
      * @param   name      The name of the test method to execute.
 57   
      */
 58  20
     public TestHelperT(final String name) {
 59  20
         super(name);
 60   
     }
 61   
 
 62   
     /**
 63   
      * Overridden method that is used to setup the test fixtures.
 64   
      */
 65  20
     public void setUp() {
 66  20
         if (getHelper() == null) {
 67  20
             setHelper(new TestHelperClass());
 68   
         }
 69  20
         getHelper().cleanUp(this);
 70  20
         assertEquals("No windows should be present", 0, getHelper().getWindows().size());
 71   
     }
 72   
 
 73   
     /**
 74   
      * The entry point for this class if it is to be run as a stand-alone application.
 75   
      *
 76   
      * @param   args      The command-line arguments.
 77   
      */
 78  0
     public static void main(final String[] args) {
 79  0
         TestRunner.run(TestHelperT.class);
 80   
     }
 81   
 
 82   
     /*
 83   
     // Uncomment for testing individual methods.
 84   
     public static junit.framework.Test suite() {
 85   
         junit.framework.TestSuite suite = new junit.framework.TestSuite();
 86   
         suite.addTest(new TestHelperT("testFindComponent"));
 87   
         return suite;
 88   
     }
 89   
     */
 90   
 
 91   
 
 92   
     /**
 93   
      * Tests that the findComponent() method works.
 94   
      */
 95  1
     public void testFindComponent() {
 96  1
         setWindow(createJFrame(getName()));
 97  1
         getFrame().setName("frame");
 98  1
         getFrame().getContentPane().setLayout(new FlowLayout());
 99   
 
 100   
         // have a component with no name set
 101  1
         JMenuItem menuItem1 = new JMenuItem("MenuItem1");
 102  1
         getFrame().getContentPane().add(menuItem1);
 103   
 
 104   
         // have a component with name set
 105  1
         JMenuItem menuItem2 = new JMenuItem("MenuItem2");
 106  1
         menuItem2.setName(SOMETHING);
 107  1
         getFrame().getContentPane().add(menuItem2);
 108   
 
 109   
         // have a component with no name set
 110  1
         JMenu menu1 = new JMenu("Menu1");
 111  1
         getFrame().getContentPane().add(menu1);
 112   
 
 113   
         // have a component with name set
 114  1
         JMenu menu2 = new JMenu("Menu2");
 115  1
         menu2.setName(SOMETHING);
 116  1
         getFrame().getContentPane().add(menu2);
 117   
 
 118   
         // have a component with no name set
 119  1
         JToolBar toolBar1 = new JToolBar();
 120  1
         getFrame().getContentPane().add(toolBar1);
 121   
 
 122   
         // have a component with name set
 123  1
         JToolBar toolBar2 = new JToolBar();
 124  1
         toolBar2.setName("SomeName");
 125  1
         getFrame().getContentPane().add(toolBar2);
 126   
 
 127   
         // have a component with no name set
 128  1
         JLabel label1 = new JLabel("label1");
 129  1
         getFrame().getContentPane().add(label1);
 130   
 
 131   
         // have a component with name set
 132  1
         JLabel label2 = new JLabel("label2");
 133  1
         label2.setName(SOMETHING);
 134  1
         getFrame().getContentPane().add(label2);
 135   
 
 136   
         // have a component with no name set
 137  1
         JButton button1 = new JButton("button1");
 138  1
         getFrame().getContentPane().add(button1);
 139   
 
 140   
         // have a component with name set
 141  1
         JButton button2 = new JButton("button2");
 142  1
         button2.setName(SOMETHING);
 143  1
         getFrame().getContentPane().add(button2);
 144   
 
 145   
         // have a component with no name set
 146  1
         JToggleButton toggleButton1 = new JToggleButton("toggleButton1");
 147  1
         getFrame().getContentPane().add(toggleButton1);
 148   
 
 149   
         // have a component with name set
 150  1
         JToggleButton toggleButton2 = new JToggleButton("toggleButton2");
 151  1
         toggleButton2.setName(SOMETHING);
 152  1
         getFrame().getContentPane().add(toggleButton2);
 153   
 
 154  1
         packAndShow(getFrame());
 155   
 
 156  1
         Component component;
 157   
 
 158   
         // Note: JMenu and JMenuItem are here to test that if the class specified
 159   
         // is a super class, the find call should not return that component
 160   
 
 161   
         // 1: find by specifying class
 162  1
         component = new ComponentFinder(JMenuItem.class).find(getFrame(), 0);
 163  1
         assertEquals("1: Component found should be menuItem1", menuItem1, component);
 164   
 
 165   
         // 2: find by specifying class
 166  1
         component = new ComponentFinder(JMenuItem.class).find(getFrame(), 1);
 167  1
         assertEquals("2: Component found should be menuItem2", menuItem2, component);
 168   
 
 169   
         // 1: find by specifying class
 170  1
         component = new ComponentFinder(JMenu.class).find(getFrame(), 0);
 171  1
         assertEquals("1: Component found should be menu1", menu1, component);
 172   
 
 173   
         // 2: find by specifying class
 174  1
         Finder finder = new ComponentFinder(JMenu.class);
 175  1
         finder.setDebug(true);
 176  1
         component = finder.find(getFrame(), 1);
 177  1
         assertEquals("2: Component found should be menu2", menu2, component);
 178   
 
 179   
         // 1: find by specifying class
 180  1
         component = new ComponentFinder(JToolBar.class).find(getFrame(), 0);
 181  1
         assertEquals("1: Component found should be toolBar1", toolBar1, component);
 182   
 
 183   
         // 2: find by specifying class
 184  1
         component = new ComponentFinder(JToolBar.class).find(getFrame(), 1);
 185  1
         assertEquals("2: Component found should be toolBar2", toolBar2, component);
 186   
 
 187   
         // 1: find by specifying class
 188  1
         component = new ComponentFinder(JLabel.class).find(getFrame(), 0);
 189  1
         assertEquals("1: Component found should be label1", label1, component);
 190   
 
 191   
         // 2: find by specifying class
 192  1
         component = new ComponentFinder(JLabel.class).find(getFrame(), 1);
 193  1
         assertEquals("2: Component found should be label2", label2, component);
 194   
 
 195   
         // 1: find by specifying class
 196  1
         component = new ComponentFinder(JButton.class).find(getFrame(), 0);
 197  1
         assertEquals("1: Component found should be button1", button1, component);
 198   
 
 199   
         // 2: find by specifying class
 200  1
         component = new ComponentFinder(JButton.class).find(getFrame(), 1);
 201  1
         assertEquals("2: Component found should be button2", button2, component);
 202   
 
 203   
         // 1: find by specifying class
 204  1
         component = new ComponentFinder(JToggleButton.class).find(getFrame(), 0);
 205  1
         assertEquals("1: Component found should be toggleButton1", toggleButton1, component);
 206   
 
 207   
         // 2: find by specifying class
 208  1
         component = new ComponentFinder(JToggleButton.class).find(getFrame(), 1);
 209  1
         assertEquals("2: Component found should be toggleButton2", toggleButton2, component);
 210   
 
 211   
         // using patterns to find components
 212  1
         component = new AbstractButtonFinder("but[a-z]*1").find(getFrame(), 0);
 213  1
         assertEquals("1: Component found should be button1", button1, component);
 214   
 
 215  1
         component = new AbstractButtonFinder("but[a-z]*[0-9]").find(getFrame(), 1);
 216  1
         assertEquals("2: Component found should be button2", button2, component);
 217   
     }
 218   
 
 219   
     /**
 220   
      * Creates a frame with an 'ok' button. Tries to find the button using it's
 221   
      * component name, asserts that it was found.  Also tries to find a component
 222   
      * that doesn't exist, asserts that null is returned.
 223   
      */
 224  1
     public void testFindNamedComponent() {
 225  1
         setWindow(createJFrame(getName()));
 226  1
         getFrame().setName("frame");
 227  1
         getFrame().getContentPane().setLayout(new FlowLayout());
 228   
 
 229  1
         JButton button = new JButton("Ok Button");
 230  1
         button.setName("Ok Button");
 231  1
         getFrame().getContentPane().add(button);
 232   
 
 233  1
         JButton button2 = new JButton("Ok Button #2");
 234  1
         button2.setName("Ok Button #2");
 235  1
         getFrame().getContentPane().add(button2);
 236   
 
 237  1
         JLabel label = new JLabel("Ok Label");
 238  1
         label.setName("Ok Label");
 239  1
         getFrame().getContentPane().add(label);
 240   
 
 241  1
         JLabel nullname = new JLabel("Null name");
 242  1
         nullname.setName(null);
 243  1
         getFrame().getContentPane().add(nullname);
 244   
 
 245  1
         JMenuItem menuItem1 = new JMenuItem("MenuItem1");
 246  1
         getFrame().getContentPane().add(menuItem1);
 247   
 
 248  1
         JMenuItem menuItem2 = new JMenuItem("MenuItem2");
 249  1
         menuItem2.setName("SomeName");
 250  1
         getFrame().getContentPane().add(menuItem2);
 251   
 
 252  1
         JMenu menu1 = new JMenu("Menu1");
 253  1
         getFrame().getContentPane().add(menu1);
 254   
 
 255  1
         JMenu menu2 = new JMenu("Menu2");
 256  1
         menu2.setName("SomeName");
 257  1
         getFrame().getContentPane().add(menu2);
 258   
 
 259  1
         packAndShow(getFrame());
 260   
 
 261  1
         Component component;
 262  1
         Finder finder = new NamedComponentFinder(null, "notfound");
 263  1
         finder.setWait(0);
 264  1
         component = finder.find(getFrame(), 0);
 265  1
         assertNull("Must not find 'notfound'", component);
 266   
 
 267  1
         String ok = "Ok";
 268  1
         component = new NamedComponentFinder(null, ok).find(getFrame(), 0);
 269  1
         assertEquals("Must find 'Ok'", button, component);
 270   
 
 271  1
         component = new NamedComponentFinder(null, ok).find(getFrame(), 1);
 272  1
         assertEquals("Must find 'Ok' Button #2", button2, component);
 273   
 
 274  1
         component = new NamedComponentFinder(label.getClass(), ok).find(getFrame(), 0);
 275  1
         assertEquals("Must find 'Ok' label", label, component);
 276   
 
 277  1
         component = new NamedComponentFinder(JLabel.class, null).find(getFrame(), 0);
 278  1
         assertEquals("Must find label with null name", nullname, component);
 279   
 
 280  1
         component = new NamedComponentFinder(JLabel.class, (String) null).find(getFrame(), 0);
 281  1
         assertNull("Must find a component with null for name.", component.getName());
 282   
 
 283  1
         component = new NamedComponentFinder(JMenu.class, null).find(getFrame(), 0);
 284  1
         assertEquals("Must find menu with null name", menu1, component);
 285   
 
 286  1
         component = new NamedComponentFinder(menu1.getClass(), menu1.getName()).find(getFrame(), 0);
 287  1
         assertEquals("Must find '" + menu1.getName() + "' menu", menu1, component);
 288   
 
 289  1
         component = new NamedComponentFinder(menu2.getClass(), menu2.getName()).find(getFrame(), 0);
 290  1
         assertEquals("Must find '" + menu2.getName() + "' menu", menu2, component);
 291   
 
 292  1
         component = new NamedComponentFinder(JMenuItem.class, null).find(getFrame(), 0);
 293  1
         assertEquals("Must find menuItem with null name", menuItem1, component);
 294   
 
 295  1
         component = new NamedComponentFinder(menuItem1.getClass(), menuItem1.getName()).find(getFrame(), 0);
 296  1
         assertEquals("Must find '" + menuItem1.getName() + "' menuItem", menuItem1, component);
 297   
 
 298  1
         component = new NamedComponentFinder(menuItem2.getClass(), menuItem2.getName()).find(getFrame(), 0);
 299  1
         assertEquals("Must find '" + menuItem2.getName() + "' menuItem", menuItem2, component);
 300   
 
 301   
         // using patterns to find named components
 302  1
         component = new NamedComponentFinder(null, "[a-zA-Z]* Button").find(getFrame(), 0);
 303  1
         assertEquals("Must find 'Ok' Button #1", button, component);
 304   
 
 305  1
         component = new NamedComponentFinder(null, "[a-zA-Z]* Button").find(getFrame(), 1);
 306  1
         assertEquals("Must find 'Ok' Button #2", button2, component);
 307   
 
 308  1
         component = new NamedComponentFinder(null, "[a-zA-Z]* Button .2").find(getFrame(), 0);
 309  1
         assertEquals("Must find 'Ok' Button #2", button2, component);
 310   
     }
 311   
 
 312   
     /**
 313   
      * Checks that when finding a window using the complete title, we do find it.
 314   
      */
 315  1
     public void testGetWindowByCompleteTitle() {
 316  1
         String frameTitle = getName() + " 1";
 317  1
         setWindow(createJFrame(frameTitle));
 318  1
         packAndShow(getFrame());
 319   
 
 320  1
         assertEquals("Must find window with '" + frameTitle + "' title", getFrame(), new FrameFinder(frameTitle).find());
 321   
     }
 322   
 
 323   
     /**
 324   
      * Checks that when finding a window using the complete title,
 325   
      * we do find it.
 326   
      */
 327  1
     public void testGetWindowByPartialTitle() {
 328  1
         String frameTitle = getName() + " 2";
 329  1
         setWindow(createJFrame(frameTitle));
 330  1
         packAndShow(getFrame());
 331   
 
 332  1
         String subTitle = frameTitle.substring(frameTitle.length() - 7);
 333  1
         assertEquals("Must find window with title containing '" + subTitle + "'",
 334   
                      getFrame(),
 335   
                      new FrameFinder(subTitle).find());
 336   
     }
 337   
 
 338   
     /**
 339   
      * Checks that when finding a window using the partial title,
 340   
      * we do find it.
 341   
      */
 342  1
     public void testGetWindowByNullTitle() {
 343  1
         setWindow(createJFrame(null));
 344  1
         packAndShow(getFrame());
 345   
 
 346  1
         Finder finder = new FrameFinder("Notfound");
 347  1
         finder.setWait(0);
 348   
 
 349  1
         assertNull("Window should not be found", finder.find());
 350  1
         finder = new FrameFinder(null);
 351  1
         assertEquals("null title should find frame", getFrame(), finder.find());
 352   
     }
 353   
 
 354   
     /**
 355   
      * Checks that when finding a window using the partial title,
 356   
      * we do find the correct number of multiple windows.
 357   
      */
 358  1
     public void testGetWindowByPartialTitle2() {
 359  1
         String frame1Title = getName() + " 1";
 360  1
         setWindow(createJFrame(frame1Title));
 361  1
         packAndShow(getFrame());
 362   
 
 363  1
         String frame2Title = getName() + " 2";
 364  1
         JFrame frame2 = createJFrame(frame2Title);
 365  1
         packAndShow(frame2);
 366  1
         flushAWT();
 367   
 
 368  1
         try {
 369  1
             Finder finder = new FrameFinder("");
 370  1
             assertNotNull("Empty string must return a match", finder.find());
 371   
 
 372  1
             List wins = new FrameFinder(getName()).findAll();
 373  1
             assertEquals("getWindows must return two windows that matched title", 2, wins.size());
 374   
         } finally {
 375  1
             TestHelper.disposeWindow(frame2, this);
 376   
         }
 377   
     }
 378   
 
 379   
     /**
 380   
      * Checks that when finding a set of windows using the title,
 381   
      * we do find them. Tests with partial title as well as complete title.
 382   
      */
 383  1
     public void testGetWindows() {
 384  1
         int number = 5;
 385  1
         JFrame[] frames = new JFrame[number * 2];
 386  1
         String duplicateTitle = "Duplicate";
 387  1
         String frameTitle = getName();
 388  1
         int x = (int) (Math.random() * number);
 389  1
         int y = (int) (Math.random() * number);
 390  1
         for (int i = 0; i < number; i++) {
 391  5
             frames[i] = createJFrame(frameTitle + " " + (i + 1));
 392   
 
 393   
             // don't center - all of the frames will overlap
 394  5
             frames[i].setLocation(x + (i * 50), y + (i * 50));
 395  5
             frames[i].pack();
 396  5
             frames[i].setVisible(true);
 397   
 
 398  5
             int dupIndex = i + number;
 399  5
             frames[dupIndex] = createJFrame(duplicateTitle + " " + (i + 1));
 400   
 
 401   
             // don't center - all of the frames will overlap
 402  5
             frames[dupIndex].setLocation(x + (dupIndex * 50), y + (dupIndex * 50));
 403  5
             frames[dupIndex].pack();
 404  5
             frames[dupIndex].setVisible(true);
 405   
         }
 406  1
         flushAWT();
 407   
 
 408  1
         try {
 409  1
             List windows;
 410   
             // find by the method name set to null - all windows should show up
 411  1
             windows = TestHelper.getWindows((String) null);
 412  1
             assertEquals("(1)Could not find correct number of windows", number * 2, windows.size());
 413   
 
 414   
             // find by the method name as substring
 415  1
             windows = TestHelper.getWindows(frameTitle);
 416  1
             assertEquals("(2)Could not find correct number of windows", number, windows.size());
 417   
 
 418   
             // find by "Duplicate" as substring
 419  1
             windows = TestHelper.getWindows(duplicateTitle);
 420  1
             assertEquals("(3)Could not find correct number of windows", number, windows.size());
 421   
 
 422   
             // find by the integer part  as substring
 423  1
             windows = TestHelper.getWindows(" 4");
 424  1
             assertEquals("(4)Could not find correct number of windows", 2, windows.size());
 425   
 
 426   
             // find by the integer part  as substring
 427  1
             windows = TestHelper.getWindows(duplicateTitle + " 2");
 428  1
             assertEquals("(5)Could not find correct number of windows", 1, windows.size());
 429   
         } finally {
 430  1
             for (int i = 0; i < frames.length; i++) {
 431  10
                 TestHelper.disposeWindow(frames[i], this);
 432   
             }
 433   
         }
 434   
     }
 435   
 
 436   
     /**
 437   
      * Checks that when finding a set of dialogs without specifying the owner,
 438   
      * we do find them.
 439   
      */
 440  1
     public void testGetShowingDialogsNotSpecifyingOwner() {
 441  1
         setWindow(createJFrame(getName()));
 442  1
         getFrame().setName("frame");
 443  1
         getFrame().setVisible(true);
 444   
 
 445  1
         int number = 5;
 446  1
         JFrame[] frames = new JFrame[number];
 447  1
         JDialog[] dialogs = new JDialog[number];
 448  1
         String dialogTitle = getName();
 449  1
         int x = (int) (Math.random() * number);
 450  1
         int y = (int) (Math.random() * number);
 451  1
         for (int i = 0; i < number; i++) {
 452  5
             frames[i] = createJFrame(getName() + " " + (i + 1));
 453  5
             frames[i].setLocation(i * 25, i * 25);
 454  5
             frames[i].setVisible(true);
 455   
 
 456  5
             dialogs[i] = new JDialog(frames[i], dialogTitle + " " + (i + 1));
 457   
 
 458   
             // don't center - all of the dialogs will overlap
 459  5
             dialogs[i].setLocation(x + (i * 50), y + (i * 50));
 460  5
             dialogs[i].pack();
 461  5
             dialogs[i].setVisible(true);
 462   
         }
 463   
 
 464  1
         try {
 465  1
             List windows = new DialogFinder(null).findAll();
 466  1
             assertEquals("Could not find correct number of windows", number, windows.size());
 467   
         } finally {
 468  1
             for (int i = 0; i < dialogs.length; i++) {
 469  5
                 TestHelper.disposeWindow(frames[i], this);
 470   
             }
 471   
         }
 472   
     }
 473   
 
 474   
     /**
 475   
      * Checks that when finding a set of dialogs using the title,
 476   
      * we do find them. Tests with partial title as well as complete title.
 477   
      */
 478  1
     public void testGetShowingDialogsSpecifyingTitle() {
 479  1
         int number = 5;
 480  1
         JDialog[] dialogs = new JDialog[number * 2];
 481  1
         String duplicateTitle = "Duplicate";
 482  1
         String dialogTitle = getName();
 483  1
         int x = (int) (Math.random() * number);
 484  1
         int y = (int) (Math.random() * number);
 485  1
         for (int i = 0; i < number; i++) {
 486  5
             dialogs[i] = new JDialog((Frame) null, dialogTitle + " " + (i + 1));
 487   
 
 488   
             // don't center - all of the dialogs will overlap
 489  5
             dialogs[i].setLocation(x + (i * 50), y + (i * 50));
 490  5
             dialogs[i].pack();
 491  5
             dialogs[i].setVisible(true);
 492   
 
 493  5
             int dupIndex = i + number;
 494  5
             dialogs[dupIndex] = new JDialog((Frame) null, duplicateTitle + " " + (i + 1));
 495   
 
 496   
             // don't center - all of the dialogs will overlap
 497  5
             dialogs[dupIndex].setLocation(x + (dupIndex * 50), y + (dupIndex * 50));
 498  5
             dialogs[dupIndex].pack();
 499  5
             dialogs[dupIndex].setVisible(true);
 500   
         }
 501   
 
 502  1
         flushAWT();
 503  1
         try {
 504  1
             List windows;
 505   
             // find by the method name set to null - all dialogs should show up
 506  1
             windows = new DialogFinder((String) null).findAll();
 507  1
             assertEquals("(1)Could not find correct number of windows", number * 2, windows.size());
 508   
 
 509   
             // find by the method name as substring
 510  1
             windows = new DialogFinder(dialogTitle).findAll();
 511  1
             assertEquals("(2)Could not find correct number of windows", number, windows.size());
 512   
 
 513   
             // find by "Duplicate" as substring
 514  1
             windows = new DialogFinder(duplicateTitle).findAll();
 515  1
             assertEquals("(3)Could not find correct number of windows", number, windows.size());
 516   
 
 517   
             // find by the integer part as substring
 518  1
             windows = new DialogFinder(" 4").findAll();
 519  1
             assertEquals("(4)Could not find correct number of windows", 2, windows.size());
 520   
 
 521   
             // find by the integer part as substring
 522  1
             windows = new DialogFinder(duplicateTitle + " 2").findAll();
 523  1
             assertEquals("(5)Could not find correct number of windows", 1, windows.size());
 524   
         } finally {
 525  1
             for (int i = 0; i < dialogs.length; i++) {
 526  10
                 TestHelper.disposeWindow(dialogs[i], this);
 527   
             }
 528   
         }
 529   
     }
 530   
 
 531   
 
 532   
     /**
 533   
      * This method is used to test that when dialogs are "buried" under (owned by) other
 534   
      * dialogs, they can still be found by the default getShowingDialogs() which does not
 535   
      * accept any arguments.
 536   
      */
 537  1
     public void testGetShowingDialogsMultiLevelDeep() {
 538  1
         int number = 5;
 539  1
         JDialog[] dialogs = new JDialog[number];
 540  1
         String levelTitle = "Level";
 541  1
         String dialogTitle = getName();
 542  1
         int x = (int) (Math.random() * number);
 543  1
         int y = (int) (Math.random() * number);
 544  1
         for (int i = 0; i < number; i++) {
 545  5
             if (i == 0) {
 546  1
                 dialogs[i] = new JDialog((Frame) null, dialogTitle + ": " + levelTitle + ": " + (i + 1));
 547   
             } else {
 548  4
                 dialogs[i] = new JDialog(dialogs[i - 1], dialogTitle + ": " + levelTitle + ": " + (i + 1));
 549   
             }
 550   
 
 551   
             // don't center - all of the dialogs will overlap
 552  5
             dialogs[i].setLocation(x + (i * 50), y + (i * 50));
 553  5
             dialogs[i].pack();
 554  5
             dialogs[i].setVisible(true);
 555   
         }
 556   
 
 557  1
         try {
 558  1
             List windows;
 559   
             // find by the method name set to null - all dialogs should show up
 560  1
             windows = new DialogFinder((String) null).findAll();
 561  1
             assertEquals("(1)Could not find correct number of windows", number, windows.size());
 562   
 
 563   
             // find by the method name as substring
 564  1
             windows = new DialogFinder(dialogTitle).findAll();
 565  1
             assertEquals("(2)Could not find correct number of windows", number, windows.size());
 566   
 
 567   
             // find by "Level" as substring
 568  1
             windows = new DialogFinder(levelTitle).findAll();
 569  1
             assertEquals("(3)Could not find correct number of windows", number, windows.size());
 570   
 
 571   
             // find by the integer part as substring
 572  1
             windows = new DialogFinder(" 3").findAll();
 573  1
             assertEquals("(4)Could not find correct number of windows", 1, windows.size());
 574   
 
 575   
             // find by the integer part as substring
 576  1
             windows = new DialogFinder(levelTitle + ": 4").findAll();
 577  1
             assertEquals("(5)Could not find correct number of windows", 1, windows.size());
 578   
         } finally {
 579  1
             for (int i = 0; i < dialogs.length; i++) {
 580  5
                 TestHelper.disposeWindow(dialogs[i], this);
 581   
             }
 582   
         }
 583   
     }
 584   
 
 585   
     /**
 586   
      * Tests that the cleanUp() method works correctly.
 587   
      * No System Windows.
 588   
      */
 589  1
     public void testCleanUp() {
 590  1
         int number = 10;
 591  1
         TestHelper.removeAllSystemWindows();
 592  1
         JFrame[] frames = new JFrame[number];
 593  1
         int x = (int) (Math.random() * number);
 594  1
         int y = (int) (Math.random() * number);
 595  1
         for (int i = 0; i < number; i++) {
 596  10
             frames[i] = createJFrame(getName() + " " + (i + 1));
 597   
 
 598   
             // don't center - all of the frames will overlap
 599  10
             frames[i].setLocation(x + (i * 50), y + (i * 50));
 600  10
             frames[i].pack();
 601  10
             frames[i].setVisible(true);
 602  10
             flushAWT();
 603   
         }
 604   
 
 605  1
         assertEquals("(1)Number of windows is incorrect", number, TestHelper.getWindows().size());
 606   
 
 607  1
         TestHelper.disposeWindow(frames[4], this);
 608  1
         assertEquals("(2)Number of windows is incorrect", number - 1, TestHelper.getWindows().size());
 609   
 
 610  1
         TestHelper.cleanUp(this);
 611  1
         assertEquals("Number of windows is incorrect", 0, TestHelper.getWindows().size());
 612   
     }
 613   
 
 614   
     /**
 615   
      * Tests that the cleanUp() method works correctly.
 616   
      */
 617  1
     public void testCleanUpWithSystemWindow() {
 618  1
         int number = 10;
 619  1
         TestHelper.removeAllSystemWindows();
 620  1
         JFrame[] frames = new JFrame[number];
 621  1
         int x = (int) (Math.random() * number);
 622  1
         int y = (int) (Math.random() * number);
 623  1
         for (int i = 0; i < number; i++) {
 624  10
             frames[i] = createJFrame(getName() + " " + (i + 1));
 625   
 
 626   
             // don't center - all of the frames will overlap
 627  10
             frames[i].setLocation(x + (i * 50), y + (i * 50));
 628  10
             frames[i].pack();
 629  10
             frames[i].setVisible(true);
 630   
         }
 631  1
         flushAWT();
 632   
 
 633  1
         assertEquals("(1)Number of windows is incorrect", number, TestHelper.getWindows().size());
 634   
 
 635  1
         try {
 636  1
           TestHelper.addSystemWindow("^" + frames[4].getTitle() + "$");
 637   
         } catch (RESyntaxException ex) {
 638  0
           fail("Exception while adding System Window");
 639   
         }
 640  1
         TestHelper.cleanUp(this);
 641  1
         assertEquals("(2)Number of windows is incorrect", 1, TestHelper.getWindows().size());
 642  1
         TestHelper.removeSystemWindow("^" + frames[4].getTitle() + "$");
 643   
 
 644  1
         TestHelper.cleanUp(this);
 645  1
         assertEquals("Number of windows is incorrect", 0, TestHelper.getWindows().size());
 646   
     }
 647   
 
 648   
     /**
 649   
      * Tests that the disposeWindow() method works correctly.
 650   
      */
 651  1
     public void testDisposeWindow() {
 652  1
         JTextField textField = new JTextField(30);
 653   
 
 654  1
         setWindow(createJFrame(getName()));
 655  1
         getFrame().addWindowListener(new WindowAdapter() {
 656  1
             public void windowOpened(final WindowEvent e) {
 657  1
                 (new JDialog(getFrame(), "Dialog title")).setVisible(true);
 658   
             }
 659   
         });
 660  1
         getFrame().getContentPane().add(textField);
 661  1
         packAndShow(getFrame());
 662   
 
 663   
         // need this call here so that the dialog can also show
 664  1
         flushAWT();
 665   
 
 666  1
         DialogFinder finder = new DialogFinder(null);
 667  1
         finder.setWait(0);
 668  1
         List showingDialogs = finder.findAll(getFrame());
 669  1
         assertEquals("(1)Number of showing dialogs is wrong", 1, showingDialogs.size());
 670   
 
 671   
         // check for dialog box with null owner
 672  1
         TestHelper.disposeWindow(getFrame(), this);
 673  1
         assertTrue("Frame is still showing", !getFrame().isShowing());
 674   
 
 675   
         // check that the call to dispose the frame also disposed of its open dialogs
 676  1
         showingDialogs = finder.findAll(getFrame());
 677  1
         assertEquals("(2)Number of showing dialogs is wrong", 0, showingDialogs.size());
 678   
     }
 679   
 
 680   
     /**
 681   
      * This method is used to test the getJFileChoosers() method.
 682   
      */
 683  1
     public void testGetJFileChoosers() {
 684  1
         setWindow(createJFrame(getName()));
 685  1
         packAndShow(getFrame());
 686   
 
 687  1
         JTabbedPane tabPane = null;
 688  1
         JFileChooser fileChooser = null;
 689  1
         JDialog dialog = null;
 690  1
         int number = 5;
 691  1
         String dialogTitle = "Testing JFileChooser ";
 692  1
         int x = (int) (Math.random() * number);
 693  1
         int y = (int) (Math.random() * number);
 694  1
         for (int i = 0; i < number; i++) {
 695  5
             tabPane = new JTabbedPane();
 696   
             // use the FileSystemView to get a directory - otherwise, some versions
 697   
             // of the JDK have problems finding the default directory.
 698  5
             fileChooser = new JFileChooser(FileSystemView.getFileSystemView());
 699  5
             tabPane.add(fileChooser);
 700  5
             tabPane.setSize(200, 200);
 701   
 
 702  5
             dialog = new JDialog(getFrame(), dialogTitle + (i + 1), false);
 703  5
             dialog.getContentPane().add(new JScrollPane(tabPane));
 704   
             // don't center - all of the dialogs will overlap
 705  5
             dialog.setLocation(x + (i * 50), y + (i * 50));
 706  5
             dialog.pack();
 707  5
             dialog.setVisible(true);
 708   
         }
 709   
 
 710  1
         JFileChooserFinder finder = new JFileChooserFinder(null);
 711  1
         finder.setWait(0);
 712  1
         List returnedValues = finder.findAll(getFrame());
 713  1
         assertEquals("(1) Number of showing JFileChoosers is wrong", number, returnedValues.size());
 714   
 
 715   
         // dispose of the last dialog and then try again
 716  1
         TestHelper.disposeWindow(dialog, this);
 717   
 
 718  1
         returnedValues = finder.findAll(getFrame());
 719  1
         assertEquals("(2) Number of showing JFileChoosers is wrong", number - 1, returnedValues.size());
 720   
     }
 721   
 
 722   
     /**
 723   
      * Tests that the getMessageFromJDialog() method works.
 724   
      *
 725   
      * @exception InterruptedException  May be thrown from sleep.
 726   
      */
 727  1
     public void testGetMessageFromJDialog() throws InterruptedException {
 728  1
         final String msg = "Test Message";
 729   
 
 730   
         // call flushAWT so that any existing events in the EventQueue are processed
 731  1
         flushAWT();
 732   
 
 733  1
         SwingUtilities.invokeLater(new Runnable() {
 734  1
             public void run() {
 735  1
                 JOptionPane.showMessageDialog(null, msg, "title", JOptionPane.INFORMATION_MESSAGE);
 736   
             }
 737   
         });
 738   
 
 739   
         // call awtSleep so that the above call to SwingUtilities can be processed
 740  1
         Thread.currentThread().sleep(100);
 741  1
         flushAWT();
 742   
 
 743  1
         List showingDialogs = new DialogFinder("title").findAll();
 744  1
         assertEquals("Number of dialogs showing is incorrect", 1, showingDialogs.size());
 745  1
         JDialog dialog = (JDialog) showingDialogs.iterator().next();
 746  1
         String message = getHelper().getMessageFromJDialog(dialog);
 747  1
         assertEquals("Could not find the correct message from the dialog box", msg, message);
 748  1
         TestHelper.disposeWindow(dialog, this);
 749   
     }
 750   
 
 751   
     /**
 752   
      * Tests that the invokeAndWait() method works.
 753   
      *
 754   
      * @throws InterruptedException  May be thrown from sleep.
 755   
      * @throws InvocationTargetException from invokeAndWait.
 756   
      */
 757  1
     public void testInvokeAndWait() throws InterruptedException, InvocationTargetException {
 758  1
         Toolkit.getDefaultToolkit().getSystemEventQueue().invokeAndWait(new Runnable() {
 759  1
             public void run() {
 760  1
                 try {
 761  1
                     Thread.currentThread().sleep(10);
 762   
                 } catch (Exception e) {
 763   
                   // This should not happen.
 764  0
                   e.printStackTrace();
 765   
                 }
 766   
             }
 767   
         });
 768   
     }
 769   
 
 770   
     /**
 771   
      * Test the TestCase sleep method.
 772   
      */
 773  1
     public void testSleep() {
 774  1
         long tm = System.currentTimeMillis();
 775  1
         sleep(500);
 776  1
         long duration = System.currentTimeMillis() - tm;
 777  1
         assertTrue("Duration not sufficient:", (duration > 500));
 778   
     }
 779   
 
 780   
     /**
 781   
      * Test the pause/resume methods for managing the
 782   
      * event queue.
 783   
      */
 784  1
     public void testAWT() {
 785  1
         assertTrue("isAWTRunning", isAWTRunning());
 786  1
         pauseAWT();
 787  1
         assertTrue("!isAwtRunning", !isAWTRunning());
 788  1
         resumeAWT();
 789  1
         assertTrue("isAWTRunning", isAWTRunning());
 790   
     }
 791   
 
 792   
     /**
 793   
      * This test is more for coverage.
 794   
      * There is no access to forced wait to validate that the
 795   
      * parameter has been established.
 796   
      */
 797  1
     public void testForcedWait() {
 798  1
         setForcedWait(true);
 799  1
         setForcedWait(false);
 800  1
         resetForcedWait();
 801   
     }
 802   
 
 803   
     /**
 804   
      * Test the generation of points when the mouse is moved.
 805   
      */
 806  1
     public void testCalcNextPoint() {
 807  1
       Point last = new Point(0, 0);
 808  1
       Point dest = new Point(30, 30);
 809  1
       while (last.x != dest.x || last.y != dest.y) {
 810  12
         last = TestHelper.calcNextPoint(last, dest, 10);
 811   
       }
 812  1
       dest = new Point(0, 0);
 813  1
       while (last.x != dest.x || last.y != dest.y) {
 814  12
         last = TestHelper.calcNextPoint(last, dest, 10);
 815   
       }
 816  1
       dest = new Point(-50, 50);
 817  1
       while (last.x != dest.x || last.y != dest.y) {
 818  14
         last = TestHelper.calcNextPoint(last, dest, 5);
 819   
       }
 820  1
       dest = new Point(0, 0);
 821  1
       while (last.x != dest.x || last.y != dest.y) {
 822  14
         last = TestHelper.calcNextPoint(last, dest, 5);
 823   
       }
 824   
     }
 825   
 }
 826