Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Choice/PopdownGeneratesMouseEvents/PopdownGeneratesMouseEvents.java
47376 views
/*1* Copyright (c) 2011, 2014, 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/*24test25@bug 620067026@summary MouseMoved events are triggered by Choice when mouse is moved outside the component, XToolkit27@library ../../regtesthelpers/28@author andrei.dmitriev area=choice29@build Util30@run applet PopdownGeneratesMouseEvents.html31*/3233import test.java.awt.regtesthelpers.Util;3435import java.applet.Applet;36import java.awt.*;37import java.awt.event.*;3839public class PopdownGeneratesMouseEvents extends Applet {40private volatile Robot robot;41private final Choice choice1 = new Choice();4243private volatile MouseMotionHandler mmh;4445public void init() {46for (int i = 1; i < 10; i++) {47choice1.add("item-0" + i);48}49choice1.setForeground(Color.RED);50choice1.setBackground(Color.RED);51mmh = new MouseMotionHandler();52choice1.addMouseMotionListener(mmh);53Button b1 = new Button("FirstButton");54Button b2 = new Button("SecondButton");55add(b1);56add(choice1);57add(b2);58setLayout (new FlowLayout());59}6061public void start() {62setSize(300, 200);63setVisible(true);64validate();65String toolkit = Toolkit.getDefaultToolkit().getClass().getName();6667/*68* Choice should not generate MouseEvents outside of Choice69* Test for XAWT only.70*/71try{72robot = new Robot();73robot.setAutoWaitForIdle(true);74robot.setAutoDelay(50);7576if (toolkit.equals("sun.awt.X11.XToolkit")) {77testMouseMoveOutside();78} else {79System.out.println("This test is for XToolkit only. Now using "80+ toolkit + ". Automatically passed.");81return;82}83} catch (Throwable e) {84throw new RuntimeException("Test failed. Exception thrown: " + e);85}86System.out.println("Passed : Choice should not generate MouseEvents outside of Choice.");87}8889private void testMouseMoveOutside() {90waitForIdle();91Point pt = choice1.getLocationOnScreen();92robot.mouseMove(pt.x + choice1.getWidth() / 2, pt.y + choice1.getHeight() / 2);93waitForIdle();94robot.mousePress(InputEvent.BUTTON1_MASK);95robot.mouseRelease(InputEvent.BUTTON1_MASK);96waitForIdle();9798Color color = robot.getPixelColor(pt.x + choice1.getWidth() / 2,99pt.y + 3 * choice1.getHeight());100if (!color.equals(Color.RED)) {101throw new RuntimeException("Choice wasn't opened with LEFTMOUSE button");102}103104pt = getLocationOnScreen();105robot.mouseMove(pt.x + getWidth() * 2, pt.y + getHeight() * 2);106mmh.testStarted = true;107108int x0 = pt.x + getWidth() * 3 / 2;109int y0 = pt.y + getHeight() * 3 / 2;110int x1 = pt.x + getWidth() * 2;111int y1 = pt.y + getHeight() * 2;112113Util.mouseMove(robot, new Point(x0, y0), new Point(x1, y0));114Util.mouseMove(robot, new Point(x1, y0), new Point(x1, y1));115116waitForIdle();117//close opened choice118robot.keyPress(KeyEvent.VK_ESCAPE);119robot.keyRelease(KeyEvent.VK_ESCAPE);120}121122private void waitForIdle() {123Util.waitForIdle(robot);124robot.delay(500);125}126}127128class MouseMotionHandler extends MouseMotionAdapter {129public volatile boolean testStarted;130public void mouseMoved(MouseEvent ke) {131if (testStarted) {132throw new RuntimeException("Test failed: Choice generated MouseMove events while moving mouse outside of Choice");133}134}135public void mouseDragged(MouseEvent ke) {136}137}138139140