Path: blob/master/test/jdk/javax/swing/JList/6462008/bug6462008.java
41154 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* @key headful26* @bug 646200827* @summary Tests that mouse/keyboard work properly on JList with lead < 0 or > list.getModel().getSize()28* @author Shannon Hickey29* @run main bug646200830*/31import java.awt.*;32import java.awt.event.*;33import javax.swing.*;34import java.util.*;3536public class bug6462008 {3738private static final int DONT_CARE = -2;39private static int anchorLead;40private static boolean isAquaLAF;41private static int controlKey;42private static JList list;43private static Robot robot;44private static JFrame frame;4546public static void main(String[] args) throws Exception {47try {48robot = new Robot();49robot.setAutoDelay(100);5051isAquaLAF = "Aqua".equals(UIManager.getLookAndFeel().getID());52controlKey = isAquaLAF ? KeyEvent.VK_META : KeyEvent.VK_CONTROL;5354SwingUtilities.invokeAndWait(new Runnable() {5556@Override57public void run() {58createAndShowGUI();59}60});6162robot.waitForIdle();63robot.delay(1000);6465setAnchorLead(-1);66robot.waitForIdle();6768testListSelection();6970setAnchorLead(100);71robot.waitForIdle();7273testListSelection();74} finally {75if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());76}77}7879public static void testListSelection() throws Exception {8081// Space82robot.keyPress(KeyEvent.VK_SPACE);83robot.keyRelease(KeyEvent.VK_SPACE);8485robot.waitForIdle();86checkSelection();87resetList();88robot.waitForIdle();8990// Control + Space91robot.keyPress(KeyEvent.VK_CONTROL);92robot.keyPress(KeyEvent.VK_SPACE);93robot.keyRelease(KeyEvent.VK_SPACE);94robot.keyRelease(KeyEvent.VK_CONTROL);9596robot.waitForIdle();97checkSelection();98resetList();99robot.waitForIdle();100101// Shift + Space102robot.keyPress(KeyEvent.VK_SHIFT);103robot.keyPress(KeyEvent.VK_SPACE);104robot.keyRelease(KeyEvent.VK_SPACE);105robot.keyRelease(KeyEvent.VK_SHIFT);106107robot.waitForIdle();108checkSelection();109resetList();110robot.waitForIdle();111112// Control + Shift + Space113robot.keyPress(KeyEvent.VK_CONTROL);114robot.keyPress(KeyEvent.VK_SHIFT);115robot.keyPress(KeyEvent.VK_SPACE);116robot.keyRelease(KeyEvent.VK_SPACE);117robot.keyRelease(KeyEvent.VK_SHIFT);118robot.keyRelease(KeyEvent.VK_CONTROL);119120robot.waitForIdle();121checkSelection();122resetList();123robot.waitForIdle();124125126// Control + A Multiple Selection127128robot.keyPress(controlKey);129robot.keyPress(KeyEvent.VK_A);130robot.keyRelease(KeyEvent.VK_A);131robot.keyRelease(controlKey);132133robot.waitForIdle();134checkSelectionAL(-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);135resetList();136setSelectionMode(ListSelectionModel.SINGLE_SELECTION);137robot.waitForIdle();138139// Control + A Single Selection140robot.keyPress(controlKey);141robot.keyPress(KeyEvent.VK_A);142robot.keyRelease(KeyEvent.VK_A);143robot.keyRelease(controlKey);144145robot.waitForIdle();146checkSelectionAL(0, 0, 0);147resetList();148setSelectionMode(ListSelectionModel.SINGLE_SELECTION);149setSelectionInterval(5, 5);150robot.waitForIdle();151152153// Control + A Selection interval (5, 5)154robot.keyPress(controlKey);155robot.keyPress(KeyEvent.VK_A);156robot.keyRelease(KeyEvent.VK_A);157robot.keyRelease(controlKey);158159robot.waitForIdle();160checkSelection(5);161resetList();162robot.waitForIdle();163164// Page Down165// Not applicable for the Aqua L&F166if (!isAquaLAF) {167robot.keyPress(KeyEvent.VK_PAGE_DOWN);168robot.keyRelease(KeyEvent.VK_PAGE_DOWN);169170robot.waitForIdle();171checkSelection(9, 9, 9);172resetList();173robot.waitForIdle();174}175176// Shift + Page Down177/*178* We really want to use robot here, but there seems to be a bug in AWT's179* robot implementation (see 6463168). For now, we'll invoke the action180* directly instead. When the bug is fixed, we'll use the following four181* lines instead:182* robot.keyPress(KeyEvent.VK_SHIFT);183* robot.keyPress(KeyEvent.VK_PAGE_DOWN);184* robot.keyRelease(KeyEvent.VK_PAGE_DOWN);185* robot.keyRelease(KeyEvent.VK_SHIFT);186*/187188scrollDownExtendSelection();189190robot.waitForIdle();191checkSelection(0, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);192resetList();193robot.waitForIdle();194195// Down196robot.keyPress(KeyEvent.VK_DOWN);197robot.keyRelease(KeyEvent.VK_DOWN);198199robot.waitForIdle();200checkSelectionAL(0, 0, 0);201resetList();202robot.waitForIdle();203204// L205robot.keyPress(KeyEvent.VK_L);206robot.keyRelease(KeyEvent.VK_L);207208robot.waitForIdle();209checkSelectionAL(0, 0, 0);210resetList();211robot.waitForIdle();212213// Click item 4214Point p = clickItem4();215robot.mouseMove(p.x, p.y);216robot.mousePress(InputEvent.BUTTON1_MASK);217robot.mouseRelease(InputEvent.BUTTON1_MASK);218219220robot.waitForIdle();221checkSelectionAL(4, 4, 4);222resetList();223robot.waitForIdle();224225226// Control + Click item 4227robot.keyPress(controlKey);228p = clickItem4();229robot.mouseMove(p.x, p.y);230robot.mousePress(InputEvent.BUTTON1_MASK);231robot.mouseRelease(InputEvent.BUTTON1_MASK);232robot.keyRelease(controlKey);233234235robot.waitForIdle();236checkSelectionAL(4, 4, 4);237resetList();238robot.waitForIdle();239240// Shift + Click item 4241robot.keyPress(KeyEvent.VK_SHIFT);242p = clickItem4();243robot.mouseMove(p.x, p.y);244robot.mousePress(InputEvent.BUTTON1_MASK);245robot.mouseRelease(InputEvent.BUTTON1_MASK);246robot.keyRelease(KeyEvent.VK_SHIFT);247248249robot.waitForIdle();250checkSelectionAL(0, 4, 0, 1, 2, 3, 4);251resetList();252robot.waitForIdle();253254255// Control + Shift + Click item 4256robot.keyPress(controlKey);257robot.keyPress(KeyEvent.VK_SHIFT);258p = clickItem4();259robot.mouseMove(p.x, p.y);260robot.mousePress(InputEvent.BUTTON1_MASK);261robot.mouseRelease(InputEvent.BUTTON1_MASK);262robot.keyRelease(KeyEvent.VK_SHIFT);263robot.keyRelease(controlKey);264265robot.waitForIdle();266checkSelectionAL(0, 4);267resetList();268robot.waitForIdle();269}270271private static DefaultListModel getModel() {272DefaultListModel listModel = new DefaultListModel();273for (int i = 0; i < 10; i++) {274listModel.addElement("List Item " + i);275}276return listModel;277}278279private static Point clickItem4() throws Exception {280281final Point[] result = new Point[1];282SwingUtilities.invokeAndWait(new Runnable() {283284@Override285public void run() {286Rectangle r = list.getCellBounds(4, 4);287Point p = new Point(r.x + r.width / 2, r.y + r.height / 2);288SwingUtilities.convertPointToScreen(p, list);289result[0] = p;290}291});292293return result[0];294}295296private static void resetList() throws Exception {297SwingUtilities.invokeAndWait(new Runnable() {298299@Override300public void run() {301list.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);302list.getSelectionModel().clearSelection();303setAnchorLeadNonThreadSafe();304}305});306}307308private static void scrollDownExtendSelection() throws Exception {309SwingUtilities.invokeAndWait(new Runnable() {310311@Override312public void run() {313list.getActionMap().get("scrollDownExtendSelection").314actionPerformed(new ActionEvent(list,315ActionEvent.ACTION_PERFORMED, null));316}317});318}319320private static void setSelectionMode(final int selectionMode) throws Exception {321SwingUtilities.invokeAndWait(new Runnable() {322323@Override324public void run() {325list.getSelectionModel().setSelectionMode(selectionMode);326setAnchorLeadNonThreadSafe();327}328});329}330331private static void setSelectionInterval(final int index0, final int index1) throws Exception {332SwingUtilities.invokeAndWait(new Runnable() {333334@Override335public void run() {336list.getSelectionModel().setSelectionInterval(index0, index1);337setAnchorLeadNonThreadSafe();338}339});340}341342private static void setAnchorLead(final int anchorLeadValue) throws Exception {343SwingUtilities.invokeAndWait(new Runnable() {344345@Override346public void run() {347anchorLead = anchorLeadValue;348setAnchorLeadNonThreadSafe();349}350});351}352353private static void setAnchorLeadNonThreadSafe() {354list.getSelectionModel().setAnchorSelectionIndex(anchorLead);355((DefaultListSelectionModel) list.getSelectionModel()).moveLeadSelectionIndex(anchorLead);356}357358private static void createAndShowGUI() {359frame = new JFrame("bug6462008");360frame.setSize(200, 500);361frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);362363list = new JList(getModel());364JPanel panel = new JPanel(new BorderLayout());365panel.add(list);366frame.getContentPane().add(panel);367368frame.setVisible(true);369frame.setLocationRelativeTo(null);370}371372private static void checkSelection(int... sels) throws Exception {373checkSelectionAL(DONT_CARE, DONT_CARE, sels);374}375376private static void checkSelectionAL(final int anchor, final int lead, final int... sels) throws Exception {377SwingUtilities.invokeAndWait(new Runnable() {378379@Override380public void run() {381checkSelectionNonThreadSafe(anchor, lead, sels);382}383});384}385386private static void checkSelectionNonThreadSafe(int anchor, int lead, int... sels) {387ListSelectionModel lsm = list.getSelectionModel();388389int actualAnchor = lsm.getAnchorSelectionIndex();390int actualLead = lsm.getLeadSelectionIndex();391392if (anchor != DONT_CARE && actualAnchor != anchor) {393throw new RuntimeException("anchor is " + actualAnchor + ", should be " + anchor);394}395396if (lead != DONT_CARE && actualLead != lead) {397throw new RuntimeException("lead is " + actualLead + ", should be " + lead);398}399400Arrays.sort(sels);401boolean[] checks = new boolean[list.getModel().getSize()];402for (int i : sels) {403checks[i] = true;404}405406int index0 = Math.min(lsm.getMinSelectionIndex(), 0);407int index1 = Math.max(lsm.getMaxSelectionIndex(), list.getModel().getSize() - 1);408409for (int i = index0; i <= index1; i++) {410if (lsm.isSelectedIndex(i)) {411if (i < 0 || i >= list.getModel().getSize() || !checks[i]) {412throw new RuntimeException(i + " is selected when it should not be");413}414} else if (i >= 0 && i < list.getModel().getSize() && checks[i]) {415throw new RuntimeException(i + " is supposed to be selected");416}417}418}419}420421422