1 /*
   2  * Copyright (c) 2005, 2018, 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     }
  61 
  62     public void start() {
  63 
  64         if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
  65             System.out.println("No testing on MToolkit.");
  66             return;
  67         }
  68 
  69         try {
  70             EventQueue.invokeLater(new Runnable() {
  71                 public void run() {
  72                     frame.setSize(300, 200);
  73                     frame.setVisible(true);
  74 
  75                     excluded.setSize(300, 200);
  76                     excluded.setLocation(0, 400);
  77                     excluded.setModalExclusionType(Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
  78                     excluded.setLayout(new FlowLayout());
  79                     excluded.add(button);
  80                     excluded.setVisible(true);
  81 
  82                     dialog.setSize(200, 100);
  83                     dialog.setLocation(0, 250);
  84                     dialog.setVisible(true);
  85                 }
  86             });
  87         } catch (Exception e) {
  88             e.printStackTrace();
  89         }
  90 
  91         waitTillShown(dialog);
  92         clickOn(button);
  93         if (frame == KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow()) {
  94             throw new RuntimeException("Test failed!");
  95         }
  96         if (excluded == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow()) {
  97             throw new RuntimeException("Test failed!");
  98         }
  99         if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
 100             throw new RuntimeException("Test failed!");
 101         }
 102         System.out.println("Test passed.");
 103     }
 104 
 105     void clickOn(Component c) {
 106         Point p = c.getLocationOnScreen();
 107         Dimension d = c.getSize();
 108 
 109         System.out.println("Clicking " + c);
 110 
 111         if (c instanceof Frame) {
 112             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
 113         } else {
 114             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 115         }
 116         robot.mousePress(InputEvent.BUTTON1_MASK);
 117         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 118         waitForIdle();
 119     }
 120 
 121     void waitTillShown(Component c) {
 122         while (true) {
 123             try {
 124                 Thread.sleep(100);
 125                 c.getLocationOnScreen();
 126                 break;
 127             } catch (InterruptedException e) {
 128                 throw new RuntimeException(e);
 129             } catch (IllegalComponentStateException e) {}
 130         }
 131     }
 132     void waitForIdle() {
 133         try {
 134             robot.waitForIdle();
 135             EventQueue.invokeAndWait( new Runnable() {
 136                     public void run() {} // Dummy implementation
 137                 });
 138         } catch(InterruptedException ie) {
 139             System.out.println("waitForIdle, non-fatal exception caught:");
 140             ie.printStackTrace();
 141         } catch(InvocationTargetException ite) {
 142             System.out.println("waitForIdle, non-fatal exception caught:");
 143             ite.printStackTrace();
 144         }
 145 
 146         // wait longer...
 147         robot.delay(200);
 148     }
 149 }
--- EOF ---