Path: blob/master/test/jdk/java/awt/Choice/PopdownGeneratesMouseEvents/PopdownGeneratesMouseEvents.java
41152 views
/*1* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 620067027@summary MouseMoved events are triggered by Choice when mouse is moved outside the component, XToolkit28@library ../../regtesthelpers/29@build Util30@run main PopdownGeneratesMouseEvents31*/3233import test.java.awt.regtesthelpers.Util;3435import java.awt.*;36import java.awt.event.*;3738public class PopdownGeneratesMouseEvents extends Frame {39private volatile Robot robot;40private final Choice choice1 = new Choice();4142private volatile MouseMotionHandler mmh;4344public static void main(final String[] args) {45PopdownGeneratesMouseEvents app = new PopdownGeneratesMouseEvents();46app.init();47app.start();48}4950public void init() {51for (int i = 1; i < 10; i++) {52choice1.add("item-0" + i);53}54choice1.setForeground(Color.RED);55choice1.setBackground(Color.RED);56mmh = new MouseMotionHandler();57choice1.addMouseMotionListener(mmh);58Button b1 = new Button("FirstButton");59Button b2 = new Button("SecondButton");60add(b1);61add(choice1);62add(b2);63setLayout (new FlowLayout());64}6566public void start() {67setSize(300, 200);68setLocationRelativeTo(null);69setVisible(true);70validate();71String toolkit = Toolkit.getDefaultToolkit().getClass().getName();7273/*74* Choice should not generate MouseEvents outside of Choice75* Test for XAWT only.76*/77try{78robot = new Robot();79robot.setAutoWaitForIdle(true);80robot.setAutoDelay(50);8182if (toolkit.equals("sun.awt.X11.XToolkit")) {83testMouseMoveOutside();84} else {85System.out.println("This test is for XToolkit only. Now using "86+ toolkit + ". Automatically passed.");87return;88}89} catch (Throwable e) {90throw new RuntimeException("Test failed. Exception thrown: " + e);91}92System.out.println("Passed : Choice should not generate MouseEvents outside of Choice.");93}9495private void testMouseMoveOutside() {96waitForIdle();97Point pt = choice1.getLocationOnScreen();98robot.mouseMove(pt.x + choice1.getWidth() / 2, pt.y + choice1.getHeight() / 2);99waitForIdle();100robot.mousePress(InputEvent.BUTTON1_MASK);101robot.mouseRelease(InputEvent.BUTTON1_MASK);102waitForIdle();103104Color color = robot.getPixelColor(pt.x + choice1.getWidth() / 2,105pt.y + 3 * choice1.getHeight());106if (!color.equals(Color.RED)) {107throw new RuntimeException("Choice wasn't opened with LEFTMOUSE button");108}109110pt = getLocationOnScreen();111robot.mouseMove(pt.x + getWidth() * 2, pt.y + getHeight() * 2);112mmh.testStarted = true;113114int x0 = pt.x + getWidth() * 3 / 2;115int y0 = pt.y + getHeight() * 3 / 2;116int x1 = pt.x + getWidth() * 2;117int y1 = pt.y + getHeight() * 2;118119Util.mouseMove(robot, new Point(x0, y0), new Point(x1, y0));120Util.mouseMove(robot, new Point(x1, y0), new Point(x1, y1));121122waitForIdle();123//close opened choice124robot.keyPress(KeyEvent.VK_ESCAPE);125robot.keyRelease(KeyEvent.VK_ESCAPE);126}127128private void waitForIdle() {129Util.waitForIdle(robot);130robot.delay(500);131}132}133134class MouseMotionHandler extends MouseMotionAdapter {135public volatile boolean testStarted;136public void mouseMoved(MouseEvent ke) {137if (testStarted) {138throw new RuntimeException("Test failed: Choice generated MouseMove events while moving mouse outside of Choice");139}140}141public void mouseDragged(MouseEvent ke) {142}143}144145146