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