Path: blob/master/test/jdk/java/awt/List/ItemEventTest/ItemEventTest.java
41153 views
/*1* Copyright (c) 2016, 2016, 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*/2223/*24* @test25* @key headful26* @bug 8033936 817251027* @summary Verify that correct ItemEvent is received while selection &28* deselection of multi select List items.29*/3031import java.awt.AWTException;32import java.awt.Event;33import java.awt.FlowLayout;34import java.awt.Frame;35import java.awt.List;36import java.awt.Point;37import java.awt.Rectangle;38import java.awt.Robot;39import java.awt.event.KeyEvent;40import java.awt.event.InputEvent;41import java.awt.event.ItemEvent;42import java.awt.event.ItemListener;4344public class ItemEventTest extends Frame45{46List list;47final String expectedSelectionOrder;48StringBuilder actualSelectionOrder;49Robot robot;5051public ItemEventTest()52{53try {54robot = new Robot();55} catch(AWTException e) {56throw new RuntimeException(e.getMessage());57}58expectedSelectionOrder = "01230123";5960list = new List(4, true);61list.add("0");62list.add("1");63list.add("2");64list.add("3");6566add(list);67setSize(400,400);68setLayout(new FlowLayout());69pack();70setVisible(true);71robot.waitForIdle();72}7374@Override75public boolean handleEvent(Event e) {76if (e.target instanceof List) {77if (e.id == Event.LIST_DESELECT || e.id == Event.LIST_SELECT) {78actualSelectionOrder.append(e.arg);79}80}81return true;82}8384void testHandleEvent() {85// When no ItemListener is added to List, parent's handleEvent is86// called with ItemEvent.87performTest();88}8990void testItemListener() {91list.addItemListener(new ItemListener() {92@Override93public void itemStateChanged(ItemEvent ie) {94actualSelectionOrder.append(ie.getItem());95}96});97performTest();98}99100void performTest() {101actualSelectionOrder = new StringBuilder();102Point loc = list.getLocationOnScreen();103Rectangle rect = list.getBounds();104int dY = rect.height / list.getItemCount();105loc = new Point(loc.x + 10, loc.y + 5);106107String osName = System.getProperty("os.name");108boolean isMac = osName.contains("Mac") || osName.contains("mac");109if(isMac) {110robot.keyPress(KeyEvent.VK_META);111robot.waitForIdle();112}113114// First loop to select & Second loop to deselect the list items.115for (int j = 0; j < 2; ++j) {116for (int i = 0; i < list.getItemCount(); ++i) {117robot.mouseMove(loc.x, loc.y + i * dY);118robot.waitForIdle();119robot.mousePress(InputEvent.BUTTON1_MASK);120robot.waitForIdle();121robot.mouseRelease(InputEvent.BUTTON1_MASK);122robot.waitForIdle();123}124}125126if(isMac) {127robot.keyRelease(KeyEvent.VK_META);128}129130if (!expectedSelectionOrder.equals(actualSelectionOrder.toString())) {131dispose();132throw new RuntimeException("ItemEvent for selection & deselection"133+ " of multi select List's item is not correct"134+ " Expected : " + expectedSelectionOrder135+ " Actual : " + actualSelectionOrder);136}137}138139public static void main(String args[]) {140ItemEventTest test = new ItemEventTest();141test.testHandleEvent();142test.testItemListener();143test.dispose();144}145}146147148