1 /* 2 * Copyright (c) 2012, 2013, 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 6385277 28 * @summary Tests that override redirect window gets activated on click. 29 * @author anton.tarasov@sun.com: area=awt.focus 30 * @library ../../regtesthelpers 31 * @build Util 32 * @run main SimpleWindowActivationTest 33 */ 34 import java.awt.*; 35 import java.awt.event.*; 36 import java.util.concurrent.Callable; 37 import javax.swing.SwingUtilities; 38 import sun.awt.SunToolkit; 39 import test.java.awt.regtesthelpers.Util; 40 41 public class SimpleWindowActivationTest { 42 43 private static Frame frame; 44 private static Window window; 45 private static Button fbutton; 46 private static Button wbutton; 47 private static Label label; 48 private static Robot robot; 49 private static SunToolkit toolkit; 50 51 public static void main(String[] args) throws Exception { 52 53 if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) { 54 System.out.println("No testing on Motif. Test passed."); 55 return; 56 } 57 58 toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 59 robot = new Robot(); 60 robot.setAutoDelay(50); 61 62 Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { 63 64 public void eventDispatched(AWTEvent e) { 65 System.out.println(e); 66 } 67 }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK); 68 69 createAndShowWindow(); 70 toolkit.realSync(); 71 72 createAndShowFrame(); 73 toolkit.realSync(); 74 75 // click on Frame 76 clickOn(getClickPoint(frame)); 77 78 if (!frame.isFocused()) { 79 throw new RuntimeException("Error: a frame couldn't be focused by click."); 80 } 81 82 //click on Label in Window 83 clickOn(getClickPoint(label)); 84 85 if (!window.isFocused()) { 86 throw new RuntimeException("Test failed: the window couldn't be activated by click!"); 87 } 88 89 // bring focus back to the frame 90 clickOn(getClickPoint(fbutton)); 91 92 if (!frame.isFocused()) { 93 throw new RuntimeException("Error: a frame couldn't be focused by click."); 94 } 95 96 // Test 2. Verifies that clicking on a component of unfocusable Window 97 // won't activate it. 98 99 window.setFocusableWindowState(false); 100 toolkit.realSync(); 101 102 103 clickOn(getClickPoint(label)); 104 105 if (window.isFocused()) { 106 throw new RuntimeException("Test failed: unfocusable window got activated by click!"); 107 } 108 System.out.println("Test passed."); 109 110 } 111 112 private static void createAndShowWindow() { 113 114 frame = new Frame("Test Frame"); 115 window = new Window(frame); 116 wbutton = new Button("wbutton"); 117 label = new Label("label"); 118 119 window.setBounds(800, 200, 300, 100); 120 window.setLayout(new FlowLayout()); 121 window.add(wbutton); 122 window.add(label); 123 window.setVisible(true); 124 125 } 126 127 private static void createAndShowFrame() { 128 fbutton = new Button("fbutton"); 129 130 frame.setBounds(800, 0, 300, 100); 131 frame.setLayout(new FlowLayout()); 132 frame.add(fbutton); 133 frame.setVisible(true); 134 135 } 136 137 static void clickOn(Point point) { 138 139 robot.mouseMove(point.x, point.y); 140 141 robot.mousePress(InputEvent.BUTTON1_MASK); 142 robot.mouseRelease(InputEvent.BUTTON1_MASK); 143 144 toolkit.realSync(); 145 } 146 147 static Point getClickPoint(Component c) { 148 Point p = c.getLocationOnScreen(); 149 Dimension d = c.getSize(); 150 return new Point(p.x + (int) (d.getWidth() / 2), p.y + (int) (d.getHeight() / 2)); 151 } 152 153 static Point getClickPoint(Frame frame) { 154 Point p = frame.getLocationOnScreen(); 155 Dimension d = frame.getSize(); 156 return new Point(p.x + (int) (d.getWidth() / 2), p.y + (frame.getInsets().top / 2)); 157 } 158 }