Path: blob/master/test/jdk/java/awt/Choice/SelectNewItemTest/SelectNewItemTest.java
41152 views
/*1* Copyright (c) 2019, 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*/22/*23@test24@bug 821592125@summary Test that selecting a different item does send an ItemEvent26@key headful27@run main SelectNewItemTest28*/2930import java.awt.Choice;31import java.awt.Robot;32import java.awt.Frame;33import java.awt.BorderLayout;34import java.awt.AWTException;35import java.awt.Point;36import java.awt.Dimension;37import java.awt.event.InputEvent;38import java.awt.event.ItemListener;39import java.awt.event.WindowListener;40import java.awt.event.ItemEvent;41import java.awt.event.WindowEvent;42import java.util.concurrent.CountDownLatch;43import java.util.concurrent.TimeUnit;4445public class SelectNewItemTest implements ItemListener, WindowListener {46//Declare things used in the test, like buttons and labels here47private Frame frame;48private Choice theChoice;49private Robot robot;5051private CountDownLatch latch = new CountDownLatch(1);52private volatile boolean passed = false;5354private void init()55{56try {57robot = new Robot();58robot.setAutoDelay(500);59} catch (AWTException e) {60throw new RuntimeException("Unable to create Robot. Test fails.");61}6263frame = new Frame("SelectNewItemTest");64frame.setLayout(new BorderLayout());65theChoice = new Choice();66for (int i = 0; i < 10; i++) {67theChoice.add(new String("Choice Item " + i));68}69theChoice.addItemListener(this);70frame.add(theChoice);71frame.addWindowListener(this);7273frame.setLocation(1,20);74frame.setSize(200, 50);75robot.mouseMove(10, 30);76frame.pack();77frame.setVisible(true);78}7980public static void main(String... args) {81SelectNewItemTest test = new SelectNewItemTest();82test.init();83try {84test.latch.await(12000, TimeUnit.MILLISECONDS);85} catch (InterruptedException e) {}86test.robot.waitForIdle();8788try {89if (!test.passed) {90throw new RuntimeException("TEST FAILED.");91}92} finally {93test.frame.dispose();94}95}9697private void run() {98try {99Thread.sleep(1000);100101Point loc = theChoice.getLocationOnScreen();102int selectedIndex = theChoice.getSelectedIndex();103Dimension size = theChoice.getSize();104105robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2);106107robot.setAutoDelay(250);108robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);109robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);110111robot.delay(1000);112113//make sure that the mouse moves to a different item, so that114//itemStateChanged is called.115robot.mouseMove(loc.x + size.width / 2, loc.y + 3 * size.height);116robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);117robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);118robot.waitForIdle();119120if (selectedIndex == theChoice.getSelectedIndex())121throw new RuntimeException("Test case failed - expected to select" +122" a different item than " + selectedIndex);123124selectedIndex = theChoice.getSelectedIndex();125//now click on the same item and make sure that item event is126//not generated.127robot.delay(1000);128robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2);129130robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);131//Make sure that the popup menu scrolls back to show the index from132//beginning, so that the second mouse click happens on the previously133//selected item.134//For example, on windows, it automatically scrolls the list to show135//the currently selected item just below the choice, which can136//throw off the test.137if (System.getProperty("os.name").toLowerCase().startsWith("win")) {138robot.mouseWheel(-100);139}140robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);141142robot.delay(1000);143robot.mouseMove(loc.x + size.width / 2, loc.y + 3 * size.height);144robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);145robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);146robot.waitForIdle();147148if (selectedIndex != theChoice.getSelectedIndex())149throw new RuntimeException("Test failed. Expected to select the same item " +150"located at: " + selectedIndex + " but got an item selected at: " + theChoice.getSelectedIndex());151} catch(InterruptedException e) {152throw new RuntimeException(e.getCause());153} finally {154latch.countDown();155}156}157158@Override public void itemStateChanged(ItemEvent e) {159if (!passed) {160System.out.println("ItemEvent received. Test passes");161passed = true;162} else {163System.out.println("ItemEvent received for second click. Test fails");164passed = false;165}166}167168@Override public void windowOpened(WindowEvent e) {169System.out.println("windowActivated()");170(new Thread(this::run)).start();171}172173@Override public void windowActivated(WindowEvent e) {}174@Override public void windowDeactivated(WindowEvent e) {}175@Override public void windowClosed(WindowEvent e) {}176@Override public void windowClosing(WindowEvent e) {}177@Override public void windowIconified(WindowEvent e) {}178@Override public void windowDeiconified(WindowEvent e) {}179}180181182