1 /*
   2  * Copyright (c) 2012, 2020, 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, 300, 300);
  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, 300, 300);
  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.setSize(200, 200);
 107         window1.setLocationRelativeTo(null);
 108         window1.setBackground(Color.blue);
 109 
 110         window2 = new Window(window1);
 111         window2.setSize(100, 100);
 112         window2.setLocationRelativeTo(null);
 113         window2.setBackground(Color.green);
 114 
 115         tk = (sun.awt.SunToolkit)Toolkit.getDefaultToolkit();
 116 
 117         try {
 118             robot = new Robot();
 119         } catch (AWTException ex) {
 120             throw new RuntimeException(ex);
 121         }
 122 
 123         Util.waitForIdle(robot);
 124 
 125         test();
 126     }
 127 
 128     public static void test() {
 129         tk.grab(w);
 130 
 131         // 1. Check that button press doesn't cause ungrab
 132         Util.clickOnComp(b, robot);
 133         Util.waitForIdle(robot);
 134         checkAndThrow(buttonPressed, "Error: Button can not be pressed");
 135         if (ungrabbed) {
 136             passed = false;
 137             tk.grab(w);
 138             System.err.println("Failure: [1] Press inside of Window (on Button) caused ungrab");
 139         }
 140 
 141         // 2. Check that press on the window itself doesn't cause ungrab
 142         Util.clickOnComp(w, robot);
 143         Util.waitForIdle(robot);
 144         checkAndThrow(windowPressed, "Error: Window can't be pressed");
 145         if (ungrabbed) {
 146             passed = false;
 147             tk.grab(w);
 148             System.err.println("Failure: [2] Press inside of Window caused ungrab");
 149         }
 150 
 151         // 3. Check that press on the frame causes ungrab, event must be dispatched
 152         Util.clickOnComp(f, robot);
 153         Util.waitForIdle(robot);
 154         checkAndThrow(framePressed, "Error: Frame can't be pressed");
 155         if (!ungrabbed) {
 156             passed = false;
 157             System.err.println("Failure: [3] Press inside of Frame didn't cause ungrab");
 158         }
 159         ungrabbed = false;
 160         tk.grab(w);
 161 
 162         // 4. Check that press on the frame's title causes ungrab
 163         Util.clickOnTitle(f, robot);
 164         Util.waitForIdle(robot);
 165         if (!ungrabbed) {
 166             passed = false;
 167             System.err.println("Failure: [4] Press inside of Frame's title didn't cause ungrab");
 168         }
 169         ungrabbed = false;
 170         tk.grab(w);
 171 
 172 
 173         // 5. Check that press on the other frame's title causes ungrab
 174         f1.setVisible(true);
 175         Util.waitForIdle(robot);
 176         Util.clickOnTitle(f1, robot);
 177         if (!ungrabbed) {
 178             passed = false;
 179             System.err.println("Failure: [5] Press inside of other Frame's title didn't cause ungrab");
 180         }
 181         f.requestFocus(); // restore focus
 182         Util.waitForIdle(robot);
 183         if (!f.hasFocus()) {
 184             System.err.println("Error: Frame can't be focused");
 185         }
 186         ungrabbed = false;
 187         tk.grab(w);
 188 
 189 
 190         // 6. Check that press on the outside area causes ungrab
 191         Point loc = f.getLocationOnScreen();
 192         robot.mouseMove(loc.x + 100, loc.y + f.getSize().height + 10);
 193         Util.waitForIdle(robot);
 194         robot.mousePress(InputEvent.BUTTON1_MASK);
 195         robot.delay(50);
 196         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 197         Util.waitForIdle(robot);
 198         if (!ungrabbed) {
 199             passed = false;
 200             System.err.println("Failure: [6] Press on the outside area didn't cause ungrab");
 201         }
 202         ungrabbed = false;
 203         tk.grab(w);
 204 
 205 
 206         // 7. Check that disposing the window causes ungrab
 207         w.dispose();
 208         Util.waitForIdle(robot);
 209         if (!ungrabbed) {
 210             passed = false;
 211             System.err.println("Failure: [7] Window disposal didn't cause ungrab");
 212         }
 213         ungrabbed = false;
 214 
 215 
 216         // 8. Check that mouse click on subwindow does not cause ungrab
 217         frame.setVisible(true);
 218         window1.setVisible(true);
 219         window2.setVisible(true);
 220         Util.waitForIdle(robot);
 221 
 222         tk.grab(window1);
 223 
 224         Util.clickOnComp(window2, robot);
 225         Util.waitForIdle(robot);
 226 
 227         if (ungrabbed) {
 228             passed = false;
 229             System.err.println("Failure: [8] Press on the subwindow caused ungrab");
 230         }
 231 
 232         if (passed) {
 233             System.out.println("Test passed.");
 234         } else {
 235             throw new RuntimeException("Test failed.");
 236         }
 237     }
 238 
 239     public static void checkAndThrow(boolean condition, String msg) {
 240         if (!condition) {
 241             throw new RuntimeException(msg);
 242         }
 243     }
 244 }
--- EOF ---