Path: blob/master/test/jdk/javax/swing/DefaultButtonModel/DefaultButtonModelCrashTest.java
66644 views
/*1* Copyright (c) 2017, 2021, 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 818257726* @summary Verifies if moving focus to JToggleButton with DefaultButtonModel27* that is added to a ButtonGroup doesn't throw ClassCastException28* @key headful29* @run main DefaultButtonModelCrashTest30*/3132import java.awt.BorderLayout;33import java.awt.Robot;34import java.awt.event.KeyEvent;35import javax.swing.ButtonModel;36import javax.swing.DefaultButtonModel;37import javax.swing.JCheckBox;38import javax.swing.JFrame;39import javax.swing.JPanel;40import javax.swing.JTextField;41import javax.swing.SwingUtilities;4243public class DefaultButtonModelCrashTest {44private JFrame frame = null;4546public static void main(String[] args) throws Exception {47new DefaultButtonModelCrashTest();48}4950public DefaultButtonModelCrashTest() throws Exception {51try {52Robot robot = new Robot();53robot.setAutoDelay(200);54SwingUtilities.invokeAndWait(() -> go());55robot.waitForIdle();56robot.delay(1000);57robot.keyPress(KeyEvent.VK_TAB);58robot.keyRelease(KeyEvent.VK_TAB);59robot.delay(100);60robot.keyPress(KeyEvent.VK_TAB);61robot.keyRelease(KeyEvent.VK_TAB);62} finally {63SwingUtilities.invokeAndWait(() -> {64if (frame != null) {65frame.dispose();66}67});68}69}7071private void go() {72frame = new JFrame();73frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);7475ButtonModel model = new DefaultButtonModel();76JCheckBox check = new JCheckBox("a bit broken");77check.setModel(model);7879JPanel panel = new JPanel(new BorderLayout());80panel.add(new JTextField("Press Tab (twice?)"), BorderLayout.NORTH);81panel.add(check);8283frame.getContentPane().add(panel);84frame.setLocationRelativeTo(null);85frame.pack();86frame.setVisible(true);87}88}899091