1 /*
   2  * Copyright (c) 1996, 2020, 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 import java.awt.Dimension;
  25 import java.awt.FlowLayout;
  26 import java.awt.Frame;
  27 import java.awt.List;
  28 import java.awt.Panel;
  29 import java.awt.Point;
  30 import java.awt.Robot;
  31 import java.awt.event.InputEvent;
  32 
  33 /**
  34  * @test
  35  * @bug 4909485 8211999
  36  * @key headful
  37  */
  38 public final class ListMultipleSelectTest {
  39 
  40     private static List aList;
  41     private static Panel panel;
  42     private static Point p;
  43     private static Robot robot;
  44     private static int[] selected;
  45 
  46     public static void main(String[] args) throws Exception {
  47         Frame frame = new Frame();
  48         try {
  49             panel = new Panel();
  50             aList = new List();
  51             aList.add("Test item1");
  52             aList.add("Test item2");
  53             aList.add("Test item3");
  54             aList.add("Test item4");
  55 
  56             frame.setLayout(new FlowLayout()); //list should fill whole frame's space
  57             panel.add(aList);
  58             frame.setSize(200, 200);
  59             frame.setLocationRelativeTo(null);
  60             panel.setVisible(true);
  61             frame.add(panel);
  62             frame.setVisible(true);
  63             try {
  64                 Thread.sleep(1000);
  65             } catch (InterruptedException e) {
  66                 throw new RuntimeException(" InterruptedException. Test failed. ");
  67             }
  68 
  69             Dimension listSize = aList.getSize();
  70             p = aList.getLocationOnScreen();
  71             int stepY = listSize.height / aList.getItemCount();
  72 
  73 //          System.out.println("itemCount = "+aList.getItemCount());
  74 //          System.out.println("Size Of aList="+ listSize);
  75 //          System.out.println("stepY = "+stepY);
  76 //          System.out.println("Point:" +p);
  77             System.out.println("Multiple mode is ON");
  78             aList.setMultipleMode(true);
  79 //=================================================
  80             robot = new Robot();
  81             robot.setAutoDelay(0);
  82             robot.setAutoWaitForIdle(false);
  83             robot.delay(10);
  84             robot.waitForIdle();
  85 
  86 //=================================================
  87 
  88             for (int i = 0; i < aList.getItemCount(); i++) {
  89                 //select all items in the List
  90                 mousePress(p.x + listSize.height / 2, p.y + stepY / 2 + stepY * i);
  91             }
  92 
  93             selected = aList.getSelectedIndexes();
  94             System.out.println("Multiple mode is ON");
  95             aList.setMultipleMode(true);
  96             int[] newSelected = aList.getSelectedIndexes();
  97             if (!java.util.Arrays.equals(newSelected, selected)) {
  98                 throw new RuntimeException(" Incorrect item remains selected " +
  99                         "after setMultipleMode(true). ");
 100             }
 101 
 102             aList.setMultipleMode(false);
 103             System.out.println("Multiple mode is OFF");
 104             selected = aList.getSelectedIndexes();
 105             if (selected[0] != 3 || selected.length != 1) {
 106                 throw new RuntimeException(" Incorrect item remains selected " +
 107                         "after setMultipleMode(false) or it is more then one " +
 108                         "item remaining.Forward. ");
 109             }
 110 
 111             System.out.println("Multiple mode is ON");
 112             aList.setMultipleMode(true);
 113 
 114             if (selected[0] != 3 || selected.length != 1) {
 115                 throw new RuntimeException(" Incorrect item remains selected " +
 116                         "after setMultipleMode(true) or it is more then one " +
 117                         "item remaining. ");
 118             }
 119 
 120             deselectAll();
 121             for (int i = aList.getItemCount() - 1; i >= 0; i--) {
 122                 mousePress(p.x + listSize.height / 2, p.y + stepY / 2 + stepY * i);
 123             }
 124 
 125             System.out.println("Multiple mode is OFF");
 126             aList.setMultipleMode(false);
 127 
 128             selected = aList.getSelectedIndexes();
 129             if (selected[0] != 0 || selected.length != 1) {
 130                 throw new RuntimeException(" Incorrect item remains selected " +
 131                         "after setMultipleMode(false) or it is more then one " +
 132                         "item remaining.Backward. ");
 133             }
 134             System.out.println("Test succeeded.");
 135         } finally {
 136             frame.dispose();
 137         }
 138     }
 139 
 140     private static void mousePress(int x, int y) {
 141         robot.mouseMove(x, y);
 142         robot.mousePress(InputEvent.BUTTON1_MASK);
 143         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 144         robot.delay(1000);
 145     }
 146 
 147     private static void deselectAll() {
 148         for (int i = 0; i < selected.length; i++) {
 149             aList.deselect(selected[i]);
 150         }
 151     }
 152 }