1 /* 2 * Copyright (c) 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 * @bug 8033936 27 * @summary Verify that correct ItemEvent is received while selection & 28 * deselection of multi select List items. 29 */ 30 31 import java.awt.AWTException; 32 import java.awt.Event; 33 import java.awt.FlowLayout; 34 import java.awt.Frame; 35 import java.awt.List; 36 import java.awt.Point; 37 import java.awt.Rectangle; 38 import java.awt.Robot; 39 import java.awt.event.KeyEvent; 40 import java.awt.event.InputEvent; 41 import java.awt.event.ItemEvent; 42 import java.awt.event.ItemListener; 43 44 public class ItemEventTest extends Frame 45 { 46 List list; 47 final String expectedSelectionOrder; 48 StringBuilder actualSelectionOrder; 49 Robot robot; 50 51 public ItemEventTest() 52 { 53 try { 54 robot = new Robot(); 55 } catch(AWTException e) { 56 throw new RuntimeException(e.getMessage()); 57 } 58 expectedSelectionOrder = "01230123"; 59 60 list = new List(4, true); 61 list.add("0"); 62 list.add("1"); 63 list.add("2"); 64 list.add("3"); 65 66 add(list); 67 setSize(400,400); 68 setLayout(new FlowLayout()); 69 pack(); 70 setVisible(true); 71 robot.waitForIdle(); 72 } 73 74 @Override 75 public boolean handleEvent(Event e) { 76 if (e.target instanceof List) { 77 if (e.id == Event.LIST_DESELECT || e.id == Event.LIST_SELECT) { 78 actualSelectionOrder.append(e.arg); 79 } 80 } 81 return true; 82 } 83 84 void testHandleEvent() { 85 // When no ItemListener is added to List, parent's handleEvent is 86 // called with ItemEvent. 87 performTest(); 88 } 89 90 void testItemListener() { 91 list.addItemListener(new ItemListener() { 92 @Override 93 public void itemStateChanged(ItemEvent ie) { 94 actualSelectionOrder.append(ie.getItem()); 95 } 96 }); 97 performTest(); 98 } 99 100 void performTest() { 101 actualSelectionOrder = new StringBuilder(); 102 Point loc = list.getLocationOnScreen(); 103 Rectangle rect = list.getBounds(); 104 int dY = rect.height / list.getItemCount(); 105 loc = new Point(loc.x + 10, loc.y + 5); 106 107 String osName = System.getProperty("os.name"); 108 boolean isMac = osName.contains("Mac") || osName.contains("mac"); 109 if(isMac) { 110 robot.keyPress(KeyEvent.VK_META); 111 } 112 113 // First loop to select & Second loop to deselect the list items. 114 for (int j = 0; j < 2; ++j) { 115 for (int i = 0; i < list.getItemCount(); ++i) { 116 robot.mouseMove(loc.x, loc.y + i * dY); 117 robot.mousePress(InputEvent.BUTTON1_MASK); 118 robot.delay(100); 119 robot.mouseRelease(InputEvent.BUTTON1_MASK); 120 robot.waitForIdle(); 121 } 122 } 123 124 if(isMac) { 125 robot.keyRelease(KeyEvent.VK_META); 126 } 127 128 if (!expectedSelectionOrder.equals(actualSelectionOrder.toString())) { 129 dispose(); 130 throw new RuntimeException("ItemEvent for selection & deselection" 131 + " of multi select List's item is not correct" 132 + " Expected : " + expectedSelectionOrder 133 + " Actual : " + actualSelectionOrder); 134 } 135 } 136 137 public static void main(String args[]) { 138 ItemEventTest test = new ItemEventTest(); 139 test.testHandleEvent(); 140 test.testItemListener(); 141 test.dispose(); 142 } 143 }