blob: 57bd73a2b28f681bc5f23e99027449b7c5fd4b52 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 test
26 @bug 4092033 4529626
27 @summary Tests that toFront makes window focused unless it is non-focusable
28 @author area=awt.focus
29 @run applet ToFrontFocus.html
30*/
31
32/**
33 * ToFrontFocus.java
34 *
35 * summary:
36 */
37
38import java.applet.Applet;
39import java.awt.*;
40import java.awt.event.*;
41import test.java.awt.regtesthelpers.Util;
42
43public class ToFrontFocus extends Applet
44 {
45 //Declare things used in the test, like buttons and labels here
46
47 Frame cover, focus_frame, nonfocus_frame;
48 Button focus_button, nonfocus_button;
49 volatile boolean focus_gained = false, nonfocus_gained = false;
50 public void init()
51 {
52 //Create instructions for the user here, as well as set up
53 // the environment -- set the layout manager, add buttons,
54 // etc.
55
56 this.setLayout (new BorderLayout ());
57
58 String[] instructions =
59 {
60 "This is an AUTOMATIC test",
61 "simply wait until it is done"
62 };
63 Sysout.createDialog( );
64 Sysout.printInstructions( instructions );
65 cover = new Frame("Cover frame");
66 cover.setBounds(100, 100, 200, 200);
67 focus_frame = new Frame("Focusable frame");
68 focus_frame.setBounds(150, 100, 250, 150);
69 nonfocus_frame = new Frame("Non-focusable frame");
70 nonfocus_frame.setFocusableWindowState(false);
71 nonfocus_frame.setBounds(150, 150, 250, 200);
72 focus_button = new Button("Button in focusable frame");
73 focus_button.addFocusListener(new FocusAdapter() {
74 public void focusGained(FocusEvent e) {
75 focus_gained = true;
76 }
77 });
78 nonfocus_button = new Button("Button in non-focusable frame");
79 nonfocus_button.addFocusListener(new FocusAdapter() {
80 public void focusGained(FocusEvent e) {
81 nonfocus_gained = true;
82 }
83 });
84 }//End init()
85
86 public void start ()
87 {
88 //Get things going. Request focus, set size, et cetera
89 setSize (200,200);
90 show();
91 Util.waitForIdle(null);
92
93 focus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
94 public Component getInitialComponent(Window w) {
95 return null;
96 }
97 });
98 focus_frame.setVisible(true);
99 nonfocus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
100 public Component getInitialComponent(Window w) {
101 return null;
102 }
103 });
104 nonfocus_frame.setVisible(true);
105 cover.setVisible(true);
106
107 Util.waitForIdle(null);
108
109 // So events are no generated at the creation add buttons here.
110 focus_frame.add(focus_button);
111 focus_frame.pack();
112 focus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
113 public Component getInitialComponent(Window w) {
114 return focus_button;
115 }
116 });
117 nonfocus_frame.add(nonfocus_button);
118 nonfocus_frame.pack();
119 nonfocus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() {
120 public Component getInitialComponent(Window w) {
121 return nonfocus_button;
122 }
123 });
124
125 System.err.println("------------ Starting test ------------");
126 // Make frame focused - focus_gained will be genereated for button.
127 focus_frame.toFront();
128 // focus_gained should not be generated
129 nonfocus_frame.toFront();
130
131 // Wait for events.
132 Util.waitForIdle(null);
133
134 if (!focus_gained || nonfocus_gained) {
135 throw new RuntimeException("ToFront doesn't work as expected");
136 }
137 }// start()
138
139 }// class ToFrontFocus
140
141
142/****************************************************
143 Standard Test Machinery
144 DO NOT modify anything below -- it's a standard
145 chunk of code whose purpose is to make user
146 interaction uniform, and thereby make it simpler
147 to read and understand someone else's test.
148 ****************************************************/
149
150/**
151 This is part of the standard test machinery.
152 It creates a dialog (with the instructions), and is the interface
153 for sending text messages to the user.
154 To print the instructions, send an array of strings to Sysout.createDialog
155 WithInstructions method. Put one line of instructions per array entry.
156 To display a message for the tester to see, simply call Sysout.println
157 with the string to be displayed.
158 This mimics System.out.println but works within the test harness as well
159 as standalone.
160 */
161
162class Sysout
163 {
164 private static TestDialog dialog;
165
166 public static void createDialogWithInstructions( String[] instructions )
167 {
168 dialog = new TestDialog( new Frame(), "Instructions" );
169 dialog.printInstructions( instructions );
170 dialog.show();
171 println( "Any messages for the tester will display here." );
172 }
173
174 public static void createDialog( )
175 {
176 dialog = new TestDialog( new Frame(), "Instructions" );
177 String[] defInstr = { "Instructions will appear here. ", "" } ;
178 dialog.printInstructions( defInstr );
179 dialog.show();
180 println( "Any messages for the tester will display here." );
181 }
182
183
184 public static void printInstructions( String[] instructions )
185 {
186 dialog.printInstructions( instructions );
187 }
188
189
190 public static void println( String messageIn )
191 {
192 dialog.displayMessage( messageIn );
193 }
194
195 }// Sysout class
196
197/**
198 This is part of the standard test machinery. It provides a place for the
199 test instructions to be displayed, and a place for interactive messages
200 to the user to be displayed.
201 To have the test instructions displayed, see Sysout.
202 To have a message to the user be displayed, see Sysout.
203 Do not call anything in this dialog directly.
204 */
205class TestDialog extends Dialog
206 {
207
208 TextArea instructionsText;
209 TextArea messageText;
210 int maxStringLength = 80;
211
212 //DO NOT call this directly, go through Sysout
213 public TestDialog( Frame frame, String name )
214 {
215 super( frame, name );
216 int scrollBoth = TextArea.SCROLLBARS_BOTH;
217 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
218 add( "North", instructionsText );
219
220 messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
221 add("South", messageText);
222
223 pack();
224
225 show();
226 }// TestDialog()
227
228 //DO NOT call this directly, go through Sysout
229 public void printInstructions( String[] instructions )
230 {
231 //Clear out any current instructions
232 instructionsText.setText( "" );
233
234 //Go down array of instruction strings
235
236 String printStr, remainingStr;
237 for( int i=0; i < instructions.length; i++ )
238 {
239 //chop up each into pieces maxSringLength long
240 remainingStr = instructions[ i ];
241 while( remainingStr.length() > 0 )
242 {
243 //if longer than max then chop off first max chars to print
244 if( remainingStr.length() >= maxStringLength )
245 {
246 //Try to chop on a word boundary
247 int posOfSpace = remainingStr.
248 lastIndexOf( ' ', maxStringLength - 1 );
249
250 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
251
252 printStr = remainingStr.substring( 0, posOfSpace + 1 );
253 remainingStr = remainingStr.substring( posOfSpace + 1 );
254 }
255 //else just print
256 else
257 {
258 printStr = remainingStr;
259 remainingStr = "";
260 }
261
262 instructionsText.append( printStr + "\n" );
263
264 }// while
265
266 }// for
267
268 }//printInstructions()
269
270 //DO NOT call this directly, go through Sysout
271 public void displayMessage( String messageIn )
272 {
273 messageText.append( messageIn + "\n" );
274 }
275
276 }// TestDialog class