1 /*
2  * Copyright (c) 2003, 2007, 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 4507585
27   @summary Native modal dialog shouldn't block event dispatching when called on EventDispatchThread.
28   @author tav@sparc.spb.su: area=awt.PrintJob
29   @run main/manual=yesno PageSetupDlgBlockingTest
30 
31 */
32 
33 import java.awt.*;
34 import java.awt.print.*;
35 import java.awt.event.*;
36 import javax.swing.*;
37 import java.applet.*;
38 
39 public class PageSetupDlgBlockingTest extends Panel {
40     public static Frame frame = new TestFrame("Test Frame");
41 
main(String[] args)42     public static void main(String[] args) {
43         PageSetupDlgBlockingTest a = new PageSetupDlgBlockingTest();
44 
45         a.init();
46         a.start();
47     }
48 
init()49     public void init()
50     {
51         //Create instructions for the user here, as well as set up
52         // the environment -- set the layout manager, add buttons,
53         // etc.
54         this.setLayout (new BorderLayout ());
55 
56         String[] instructions =
57         {
58             "This test verifies that native modal 'Page Setup' dialog doesn't block event",
59             "handling when called on EventDispatchThread.",
60             " ",
61             "After test started you will see 'Test Frame' frame which contains",
62             "one 'Click Me' button.",
63             "1. Click the button:",
64             "   - 'Page Setup' dialog will appear.",
65             "2. Drag the dialog over the 'Test Frame' so that to enforce its button redraw:",
66             "   - if you're seeing the button redraw (as long as PAINT events are displayed)",
67             "     the test PASSED else FAILED."
68         };
69         Sysout.createDialogWithInstructions(instructions);
70     }
71 
72 
start()73     public void start() {
74         JButton button = new JButton("Click Me");
75         final AWTEventListener listener = new AWTEventListener() {
76                 public void eventDispatched(AWTEvent e) {
77                     if (e.getSource().getClass() == TestFrame.class) {
78                         Sysout.println(e.paramString() + " on <Test Frame>");
79                     }
80                 }
81             };
82 
83         button.addActionListener(new ActionListener() {
84                 public void actionPerformed(ActionEvent e) {
85 
86                     // Show PAINT events only when the dialog is displayed.
87                     Toolkit.getDefaultToolkit().addAWTEventListener(listener, AWTEvent.PAINT_EVENT_MASK);
88 
89                     PrinterJob job = PrinterJob.getPrinterJob();
90                     job.pageDialog(job.defaultPage());
91 
92                     Toolkit.getDefaultToolkit().removeAWTEventListener(listener);
93                 }
94             });
95 
96         button.setSize(100, 50);
97 
98         frame.setLayout(new BorderLayout());
99         frame.setSize(200, 200);
100         frame.setLocation(500, 0);
101         frame.add(button, BorderLayout.CENTER);
102         frame.setVisible(true);
103     }
104 }
105 
106 class TestFrame extends Frame {
TestFrame(String title)107     TestFrame(String title) {
108         super(title);
109     }
110 }
111 
112 /****************************************************
113  Standard Test Machinery
114  DO NOT modify anything below -- it's a standard
115   chunk of code whose purpose is to make user
116   interaction uniform, and thereby make it simpler
117   to read and understand someone else's test.
118  ****************************************************/
119 
120 /**
121  This is part of the standard test machinery.
122  It creates a dialog (with the instructions), and is the interface
123   for sending text messages to the user.
124  To print the instructions, send an array of strings to Sysout.createDialog
125   WithInstructions method.  Put one line of instructions per array entry.
126  To display a message for the tester to see, simply call Sysout.println
127   with the string to be displayed.
128  This mimics System.out.println but works within the test harness as well
129   as standalone.
130  */
131 
132 class Sysout
133 {
134     private static TestDialog dialog;
135 
createDialogWithInstructions( String[] instructions )136     public static void createDialogWithInstructions( String[] instructions )
137     {
138         dialog = new TestDialog( new Frame(), "Instructions" );
139         dialog.printInstructions( instructions );
140         dialog.setVisible(true);
141         println( "Any messages for the tester will display here." );
142     }
143 
createDialog( )144     public static void createDialog( )
145     {
146         dialog = new TestDialog( new Frame(), "Instructions" );
147         String[] defInstr = { "Instructions will appear here. ", "" } ;
148         dialog.printInstructions( defInstr );
149         dialog.setVisible(true);
150         println( "Any messages for the tester will display here." );
151     }
152 
153 
printInstructions( String[] instructions )154     public static void printInstructions( String[] instructions )
155     {
156         dialog.printInstructions( instructions );
157     }
158 
159 
println( String messageIn )160     public static void println( String messageIn )
161     {
162         dialog.displayMessage( messageIn );
163     }
164 
165 }// Sysout  class
166 
167 /**
168   This is part of the standard test machinery.  It provides a place for the
169    test instructions to be displayed, and a place for interactive messages
170    to the user to be displayed.
171   To have the test instructions displayed, see Sysout.
172   To have a message to the user be displayed, see Sysout.
173   Do not call anything in this dialog directly.
174   */
175 class TestDialog extends Dialog
176 {
177 
178     TextArea instructionsText;
179     TextArea messageText;
180     int maxStringLength = 80;
181 
182     //DO NOT call this directly, go through Sysout
TestDialog( Frame frame, String name )183     public TestDialog( Frame frame, String name )
184     {
185         super( frame, name );
186         int scrollBoth = TextArea.SCROLLBARS_BOTH;
187         instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
188         add( "North", instructionsText );
189 
190         messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
191         add("Center", messageText);
192 
193         pack();
194 
195         setVisible(true);
196     }// TestDialog()
197 
198     //DO NOT call this directly, go through Sysout
printInstructions( String[] instructions )199     public void printInstructions( String[] instructions )
200     {
201         //Clear out any current instructions
202         instructionsText.setText( "" );
203 
204         //Go down array of instruction strings
205 
206         String printStr, remainingStr;
207         for( int i=0; i < instructions.length; i++ )
208         {
209             //chop up each into pieces maxSringLength long
210             remainingStr = instructions[ i ];
211             while( remainingStr.length() > 0 )
212             {
213                 //if longer than max then chop off first max chars to print
214                 if( remainingStr.length() >= maxStringLength )
215                 {
216                     //Try to chop on a word boundary
217                     int posOfSpace = remainingStr.
218                         lastIndexOf( ' ', maxStringLength - 1 );
219 
220                     if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
221 
222                     printStr = remainingStr.substring( 0, posOfSpace + 1 );
223                     remainingStr = remainingStr.substring( posOfSpace + 1 );
224                 }
225                 //else just print
226                 else
227                 {
228                     printStr = remainingStr;
229                     remainingStr = "";
230                 }
231 
232                 instructionsText.append( printStr + "\n" );
233 
234             }// while
235 
236         }// for
237 
238     }//printInstructions()
239 
240     //DO NOT call this directly, go through Sysout
displayMessage( String messageIn )241     public void displayMessage( String messageIn )
242     {
243         messageText.append( messageIn + "\n" );
244         System.out.println(messageIn);
245     }
246 
247 }// TestDialog  class
248