8152332: [macosx] JFileChooser cannot be serialized on Mac OS X

Reviewed-by: pbansal, jdv
This commit is contained in:
Sergey Bylokhov 2020-04-13 23:43:37 -07:00
parent 816d9483f0
commit 2d350addf7
2 changed files with 53 additions and 33 deletions

View File

@ -785,7 +785,6 @@ javax/swing/JComboBox/8072767/bug8072767.java 8196093 windows-all,macosx-all
javax/swing/JComponent/4337267/bug4337267.java 8146451 windows-all
javax/swing/JFileChooser/4524490/bug4524490.java 8042380 generic-all
javax/swing/JFileChooser/8002077/bug8002077.java 8196094 windows-all,macosx-all
javax/swing/JFileChooser/DeserializedJFileChooser/DeserializedJFileChooserTest.java 8196095 generic-all
javax/swing/JFileChooser/6396844/TwentyThousandTest.java 8198003 generic-all
javax/swing/JFrame/8175301/ScaledFrameBackgroundTest.java 8193942 generic-all
javax/swing/JList/6462008/bug6462008.java 7156347 generic-all

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -29,48 +29,69 @@
* @run main DeserializedJFileChooserTest
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.EventQueue;
import java.awt.Robot;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import static javax.swing.UIManager.getInstalledLookAndFeels;
public class DeserializedJFileChooserTest {
private static int state = -1;
private static volatile JButton defaultSet;
private static JFileChooser deserialized;
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater( () -> {
try {
JFileChooser jfc = new JFileChooser();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(jfc);
oos.close();
ByteArrayInputStream bis =
new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
deserialized = (JFileChooser) ois.readObject();
state = deserialized.showOpenDialog(null);
} catch (Exception e) {
throw new RuntimeException(e);
for (UIManager.LookAndFeelInfo laf : getInstalledLookAndFeels()) {
EventQueue.invokeAndWait(() -> setLookAndFeel(laf));
SwingUtilities.invokeLater( () -> {
try {
JFileChooser jfc = new JFileChooser();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(jfc);
oos.close();
ByteArrayInputStream bis =
new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);
deserialized = (JFileChooser) ois.readObject();
deserialized.showOpenDialog(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
});
Robot robot = new Robot();
robot.waitForIdle();
EventQueue.invokeAndWait(()->{
defaultSet = deserialized.getRootPane().getDefaultButton();
// Trick to close the modal dialog
deserialized.setVisible(false);
Thread.currentThread().interrupt();
});
robot.waitForIdle();
if (defaultSet == null) {
throw new RuntimeException("default button is null");
}
});
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_A);
robot.keyRelease(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.waitForIdle();
robot.delay(1000);
if (state != JFileChooser.APPROVE_OPTION) {
deserialized.cancelSelection();
throw new RuntimeException("Failed");
}
}
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
try {
System.out.println("laf = " + laf);
UIManager.setLookAndFeel(laf.getClassName());
} catch (UnsupportedLookAndFeelException ignored) {
System.out.println("Unsupported LookAndFeel: " + laf.getClassName());
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}