1/*
2 * Copyright (c) 2016, 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 * @key headful
27 * @bug 8146301
28 * @summary Enter key does not work in a deserialized JFileChooser.
29 * @run main DeserializedJFileChooserTest
30 */
31
32import javax.swing.*;
33import java.awt.*;
34import java.awt.event.KeyEvent;
35import java.io.ByteArrayInputStream;
36import java.io.ByteArrayOutputStream;
37import java.io.ObjectInputStream;
38import java.io.ObjectOutputStream;
39
40public class DeserializedJFileChooserTest {
41
42    private static int state = -1;
43    private static JFileChooser deserialized;
44
45    public static void main(String[] args) throws Exception {
46        SwingUtilities.invokeLater( () -> {
47            try {
48                JFileChooser jfc = new JFileChooser();
49                ByteArrayOutputStream bos = new ByteArrayOutputStream();
50                ObjectOutputStream oos = new ObjectOutputStream(bos);
51                oos.writeObject(jfc);
52                oos.close();
53                ByteArrayInputStream bis =
54                        new ByteArrayInputStream(bos.toByteArray());
55                ObjectInputStream ois = new ObjectInputStream(bis);
56                deserialized = (JFileChooser) ois.readObject();
57                state = deserialized.showOpenDialog(null);
58            } catch (Exception e) {
59                throw new RuntimeException(e);
60            }
61        });
62        Robot robot = new Robot();
63        robot.setAutoDelay(50);
64        robot.waitForIdle();
65        robot.keyPress(KeyEvent.VK_A);
66        robot.keyRelease(KeyEvent.VK_A);
67        robot.keyPress(KeyEvent.VK_ENTER);
68        robot.keyRelease(KeyEvent.VK_ENTER);
69        robot.waitForIdle();
70        robot.delay(1000);
71        if (state != JFileChooser.APPROVE_OPTION) {
72            deserialized.cancelSelection();
73            throw new RuntimeException("Failed");
74        }
75    }
76}
77