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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 @test 28 @bug 7124430 29 @summary Tests that SunToolkit.grab API works 30 @author anton.tarasov@oracle.com: area=awt.toolkit 31 @library ../../regtesthelpers 32 @build Util 33 @run main GrabTest 34 */ 35 36 import java.awt.*; 37 import java.awt.event.*; 38 import test.java.awt.regtesthelpers.Util; 39 40 public class GrabTest { 41 private static Frame f; 42 private static Frame f1; 43 private static Window w; 44 private static Button b; 45 46 private static Robot robot; 47 private static sun.awt.SunToolkit tk; 48 49 static volatile boolean ungrabbed; 50 static volatile boolean buttonPressed; 51 static volatile boolean windowPressed; 52 static volatile boolean framePressed; 53 54 static volatile boolean passed = true; 55 56 public static void main(String[] args) { 57 58 Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() { 59 public void eventDispatched(AWTEvent e) { 60 System.out.println(e); 61 if (e instanceof sun.awt.UngrabEvent) { 62 ungrabbed = true; 63 } 64 } 65 }, sun.awt.SunToolkit.GRAB_EVENT_MASK); 66 67 f = new Frame("Frame"); 68 f.setSize(200, 200); 69 f.addMouseListener(new MouseAdapter() { 70 public void mousePressed(MouseEvent e) { 71 System.out.println(e); 72 framePressed = true; 73 } 74 }); 75 76 f1 = new Frame("OtherFrame"); 77 f1.setBounds(600, 100, 200, 200); 78 79 w = new Window(f); 80 w.setLayout(new FlowLayout()); 81 b = new Button("Press"); 82 b.addActionListener(new ActionListener() { 83 public void actionPerformed(ActionEvent e) { 84 System.out.println(e); 85 buttonPressed = true; 86 } 87 }); 88 w.add(b); 89 w.setBounds(300, 100, 200, 200); 90 w.setBackground(Color.blue); 91 w.addMouseListener(new MouseAdapter() { 92 public void mousePressed(MouseEvent e) { 93 System.out.println(e); 94 windowPressed = true; 95 } 96 }); 97 98 f.setVisible(true); 99 w.setVisible(true); 100 101 tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit(); 102 103 try { 104 robot = new Robot(); 105 } catch (AWTException ex) { 106 throw new RuntimeException(ex); 107 } 108 109 Util.waitForIdle(robot); 110 111 test(); 112 } 113 114 public static void test() { 115 tk.grab(w); 116 117 // 1. Check that button press doesn't cause ungrab 118 Util.clickOnComp(b, robot); 119 Util.waitForIdle(robot); 120 checkAndThrow(buttonPressed, "Error: Button can not be pressed"); 121 if (ungrabbed) { 122 passed = false; 123 tk.grab(w); 124 System.err.println("Failure: [1] Press inside of Window (on Button) caused ungrab"); 125 } 126 127 // 2. Check that press on the window itself doesn't cause ungrab 128 Util.clickOnComp(w, robot); 129 Util.waitForIdle(robot); 130 checkAndThrow(windowPressed, "Error: Window can't be pressed"); 131 if (ungrabbed) { 132 passed = false; 133 tk.grab(w); 134 System.err.println("Failure: [2] Press inside of Window caused ungrab"); 135 } 136 137 // 3. Check that press on the frame causes ungrab, event must be dispatched 138 Util.clickOnComp(f, robot); 139 Util.waitForIdle(robot); 140 checkAndThrow(framePressed, "Error: Frame can't be pressed"); 141 if (!ungrabbed) { 142 passed = false; 143 System.err.println("Failure: [3] Press inside of Frame didn't cause ungrab"); 144 } 145 ungrabbed = false; 146 tk.grab(w); 147 148 // 4. Check that press on the frame's title causes ungrab 149 Util.clickOnTitle(f, robot); 150 Util.waitForIdle(robot); 151 if (!ungrabbed) { 152 passed = false; 153 System.err.println("Failure: [4] Press inside of Frame's title didn't cause ungrab"); 154 } 155 ungrabbed = false; 156 tk.grab(w); 157 158 159 // 5. Check that press on the other frame's title causes ungrab 160 f1.setVisible(true); 161 Util.waitForIdle(robot); 162 Util.clickOnTitle(f1, robot); 163 if (!ungrabbed) { 164 passed = false; 165 System.err.println("Failure: [5] Press inside of other Frame's title didn't cause ungrab"); 166 } 167 f.requestFocus(); // restore focus 168 Util.waitForIdle(robot); 169 if (!f.hasFocus()) { 170 System.err.println("Error: Frame can't be focused"); 171 } 172 ungrabbed = false; 173 tk.grab(w); 174 175 176 // 6. Check that press on the outside area causes ungrab 177 Point loc = f.getLocationOnScreen(); 178 robot.mouseMove(loc.x + 100, loc.y + f.getSize().height + 300); 179 robot.mousePress(InputEvent.BUTTON1_MASK); 180 robot.delay(50); 181 robot.mouseRelease(InputEvent.BUTTON1_MASK); 182 Util.waitForIdle(robot); 183 if (!ungrabbed) { 184 passed = false; 185 System.err.println("Failure: [6] Press on the outside area didn't cause ungrab"); 186 } 187 ungrabbed = false; 188 tk.grab(w); 189 190 191 // 7. Check that disposing the window causes ungrab 192 w.dispose(); 193 Util.waitForIdle(robot); 194 if (!ungrabbed) { 195 passed = false; 196 System.err.println("Failure: [7] Window disposal didn't cause ungrab"); 197 } 198 199 if (passed) { 200 System.out.println("Test passed."); 201 } else { 202 throw new RuntimeException("Test failed."); 203 } 204 } 205 206 public static void checkAndThrow(boolean condition, String msg) { 207 if (!condition) { 208 throw new RuntimeException(msg); 209 } 210 } 211 }