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