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 @build Util 32 @run main ItemStateChangeTest 33 */ 34 35 import test.java.awt.regtesthelpers.Util; 36 37 import java.awt.*; 38 import java.awt.event.*; 39 import sun.awt.OSInfo; 40 41 public class ItemStateChangeTest extends Frame { 42 43 int events = 0; 44 45 public static void main(String args[]) { 46 new ItemStateChangeTest(); 47 } 48 49 public ItemStateChangeTest() { 50 51 if (OSInfo.getOSType() != OSInfo.OSType.WINDOWS) { 52 return; 53 } 54 55 try { 56 57 final Robot robot = new Robot(); 58 robot.setAutoDelay(20); 59 Util.waitForIdle(robot); 60 61 addWindowListener(new WindowAdapter() { 62 @Override 63 public void windowClosing(WindowEvent e) { 64 System.exit(0); 65 } 66 }); 67 68 final Choice choice = new Choice(); 69 choice.add("A"); 70 choice.add("B"); 71 choice.addItemListener(new ItemListener() { 72 @Override 73 public void itemStateChanged(ItemEvent e) { 74 ++events; 75 } 76 }); 77 78 add(choice); 79 setSize(200, 150); 80 setVisible(true); 81 toFront(); 82 83 // choose B 84 int y = chooseB(choice, robot, 16); 85 86 // reset to A 87 choice.select(0); 88 robot.delay(20); 89 Util.waitForIdle(robot); 90 91 // choose B again 92 chooseB(choice, robot, y); 93 94 if (events == 2) { 95 System.out.println("Test passed!"); 96 } 97 else { 98 throw new RuntimeException("Test failed!"); 99 } 100 101 } 102 catch (AWTException e) { 103 throw new RuntimeException("Test failed!"); 104 } 105 } 106 107 final int chooseB(Choice choice, Robot robot, int y) { 108 while (true) { 109 // show drop-down list 110 Util.clickOnComp(choice, robot); 111 Util.waitForIdle(robot); 112 Point pt = choice.getLocationOnScreen(); 113 Dimension size = choice.getSize(); 114 // try to click B item 115 robot.mouseMove(pt.x + size.width / 2, pt.y + size.height + y); 116 Util.waitForIdle(robot); 117 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 118 Util.waitForIdle(robot); 119 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 120 Util.waitForIdle(robot); 121 if (choice.getSelectedIndex() == 1) { 122 break; 123 } 124 // if it's not B, position cursor lower by 2 pixels and try again 125 y += 2; 126 } 127 return y; 128 } 129 }