Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Choice/ItemStateChangeTest/ItemStateChangeTest.java
47490 views
/*1* Copyright (c) 2012, 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@bug 717141226@summary awt Choice doesn't fire ItemStateChange when selecting item after select() call27@author Oleg Pekhovskiy: area=awt-choice28@library ../../regtesthelpers29@build Util30@run main ItemStateChangeTest31*/3233import test.java.awt.regtesthelpers.Util;3435import java.awt.*;36import java.awt.event.*;37import sun.awt.OSInfo;3839public class ItemStateChangeTest extends Frame {4041int events = 0;4243public static void main(String args[]) {44new ItemStateChangeTest();45}4647public ItemStateChangeTest() {4849if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) {50return;51}5253try {5455final Robot robot = new Robot();56robot.setAutoDelay(20);57Util.waitForIdle(robot);5859addWindowListener(new WindowAdapter() {60@Override61public void windowClosing(WindowEvent e) {62System.exit(0);63}64});6566final Choice choice = new Choice();67choice.add("A");68choice.add("B");69choice.addItemListener(new ItemListener() {70@Override71public void itemStateChanged(ItemEvent e) {72++events;73}74});7576add(choice);77setSize(200, 150);78setVisible(true);79toFront();8081// choose B82int y = chooseB(choice, robot, 16);8384// reset to A85choice.select(0);86robot.delay(20);87Util.waitForIdle(robot);8889// choose B again90chooseB(choice, robot, y);9192if (events == 2) {93System.out.println("Test passed!");94}95else {96throw new RuntimeException("Test failed!");97}9899}100catch (AWTException e) {101throw new RuntimeException("Test failed!");102}103}104105final int chooseB(Choice choice, Robot robot, int y) {106while (true) {107// show drop-down list108Util.clickOnComp(choice, robot);109Util.waitForIdle(robot);110Point pt = choice.getLocationOnScreen();111Dimension size = choice.getSize();112// try to click B item113robot.mouseMove(pt.x + size.width / 2, pt.y + size.height + y);114Util.waitForIdle(robot);115robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);116Util.waitForIdle(robot);117robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);118Util.waitForIdle(robot);119if (choice.getSelectedIndex() == 1) {120break;121}122// if it's not B, position cursor lower by 2 pixels and try again123y += 2;124}125return y;126}127}128129130