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  * @key headful
  27  * @bug 8033936
  28  * @summary Verify that correct ItemEvent is received while selection &
  29  *          deselection of multi select List items.
  30  */
  31 
  32 import java.awt.AWTException;
  33 import java.awt.Event;
  34 import java.awt.FlowLayout;
  35 import java.awt.Frame;
  36 import java.awt.List;
  37 import java.awt.Point;
  38 import java.awt.Rectangle;
  39 import java.awt.Robot;
  40 import java.awt.event.KeyEvent;
  41 import java.awt.event.InputEvent;
  42 import java.awt.event.ItemEvent;
  43 import java.awt.event.ItemListener;
  44 
  45 public 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         }
 113 
 114         // First loop to select & Second loop to deselect the list items.
 115         for (int j = 0; j < 2; ++j) {
 116             for (int i = 0; i < list.getItemCount(); ++i) {
 117                 robot.mouseMove(loc.x, loc.y + i * dY);
 118                 robot.mousePress(InputEvent.BUTTON1_MASK);
 119                 robot.delay(100);
 120                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
 121                 robot.waitForIdle();
 122             }
 123         }
 124 
 125         if(isMac) {
 126             robot.keyRelease(KeyEvent.VK_META);
 127         }
 128 
 129         if (!expectedSelectionOrder.equals(actualSelectionOrder.toString())) {
 130             dispose();
 131             throw new RuntimeException("ItemEvent for selection & deselection"
 132                 + " of multi select List's item is not correct"
 133                 + " Expected : " + expectedSelectionOrder
 134                 + " Actual : " + actualSelectionOrder);
 135         }
 136     }
 137 
 138     public static void main(String args[]) {
 139        ItemEventTest test = new ItemEventTest();
 140        test.testHandleEvent();
 141        test.testItemListener();
 142        test.dispose();
 143     }
 144 }