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