1/*
2 * Copyright (c) 2007, 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 * @bug 6365992 6379599 8137137
27 * @summary REG: Showing and disposing a native print dialog makes the main
28 *  frame inactive, Win32
29 * @run main/manual RestoreActiveWindowTest
30 */
31import java.awt.Frame;
32import java.awt.Button;
33import java.awt.GridBagLayout;
34import java.awt.Panel;
35import java.awt.TextArea;
36import java.awt.GridLayout;
37import java.awt.GridBagConstraints;
38import java.awt.Color;
39import java.awt.event.ActionEvent;
40import java.awt.event.ActionListener;
41import java.awt.event.WindowAdapter;
42import java.awt.event.WindowEvent;
43import java.awt.print.PrinterJob;
44import java.awt.print.PageFormat;
45
46public class RestoreActiveWindowTest implements ActionListener {
47
48    private static Frame mainFrame;
49    private static Button printDialogButton;
50    private static Button pageDialogButton;
51    private static Frame instructionFrame;
52    private static GridBagLayout layout;
53    private static Panel mainControlPanel;
54    private static Panel resultButtonPanel;
55    private static TextArea instructionTextArea;
56    private static Button passButton;
57    private static Button failButton;
58    private static Thread mainThread = null;
59    private static boolean testPassed = false;
60    private static boolean isInterrupted = false;
61    private static final int testTimeOut = 300000;
62    private static String testFailMessage;
63
64    public void createAndShowGUI() {
65        mainFrame = new Frame("Test");
66        mainFrame.setSize(200, 200);
67        mainFrame.setLocationRelativeTo(null);
68        mainFrame.setLayout(new GridLayout(2, 1));
69
70        printDialogButton = new Button("show a native print dialog");
71        pageDialogButton = new Button("show a native page dialog");
72        printDialogButton.addActionListener(new ActionListener() {
73            @Override
74            public void actionPerformed(ActionEvent ae) {
75                PrinterJob.getPrinterJob().printDialog();
76                setButtonEnable(true);
77                testFailMessage = "Print dialog test failed.";
78            }
79        });
80        pageDialogButton.addActionListener(new ActionListener() {
81            @Override
82            public void actionPerformed(ActionEvent ae) {
83                PrinterJob.getPrinterJob().pageDialog(new PageFormat());
84                setButtonEnable(true);
85                testFailMessage = "Page dialog test failed.";
86            }
87        });
88
89        mainFrame.add(printDialogButton);
90        mainFrame.add(pageDialogButton);
91        mainFrame.setVisible(true);
92
93       mainFrame.addWindowListener(new WindowAdapter() {
94            public void windowClosing(WindowEvent we) {
95                cleanUp();
96                throw new RuntimeException("User has closed the test window "
97                        + "without clicking Pass or Fail.");
98            }
99        });
100    }
101
102    private void createInstructionUI() {
103        instructionFrame = new Frame("Native Print Dialog and Page Dialog");
104        layout = new GridBagLayout();
105        mainControlPanel = new Panel(layout);
106        resultButtonPanel = new Panel(layout);
107
108        GridBagConstraints gbc = new GridBagConstraints();
109        String instructions
110                = "\nINSTRUCTIONS:\n"
111                + "\n   1. Click on the 'show a native print dialog' button. A "
112                + "native print dialog will come up."
113                + "\n   2. Click on the 'Cancel' button on Mac OS X or "
114                + "'close'(X) on other paltforms. Dialog will be closed."
115                + "\n   3. After the dialog is closed another window should "
116                + "become the active window."
117                + "\n   4. If there no any active window then the test has "
118                + "failed. Click on 'Fail' Button."
119                + "\n   5. Click on the 'show a native page dialog' button. A "
120                + "native page dialog will come up."
121                + "\n   6. Click on the 'Cancel' button on Mac OS X or "
122                + "'close'(X) on other paltforms. Dialog will be closed."
123                + "\n   7. After the dialog is closed another window should "
124                + "become the active window."
125                + "\n   8. If there no any active window then the test has "
126                + "failed. Click on 'Fail' Button."
127                + "\n   9. Test Passed. Click on 'Pass' Button.";
128
129        instructionTextArea = new TextArea(13, 80);
130        instructionTextArea.setText(instructions);
131        instructionTextArea.setEnabled(false);
132        instructionTextArea.setBackground(Color.white);
133
134        gbc.gridx = 0;
135        gbc.gridy = 0;
136        gbc.weightx = 0.5;
137        gbc.fill = GridBagConstraints.HORIZONTAL;
138        mainControlPanel.add(instructionTextArea, gbc);
139
140        passButton = new Button("Pass");
141        passButton.setName("Pass");
142        passButton.addActionListener((ActionListener) this);
143
144        failButton = new Button("Fail");
145        failButton.setName("Fail");
146        failButton.addActionListener((ActionListener) this);
147
148        setButtonEnable(false);
149
150        gbc.gridx = 0;
151        gbc.gridy = 0;
152        resultButtonPanel.add(passButton, gbc);
153        gbc.gridx = 1;
154        gbc.gridy = 0;
155        resultButtonPanel.add(failButton, gbc);
156        gbc.gridx = 0;
157        gbc.gridy = 1;
158        mainControlPanel.add(resultButtonPanel, gbc);
159
160        instructionFrame.add(mainControlPanel);
161        instructionFrame.pack();
162        instructionFrame.setVisible(true);
163    }
164
165    @Override
166    public void actionPerformed(ActionEvent ae) {
167        if (ae.getSource() instanceof Button) {
168            Button btn = (Button) ae.getSource();
169            switch (btn.getName()) {
170                case "Pass":
171                    testPassed = true;
172                    isInterrupted = true;
173                    mainThread.interrupt();
174                    break;
175                case "Fail":
176                    testPassed = false;
177                    isInterrupted = true;
178                    mainThread.interrupt();
179                    break;
180            }
181        }
182    }
183
184    private static void setButtonEnable(boolean status) {
185        passButton.setEnabled(status);
186        failButton.setEnabled(status);
187    }
188
189    private static void cleanUp() {
190        mainFrame.dispose();
191        instructionFrame.dispose();
192    }
193
194    public static void main(String args[]) {
195        RestoreActiveWindowTest printDialogs = new RestoreActiveWindowTest();
196        printDialogs.createInstructionUI();
197        printDialogs.createAndShowGUI();
198
199        mainThread = Thread.currentThread();
200        try {
201            mainThread.sleep(testTimeOut);
202        } catch (InterruptedException ex) {
203            if (!testPassed) {
204                throw new RuntimeException(testFailMessage);
205            }
206        } finally {
207            cleanUp();
208        }
209
210        if (!isInterrupted) {
211            throw new RuntimeException("Test Timed out after "
212                    + testTimeOut / 1000 + " seconds");
213        }
214    }
215}
216