< prev index next >

test/java/awt/Mouse/EnterExitEvents/DragWindowOutOfFrameTest.java

Print this page


   1 /*
   2  * Copyright (c) 2005, 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  * @key headful
  27  * @bug 7154048
  28  * @summary Window created under a mouse does not receive mouse enter event.
  29  *     Mouse Entered/Exited events should be generated during dragging the window
  30  *     out of the frame and to the frame.
  31  * @library ../../regtesthelpers
  32  * @build Util
  33  * @author alexandr.scherbatiy area=awt.event
  34  * @run main DragWindowOutOfFrameTest
  35  */
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 import javax.swing.*;
  39 
  40 import java.util.concurrent.*;
  41 











  42 import test.java.awt.regtesthelpers.Util;
  43 
  44 public class DragWindowOutOfFrameTest {
  45 
  46     private static volatile int dragWindowMouseEnteredCount = 0;
  47     private static volatile int dragWindowMouseExitedCount = 0;
  48     private static volatile int dragWindowMouseReleasedCount = 0;
  49     private static volatile int buttonMouseEnteredCount = 0;
  50     private static volatile int buttonMouseExitedCount = 0;
  51     private static volatile int labelMouseEnteredCount = 0;
  52     private static volatile int labelMouseExitedCount = 0;
  53     private static volatile int labelMouseReleasedCount = 0;
  54     private static MyDragWindow dragWindow;
  55     private static JLabel label;
  56     private static JButton button;

  57 
  58     public static void main(String[] args) throws Exception {
  59 
  60         Robot robot = new Robot();
  61         robot.setAutoDelay(50);
  62 
  63         SwingUtilities.invokeAndWait(new Runnable() {
  64 
  65             @Override
  66             public void run() {
  67                 createAndShowGUI();
  68             }
  69         });
  70 
  71         robot.waitForIdle();
  72 
  73         Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {
  74 
  75             @Override
  76             public Point call() throws Exception {
  77                 return getCenterPoint(label);
  78             }
  79         });
  80 






  81 
  82         robot.mouseMove(pointToClick.x, pointToClick.y);
  83         robot.mousePress(InputEvent.BUTTON1_MASK);
  84         robot.waitForIdle();
  85 
  86         if (dragWindowMouseEnteredCount != 1 && dragWindowMouseExitedCount != 0) {
  87             throw new RuntimeException(
  88                     "Wrong number mouse Entered/Exited events on Drag Window!");
  89         }
  90 
  91         Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {
  92 
  93             @Override
  94             public Point call() throws Exception {
  95                 label.addMouseListener(new LabelMouseListener());
  96                 button.addMouseListener(new ButtonMouseListener());
  97                 return getCenterPoint(button);
  98             }
  99         });
 100 
 101         robot.mouseMove(450, pointToClick.y);
 102         robot.waitForIdle();
 103 


 110         robot.waitForIdle();
 111 
 112         if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {
 113             throw new RuntimeException(
 114                     "Wrong number Mouse Entered/Exited events on label!");
 115         }
 116 
 117         if (buttonMouseEnteredCount != 0 && buttonMouseExitedCount != 0) {
 118             throw new RuntimeException(
 119                     "Wrong number Mouse Entered/Exited events on button!");
 120         }
 121 
 122         robot.mouseMove(pointToDrag.y, pointToDrag.y);
 123         robot.waitForIdle();
 124 
 125         if (buttonMouseEnteredCount != 1 && buttonMouseExitedCount != 0) {
 126             throw new RuntimeException(
 127                     "Wrong number Mouse Entered/Exited events on button!");
 128         }
 129 
 130         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 131         robot.waitForIdle();
 132 
 133         if (labelMouseReleasedCount != 1) {
 134             throw new RuntimeException("No MouseReleased event on label!");
 135         }


 136     }
 137 
 138     private static Point getCenterPoint(Component comp) {
 139         Point p = comp.getLocationOnScreen();
 140         Rectangle rect = comp.getBounds();
 141         return new Point(p.x + rect.width / 2, p.y + rect.height / 2);
 142     }
 143 
 144     private static void createAndShowGUI() {
 145 
 146         JFrame frame = new JFrame("Main Frame");
 147         frame.setLocation(100, 100);
 148         frame.setSize(300, 200);
 149         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 150 
 151         label = new JLabel("Label");
 152 
 153         DragWindowCreationMouseListener listener = new DragWindowCreationMouseListener(frame);
 154         label.addMouseListener(listener);
 155         label.addMouseMotionListener(listener);
 156 
 157         button = new JButton("Button");
 158         Panel panel = new Panel(new BorderLayout());
 159 
 160         panel.add(label, BorderLayout.NORTH);
 161         panel.add(button, BorderLayout.CENTER);
 162 
 163         frame.getContentPane().add(panel);
 164         frame.setVisible(true);
 165 
 166     }


   1 /*
   2  * Copyright (c) 2005, 2017, 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 7154048 8177326
  28  * @summary Window created under a mouse does not receive mouse enter event.
  29  *     Mouse Entered/Exited events should be generated during dragging the window
  30  *     out of the frame and to the frame.
  31  * @library ../../regtesthelpers
  32  * @build Util
  33  * @author alexandr.scherbatiy area=awt.event
  34  * @run main DragWindowOutOfFrameTest
  35  */
  36 import java.awt.Robot;
  37 import java.awt.Point;
  38 import java.awt.Component;
  39 import java.awt.Rectangle;
  40 import java.awt.Window;
  41 import java.awt.BorderLayout;
  42 import java.awt.Panel;
  43 import java.awt.event.InputEvent;
  44 import java.awt.event.MouseEvent;
  45 import java.awt.event.MouseAdapter;
  46 import javax.swing.JFrame;
  47 import javax.swing.JPanel;
  48 import javax.swing.JButton;
  49 import javax.swing.JLabel;
  50 import javax.swing.SwingUtilities;
  51 import java.util.concurrent.Callable;
  52 import java.util.Random;
  53 import test.java.awt.regtesthelpers.Util;
  54 
  55 public class DragWindowOutOfFrameTest {
  56 
  57     private static volatile int dragWindowMouseEnteredCount = 0;
  58     private static volatile int dragWindowMouseExitedCount = 0;
  59     private static volatile int dragWindowMouseReleasedCount = 0;
  60     private static volatile int buttonMouseEnteredCount = 0;
  61     private static volatile int buttonMouseExitedCount = 0;
  62     private static volatile int labelMouseEnteredCount = 0;
  63     private static volatile int labelMouseExitedCount = 0;
  64     private static volatile int labelMouseReleasedCount = 0;
  65     private static MyDragWindow dragWindow;
  66     private static JLabel label;
  67     private static JButton button;
  68     private static JFrame frame;
  69 
  70     public static void main(String[] args) throws Exception {
  71 
  72         Robot robot = new Robot();
  73         robot.setAutoDelay(50);
  74 
  75         SwingUtilities.invokeAndWait(new Runnable() {
  76 
  77             @Override
  78             public void run() {
  79                 createAndShowGUI();
  80             }
  81         });
  82 
  83         robot.waitForIdle();
  84 
  85         Point pointToClick = Util.invokeOnEDT(new Callable<Point>() {
  86 
  87             @Override
  88             public Point call() throws Exception {
  89                 return getCenterPoint(label);
  90             }
  91         });
  92 
  93         /* The mouse is moved at the beginning of the test to make sure the initial mouse move 
  94            on the label always happens.
  95         */
  96         robot.mouseMove(pointToClick.x, pointToClick.y + 10);
  97         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  98         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  99 
 100         robot.mouseMove(pointToClick.x, pointToClick.y);
 101         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 102         robot.waitForIdle();
 103 
 104         if (dragWindowMouseEnteredCount != 1 && dragWindowMouseExitedCount != 0) {
 105             throw new RuntimeException(
 106                     "Wrong number mouse Entered/Exited events on Drag Window!");
 107         }
 108 
 109         Point pointToDrag = Util.invokeOnEDT(new Callable<Point>() {
 110 
 111             @Override
 112             public Point call() throws Exception {
 113                 label.addMouseListener(new LabelMouseListener());
 114                 button.addMouseListener(new ButtonMouseListener());
 115                 return getCenterPoint(button);
 116             }
 117         });
 118 
 119         robot.mouseMove(450, pointToClick.y);
 120         robot.waitForIdle();
 121 


 128         robot.waitForIdle();
 129 
 130         if (labelMouseEnteredCount != 0 && labelMouseExitedCount != 1) {
 131             throw new RuntimeException(
 132                     "Wrong number Mouse Entered/Exited events on label!");
 133         }
 134 
 135         if (buttonMouseEnteredCount != 0 && buttonMouseExitedCount != 0) {
 136             throw new RuntimeException(
 137                     "Wrong number Mouse Entered/Exited events on button!");
 138         }
 139 
 140         robot.mouseMove(pointToDrag.y, pointToDrag.y);
 141         robot.waitForIdle();
 142 
 143         if (buttonMouseEnteredCount != 1 && buttonMouseExitedCount != 0) {
 144             throw new RuntimeException(
 145                     "Wrong number Mouse Entered/Exited events on button!");
 146         }
 147 
 148         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 149         robot.waitForIdle();
 150 
 151         if (labelMouseReleasedCount != 1) {
 152             throw new RuntimeException("No MouseReleased event on label!");
 153         }
 154 
 155         frame.dispose();
 156     }
 157 
 158     private static Point getCenterPoint(Component comp) {
 159         Point p = comp.getLocationOnScreen();
 160         Rectangle rect = comp.getBounds();
 161         return new Point(p.x + rect.width / 2, p.y + rect.height / 2);
 162     }
 163 
 164     private static void createAndShowGUI() {
 165 
 166         frame = new JFrame("Main Frame");
 167         frame.setLocation(100, 100);
 168         frame.setSize(300, 200);
 169         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 170 
 171         label = new JLabel("Label");
 172 
 173         DragWindowCreationMouseListener listener = new DragWindowCreationMouseListener(frame);
 174         label.addMouseListener(listener);
 175         label.addMouseMotionListener(listener);
 176 
 177         button = new JButton("Button");
 178         Panel panel = new Panel(new BorderLayout());
 179 
 180         panel.add(label, BorderLayout.NORTH);
 181         panel.add(button, BorderLayout.CENTER);
 182 
 183         frame.getContentPane().add(panel);
 184         frame.setVisible(true);
 185 
 186     }


< prev index next >