1 /*
   2  * Copyright (c) 2011, 2018, 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 6200670
  28   @summary MouseMoved events are triggered by Choice when mouse is moved outside the component, XToolkit
  29   @library ../../regtesthelpers/
  30   @build Util
  31   @run main PopdownGeneratesMouseEvents
  32 */
  33 
  34 import test.java.awt.regtesthelpers.Util;
  35 
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 
  39 public class PopdownGeneratesMouseEvents extends Frame {
  40     private volatile Robot robot;
  41     private final Choice choice1 = new Choice();
  42 
  43     private volatile MouseMotionHandler mmh;
  44 
  45     public static void main(final String[] args) {
  46         PopdownGeneratesMouseEvents app = new PopdownGeneratesMouseEvents();
  47         app.init();
  48         app.start();
  49     }
  50 
  51     public void init() {
  52         for (int i = 1; i < 10; i++) {
  53             choice1.add("item-0" + i);
  54         }
  55         choice1.setForeground(Color.RED);
  56         choice1.setBackground(Color.RED);
  57         mmh = new MouseMotionHandler();
  58         choice1.addMouseMotionListener(mmh);
  59         Button b1 = new Button("FirstButton");
  60         Button b2 = new Button("SecondButton");
  61         add(b1);
  62         add(choice1);
  63         add(b2);
  64         setLayout (new FlowLayout());
  65     }
  66 
  67     public void start() {
  68         setSize(300, 200);
  69         setLocationRelativeTo(null);
  70         setVisible(true);
  71         validate();
  72         String toolkit = Toolkit.getDefaultToolkit().getClass().getName();
  73 
  74         /*
  75          * Choice should not generate MouseEvents outside of Choice
  76          * Test for XAWT only.
  77          */
  78         try{
  79             robot = new Robot();
  80             robot.setAutoWaitForIdle(true);
  81             robot.setAutoDelay(50);
  82 
  83             if (toolkit.equals("sun.awt.X11.XToolkit")) {
  84                 testMouseMoveOutside();
  85             } else {
  86                 System.out.println("This test is for XToolkit only. Now using "
  87                                         + toolkit + ". Automatically passed.");
  88                 return;
  89             }
  90         } catch (Throwable e) {
  91             throw new RuntimeException("Test failed. Exception thrown: " + e);
  92         }
  93         System.out.println("Passed : Choice should not generate MouseEvents outside of Choice.");
  94     }
  95 
  96     private void testMouseMoveOutside() {
  97         waitForIdle();
  98         Point pt = choice1.getLocationOnScreen();
  99         robot.mouseMove(pt.x + choice1.getWidth() / 2, pt.y + choice1.getHeight() / 2);
 100         waitForIdle();
 101         robot.mousePress(InputEvent.BUTTON1_MASK);
 102         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 103         waitForIdle();
 104 
 105         Color color = robot.getPixelColor(pt.x + choice1.getWidth() / 2,
 106                                           pt.y + 3 * choice1.getHeight());
 107         if (!color.equals(Color.RED)) {
 108             throw new RuntimeException("Choice wasn't opened with LEFTMOUSE button");
 109         }
 110 
 111         pt = getLocationOnScreen();
 112         robot.mouseMove(pt.x + getWidth() * 2, pt.y + getHeight() * 2);
 113         mmh.testStarted = true;
 114 
 115         int x0 = pt.x + getWidth() * 3 / 2;
 116         int y0 = pt.y + getHeight() * 3 / 2;
 117         int x1 = pt.x + getWidth() * 2;
 118         int y1 = pt.y + getHeight() * 2;
 119 
 120         Util.mouseMove(robot, new Point(x0, y0), new Point(x1, y0));
 121         Util.mouseMove(robot, new Point(x1, y0), new Point(x1, y1));
 122 
 123         waitForIdle();
 124         //close opened choice
 125         robot.keyPress(KeyEvent.VK_ESCAPE);
 126         robot.keyRelease(KeyEvent.VK_ESCAPE);
 127     }
 128 
 129     private void waitForIdle() {
 130         Util.waitForIdle(robot);
 131         robot.delay(500);
 132     }
 133 }
 134 
 135 class MouseMotionHandler extends MouseMotionAdapter {
 136     public volatile boolean testStarted;
 137     public void mouseMoved(MouseEvent ke) {
 138         if (testStarted) {
 139             throw new RuntimeException("Test failed: Choice generated MouseMove events while moving mouse outside of Choice");
 140         }
 141     }
 142     public void mouseDragged(MouseEvent ke) {
 143     }
 144 }