1 /*
   2   test
   3   @bug       6271849
   4   @summary   Tests that component in modal excluded Window which parent is blocked responses to mouse clicks.
   5   @author    anton.tarasov@sun.com: area=awt.focus
   6   @run       applet ModalExcludedWindowClickTest.html
   7 */
   8 
   9 import java.applet.Applet;
  10 import java.awt.*;
  11 import java.awt.event.*;
  12 import java.lang.reflect.*;
  13 
  14 public class ModalExcludedWindowClickTest extends Applet {
  15     Robot robot;
  16     Frame frame = new Frame("Frame");
  17     Window w = new Window(frame);
  18     Dialog d = new Dialog ((Dialog)null, "NullParentDialog", true);
  19     Button button = new Button("Button");
  20     boolean actionPerformed = false;
  21 
  22     public static void main (String args[]) {
  23         ModalExcludedWindowClickTest app = new ModalExcludedWindowClickTest();
  24         app.init();
  25         app.start();
  26     }
  27 
  28     public void init() {
  29         try {
  30             robot = new Robot();
  31         } catch (AWTException e) {
  32             throw new RuntimeException("Error: unable to create robot", e);
  33         }
  34         // Create instructions for the user here, as well as set up
  35         // the environment -- set the layout manager, add buttons,
  36         // etc.
  37         this.setLayout (new BorderLayout ());
  38         Sysout.createDialogWithInstructions(new String[]
  39             {"This is an AUTOMATIC test", "simply wait until it is done"});
  40     }
  41 
  42     public void start() {
  43 
  44         if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
  45             Sysout.println("No testing on MToolkit.");
  46             return;
  47         }
  48 
  49         button.addActionListener(new ActionListener() {
  50                 public void actionPerformed(ActionEvent e) {
  51                     actionPerformed = true;
  52                     Sysout.println(e.paramString());
  53                 }
  54             });
  55 
  56         EventQueue.invokeLater(new Runnable() {
  57                 public void run() {
  58                     frame.setSize(200, 200);
  59                     frame.setVisible(true);
  60 
  61                     w.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  62                     w.add(button);
  63                     w.setSize(200, 200);
  64                     w.setLocation(230, 230);
  65                     w.setVisible(true);
  66 
  67                     d.setSize(200, 200);
  68                     d.setLocation(0, 230);
  69                     d.setVisible(true);
  70 
  71                 }
  72             });
  73 
  74         waitTillShown(d);
  75 
  76         test();
  77     }
  78 
  79     void test() {
  80         clickOn(button);
  81         waitForIdle();
  82         if (!actionPerformed) {
  83             throw new RuntimeException("Test failed!");
  84         }
  85         Sysout.println("Test passed.");
  86     }
  87 
  88     void clickOn(Component c) {
  89         Point p = c.getLocationOnScreen();
  90         Dimension d = c.getSize();
  91 
  92         Sysout.println("Clicking " + c);
  93 
  94         if (c instanceof Frame) {
  95             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
  96         } else {
  97             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
  98         }
  99         robot.mousePress(InputEvent.BUTTON1_MASK);
 100         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 101         waitForIdle();
 102     }
 103     void waitTillShown(Component c) {
 104         while (true) {
 105             try {
 106                 Thread.sleep(100);
 107                 c.getLocationOnScreen();
 108                 break;
 109             } catch (InterruptedException e) {
 110                 throw new RuntimeException(e);
 111             } catch (IllegalComponentStateException e) {}
 112         }
 113     }
 114     void waitForIdle() {
 115         try {
 116             Toolkit.getDefaultToolkit().sync();
 117             sun.awt.SunToolkit.flushPendingEvents();
 118             EventQueue.invokeAndWait( new Runnable() {
 119                     public void run() {} // Dummy implementation
 120                 });
 121         } catch(InterruptedException ie) {
 122             Sysout.println("waitForIdle, non-fatal exception caught:");
 123             ie.printStackTrace();
 124         } catch(InvocationTargetException ite) {
 125             Sysout.println("waitForIdle, non-fatal exception caught:");
 126             ite.printStackTrace();
 127         }
 128 
 129         // wait longer...
 130         robot.delay(200);
 131     }
 132 }
 133 
 134 /****************************************************
 135  Standard Test Machinery
 136  DO NOT modify anything below -- it's a standard
 137   chunk of code whose purpose is to make user
 138   interaction uniform, and thereby make it simpler
 139   to read and understand someone else's test.
 140  ****************************************************/
 141 
 142 /**
 143  This is part of the standard test machinery.
 144  It creates a dialog (with the instructions), and is the interface
 145   for sending text messages to the user.
 146  To print the instructions, send an array of strings to Sysout.createDialog
 147   WithInstructions method.  Put one line of instructions per array entry.
 148  To display a message for the tester to see, simply call Sysout.println
 149   with the string to be displayed.
 150  This mimics System.out.println but works within the test harness as well
 151   as standalone.
 152  */
 153 
 154 class Sysout
 155 {
 156     static TestDialog dialog;
 157 
 158     public static void createDialogWithInstructions( String[] instructions )
 159     {
 160         dialog = new TestDialog( new Frame(), "Instructions" );
 161         dialog.printInstructions( instructions );
 162         dialog.setVisible(true);
 163         println( "Any messages for the tester will display here." );
 164     }
 165 
 166     public static void createDialog( )
 167     {
 168         dialog = new TestDialog( new Frame(), "Instructions" );
 169         String[] defInstr = { "Instructions will appear here. ", "" } ;
 170         dialog.printInstructions( defInstr );
 171         dialog.setVisible(true);
 172         println( "Any messages for the tester will display here." );
 173     }
 174 
 175 
 176     public static void printInstructions( String[] instructions )
 177     {
 178         dialog.printInstructions( instructions );
 179     }
 180 
 181 
 182     public static void println( String messageIn )
 183     {
 184         dialog.displayMessage( messageIn );
 185     }
 186 
 187 }// Sysout  class
 188 
 189 /**
 190   This is part of the standard test machinery.  It provides a place for the
 191    test instructions to be displayed, and a place for interactive messages
 192    to the user to be displayed.
 193   To have the test instructions displayed, see Sysout.
 194   To have a message to the user be displayed, see Sysout.
 195   Do not call anything in this dialog directly.
 196   */
 197 class TestDialog extends Dialog
 198 {
 199 
 200     TextArea instructionsText;
 201     TextArea messageText;
 202     int maxStringLength = 80;
 203 
 204     //DO NOT call this directly, go through Sysout
 205     public TestDialog( Frame frame, String name )
 206     {
 207         super( frame, name );
 208         int scrollBoth = TextArea.SCROLLBARS_BOTH;
 209         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
 210         add( "North", instructionsText );
 211 
 212         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
 213         add("Center", messageText);
 214 
 215         pack();
 216 
 217         setVisible(true);
 218     }// TestDialog()
 219 
 220     //DO NOT call this directly, go through Sysout
 221     public void printInstructions( String[] instructions )
 222     {
 223         //Clear out any current instructions
 224         instructionsText.setText( "" );
 225 
 226         //Go down array of instruction strings
 227 
 228         String printStr, remainingStr;
 229         for( int i=0; i < instructions.length; i++ )
 230         {
 231             //chop up each into pieces maxSringLength long
 232             remainingStr = instructions[ i ];
 233             while( remainingStr.length() > 0 )
 234             {
 235                 //if longer than max then chop off first max chars to print
 236                 if( remainingStr.length() >= maxStringLength )
 237                 {
 238                     //Try to chop on a word boundary
 239                     int posOfSpace = remainingStr.
 240                         lastIndexOf( ' ', maxStringLength - 1 );
 241 
 242                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
 243 
 244                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
 245                     remainingStr = remainingStr.substring( posOfSpace + 1 );
 246                 }
 247                 //else just print
 248                 else
 249                 {
 250                     printStr = remainingStr;
 251                     remainingStr = "";
 252                 }
 253 
 254                 instructionsText.append( printStr + "\n" );
 255 
 256             }// while
 257 
 258         }// for
 259 
 260     }//printInstructions()
 261 
 262     //DO NOT call this directly, go through Sysout
 263     public void displayMessage( String messageIn )
 264     {
 265         messageText.append( messageIn + "\n" );
 266         System.out.println(messageIn);
 267     }
 268 
 269 }// TestDialog  class