Path: blob/master/test/jdk/javax/swing/JFileChooser/DeserializedJFileChooser/DeserializedJFileChooserTest.java
41152 views
/*1* Copyright (c) 2016, 2020, 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 814630127* @summary Enter key does not work in a deserialized JFileChooser.28* @run main DeserializedJFileChooserTest29*/3031import java.awt.EventQueue;32import java.awt.Robot;33import java.io.ByteArrayInputStream;34import java.io.ByteArrayOutputStream;35import java.io.ObjectInputStream;36import java.io.ObjectOutputStream;3738import javax.swing.JButton;39import javax.swing.JFileChooser;40import javax.swing.SwingUtilities;41import javax.swing.UIManager;42import javax.swing.UnsupportedLookAndFeelException;4344import static javax.swing.UIManager.getInstalledLookAndFeels;4546public class DeserializedJFileChooserTest {4748private static volatile JButton defaultSet;49private static JFileChooser deserialized;5051public static void main(String[] args) throws Exception {52for (UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {53EventQueue.invokeAndWait(() -> setLookAndFeel(laf));54SwingUtilities.invokeLater( () -> {55try {56JFileChooser jfc = new JFileChooser();57ByteArrayOutputStream bos = new ByteArrayOutputStream();58ObjectOutputStream oos = new ObjectOutputStream(bos);59oos.writeObject(jfc);60oos.close();61ByteArrayInputStream bis =62new ByteArrayInputStream(bos.toByteArray());63ObjectInputStream ois = new ObjectInputStream(bis);64deserialized = (JFileChooser) ois.readObject();65deserialized.showOpenDialog(null);66} catch (Exception e) {67throw new RuntimeException(e);68}69});70Robot robot = new Robot();71robot.waitForIdle();72EventQueue.invokeAndWait(()->{73defaultSet = deserialized.getRootPane().getDefaultButton();74// Trick to close the modal dialog75deserialized.setVisible(false);76Thread.currentThread().interrupt();77});78robot.waitForIdle();79if (defaultSet == null) {80throw new RuntimeException("default button is null");81}82}83}8485private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {86try {87System.out.println("laf = " + laf);88UIManager.setLookAndFeel(laf.getClassName());89} catch (UnsupportedLookAndFeelException ignored) {90System.out.println("Unsupported LookAndFeel: " + laf.getClassName());91} catch (ClassNotFoundException | InstantiationException |92IllegalAccessException e) {93throw new RuntimeException(e);94}95}96}979899