1 /* 2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 @test 26 @key headful 27 @bug 7171412 28 @summary awt Choice doesn't fire ItemStateChange when selecting item after select() call 29 @author Oleg Pekhovskiy: area=awt-choice 30 @library ../../regtesthelpers 31 @library ../../../../lib/testlibrary 32 @modules java.desktop/sun.awt 33 @build Util 34 @build jdk.testlibrary.OSInfo 35 @run main ItemStateChangeTest 36 */ 37 38 import test.java.awt.regtesthelpers.Util; 39 import jdk.testlibrary.OSInfo; 40 41 import java.awt.*; 42 import java.awt.event.*; 43 44 public class ItemStateChangeTest extends Frame { 45 46 int events = 0; 47 48 public static void main(String args[]) { 49 new ItemStateChangeTest(); 50 } 51 52 public ItemStateChangeTest() { 53 54 if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) { 55 return; 56 } 57 58 try { 59 60 final Robot robot = new Robot(); 61 robot.setAutoDelay(20); 62 Util.waitForIdle(robot); 63 64 addWindowListener(new WindowAdapter() { 65 @Override 66 public void windowClosing(WindowEvent e) { 67 System.exit(0); 68 } 69 }); 70 71 final Choice choice = new Choice(); 72 choice.add("A"); 73 choice.add("B"); 74 choice.addItemListener(new ItemListener() { 75 @Override 76 public void itemStateChanged(ItemEvent e) { 77 ++events; 78 } 79 }); 80 81 add(choice); 82 setSize(200, 150); 83 setVisible(true); 84 toFront(); 85 86 // choose B 87 int y = chooseB(choice, robot, 16); 88 89 // reset to A 90 choice.select(0); 91 robot.delay(20); 92 Util.waitForIdle(robot); 93 94 // choose B again 95 chooseB(choice, robot, y); 96 97 if (events == 2) { 98 System.out.println("Test passed!"); 99 } 100 else { 101 throw new RuntimeException("Test failed!"); 102 } 103 104 } 105 catch (AWTException e) { 106 throw new RuntimeException("Test failed!"); 107 } 108 } 109 110 final int chooseB(Choice choice, Robot robot, int y) { 111 while (true) { 112 // show drop-down list 113 Util.clickOnComp(choice, robot); 114 Util.waitForIdle(robot); 115 Point pt = choice.getLocationOnScreen(); 116 Dimension size = choice.getSize(); 117 // try to click B item 118 robot.mouseMove(pt.x + size.width / 2, pt.y + size.height + y); 119 Util.waitForIdle(robot); 120 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 121 Util.waitForIdle(robot); 122 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 123 Util.waitForIdle(robot); 124 if (choice.getSelectedIndex() == 1) { 125 break; 126 } 127 // if it's not B, position cursor lower by 2 pixels and try again 128 y += 2; 129 } 130 return y; 131 } 132 }