Path: blob/master/test/jdk/javax/swing/JList/BasicListTest.java
41149 views
/*1* Copyright (c) 2017, 2018, 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*/22/*23* @test24* @bug 8191639 819644325* @key headful26* @summary Verifies no NPE is thrown when pageup/down is pressed in a JList27* @run main BasicListTest28*/2930import java.awt.Point;31import java.awt.Rectangle;32import java.awt.Robot;33import java.awt.event.InputEvent;34import java.awt.event.KeyEvent;35import javax.swing.JFrame;36import javax.swing.JList;37import javax.swing.JScrollPane;38import javax.swing.SwingUtilities;39import javax.swing.UIManager;40import javax.swing.UnsupportedLookAndFeelException;4142class MyList extends JList {43// I need this to be able to unselect when clicking outside list content44@Override45public int locationToIndex(final Point location) {46final int n = super.locationToIndex(location);47//return n;48final Rectangle q = getCellBounds(n, n);49return q != null && q.contains(location)?n:-1;50}51}5253public class BasicListTest {54private static void initComponents() {55f = new JFrame();56jScrollPane1 = new JScrollPane();57list1 = new MyList();5859f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);6061list1.setModel(new javax.swing.AbstractListModel() {62String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };63@Override64public int getSize() { return strings.length; }65@Override66public Object getElementAt(int i) { return strings[i]; }67});68jScrollPane1.setViewportView(list1);6970f.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);7172f.pack();73f.setVisible(true);74p = list1.getLocationOnScreen();75}7677private static void setLookAndFeel(final UIManager.LookAndFeelInfo laf) {78try {79UIManager.setLookAndFeel(laf.getClassName());80} catch (ClassNotFoundException | InstantiationException |81UnsupportedLookAndFeelException | IllegalAccessException e) {82throw new RuntimeException(e);83}84}8586public static void main(String args[]) throws Exception {87for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {88try {89SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));90System.out.println("Test for LookAndFeel " + laf.getClassName());91SwingUtilities.invokeAndWait(() -> {92initComponents();93});94System.out.println("Test passed for LookAndFeel " + laf.getClassName());95} catch (Exception e) {96throw new RuntimeException(e);97}9899Robot robot = new Robot();100robot.setAutoDelay(200);101robot.mouseMove(p.x, p.y);102robot.waitForIdle();103robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);104robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);105robot.waitForIdle();106robot.keyPress(KeyEvent.VK_PAGE_DOWN);107robot.keyRelease(KeyEvent.VK_PAGE_DOWN);108109try {110Thread.sleep(1000);111} catch (InterruptedException ex) {112}113SwingUtilities.invokeAndWait(() -> {114f.dispose();115});116}117}118119private static JScrollPane jScrollPane1;120private static MyList list1;121private static Point p;122private static JFrame f;123124}125126127