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       6253913
  27   @summary   Tests that a Window shown before its owner is focusable.
  28   @author    anton.tarasov@sun.com: area=awt-focus
  29   @run       applet WindowUpdateFocusabilityTest.html
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 import java.applet.Applet;
  35 import java.lang.reflect.*;
  36 
  37 public class WindowUpdateFocusabilityTest extends Applet {
  38     Robot robot;
  39     boolean focusGained = false;
  40     final Object monitor = new Object();
  41     FocusListener listener = new FocusAdapter () {
  42             public void focusGained(FocusEvent e) {
  43                 System.out.println(e.toString());
  44                 synchronized (monitor) {
  45                     focusGained = true;
  46                     monitor.notifyAll();
  47                 }
  48             }
  49         };
  50 
  51     public static void main(String[] args) {
  52         WindowUpdateFocusabilityTest app = new WindowUpdateFocusabilityTest();
  53         app.init();
  54         app.start();
  55     }
  56 
  57     public void init() {
  58         try {
  59             robot = new Robot();
  60         } catch (AWTException e) {
  61             throw new RuntimeException("Error: couldn't create robot");
  62         }
  63         // Create instructions for the user here, as well as set up
  64         // the environment -- set the layout manager, add buttons,
  65         // etc.
  66         this.setLayout (new BorderLayout ());
  67     }
  68 
  69     public void start() {
  70         if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
  71             System.out.println("No testing on Motif.");
  72             return;
  73         }
  74 
  75         test(new Frame("Frame owner"));
  76         Frame dialog_owner = new Frame("dialog's owner");
  77         test(new Dialog(dialog_owner));
  78         test(new Dialog(dialog_owner, Dialog.ModalityType.DOCUMENT_MODAL));
  79         test(new Dialog(dialog_owner, Dialog.ModalityType.APPLICATION_MODAL));
  80         test(new Dialog(dialog_owner, Dialog.ModalityType.TOOLKIT_MODAL));
  81         test(new Dialog((Window) null, Dialog.ModalityType.MODELESS));
  82         test(new Dialog((Window) null, Dialog.ModalityType.DOCUMENT_MODAL));
  83         test(new Dialog((Window) null, Dialog.ModalityType.APPLICATION_MODAL));
  84         test(new Dialog((Window) null, Dialog.ModalityType.TOOLKIT_MODAL));
  85         dialog_owner.dispose();
  86     }
  87 
  88     private void test(final Window owner)
  89     {
  90         Window window0 = new Window(owner); // will not be shown
  91         Window window1 = new Window(window0);
  92         Window window2 = new Window(window1);
  93         Button button1 = new Button("button1");
  94         Button button2 = new Button("button2");
  95         button1.addFocusListener(listener);
  96         button2.addFocusListener(listener);
  97 
  98         owner.setBounds(800, 0, 100, 100);
  99         window1.setBounds(800, 300, 100, 100);
 100         window2.setBounds(800, 150, 100, 100);
 101 
 102         window1.add(button1);
 103         window2.add(button2);
 104 
 105         window2.setVisible(true);
 106         window1.setVisible(true);
 107         EventQueue.invokeLater(new Runnable() {
 108                 public void run() {
 109                     owner.setVisible(true);
 110                 }
 111             });
 112 
 113         try {
 114             EventQueue.invokeAndWait(new Runnable() {
 115                     public void run() {
 116                         // do nothing just wait until previous invokeLater will be executed
 117                     }
 118                 });
 119         } catch (InterruptedException ie) {
 120             throw new RuntimeException(ie);
 121         } catch (InvocationTargetException ite) {
 122             throw new RuntimeException(ite);
 123         }
 124 
 125         robot.delay(1000);
 126 
 127         clickOn(button1);
 128 
 129         if (!isFocusGained()) {
 130             throw new RuntimeException("Test failed: window1 is not focusable!");
 131         }
 132 
 133         focusGained = false;
 134         clickOn(button2);
 135 
 136         if (!isFocusGained()) {
 137             throw new RuntimeException("Test failed: window2 is not focusable!");
 138         }
 139 
 140         System.out.println("Test for " + owner.getName() + " passed.");
 141         owner.dispose();
 142     }
 143 
 144     void clickOn(Component c) {
 145         Point p = c.getLocationOnScreen();
 146         Dimension d = c.getSize();
 147 
 148         System.out.println("Clicking " + c);
 149 
 150         robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 151 
 152         robot.mousePress(InputEvent.BUTTON1_MASK);
 153         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 154         waitForIdle();
 155     }
 156 
 157     void waitForIdle() {
 158         try {
 159             robot.waitForIdle();
 160             robot.delay(50);
 161             EventQueue.invokeAndWait( new Runnable() {
 162                     public void run() {} // Dummy implementation
 163                 });
 164         } catch(InterruptedException ie) {
 165             System.out.println("waitForIdle, non-fatal exception caught:");
 166             ie.printStackTrace();
 167         } catch(InvocationTargetException ite) {
 168             System.out.println("waitForIdle, non-fatal exception caught:");
 169             ite.printStackTrace();
 170         }
 171     }
 172 
 173     boolean isFocusGained() {
 174         synchronized (monitor) {
 175             if (!focusGained) {
 176                 try {
 177                     monitor.wait(3000);
 178                 } catch (InterruptedException e) {
 179                     System.out.println("Interrupted unexpectedly!");
 180                     throw new RuntimeException(e);
 181                 }
 182             }
 183         }
 184         return focusGained;
 185     }
 186 }