Path: blob/master/test/jdk/java/awt/List/ListMultipleSelectTest/ListMultipleSelectTest.java
41153 views
/*1* Copyright (c) 1996, 2020, 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*/2223import java.awt.Dimension;24import java.awt.FlowLayout;25import java.awt.Frame;26import java.awt.List;27import java.awt.Panel;28import java.awt.Point;29import java.awt.Robot;30import java.awt.event.InputEvent;3132/**33* @test34* @bug 4909485 821199935* @key headful36*/37public final class ListMultipleSelectTest {3839private static List aList;40private static Panel panel;41private static Point p;42private static Robot robot;43private static int[] selected;4445public static void main(String[] args) throws Exception {46Frame frame = new Frame();47try {48panel = new Panel();49aList = new List();50aList.add("Test item1");51aList.add("Test item2");52aList.add("Test item3");53aList.add("Test item4");5455frame.setLayout(new FlowLayout()); //list should fill whole frame's space56panel.add(aList);57frame.setSize(200, 200);58frame.setLocationRelativeTo(null);59panel.setVisible(true);60frame.add(panel);61frame.setVisible(true);62try {63Thread.sleep(1000);64} catch (InterruptedException e) {65throw new RuntimeException(" InterruptedException. Test failed. ");66}6768Dimension listSize = aList.getSize();69p = aList.getLocationOnScreen();70int stepY = listSize.height / aList.getItemCount();7172// System.out.println("itemCount = "+aList.getItemCount());73// System.out.println("Size Of aList="+ listSize);74// System.out.println("stepY = "+stepY);75// System.out.println("Point:" +p);76System.out.println("Multiple mode is ON");77aList.setMultipleMode(true);78//=================================================79robot = new Robot();80robot.setAutoDelay(0);81robot.setAutoWaitForIdle(false);82robot.delay(10);83robot.waitForIdle();8485//=================================================8687for (int i = 0; i < aList.getItemCount(); i++) {88//select all items in the List89mousePress(p.x + listSize.width / 2, p.y + stepY / 2 + stepY * i);90}9192selected = aList.getSelectedIndexes();93System.out.println("Multiple mode is ON");94aList.setMultipleMode(true);95int[] newSelected = aList.getSelectedIndexes();96if (!java.util.Arrays.equals(newSelected, selected)) {97throw new RuntimeException(" Incorrect item remains selected " +98"after setMultipleMode(true). ");99}100101aList.setMultipleMode(false);102System.out.println("Multiple mode is OFF");103selected = aList.getSelectedIndexes();104if (selected[0] != 3 || selected.length != 1) {105throw new RuntimeException(" Incorrect item remains selected " +106"after setMultipleMode(false) or it is more then one " +107"item remaining.Forward. ");108}109110System.out.println("Multiple mode is ON");111aList.setMultipleMode(true);112113if (selected[0] != 3 || selected.length != 1) {114throw new RuntimeException(" Incorrect item remains selected " +115"after setMultipleMode(true) or it is more then one " +116"item remaining. ");117}118119deselectAll();120for (int i = aList.getItemCount() - 1; i >= 0; i--) {121mousePress(p.x + listSize.width / 2, p.y + stepY / 2 + stepY * i);122}123124System.out.println("Multiple mode is OFF");125aList.setMultipleMode(false);126127selected = aList.getSelectedIndexes();128if (selected[0] != 0 || selected.length != 1) {129throw new RuntimeException(" Incorrect item remains selected " +130"after setMultipleMode(false) or it is more then one " +131"item remaining.Backward. ");132}133System.out.println("Test succeeded.");134} finally {135frame.dispose();136}137}138139private static void mousePress(int x, int y) {140robot.mouseMove(x, y);141robot.mousePress(InputEvent.BUTTON1_MASK);142robot.mouseRelease(InputEvent.BUTTON1_MASK);143robot.delay(1000);144}145146private static void deselectAll() {147for (int i = 0; i < selected.length; i++) {148aList.deselect(selected[i]);149}150}151}152153154