Path: blob/master/test/jdk/java/awt/Choice/ChoiceMouseWheelTest/ChoiceMouseWheelTest.java
41152 views
/*1* Copyright (c) 2011, 2016, 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 705093527@summary closed/java/awt/Choice/WheelEventsConsumed/WheelEventsConsumed.html fails on win3228@library ../../regtesthelpers29@author Oleg Pekhovskiy: area=awt-choice30@build Util31@run main ChoiceMouseWheelTest32*/3334import test.java.awt.regtesthelpers.Util;3536import java.awt.*;37import java.awt.event.*;3839public class ChoiceMouseWheelTest extends Frame {4041private volatile boolean itemChanged = false;42private volatile boolean wheelMoved = false;43private volatile boolean frameExited = false;4445public static void main(String[] args) {46new ChoiceMouseWheelTest();47}4849ChoiceMouseWheelTest() {50super("ChoiceMouseWheelTest");51setLayout(new FlowLayout());5253Choice choice = new Choice();5455addWindowListener(new WindowAdapter() {56@Override57public void windowClosing(WindowEvent e) {58System.exit(0);59}60});6162for(Integer i = 0; i < 50; i++) {63choice.add(i.toString());64}6566choice.addItemListener(new ItemListener() {67public void itemStateChanged(ItemEvent e) {68itemChanged = true;69}70});71choice.addMouseWheelListener(new MouseWheelListener() {72public void mouseWheelMoved(MouseWheelEvent e) {73wheelMoved = true;74}75});7677addMouseListener(new MouseAdapter() {78@Override79public void mouseExited(MouseEvent e) {80frameExited = true;81}82});8384add(choice);85setSize(200, 300);86setVisible(true);87toFront();8889try {90Robot robot = new Robot();91robot.setAutoDelay(20);92Util.waitForIdle(robot);9394Point pt = choice.getLocationOnScreen();95Dimension size = choice.getSize();96int x = pt.x + size.width / 3;97robot.mouseMove(x, pt.y + size.height / 2);9899// Test mouse wheel over the choice100String name = Toolkit.getDefaultToolkit().getClass().getName();101102// mouse wheel doesn't work for the choice on X11 and Mac, so skip it103if(!name.equals("sun.awt.X11.XToolkit")104&& !name.equals("sun.lwawt.macosx.LWCToolkit")) {105robot.mouseWheel(1);106Util.waitForIdle(robot);107108if(!wheelMoved || !itemChanged) {109throw new RuntimeException("Mouse Wheel over the choice failed!");110}111}112113// Test mouse wheel over the drop-down list114robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);115Util.waitForIdle(robot);116robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);117Util.waitForIdle(robot);118119int y = getLocationOnScreen().y + getSize().height;120while(!frameExited && y >= 0) { // move to the bottom of drop-down list121robot.mouseMove(x, --y);122Util.waitForIdle(robot);123}124125if(x < 0) {126throw new RuntimeException("Could not enter drop-down list!");127}128129y -= choice.getHeight() / 2;130robot.mouseMove(x, y); // move to the last visible item in the drop-down list131Util.waitForIdle(robot);132133robot.mouseWheel(choice.getItemCount()); // wheel to the last item134Util.waitForIdle(robot);135136// click the last item137itemChanged = false;138robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);139Util.waitForIdle(robot);140robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);141Util.waitForIdle(robot);142143if(!itemChanged || choice.getSelectedIndex() != choice.getItemCount() - 1) {144throw new RuntimeException("Mouse Wheel scroll position error!");145}146147dispose();148} catch (AWTException e) {149throw new RuntimeException("AWTException occurred - problem creating robot!");150}151}152}153154155156