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 6253913 28 @summary Tests that a Window shown before its owner is focusable. 29 @modules java.desktop/sun.awt 30 @run main WindowUpdateFocusabilityTest 31 */ 32 33 import java.awt.*; 34 import java.awt.event.*; 35 import java.lang.reflect.*; 36 37 public class WindowUpdateFocusabilityTest { 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 } 64 65 public void start() { 66 if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) { 67 System.out.println("No testing on Motif."); 68 return; 69 } 70 71 test(new Frame("Frame owner")); 72 Frame dialog_owner = new Frame("dialog's owner"); 73 test(new Dialog(dialog_owner)); 74 test(new Dialog(dialog_owner, Dialog.ModalityType.DOCUMENT_MODAL)); 75 test(new Dialog(dialog_owner, Dialog.ModalityType.APPLICATION_MODAL)); 76 test(new Dialog(dialog_owner, Dialog.ModalityType.TOOLKIT_MODAL)); 77 test(new Dialog((Window) null, Dialog.ModalityType.MODELESS)); 78 test(new Dialog((Window) null, Dialog.ModalityType.DOCUMENT_MODAL)); 79 test(new Dialog((Window) null, Dialog.ModalityType.APPLICATION_MODAL)); 80 test(new Dialog((Window) null, Dialog.ModalityType.TOOLKIT_MODAL)); 81 dialog_owner.dispose(); 82 } 83 84 private void test(final Window owner) 85 { 86 Window window0 = new Window(owner); // will not be shown 87 Window window1 = new Window(window0); 88 Window window2 = new Window(window1); 89 Button button1 = new Button("button1"); 90 Button button2 = new Button("button2"); 91 button1.addFocusListener(listener); 92 button2.addFocusListener(listener); 93 94 owner.setBounds(800, 0, 100, 100); 95 window1.setBounds(800, 300, 100, 100); 96 window2.setBounds(800, 150, 100, 100); 97 98 window1.add(button1); 99 window2.add(button2); 100 101 window2.setVisible(true); 102 window1.setVisible(true); 103 EventQueue.invokeLater(new Runnable() { 104 public void run() { 105 owner.setVisible(true); 106 } 107 }); 108 109 try { 110 EventQueue.invokeAndWait(new Runnable() { 111 public void run() { 112 // do nothing just wait until previous invokeLater will be executed 113 } 114 }); 115 } catch (InterruptedException ie) { 116 throw new RuntimeException(ie); 117 } catch (InvocationTargetException ite) { 118 throw new RuntimeException(ite); 119 } 120 121 robot.delay(1000); 122 123 clickOn(button1); 124 125 if (!isFocusGained()) { 126 throw new RuntimeException("Test failed: window1 is not focusable!"); 127 } 128 129 focusGained = false; 130 clickOn(button2); 131 132 if (!isFocusGained()) { 133 throw new RuntimeException("Test failed: window2 is not focusable!"); 134 } 135 136 System.out.println("Test for " + owner.getName() + " passed."); 137 owner.dispose(); 138 } 139 140 void clickOn(Component c) { 141 Point p = c.getLocationOnScreen(); 142 Dimension d = c.getSize(); 143 144 System.out.println("Clicking " + c); 145 146 robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2)); 147 148 robot.mousePress(InputEvent.BUTTON1_MASK); 149 robot.mouseRelease(InputEvent.BUTTON1_MASK); 150 waitForIdle(); 151 } 152 153 void waitForIdle() { 154 try { 155 robot.waitForIdle(); 156 robot.delay(50); 157 EventQueue.invokeAndWait( new Runnable() { 158 public void run() {} // Dummy implementation 159 }); 160 } catch(InterruptedException ie) { 161 System.out.println("waitForIdle, non-fatal exception caught:"); 162 ie.printStackTrace(); 163 } catch(InvocationTargetException ite) { 164 System.out.println("waitForIdle, non-fatal exception caught:"); 165 ite.printStackTrace(); 166 } 167 } 168 169 boolean isFocusGained() { 170 synchronized (monitor) { 171 if (!focusGained) { 172 try { 173 monitor.wait(3000); 174 } catch (InterruptedException e) { 175 System.out.println("Interrupted unexpectedly!"); 176 throw new RuntimeException(e); 177 } 178 } 179 } 180 return focusGained; 181 } 182 }