1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @bug 8191639
  26  * @summary  Verifies no NPE is thrown wjen pageup/down is pressed in a JList
  27  * @run main BasicListTest
  28  */
  29 
  30 import java.awt.Point;
  31 import java.awt.Rectangle;
  32 import java.awt.Robot;
  33 import java.awt.event.InputEvent;
  34 import java.awt.event.KeyEvent;
  35 import javax.swing.JFrame;
  36 import javax.swing.JList;
  37 import javax.swing.JScrollPane;
  38 import javax.swing.SwingUtilities;
  39 
  40 class MyList extends JList {
  41     // I need this to be able to unselect when clicking outside list content
  42     @Override
  43     public int locationToIndex(final Point location) {
  44         final int n = super.locationToIndex(location);
  45         //return n;
  46         final Rectangle q = getCellBounds(n, n);
  47         return q != null && q.contains(location)?n:-1;
  48     }
  49 }
  50 
  51 public class BasicListTest {    
  52     private static void initComponents() {
  53         f = new JFrame();
  54         jScrollPane1 = new JScrollPane();
  55         list1 = new MyList();
  56         
  57         f.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  58         
  59         list1.setModel(new javax.swing.AbstractListModel() {
  60             String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
  61             @Override
  62             public int getSize() { return strings.length; }
  63             @Override
  64             public Object getElementAt(int i) { return strings[i]; }
  65         });
  66         jScrollPane1.setViewportView(list1);
  67         
  68         f.getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
  69         
  70         f.pack();
  71         f.setVisible(true);
  72         p = list1.getLocationOnScreen();
  73     }
  74     
  75     public static void main(String args[]) throws Exception {
  76         try {
  77             SwingUtilities.invokeAndWait(() -> {
  78                 initComponents();
  79             });
  80             Robot robot = new Robot();
  81             robot.setAutoDelay(200);
  82             robot.mouseMove(p.x, p.y);
  83             robot.waitForIdle();
  84             robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
  85             robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
  86             robot.waitForIdle();
  87             robot.keyPress(KeyEvent.VK_PAGE_DOWN);
  88             robot.keyRelease(KeyEvent.VK_PAGE_DOWN);
  89         
  90             try {
  91                 Thread.sleep(1000);
  92             } catch (InterruptedException ex) {
  93             }
  94         } finally {
  95             SwingUtilities.invokeAndWait(() -> {
  96                 f.dispose();
  97             });
  98         }
  99     }   
 100     
 101     private static JScrollPane jScrollPane1;
 102     private static MyList list1;
 103     private static Point p;
 104     private static JFrame f;
 105     
 106 }