Path: blob/master/test/jdk/java/awt/Choice/ItemStateChangeTest/ItemStateChangeTest.java
41152 views
/*1* Copyright (c) 2012, 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 717141227@summary awt Choice doesn't fire ItemStateChange when selecting item after select() call28@author Oleg Pekhovskiy: area=awt-choice29@library ../../regtesthelpers30@library /test/lib31@modules java.desktop/sun.awt32@build Util33@build jdk.test.lib.Platform34@run main ItemStateChangeTest35*/3637import jdk.test.lib.Platform;38import test.java.awt.regtesthelpers.Util;3940import java.awt.*;41import java.awt.event.*;4243public class ItemStateChangeTest extends Frame {4445int events = 0;4647public static void main(String args[]) {48new ItemStateChangeTest();49}5051public ItemStateChangeTest() {5253if (!Platform.isWindows()) {54return;55}5657try {5859final Robot robot = new Robot();60robot.setAutoDelay(20);61Util.waitForIdle(robot);6263addWindowListener(new WindowAdapter() {64@Override65public void windowClosing(WindowEvent e) {66System.exit(0);67}68});6970final Choice choice = new Choice();71choice.add("A");72choice.add("B");73choice.addItemListener(new ItemListener() {74@Override75public void itemStateChanged(ItemEvent e) {76++events;77}78});7980add(choice);81setSize(200, 150);82setVisible(true);83toFront();8485// choose B86int y = chooseB(choice, robot, 16);8788// reset to A89choice.select(0);90robot.delay(20);91Util.waitForIdle(robot);9293// choose B again94chooseB(choice, robot, y);9596if (events == 2) {97System.out.println("Test passed!");98}99else {100throw new RuntimeException("Test failed!");101}102103}104catch (AWTException e) {105throw new RuntimeException("Test failed!");106}107}108109final int chooseB(Choice choice, Robot robot, int y) {110while (true) {111// show drop-down list112Util.clickOnComp(choice, robot);113Util.waitForIdle(robot);114Point pt = choice.getLocationOnScreen();115Dimension size = choice.getSize();116// try to click B item117robot.mouseMove(pt.x + size.width / 2, pt.y + size.height + y);118Util.waitForIdle(robot);119robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);120Util.waitForIdle(robot);121robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);122Util.waitForIdle(robot);123if (choice.getSelectedIndex() == 1) {124break;125}126// if it's not B, position cursor lower by 2 pixels and try again127y += 2;128}129return y;130}131}132133134