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   @key headful
  27   @bug        6272324
  28   @summary    Modal excluded Window which decorated parent is blocked should be non-focusable.
  29   @modules java.desktop/sun.awt
  30   @run        main NonFocusableBlockedOwnerTest
  31 */
  32 

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