Path: blob/master/test/jdk/java/awt/Choice/SelectCurrentItemTest/SelectCurrentItemTest.java
41153 views
/*1* Copyright (c) 2002, 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*/22/*23@test24@bug 4902933 819781025@summary Test that selecting the current item doesnot send an ItemEvent26@key headful27@run main SelectCurrentItemTest28*/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 SelectCurrentItemTest 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 = true;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("SelectCurrentItemTest");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);74robot.mouseMove(10, 30);75frame.pack();76frame.setVisible(true);77}7879public static void main(String... args) {80SelectCurrentItemTest test = new SelectCurrentItemTest();81test.init();82try {83test.latch.await(12000, TimeUnit.MILLISECONDS);84} catch (InterruptedException e) {}85test.robot.waitForIdle();8687try {88if (!test.passed) {89throw new RuntimeException("TEST FAILED.");90}91} finally {92test.frame.dispose();93}94}9596private void run() {97try {Thread.sleep(1000);} catch (InterruptedException e){}98// get loc of Choice on screen99Point loc = theChoice.getLocationOnScreen();100// get bounds of Choice101Dimension size = theChoice.getSize();102robot.mouseMove(loc.x + size.width - 10, loc.y + size.height / 2);103104robot.setAutoDelay(250);105robot.mousePress(InputEvent.BUTTON1_MASK);106robot.mouseRelease(InputEvent.BUTTON1_MASK);107108robot.delay(1000);109110robot.mouseMove(loc.x + size.width / 2, loc.y + size.height);111robot.mousePress(InputEvent.BUTTON1_MASK);112robot.mouseRelease(InputEvent.BUTTON1_MASK);113robot.waitForIdle();114latch.countDown();115}116117@Override public void itemStateChanged(ItemEvent e) {118System.out.println("ItemEvent received. Test fails");119passed = false;120}121122@Override public void windowOpened(WindowEvent e) {123System.out.println("windowActivated()");124(new Thread(this::run)).start();125}126127@Override public void windowActivated(WindowEvent e) {}128@Override public void windowDeactivated(WindowEvent e) {}129@Override public void windowClosed(WindowEvent e) {}130@Override public void windowClosing(WindowEvent e) {}131@Override public void windowIconified(WindowEvent e) {}132@Override public void windowDeiconified(WindowEvent e) {}133}134135136