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 /**
  25  * @test
  26  * @bug 8182577
  27  * @summary  Verifies if moving focus via custom ButtonModel causes crash
  28  * @key headful
  29  * @run main DefaultButtonModelCrashTest
  30  */
  31 
  32 import java.awt.BorderLayout;
  33 import java.awt.Container;
  34 import java.awt.Point;
  35 import java.awt.Robot;
  36 import java.awt.event.KeyEvent;
  37 import javax.swing.ButtonModel;
  38 import javax.swing.DefaultButtonModel;
  39 import javax.swing.JCheckBox;
  40 import javax.swing.JComponent;
  41 import javax.swing.JFrame;
  42 import javax.swing.JPanel;
  43 import javax.swing.JTextField;
  44 import javax.swing.SwingUtilities;
  45 
  46 public class DefaultButtonModelCrashTest {
  47     private JFrame frame = null;
  48     private JPanel panel;
  49     private volatile Point p = null;
  50 
  51     public static void main(String[] args) throws Exception {
  52         new DefaultButtonModelCrashTest();
  53     }
  54 
  55     public DefaultButtonModelCrashTest() throws Exception {
  56         try {
  57             Robot robot = new Robot();
  58             robot.setAutoDelay(200);
  59             SwingUtilities.invokeAndWait(() -> go());
  60             robot.waitForIdle();
  61             robot.keyPress(KeyEvent.VK_TAB);
  62             robot.keyRelease(KeyEvent.VK_TAB);
  63             robot.delay(100);
  64             robot.keyPress(KeyEvent.VK_TAB);
  65             robot.keyRelease(KeyEvent.VK_TAB);
  66         } finally {
  67             if (frame != null) { SwingUtilities.invokeAndWait(()->frame.dispose()); }
  68         }
  69     }
  70 
  71     private void go() {
  72 
  73         frame = new JFrame();
  74         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  75         Container contentPane = frame.getContentPane();
  76         ButtonModel model = new DefaultButtonModel();
  77 
  78         JCheckBox check = new JCheckBox("a bit broken");
  79         check.setModel(model);
  80         panel = new JPanel(new BorderLayout());
  81         panel.add(new JTextField("Press Tab (twice?)"), BorderLayout.NORTH);
  82         panel.add(check);
  83         contentPane.add(panel);
  84         frame.setLocationRelativeTo(null);
  85         frame.pack();
  86         frame.setVisible(true);
  87     }
  88 }