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