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