Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JList/6462008/bug6462008.java
38854 views
/*1* Copyright (c) 2011, 2012, 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*/2223/*24* @test25* @bug 646200826* @summary Tests that mouse/keyboard work properly on JList with lead < 0 or > list.getModel().getSize()27* @author Shannon Hickey28* @run main bug646200829*/30import java.awt.*;31import java.awt.event.*;32import javax.swing.*;33import java.util.*;3435public class bug6462008 {3637private static final int DONT_CARE = -2;38private static int anchorLead;39private static boolean isAquaLAF;40private static int controlKey;41private static JList list;42private static Robot robot;4344public static void main(String[] args) throws Exception {45robot = new Robot();46robot.setAutoDelay(100);4748isAquaLAF = "Aqua".equals(UIManager.getLookAndFeel().getID());49controlKey = isAquaLAF ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;5051SwingUtilities.invokeAndWait(new Runnable() {5253@Override54public void run() {55createAndShowGUI();56}57});5859robot.waitForIdle();6061setAnchorLead(-1);62robot.waitForIdle();6364testListSelection();6566setAnchorLead(100);67robot.waitForIdle();6869testListSelection();70}7172public static void testListSelection() throws Exception {7374// Space75robot.keyPress(KeyEvent.VK_SPACE);76robot.keyRelease(KeyEvent.VK_SPACE);7778robot.waitForIdle();79checkSelection();80resetList();81robot.waitForIdle();8283// Control + Space84robot.keyPress(KeyEvent.VK_CONTROL);85robot.keyPress(KeyEvent.VK_SPACE);86robot.keyRelease(KeyEvent.VK_SPACE);87robot.keyRelease(KeyEvent.VK_CONTROL);8889robot.waitForIdle();90checkSelection();91resetList();92robot.waitForIdle();9394// Shift + Space95robot.keyPress(KeyEvent.VK_SHIFT);96robot.keyPress(KeyEvent.VK_SPACE);97robot.keyRelease(KeyEvent.VK_SPACE);98robot.keyRelease(KeyEvent.VK_SHIFT);99100robot.waitForIdle();101checkSelection();102resetList();103robot.waitForIdle();104105// Control + Shift + Space106robot.keyPress(KeyEvent.VK_CONTROL);107robot.keyPress(KeyEvent.VK_SHIFT);108robot.keyPress(KeyEvent.VK_SPACE);109robot.keyRelease(KeyEvent.VK_SPACE);110robot.keyRelease(KeyEvent.VK_SHIFT);111robot.keyRelease(KeyEvent.VK_CONTROL);112113robot.waitForIdle();114checkSelection();115resetList();116robot.waitForIdle();117118119// Control + A Multiple Selection120121robot.keyPress(controlKey);122robot.keyPress(KeyEvent.VK_A);123robot.keyRelease(KeyEvent.VK_A);124robot.keyRelease(controlKey);125126robot.waitForIdle();127checkSelectionAL(-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);128resetList();129setSelectionMode(ListSelectionModel.SINGLE_SELECTION);130robot.waitForIdle();131132// Control + A Single Selection133robot.keyPress(controlKey);134robot.keyPress(KeyEvent.VK_A);135robot.keyRelease(KeyEvent.VK_A);136robot.keyRelease(controlKey);137138robot.waitForIdle();139checkSelectionAL(0, 0, 0);140resetList();141setSelectionMode(ListSelectionModel.SINGLE_SELECTION);142setSelectionInterval(5, 5);143robot.waitForIdle();144145146// Control + A Selection interval (5, 5)147robot.keyPress(controlKey);148robot.keyPress(KeyEvent.VK_A);149robot.keyRelease(KeyEvent.VK_A);150robot.keyRelease(controlKey);151152robot.waitForIdle();153checkSelection(5);154resetList();155robot.waitForIdle();156157// Page Down158// Not applicable for the Aqua L&F159if (!isAquaLAF) {160robot.keyPress(KeyEvent.VK_PAGE_DOWN);161robot.keyRelease(KeyEvent.VK_PAGE_DOWN);162163robot.waitForIdle();164checkSelection(9, 9, 9);165resetList();166robot.waitForIdle();167}168169// Shift + Page Down170/*171* We really want to use robot here, but there seems to be a bug in AWT's172* robot implementation (see 6463168). For now, we'll invoke the action173* directly instead. When the bug is fixed, we'll use the following four174* lines instead:175* robot.keyPress(KeyEvent.VK_SHIFT);176* robot.keyPress(KeyEvent.VK_PAGE_DOWN);177* robot.keyRelease(KeyEvent.VK_PAGE_DOWN);178* robot.keyRelease(KeyEvent.VK_SHIFT);179*/180181scrollDownExtendSelection();182183robot.waitForIdle();184checkSelection(0, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);185resetList();186robot.waitForIdle();187188// Down189robot.keyPress(KeyEvent.VK_DOWN);190robot.keyRelease(KeyEvent.VK_DOWN);191192robot.waitForIdle();193checkSelectionAL(0, 0, 0);194resetList();195robot.waitForIdle();196197// L198robot.keyPress(KeyEvent.VK_L);199robot.keyRelease(KeyEvent.VK_L);200201robot.waitForIdle();202checkSelectionAL(0, 0, 0);203resetList();204robot.waitForIdle();205206// Click item 4207Point p = clickItem4();208robot.mouseMove(p.x, p.y);209robot.mousePress(InputEvent.BUTTON1_MASK);210robot.mouseRelease(InputEvent.BUTTON1_MASK);211212213robot.waitForIdle();214checkSelectionAL(4, 4, 4);215resetList();216robot.waitForIdle();217218219// Control + Click item 4220robot.keyPress(controlKey);221p = clickItem4();222robot.mouseMove(p.x, p.y);223robot.mousePress(InputEvent.BUTTON1_MASK);224robot.mouseRelease(InputEvent.BUTTON1_MASK);225robot.keyRelease(controlKey);226227228robot.waitForIdle();229checkSelectionAL(4, 4, 4);230resetList();231robot.waitForIdle();232233// Shift + Click item 4234robot.keyPress(KeyEvent.VK_SHIFT);235p = clickItem4();236robot.mouseMove(p.x, p.y);237robot.mousePress(InputEvent.BUTTON1_MASK);238robot.mouseRelease(InputEvent.BUTTON1_MASK);239robot.keyRelease(KeyEvent.VK_SHIFT);240241242robot.waitForIdle();243checkSelectionAL(0, 4, 0, 1, 2, 3, 4);244resetList();245robot.waitForIdle();246247248// Control + Shift + Click item 4249robot.keyPress(controlKey);250robot.keyPress(KeyEvent.VK_SHIFT);251p = clickItem4();252robot.mouseMove(p.x, p.y);253robot.mousePress(InputEvent.BUTTON1_MASK);254robot.mouseRelease(InputEvent.BUTTON1_MASK);255robot.keyRelease(KeyEvent.VK_SHIFT);256robot.keyRelease(controlKey);257258robot.waitForIdle();259checkSelectionAL(0, 4);260resetList();261robot.waitForIdle();262}263264private static DefaultListModel getModel() {265DefaultListModel listModel = new DefaultListModel();266for (int i = 0; i < 10; i++) {267listModel.addElement("List Item " + i);268}269return listModel;270}271272private static Point clickItem4() throws Exception {273274final Point[] result = new Point[1];275SwingUtilities.invokeAndWait(new Runnable() {276277@Override278public void run() {279Rectangle r = list.getCellBounds(4, 4);280Point p = new Point(r.x + r.width / 2, r.y + r.height / 2);281SwingUtilities.convertPointToScreen(p, list);282result[0] = p;283}284});285286return result[0];287}288289private static void resetList() throws Exception {290SwingUtilities.invokeAndWait(new Runnable() {291292@Override293public void run() {294list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);295list.getSelectionModel().clearSelection();296setAnchorLeadNonThreadSafe();297}298});299}300301private static void scrollDownExtendSelection() throws Exception {302SwingUtilities.invokeAndWait(new Runnable() {303304@Override305public void run() {306list.getActionMap().get("scrollDownExtendSelection").307actionPerformed(new ActionEvent(list,308ActionEvent.ACTION_PERFORMED, null));309}310});311}312313private static void setSelectionMode(final int selectionMode) throws Exception {314SwingUtilities.invokeAndWait(new Runnable() {315316@Override317public void run() {318list.getSelectionModel().setSelectionMode(selectionMode);319setAnchorLeadNonThreadSafe();320}321});322}323324private static void setSelectionInterval(final int index0, final int index1) throws Exception {325SwingUtilities.invokeAndWait(new Runnable() {326327@Override328public void run() {329list.getSelectionModel().setSelectionInterval(index0, index1);330setAnchorLeadNonThreadSafe();331}332});333}334335private static void setAnchorLead(final int anchorLeadValue) throws Exception {336SwingUtilities.invokeAndWait(new Runnable() {337338@Override339public void run() {340anchorLead = anchorLeadValue;341setAnchorLeadNonThreadSafe();342}343});344}345346private static void setAnchorLeadNonThreadSafe() {347list.getSelectionModel().setAnchorSelectionIndex(anchorLead);348((DefaultListSelectionModel) list.getSelectionModel()).moveLeadSelectionIndex(anchorLead);349}350351private static void createAndShowGUI() {352JFrame frame = new JFrame("bug6462008");353frame.setSize(200, 500);354frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);355356list = new JList(getModel());357JPanel panel = new JPanel(new BorderLayout());358panel.add(list);359frame.getContentPane().add(panel);360361frame.setVisible(true);362}363364private static void checkSelection(int... sels) throws Exception {365checkSelectionAL(DONT_CARE, DONT_CARE, sels);366}367368private static void checkSelectionAL(final int anchor, final int lead, final int... sels) throws Exception {369SwingUtilities.invokeAndWait(new Runnable() {370371@Override372public void run() {373checkSelectionNonThreadSafe(anchor, lead, sels);374}375});376}377378private static void checkSelectionNonThreadSafe(int anchor, int lead, int... sels) {379ListSelectionModel lsm = list.getSelectionModel();380381int actualAnchor = lsm.getAnchorSelectionIndex();382int actualLead = lsm.getLeadSelectionIndex();383384if (anchor != DONT_CARE && actualAnchor != anchor) {385throw new RuntimeException("anchor is " + actualAnchor + ", should be " + anchor);386}387388if (lead != DONT_CARE && actualLead != lead) {389throw new RuntimeException("lead is " + actualLead + ", should be " + lead);390}391392Arrays.sort(sels);393boolean[] checks = new boolean[list.getModel().getSize()];394for (int i : sels) {395checks[i] = true;396}397398int index0 = Math.min(lsm.getMinSelectionIndex(), 0);399int index1 = Math.max(lsm.getMaxSelectionIndex(), list.getModel().getSize() - 1);400401for (int i = index0; i <= index1; i++) {402if (lsm.isSelectedIndex(i)) {403if (i < 0 || i >= list.getModel().getSize() || !checks[i]) {404throw new RuntimeException(i + " is selected when it should not be");405}406} else if (i >= 0 && i < list.getModel().getSize() && checks[i]) {407throw new RuntimeException(i + " is supposed to be selected");408}409}410}411}412413414