1 /* 2 * Copyright (c) 2012, 2014, 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 7124430 27 @summary Tests that SunToolkit.grab API works 28 @author anton.tarasov@oracle.com: area=awt.toolkit 29 @library ../../regtesthelpers 30 @build Util 31 @run main GrabTest 32 */ 33 34 import java.awt.*; 35 import java.awt.event.*; 36 import test.java.awt.regtesthelpers.Util; 37 38 public class GrabTest { 39 private static Frame f; 40 private static Frame f1; 41 private static Frame frame; 42 private static Window w; 43 private static Window window1; 44 private static Window window2; 45 private static Button b; 46 47 private static Robot robot; 48 private static sun.awt.SunToolkit tk; 49 50 static volatile boolean ungrabbed; 51 static volatile boolean buttonPressed; 52 static volatile boolean windowPressed; 53 static volatile boolean framePressed; 54 55 static volatile boolean passed = true; 56 57 public static void main(String[] args) { 58 59 Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { 60 public void eventDispatched(AWTEvent e) { 61 System.out.println(e); 62 if (e instanceof sun.awt.UngrabEvent) { 63 ungrabbed = true; 64 } 65 } 66 }, sun.awt.SunToolkit.GRAB_EVENT_MASK); 67 68 f = new Frame("Frame"); 69 f.setBounds(0, 0, 300, 300); 70 f.addMouseListener(new MouseAdapter() { 71 public void mousePressed(MouseEvent e) { 72 System.out.println(e); 73 framePressed = true; 74 } 75 }); 76 77 f1 = new Frame("OtherFrame"); 78 f1.setBounds(700, 100, 200, 200); 79 80 w = new Window(f); 81 w.setLayout(new FlowLayout()); 82 b = new Button("Press"); 83 b.addActionListener(new ActionListener() { 84 public void actionPerformed(ActionEvent e) { 85 System.out.println(e); 86 buttonPressed = true; 87 } 88 }); 89 w.add(b); 90 w.setBounds(400, 100, 200, 200); 91 w.setBackground(Color.blue); 92 w.addMouseListener(new MouseAdapter() { 93 public void mousePressed(MouseEvent e) { 94 System.out.println(e); 95 windowPressed = true; 96 } 97 }); 98 99 f.setVisible(true); 100 w.setVisible(true); 101 102 frame = new Frame(); 103 window1 = new Window(frame); 104 window1.setBounds(0, 0, 100, 100); 105 window1.setBackground(Color.blue); 106 107 window2 = new Window(window1); 108 window2.setBounds(0, 0, 50, 50); 109 window2.setBackground(Color.green); 110 111 tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit(); 112 113 try { 114 robot = new Robot(); 115 } catch (AWTException ex) { 116 throw new RuntimeException(ex); 117 } 118 119 Util.waitForIdle(robot); 120 121 test(); 122 } 123 124 public static void test() { 125 tk.grab(w); 126 127 // 1. Check that button press doesn't cause ungrab 128 Util.clickOnComp(b, robot); 129 Util.waitForIdle(robot); 130 checkAndThrow(buttonPressed, "Error: Button can not be pressed"); 131 if (ungrabbed) { 132 passed = false; 133 tk.grab(w); 134 System.err.println("Failure: [1] Press inside of Window (on Button) caused ungrab"); 135 } 136 137 // 2. Check that press on the window itself doesn't cause ungrab 138 Util.clickOnComp(w, robot); 139 Util.waitForIdle(robot); 140 checkAndThrow(windowPressed, "Error: Window can't be pressed"); 141 if (ungrabbed) { 142 passed = false; 143 tk.grab(w); 144 System.err.println("Failure: [2] Press inside of Window caused ungrab"); 145 } 146 147 // 3. Check that press on the frame causes ungrab, event must be dispatched 148 Util.clickOnComp(f, robot); 149 Util.waitForIdle(robot); 150 checkAndThrow(framePressed, "Error: Frame can't be pressed"); 151 if (!ungrabbed) { 152 passed = false; 153 System.err.println("Failure: [3] Press inside of Frame didn't cause ungrab"); 154 } 155 ungrabbed = false; 156 tk.grab(w); 157 158 // 4. Check that press on the frame's title causes ungrab 159 Util.clickOnTitle(f, robot); 160 Util.waitForIdle(robot); 161 if (!ungrabbed) { 162 passed = false; 163 System.err.println("Failure: [4] Press inside of Frame's title didn't cause ungrab"); 164 } 165 ungrabbed = false; 166 tk.grab(w); 167 168 169 // 5. Check that press on the other frame's title causes ungrab 170 f1.setVisible(true); 171 Util.waitForIdle(robot); 172 Util.clickOnTitle(f1, robot); 173 if (!ungrabbed) { 174 passed = false; 175 System.err.println("Failure: [5] Press inside of other Frame's title didn't cause ungrab"); 176 } 177 f.requestFocus(); // restore focus 178 Util.waitForIdle(robot); 179 if (!f.hasFocus()) { 180 System.err.println("Error: Frame can't be focused"); 181 } 182 ungrabbed = false; 183 tk.grab(w); 184 185 186 // 6. Check that press on the outside area causes ungrab 187 Point loc = f.getLocationOnScreen(); 188 robot.mouseMove(loc.x + 100, loc.y + f.getSize().height + 10); 189 Util.waitForIdle(robot); 190 robot.mousePress(InputEvent.BUTTON1_MASK); 191 robot.delay(50); 192 robot.mouseRelease(InputEvent.BUTTON1_MASK); 193 Util.waitForIdle(robot); 194 if (!ungrabbed) { 195 passed = false; 196 System.err.println("Failure: [6] Press on the outside area didn't cause ungrab"); 197 } 198 ungrabbed = false; 199 tk.grab(w); 200 201 202 // 7. Check that disposing the window causes ungrab 203 w.dispose(); 204 Util.waitForIdle(robot); 205 if (!ungrabbed) { 206 passed = false; 207 System.err.println("Failure: [7] Window disposal didn't cause ungrab"); 208 } 209 ungrabbed = false; 210 211 212 // 8. Check that mouse click on subwindow does not cause ungrab 213 frame.setVisible(true); 214 window1.setVisible(true); 215 window2.setVisible(true); 216 Util.waitForIdle(robot); 217 218 tk.grab(window1); 219 220 Util.clickOnComp(window2, robot); 221 Util.waitForIdle(robot); 222 223 if (ungrabbed) { 224 passed = false; 225 System.err.println("Failure: [8] Press on the subwindow caused ungrab"); 226 } 227 228 if (passed) { 229 System.out.println("Test passed."); 230 } else { 231 throw new RuntimeException("Test failed."); 232 } 233 } 234 235 public static void checkAndThrow(boolean condition, String msg) { 236 if (!condition) { 237 throw new RuntimeException(msg); 238 } 239 } 240 }