1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
/*
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.List;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
/**
* @test
* @bug 4909485 8211999
* @key headful
*/
public final class ListMultipleSelectTest {
private static List aList;
private static Panel panel;
private static Point p;
private static Robot robot;
private static int[] selected;
public static void main(String[] args) throws Exception {
Frame frame = new Frame();
try {
panel = new Panel();
aList = new List();
aList.add("Test item1");
aList.add("Test item2");
aList.add("Test item3");
aList.add("Test item4");
frame.setLayout(new FlowLayout()); //list should fill whole frame's space
panel.add(aList);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
panel.setVisible(true);
frame.add(panel);
frame.setVisible(true);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(" InterruptedException. Test failed. ");
}
Dimension listSize = aList.getSize();
p = aList.getLocationOnScreen();
int stepY = listSize.height / aList.getItemCount();
// System.out.println("itemCount = "+aList.getItemCount());
// System.out.println("Size Of aList="+ listSize);
// System.out.println("stepY = "+stepY);
// System.out.println("Point:" +p);
System.out.println("Multiple mode is ON");
aList.setMultipleMode(true);
//=================================================
robot = new Robot();
robot.setAutoDelay(0);
robot.setAutoWaitForIdle(false);
robot.delay(10);
robot.waitForIdle();
//=================================================
for (int i = 0; i < aList.getItemCount(); i++) {
//select all items in the List
mousePress(p.x + listSize.width / 2, p.y + stepY / 2 + stepY * i);
}
selected = aList.getSelectedIndexes();
System.out.println("Multiple mode is ON");
aList.setMultipleMode(true);
int[] newSelected = aList.getSelectedIndexes();
if (!java.util.Arrays.equals(newSelected, selected)) {
throw new RuntimeException(" Incorrect item remains selected " +
"after setMultipleMode(true). ");
}
aList.setMultipleMode(false);
System.out.println("Multiple mode is OFF");
selected = aList.getSelectedIndexes();
if (selected[0] != 3 || selected.length != 1) {
throw new RuntimeException(" Incorrect item remains selected " +
"after setMultipleMode(false) or it is more then one " +
"item remaining.Forward. ");
}
System.out.println("Multiple mode is ON");
aList.setMultipleMode(true);
if (selected[0] != 3 || selected.length != 1) {
throw new RuntimeException(" Incorrect item remains selected " +
"after setMultipleMode(true) or it is more then one " +
"item remaining. ");
}
deselectAll();
for (int i = aList.getItemCount() - 1; i >= 0; i--) {
mousePress(p.x + listSize.width / 2, p.y + stepY / 2 + stepY * i);
}
System.out.println("Multiple mode is OFF");
aList.setMultipleMode(false);
selected = aList.getSelectedIndexes();
if (selected[0] != 0 || selected.length != 1) {
throw new RuntimeException(" Incorrect item remains selected " +
"after setMultipleMode(false) or it is more then one " +
"item remaining.Backward. ");
}
System.out.println("Test succeeded.");
} finally {
frame.dispose();
}
}
private static void mousePress(int x, int y) {
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(1000);
}
private static void deselectAll() {
for (int i = 0; i < selected.length; i++) {
aList.deselect(selected[i]);
}
}
}
|