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