ShowFrameCheckForegroundTest.java revision 14851:980da45565c8
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  @key headful
27  @bug       6492970
28  @summary   Tests that showing a toplvel in a not foreground Java process activates it.
29  @library   ../../regtesthelpers
30  @build     Util
31  @author    Anton Tarasov: area=awt-focus
32  @run       main ShowFrameCheckForegroundTest
33 */
34
35import java.awt.*;
36import java.awt.event.*;
37import java.applet.Applet;
38import java.util.concurrent.atomic.AtomicBoolean;
39import java.lang.reflect.InvocationTargetException;
40import test.java.awt.regtesthelpers.Util;
41
42public class ShowFrameCheckForegroundTest extends Applet {
43    Robot robot;
44    Frame nofocusFrame = new Frame("Non-focusable");
45    Frame frame = new Frame("Frame");
46    Dialog dialog1 = new Dialog(nofocusFrame, "Owned Dialog", false);
47    Dialog dialog2 = new Dialog((Frame)null, "Owned Dialog", false);
48    Window testToplevel = null;
49    Button testButton = new Button("button");
50    Button showButton = new Button("show");
51    Runnable action = new Runnable() {
52        public void run() {
53            robot.keyPress(KeyEvent.VK_SPACE);
54            robot.delay(50);
55            robot.keyRelease(KeyEvent.VK_SPACE);
56        }
57    };
58
59
60    public static void main(String[] args) {
61        ShowFrameCheckForegroundTest app = new ShowFrameCheckForegroundTest();
62        app.init();
63        app.start();
64    }
65
66    public void init() {
67        robot = Util.createRobot();
68    }
69
70    public void start() {
71        showButton.addActionListener(new ActionListener() {
72            public void actionPerformed(ActionEvent e) {
73                testToplevel.setVisible(true);
74            }
75        });
76        nofocusFrame.add(showButton);
77        nofocusFrame.pack();
78        nofocusFrame.setFocusableWindowState(false);
79        nofocusFrame.setVisible(true);
80        Util.waitForIdle(robot);
81
82        robot.delay(3000);
83
84        // 1. Show the toplvel without clicking into the non-focusable frame.
85        test(frame, 1);
86        test(dialog1, 1);
87        test(dialog2, 1);
88
89        // 2. Showing the toplvel via clicking into the non-focusable frame.
90        test(frame, 2);
91        test(dialog1, 2);
92        test(dialog2, 2);
93
94        System.out.println("Test passed.");
95    }
96
97    private void test(Window toplevel, int stage) {
98        toplevel.add(testButton);
99        toplevel.pack();
100        toplevel.setLocation(200, 0);
101
102        switch (stage) {
103            case 1:
104                toplevel.setVisible(true);
105                break;
106            case 2:
107                testToplevel = toplevel;
108                Util.clickOnComp(showButton, robot);
109                break;
110        }
111        Util.waitForIdle(robot);
112
113        if (!Util.trackActionPerformed(testButton, action, 2000, false)) {
114            throw new TestFailedException("Stage " + stage + ". The toplevel " + toplevel + " wasn't made foreground on showing");
115        }
116        System.out.println("Stage " + stage + ". Toplevel " + toplevel + " - passed");
117        toplevel.dispose();
118    }
119}
120
121class TestFailedException extends RuntimeException {
122    TestFailedException(String msg) {
123        super("Test failed: " + msg);
124    }
125}
126
127/****************************************************
128 * Standard Test Machinery
129 * DO NOT modify anything below -- it's a standard
130 * chunk of code whose purpose is to make user
131 * interaction uniform, and thereby make it simpler
132 * to read and understand someone else's test.
133 ****************************************************/
134
135/**
136 * This is part of the standard test machinery.
137 * It creates a dialog (with the instructions), and is the interface
138 * for sending text messages to the user.
139 * To print the instructions, send an array of strings to Sysout.createDialog
140 * WithInstructions method.  Put one line of instructions per array entry.
141 * To display a message for the tester to see, simply call Sysout.println
142 * with the string to be displayed.
143 * This mimics System.out.println but works within the test harness as well
144 * as standalone.
145 */
146
147class Sysout {
148    static TestDialog dialog;
149
150    public static void createDialogWithInstructions( String[] instructions ) {
151        dialog = new TestDialog( new Frame(), "Instructions" );
152        dialog.printInstructions( instructions );
153        dialog.setVisible(true);
154        println( "Any messages for the tester will display here." );
155    }
156
157    public static void createDialog( ) {
158        dialog = new TestDialog( new Frame(), "Instructions" );
159        String[] defInstr = { "Instructions will appear here. ", "" } ;
160        dialog.printInstructions( defInstr );
161        dialog.setVisible(true);
162        println( "Any messages for the tester will display here." );
163    }
164
165
166    public static void printInstructions( String[] instructions ) {
167        dialog.printInstructions( instructions );
168    }
169
170
171    public static void println( String messageIn ) {
172        dialog.displayMessage( messageIn );
173    }
174
175}// Sysout  class
176
177/**
178 * This is part of the standard test machinery.  It provides a place for the
179 * test instructions to be displayed, and a place for interactive messages
180 * to the user to be displayed.
181 * To have the test instructions displayed, see Sysout.
182 * To have a message to the user be displayed, see Sysout.
183 * Do not call anything in this dialog directly.
184 */
185class TestDialog extends Dialog {
186
187    TextArea instructionsText;
188    TextArea messageText;
189    int maxStringLength = 80;
190
191    //DO NOT call this directly, go through Sysout
192    public TestDialog( Frame frame, String name ) {
193        super( frame, name );
194        int scrollBoth = TextArea.SCROLLBARS_BOTH;
195        instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
196        add( "North", instructionsText );
197
198        messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
199        add("Center", messageText);
200
201        pack();
202
203        setVisible(true);
204    }// TestDialog()
205
206    //DO NOT call this directly, go through Sysout
207    public void printInstructions( String[] instructions ) {
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            //chop up each into pieces maxSringLength long
216            remainingStr = instructions[ i ];
217            while( remainingStr.length() > 0 ) {
218                //if longer than max then chop off first max chars to print
219                if( remainingStr.length() >= maxStringLength ) {
220                    //Try to chop on a word boundary
221                    int posOfSpace = remainingStr.
222                            lastIndexOf( ' ', maxStringLength - 1 );
223
224                    if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
225
226                    printStr = remainingStr.substring( 0, posOfSpace + 1 );
227                    remainingStr = remainingStr.substring( posOfSpace + 1 );
228                }
229                //else just print
230                else {
231                    printStr = remainingStr;
232                    remainingStr = "";
233                }
234
235                instructionsText.append( printStr + "\n" );
236
237            }// while
238
239        }// for
240
241    }//printInstructions()
242
243    //DO NOT call this directly, go through Sysout
244    public void displayMessage( String messageIn ) {
245        messageText.append( messageIn + "\n" );
246        System.out.println(messageIn);
247    }
248
249}// TestDialog  class
250