1/*
2 * Copyright (c) 2015, 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 @bug 8037575
26 * @summary JFrame doesn't animate when setting ICONIFIED state
27 * @requires (os.family == "windows")
28 * @run main/manual bug8037575
29 */
30import java.awt.Frame;
31import javax.swing.JFrame;
32import javax.swing.SwingUtilities;
33import java.awt.*;
34import java.awt.event.*;
35import java.util.logging.Level;
36import java.util.logging.Logger;
37import javax.swing.AbstractAction;
38import javax.swing.JButton;
39import javax.swing.JDialog;
40import javax.swing.Timer;
41
42public class bug8037575 {
43
44    private static boolean theTestPassed;
45    private static boolean testGeneratedInterrupt;
46    private static Thread mainThread;
47    private static int sleepTime = 30000;
48    private static int waitTime = 2000;
49    private final static JFrame frame = new JFrame("bug8037575");
50
51    private static void init() throws Exception {
52
53        SwingUtilities.invokeAndWait(new Runnable() {
54            @Override
55            public void run() {
56                String[] instructions
57                        = {
58                            "1) You see a dialog with buttons and text area.",
59                            "2) Pressing Run button will start test. A new Frame will open automatically"
60                        + " and minimize after 2 seconds.",
61                            "3) Frame should minimize gradually with animation effect.",
62                            "4) If frame disappers without animation then test "
63                            + "failed otherwise passed.",
64                            "5) Pressing Pass/Fail button will mark test as "
65                            + "pass/fail and will shutdown JVM as well"};
66
67                Sysout.createDialogWithInstructions(instructions);
68                Sysout.printInstructions(instructions);
69            }
70        });
71    }
72
73    /**
74     * ***************************************************
75     * Standard Test Machinery Section DO NOT modify anything in this section --
76     * it's a standard chunk of code which has all of the synchronisation
77     * necessary for the test harness. By keeping it the same in all tests, it
78     * is easier to read and understand someone else's test, as well as insuring
79     * that all tests behave correctly with the test harness. There is a section
80     * following this for test-defined classes
81     */
82    public static void main(String args[]) throws InterruptedException {
83
84        mainThread = Thread.currentThread();
85        try {
86            init();
87        } catch (Exception ex) {
88            Logger.getLogger(bug8037575.class.getName()).log(Level.SEVERE, null, ex);
89        }
90        try {
91            mainThread.sleep(sleepTime);
92        } catch (InterruptedException ex) {
93            Sysout.dispose();
94            if (!theTestPassed && testGeneratedInterrupt) {
95                throw new RuntimeException("Test Failed");
96            }
97        }
98        if (!testGeneratedInterrupt) {
99            Sysout.dispose();
100            throw new RuntimeException("Test Failed");
101        }
102    }
103
104    public static synchronized void pass() {
105        theTestPassed = true;
106        testGeneratedInterrupt = true;
107        mainThread.interrupt();
108    }
109
110    public static synchronized void runTest() {
111        frame.setSize(800, 600);
112        frame.setVisible(true);
113        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
114        Timer timer = new Timer(waitTime, new AbstractAction() {
115            @Override
116            public void actionPerformed(ActionEvent ae) {
117                frame.setExtendedState(Frame.ICONIFIED);
118                frame.dispose();
119                Sysout.println("Test completed please press/fail button");
120            }
121        });
122        timer.setRepeats(false);
123        timer.start();
124    }
125
126    public static synchronized void fail() {
127        theTestPassed = false;
128        testGeneratedInterrupt = true;
129        mainThread.interrupt();
130    }
131}
132
133/**
134 * This is part of the standard test machinery. It creates a dialog (with the
135 * instructions), and is the interface for sending text messages to the user. To
136 * print the instructions, send an array of strings to Sysout.createDialog
137 * WithInstructions method. Put one line of instructions per array entry. To
138 * display a message for the tester to see, simply call Sysout.println with the
139 * string to be displayed. This mimics System.out.println but works within the
140 * test harness as well as standalone.
141 */
142class Sysout {
143
144    private static TestDialog dialog;
145    private static JFrame frame;
146
147    public static void createDialogWithInstructions(String[] instructions) {
148        frame = new JFrame();
149        dialog = new TestDialog(frame, "Instructions");
150        dialog.printInstructions(instructions);
151        dialog.setVisible(true);
152        println("Any messages for the tester will display here.");
153    }
154
155    public static void printInstructions(String[] instructions) {
156        dialog.printInstructions(instructions);
157    }
158
159    public static void println(String messageIn) {
160        dialog.displayMessage(messageIn);
161    }
162
163    public static void dispose() {
164        Sysout.println("Shutting down the Java process..");
165        frame.dispose();
166        dialog.dispose();
167    }
168}
169
170/**
171 * This is part of the standard test machinery. It provides a place for the test
172 * instructions to be displayed, and a place for interactive messages to the
173 * user to be displayed. To have the test instructions displayed, see Sysout. To
174 * have a message to the user be displayed, see Sysout. Do not call anything in
175 * this dialog directly.
176 */
177class TestDialog extends JDialog {
178
179    private TextArea instructionsText;
180    private TextArea messageText;
181    private int maxStringLength = 80;
182    private Panel buttonP = new Panel();
183    private JButton run = new JButton("Run");
184    private JButton passB = new JButton("Pass");
185    private JButton failB = new JButton("Fail");
186
187    public TestDialog(JFrame frame, String name) {
188        super(frame, name);
189        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
190        int scrollBoth = TextArea.SCROLLBARS_BOTH;
191        instructionsText = new TextArea("", 15, maxStringLength, scrollBoth);
192        add("North", instructionsText);
193
194        messageText = new TextArea("", 5, maxStringLength, scrollBoth);
195        add("Center", messageText);
196
197        buttonP.add("East", run);
198        buttonP.add("East", passB);
199        buttonP.add("West", failB);
200        passB.setEnabled(false);
201        failB.setEnabled(false);
202        add("South", buttonP);
203
204        run.addActionListener(new ActionListener() {
205
206            @Override
207            public void actionPerformed(ActionEvent ae) {
208                bug8037575.runTest();
209                passB.setEnabled(true);
210                failB.setEnabled(true);
211            }
212        });
213
214        passB.addActionListener(new ActionListener() {
215
216            @Override
217            public void actionPerformed(ActionEvent ae) {
218                bug8037575.pass();
219            }
220        });
221
222        failB.addActionListener(new ActionListener() {
223
224            @Override
225            public void actionPerformed(ActionEvent ae) {
226                bug8037575.fail();
227            }
228        });
229        pack();
230
231        setVisible(true);
232    }
233
234    public void printInstructions(String[] instructions) {
235        instructionsText.setText("");
236        String printStr, remainingStr;
237        for (String instruction : instructions) {
238            remainingStr = instruction;
239            while (remainingStr.length() > 0) {
240                if (remainingStr.length() >= maxStringLength) {
241                    int posOfSpace = remainingStr.
242                            lastIndexOf(' ', maxStringLength - 1);
243
244                    if (posOfSpace <= 0) {
245                        posOfSpace = maxStringLength - 1;
246                    }
247
248                    printStr = remainingStr.substring(0, posOfSpace + 1);
249                    remainingStr = remainingStr.substring(posOfSpace + 1);
250                } else {
251                    printStr = remainingStr;
252                    remainingStr = "";
253                }
254                instructionsText.append(printStr + "\n");
255            }
256        }
257
258    }
259
260    public void displayMessage(String messageIn) {
261        messageText.append(messageIn + "\n");
262    }
263}