1/*
2 * Copyright (c) 2006, 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       6426132
27  @summary   A Window should be initially focused on its showing (XAWT bug).
28  @author    anton.tarasov@...: area=awt.focus
29  @run       applet WindowInitialFocusTest.html
30*/
31
32import java.awt.*;
33import java.awt.event.*;
34import java.applet.Applet;
35import java.util.concurrent.atomic.AtomicBoolean;
36import test.java.awt.regtesthelpers.Util;
37
38public class WindowInitialFocusTest extends Applet {
39    Frame frame = new Frame("Test Frame");
40    Window window = new Window(frame);
41    Button button = new Button("button");
42    AtomicBoolean focused = new AtomicBoolean(false);
43    Robot robot;
44
45    public static void main(String[] args) {
46        WindowInitialFocusTest app = new WindowInitialFocusTest();
47        app.init();
48        app.start();
49    }
50
51    public void init() {
52        // Create instructions for the user here, as well as set up
53        // the environment -- set the layout manager, add buttons,
54        // etc.
55        this.setLayout (new BorderLayout ());
56        Sysout.createDialogWithInstructions(new String[]
57            {"This is an automatic test. Simply wait until it is done."
58            });
59    }
60
61    public void start() {
62        frame.setBounds(800, 0, 200, 100);
63        window.setBounds(800, 200, 200, 100);
64        window.setLayout(new FlowLayout());
65        window.add(button);
66
67        window.addWindowFocusListener(new WindowAdapter() {
68                public void windowGainedFocus(WindowEvent e) {
69                    Sysout.println(e.toString());
70                    synchronized (focused) {
71                        focused.set(true);
72                        focused.notifyAll();
73                    }
74                }});
75
76        frame.setVisible(true);
77        try {
78            robot = new Robot();
79        }catch(Exception ex) {
80            ex.printStackTrace();
81            throw new RuntimeException("Unexpected failure");
82        }
83        robot.waitForIdle();
84
85        // Test 1. Show the window, check that it become focused.
86
87        window.setVisible(true);
88        robot.waitForIdle();
89
90        if (!Util.waitForCondition(focused, 2000L)) {
91            throw new TestFailedException("the window didn't get focused on its showing!");
92        }
93
94        // Test 2. Show unfocusable window, check that it doesn't become focused.
95
96        window.setVisible(false);
97        robot.waitForIdle();
98
99        window.setFocusableWindowState(false);
100        focused.set(false);
101
102        window.setVisible(true);
103        robot.waitForIdle();
104
105        if (Util.waitForCondition(focused, 2000L)) {
106            throw new TestFailedException("the unfocusable window got focused on its showing!");
107        } else {
108            Sysout.println("Test passed");
109        }
110    }
111}
112
113class TestFailedException extends RuntimeException {
114    TestFailedException(String msg) {
115        super("Test failed: " + msg);
116    }
117}
118
119/****************************************************
120 Standard Test Machinery
121 DO NOT modify anything below -- it's a standard
122  chunk of code whose purpose is to make user
123  interaction uniform, and thereby make it simpler
124  to read and understand someone else's test.
125 ****************************************************/
126
127/**
128 This is part of the standard test machinery.
129 It creates a dialog (with the instructions), and is the interface
130  for sending text messages to the user.
131 To print the instructions, send an array of strings to Sysout.createDialog
132  WithInstructions method.  Put one line of instructions per array entry.
133 To display a message for the tester to see, simply call Sysout.println
134  with the string to be displayed.
135 This mimics System.out.println but works within the test harness as well
136  as standalone.
137 */
138
139class Sysout
140{
141    static TestDialog dialog;
142
143    public static void createDialogWithInstructions( String[] instructions )
144    {
145        dialog = new TestDialog( new Frame(), "Instructions" );
146        dialog.printInstructions( instructions );
147        dialog.setVisible(true);
148        println( "Any messages for the tester will display here." );
149    }
150
151    public static void createDialog( )
152    {
153        dialog = new TestDialog( new Frame(), "Instructions" );
154        String[] defInstr = { "Instructions will appear here. ", "" } ;
155        dialog.printInstructions( defInstr );
156        dialog.setVisible(true);
157        println( "Any messages for the tester will display here." );
158    }
159
160
161    public static void printInstructions( String[] instructions )
162    {
163        dialog.printInstructions( instructions );
164    }
165
166
167    public static void println( String messageIn )
168    {
169        dialog.displayMessage( messageIn );
170    }
171
172}// Sysout  class
173
174/**
175  This is part of the standard test machinery.  It provides a place for the
176   test instructions to be displayed, and a place for interactive messages
177   to the user to be displayed.
178  To have the test instructions displayed, see Sysout.
179  To have a message to the user be displayed, see Sysout.
180  Do not call anything in this dialog directly.
181  */
182class TestDialog extends Dialog
183{
184
185    TextArea instructionsText;
186    TextArea messageText;
187    int maxStringLength = 80;
188
189    //DO NOT call this directly, go through Sysout
190    public TestDialog( Frame frame, String name )
191    {
192        super( frame, name );
193        int scrollBoth = TextArea.SCROLLBARS_BOTH;
194        instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
195        add( "North", instructionsText );
196
197        messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
198        add("Center", messageText);
199
200        pack();
201
202        setVisible(true);
203    }// TestDialog()
204
205    //DO NOT call this directly, go through Sysout
206    public void printInstructions( String[] instructions )
207    {
208        //Clear out any current instructions
209        instructionsText.setText( "" );
210
211        //Go down array of instruction strings
212
213        String printStr, remainingStr;
214        for( int i=0; i < instructions.length; i++ )
215        {
216            //chop up each into pieces maxSringLength long
217            remainingStr = instructions[ i ];
218            while( remainingStr.length() > 0 )
219            {
220                //if longer than max then chop off first max chars to print
221                if( remainingStr.length() >= maxStringLength )
222                {
223                    //Try to chop on a word boundary
224                    int posOfSpace = remainingStr.
225                        lastIndexOf( ' ', maxStringLength - 1 );
226
227                    if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
228
229                    printStr = remainingStr.substring( 0, posOfSpace + 1 );
230                    remainingStr = remainingStr.substring( posOfSpace + 1 );
231                }
232                //else just print
233                else
234                {
235                    printStr = remainingStr;
236                    remainingStr = "";
237                }
238
239                instructionsText.append( printStr + "\n" );
240
241            }// while
242
243        }// for
244
245    }//printInstructions()
246
247    //DO NOT call this directly, go through Sysout
248    public void displayMessage( String messageIn )
249    {
250        messageText.append( messageIn + "\n" );
251        System.out.println(messageIn);
252    }
253
254}// TestDialog  class
255