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