0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A @test
0N/A @bug 6187066
0N/A @summary Tests the Window.autoRequestFocus property for the Window.setVisible() method.
0N/A @author anton.tarasov: area=awt.focus
0N/A @library ../../regtesthelpers
0N/A @build Util
0N/A @run main AutoRequestFocusSetVisibleTest
0N/A*/
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.applet.Applet;
0N/Aimport java.util.concurrent.atomic.AtomicBoolean;
0N/Aimport java.lang.reflect.InvocationTargetException;
0N/Aimport test.java.awt.regtesthelpers.Util;
0N/A
0N/Apublic class AutoRequestFocusSetVisibleTest extends Applet {
0N/A static Frame focusedFrame;
0N/A static Button focusOwner;
0N/A static Frame frame;
0N/A static Button frameButton;
0N/A static Frame frame2;
0N/A static Button frameButton2;
0N/A static Window window;
0N/A static Button winButton;
0N/A static Window ownedWindow;
0N/A static Button ownWinButton;
0N/A static Dialog ownedDialog;
0N/A static Button ownDlgButton;
0N/A static Dialog dialog;
0N/A static Button dlgButton;
0N/A
0N/A static String toolkitClassName;
0N/A static Robot robot = Util.createRobot();
0N/A
0N/A public static void main(String[] args) {
0N/A AutoRequestFocusSetVisibleTest app = new AutoRequestFocusSetVisibleTest();
0N/A app.init();
0N/A app.start();
0N/A }
0N/A
0N/A public void init() {
0N/A // Create instructions for the user here, as well as set up
0N/A // the environment -- set the layout manager, add buttons,
0N/A // etc.
0N/A this.setLayout (new BorderLayout ());
0N/A Sysout.createDialogWithInstructions(new String[]
0N/A {"This is an automatic test. Simply wait until it is done."
0N/A });
0N/A toolkitClassName = Toolkit.getDefaultToolkit().getClass().getName();
0N/A }
0N/A
0N/A void recreateGUI() {
0N/A if (focusedFrame != null) {
0N/A focusedFrame.dispose();
0N/A frame.dispose();
0N/A frame2.dispose();
0N/A window.dispose();
0N/A ownedWindow.dispose();
0N/A ownedDialog.dispose();
0N/A dialog.dispose();
0N/A }
0N/A
0N/A focusedFrame = new Frame("Base Frame");
0N/A focusOwner = new Button("button");
0N/A
0N/A frame = new Frame("Test Frame");
0N/A frameButton = new Button("button");
0N/A
0N/A frame2 = new Frame("Test Frame");
0N/A frameButton2 = new Button("button");
0N/A
0N/A window = new Window(focusedFrame);
0N/A winButton = new Button("button");
0N/A
0N/A ownedWindow = new Window(frame) {
0N/A /*
0N/A * When 'frame' is shown along with the 'ownedWindow'
0N/A * (i.e. showWithParent==true) then it can appear
0N/A * that the 'ownedWindow' is shown too early and
0N/A * it can't be focused due to its owner can't be
0N/A * yet activated. So, to avoid this race, we pospone
0N/A * a little the showing of the 'ownedWindow'.
0N/A */
0N/A public void show() {
0N/A robot.delay(100);
0N/A super.show();
0N/A }
0N/A };
0N/A ownWinButton = new Button("button");
0N/A
0N/A ownedDialog = new Dialog(frame2);
0N/A ownDlgButton = new Button("button");
0N/A
0N/A dialog = new Dialog(focusedFrame, "Test Dialog");
0N/A dlgButton = new Button("button");
0N/A
0N/A focusedFrame.add(focusOwner);
0N/A focusedFrame.setBounds(100, 100, 300, 300);
0N/A
0N/A frame.setBounds(140, 140, 220, 220);
0N/A frame.add(frameButton);
0N/A
0N/A frame2.setBounds(140, 140, 220, 220);
0N/A frame2.add(frameButton2);
0N/A
0N/A window.setBounds(140, 140, 220, 220);
0N/A window.add(winButton);
0N/A
0N/A ownedWindow.setBounds(180, 180, 140, 140);
0N/A ownedWindow.add(ownWinButton);
0N/A
0N/A ownedDialog.setBounds(180, 180, 140, 140);
0N/A ownedDialog.add(ownDlgButton);
0N/A
0N/A dialog.setBounds(140, 140, 220, 220);
0N/A dialog.add(dlgButton);
0N/A }
0N/A
0N/A public void start() {
0N/A
0N/A ///////////////////////////////////////////////////////
0N/A // 1. Show Frame with owned modal Dialog without delay.
0N/A // Check that the Dialog takes focus.
0N/A ///////////////////////////////////////////////////////
0N/A
0N/A recreateGUI();
0N/A
0N/A Sysout.println("Stage 1 in progress...");
0N/A
0N/A dialog.setModal(true);
0N/A dialog.setAutoRequestFocus(false);
0N/A setVisible(focusedFrame, true);
0N/A
0N/A TestHelper.invokeLaterAndWait(new Runnable() {
0N/A public void run() {
0N/A dialog.setVisible(true);
0N/A }
0N/A }, robot);
0N/A
0N/A if (focusOwner.hasFocus()) {
0N/A throw new TestFailedException("the modal dialog must gain focus but it didn't!");
0N/A }
0N/A setVisible(dialog, false);
0N/A
0N/A //////////////////////////////////////////////////
0N/A // 2. Show Frame, activate, auto hide, auto show.
0N/A // Check that the Frame takes focus.
0N/A //////////////////////////////////////////////////
0N/A
0N/A recreateGUI();
0N/A
0N/A Sysout.println("Stage 2 in progress...");
0N/A
0N/A setVisible(focusedFrame, false);
0N/A
0N/A focusedFrame.setAutoRequestFocus(false);
0N/A setVisible(focusedFrame, true);
0N/A
0N/A Util.clickOnTitle(focusedFrame, robot);
0N/A Util.waitForIdle(robot);
0N/A
0N/A if (!focusedFrame.isFocused()) {
0N/A throw new Error("Test error: the frame couldn't be focused.");
0N/A }
0N/A
0N/A focusedFrame.setExtendedState(Frame.ICONIFIED);
0N/A Util.waitForIdle(robot);
0N/A focusedFrame.setExtendedState(Frame.NORMAL);
0N/A Util.waitForIdle(robot);
0N/A
0N/A if (!focusedFrame.isFocused()) {
0N/A throw new TestFailedException("the restored frame must gain focus but it didn't!");
0N/A }
0N/A
0N/A
0N/A ////////////////////////
0N/A // 3.1 Show Frame normal.
0N/A ////////////////////////
0N/A
0N/A recreateGUI();
0N/A
0N/A test("Stage 3.1 in progress...", frame, frameButton);
0N/A
0N/A
0N/A // 3.2. Show Frame maximized both.
0N/A /////////////////////////////////
0N/A
0N/A if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {
0N/A System.out.println("Stage 3.2: Frame.MAXIMIZED_BOTH not supported. Skipping.");
0N/A } else {
0N/A frame.setExtendedState(Frame.MAXIMIZED_BOTH);
0N/A
0N/A test("Stage 3.2 in progress...", frame, frameButton);
0N/A }
0N/A
0N/A
0N/A // 3.3. Show Frame maximized vertically.
0N/A ///////////////////////////////////////
0N/A
0N/A if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_VERT)) {
0N/A System.out.println("Stage 3.3: Frame.MAXIMIZED_VERT not supported. Skipping.");
0N/A } else {
0N/A frame.setExtendedState(Frame.MAXIMIZED_VERT);
0N/A
0N/A test("Stage 3.3 in progress...", frame, frameButton);
0N/A }
0N/A
0N/A
0N/A // 3.4. Show Frame maximized horizontally.
0N/A /////////////////////////////////////////
0N/A
0N/A if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_HORIZ)) {
0N/A System.out.println("Stage 3.4: Frame.MAXIMIZED_HORIZ not supported. Skipping.");
0N/A } else {
0N/A frame.setExtendedState(Frame.MAXIMIZED_HORIZ);
0N/A
0N/A test("Stage 3.4 in progress...", frame, frameButton);
0N/A }
0N/A
0N/A
0N/A // 3.5. Show Frame iconified.
0N/A ////////////////////////////
0N/A
0N/A if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.ICONIFIED)) {
0N/A System.out.println("Stage 3.5: Frame.ICONIFIED not supported. Skipping.");
0N/A } else {
0N/A frame.setExtendedState(Frame.ICONIFIED);
0N/A
0N/A test("Stage 3.5 in progress...", frame, frameButton);
0N/A }
0N/A
0N/A
0N/A ///////////////////
0N/A // 4.1 Show Window.
0N/A ///////////////////
0N/A recreateGUI();
0N/A test("Stage 4.1 in progress...", window, winButton);
0N/A
0N/A
0N/A // 4.2 Show Dialog.
0N/A //////////////////
0N/A
0N/A test("Stage 4.2 in progress...", dialog, dlgButton);
0N/A
0N/A
0N/A // 4.3. Show modal Dialog.
0N/A /////////////////////////
0N/A
0N/A dialog.setModal(true);
0N/A test("Stage 4.3 in progress...", dialog, dlgButton, true);
0N/A
0N/A
0N/A ///////////////////////////////////
0N/A // 5.1 Show Frame with owned Window.
0N/A ///////////////////////////////////
0N/A
0N/A // On Windows, an owned Window will not be focused on its showing
0N/A // if the owner is not currently active.
0N/A if ("sun.awt.windows.WToolkit".equals(toolkitClassName)) {
0N/A Sysout.println("Stage 5.1 - Skiping.");
0N/A } else {
0N/A setVisible(ownedWindow, true);
0N/A setVisible(frame, false); // 'ownedWindow' will be shown along with the owner.
0N/A
0N/A test("Stage 5.1 in progress...", frame, ownedWindow, ownWinButton, true);
0N/A }
0N/A
0N/A
0N/A // 5.2 Show Frame with owned Dialog.
0N/A ///////////////////////////////////
0N/A
0N/A setVisible(ownedDialog, true);
0N/A setVisible(frame2, false); // 'ownedDialog' will be shown along with the owner.
0N/A
0N/A test("Stage 5.2 in progress...", frame2, ownedDialog, ownDlgButton, true);
0N/A
0N/A
0N/A ///////////////////////////////////
0N/A // 6. Show unblocking modal Dialog.
0N/A ///////////////////////////////////
0N/A
0N/A if ("sun.awt.motif.MToolkit".equals(toolkitClassName)) {
0N/A Sysout.println("Stage 6 - Skiping.");
0N/A } else {
0N/A Sysout.println("Stage 6 in progress...");
0N/A
0N/A // ---
0N/A // Testing the bug of activating invisible modal Dialog (awt_Window::SetAndActivateModalBlocker).
0N/A // Having some window not excluded from modality, so that it would be blocked.
0N/A Frame f = new Frame("Aux. Frame");
0N/A f.setSize(100, 100);
0N/A setVisible(f, true);
0N/A // ---
0N/A
0N/A setVisible(focusedFrame, true);
0N/A if (!focusOwner.hasFocus()) {
0N/A Util.clickOnComp(focusOwner, robot);
0N/A Util.waitForIdle(robot);
0N/A if (!focusOwner.hasFocus()) {
0N/A throw new Error("Test error: the frame couldn't be focused.");
0N/A }
0N/A }
0N/A
0N/A dialog.setModal(true);
0N/A dialog.setAutoRequestFocus(false);
0N/A focusedFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
0N/A
0N/A TestHelper.invokeLaterAndWait(new Runnable() {
0N/A public void run() {
0N/A dialog.setVisible(true);
0N/A }
0N/A }, robot);
0N/A
0N/A if (dialog.isFocused()) {
0N/A throw new TestFailedException("the unblocking dialog shouldn't gain focus but it did!");
0N/A }
0N/A setVisible(dialog, false);
0N/A }
0N/A
0N/A Sysout.println("Test passed.");
0N/A }
0N/A
0N/A /*
0N/A * @param msg notifies test stage number
0N/A * @param showWindow a window to show/test (if ownedWindow == null)
0N/A * @param ownedWindow an owned window to show/test, or null if showWindow should be tested
0N/A * @param clickButton a button of the window (owner or owned) expected to be on the top of stack order
0N/A * @param shouldFocusChange true the test window should gain focus
0N/A */
0N/A void test(String msg, final Window showWindow, Window ownedWindow, final Button clickButton, boolean shouldFocusChange) {
0N/A Window testWindow = (ownedWindow == null ? showWindow : ownedWindow);
0N/A
0N/A Sysout.println(msg);
0N/A
0N/A if (showWindow.isVisible()) {
0N/A showWindow.dispose();
0N/A Util.waitForIdle(robot);
0N/A }
0N/A if (!focusedFrame.isVisible()) {
0N/A setVisible(focusedFrame, true);
0N/A }
0N/A if (!focusOwner.hasFocus()) {
0N/A Util.clickOnComp(focusOwner, robot);
0N/A Util.waitForIdle(robot);
0N/A if (!focusOwner.hasFocus()) {
0N/A throw new Error("Test error: the frame couldn't be focused.");
0N/A }
0N/A }
0N/A
0N/A //////////////////////////////////////////
0N/A // Test focus change on showing the window
0N/A //////////////////////////////////////////
0N/A
0N/A final Runnable showAction = new Runnable() {
0N/A public void run() {
0N/A showWindow.setAutoRequestFocus(false);
0N/A showWindow.setVisible(true);
0N/A }
0N/A };
0N/A
0N/A final Runnable trackerAction = new Runnable() {
0N/A public void run() {
0N/A if (showWindow instanceof Dialog && ((Dialog)showWindow).isModal()) {
0N/A TestHelper.invokeLaterAndWait(showAction, robot);
0N/A } else {
0N/A showAction.run();
0N/A }
0N/A }
0N/A };
0N/A
0N/A if (shouldFocusChange) {
0N/A trackerAction.run();
0N/A Util.waitForIdle(robot);
0N/A
0N/A if (!testWindow.isFocused()) {
0N/A throw new TestFailedException("the window must gain focus but it didn't!");
0N/A }
0N/A
0N/A } else if (TestHelper.trackFocusChangeFor(trackerAction, robot)) {
0N/A throw new TestFailedException("the window shouldn't gain focus but it did!");
0N/A }
0N/A
0N/A
0N/A ////////////////////////////////////////////
0N/A // Test that the window was shown on the top.
0N/A // Test that it can be focused.
0N/A ////////////////////////////////////////////
0N/A
0N/A if (!(testWindow instanceof Frame) ||
0N/A ((Frame)testWindow).getExtendedState() != Frame.ICONIFIED)
0N/A {
0N/A boolean performed = Util.trackActionPerformed(clickButton, new Runnable() {
0N/A public void run() {
0N/A /*
0N/A * If 'showWindow' is not on the top then
0N/A * 'focusOwner' button completely overlaps 'clickButton'
0N/A * and we won't catch the action.
0N/A */
0N/A Util.clickOnComp(clickButton, robot);
0N/A }
0N/A }, 1000, false);
0N/A
0N/A if (!performed) {
0N/A // In case of loosing ACTION_PERFORMED, try once more.
0N/A Sysout.println("(ACTION_EVENT was not generated. One more attemp.)");
0N/A performed = Util.trackActionPerformed(clickButton, new Runnable() {
0N/A public void run() {
0N/A Util.clickOnComp(clickButton, robot);
0N/A }
0N/A }, 1000, false);
0N/A
0N/A if (!performed) {
0N/A throw new TestFailedException("the window shown is not on the top!");
0N/A }
0N/A }
0N/A }
0N/A
0N/A recreateGUI();
0N/A }
0N/A
0N/A void test(String msg, final Window showWindow, Button clickButton) {
0N/A test(msg, showWindow, null, clickButton, false);
0N/A }
0N/A void test(String msg, final Window showWindow, Button clickButton, boolean shouldFocusChange) {
0N/A test(msg, showWindow, null, clickButton, shouldFocusChange);
0N/A }
0N/A void test(String msg, final Window showWindow, Window ownedWindow, Button clickButton) {
0N/A test(msg, showWindow, ownedWindow, clickButton, false);
0N/A }
0N/A
0N/A private static void setVisible(Window w, boolean b) {
0N/A w.setVisible(b);
0N/A try {
0N/A Util.waitForIdle(robot);
0N/A } catch (RuntimeException rte) { // InfiniteLoop
0N/A rte.printStackTrace();
0N/A }
0N/A robot.delay(200);
0N/A }
0N/A}
0N/A
0N/Aclass TestFailedException extends RuntimeException {
0N/A TestFailedException(String msg) {
0N/A super("Test failed: " + msg);
0N/A }
0N/A}
0N/A
0N/A/****************************************************
0N/A Standard Test Machinery
0N/A DO NOT modify anything below -- it's a standard
0N/A chunk of code whose purpose is to make user
0N/A interaction uniform, and thereby make it simpler
0N/A to read and understand someone else's test.
0N/A ****************************************************/
0N/A
0N/A/**
0N/A This is part of the standard test machinery.
0N/A It creates a dialog (with the instructions), and is the interface
0N/A for sending text messages to the user.
0N/A To print the instructions, send an array of strings to Sysout.createDialog
0N/A WithInstructions method. Put one line of instructions per array entry.
0N/A To display a message for the tester to see, simply call Sysout.println
0N/A with the string to be displayed.
0N/A This mimics System.out.println but works within the test harness as well
0N/A as standalone.
0N/A */
0N/A
0N/Aclass Sysout
0N/A{
0N/A static TestDialog dialog;
0N/A
0N/A public static void createDialogWithInstructions( String[] instructions )
0N/A {
0N/A dialog = new TestDialog( new Frame(), "Instructions" );
0N/A dialog.printInstructions( instructions );
0N/A// dialog.setVisible(true);
0N/A println( "Any messages for the tester will display here." );
0N/A }
0N/A
0N/A public static void createDialog( )
0N/A {
0N/A dialog = new TestDialog( new Frame(), "Instructions" );
0N/A String[] defInstr = { "Instructions will appear here. ", "" } ;
0N/A dialog.printInstructions( defInstr );
0N/A// dialog.setVisible(true);
0N/A println( "Any messages for the tester will display here." );
0N/A }
0N/A
0N/A
0N/A public static void printInstructions( String[] instructions )
0N/A {
0N/A dialog.printInstructions( instructions );
0N/A }
0N/A
0N/A
0N/A public static void println( String messageIn )
0N/A {
0N/A dialog.displayMessage( messageIn );
0N/A }
0N/A
0N/A}// Sysout class
0N/A
0N/A/**
0N/A This is part of the standard test machinery. It provides a place for the
0N/A test instructions to be displayed, and a place for interactive messages
0N/A to the user to be displayed.
0N/A To have the test instructions displayed, see Sysout.
0N/A To have a message to the user be displayed, see Sysout.
0N/A Do not call anything in this dialog directly.
0N/A */
0N/Aclass TestDialog extends Dialog
0N/A{
0N/A
0N/A TextArea instructionsText;
0N/A TextArea messageText;
0N/A int maxStringLength = 80;
0N/A
0N/A //DO NOT call this directly, go through Sysout
0N/A public TestDialog( Frame frame, String name )
0N/A {
0N/A super( frame, name );
0N/A int scrollBoth = TextArea.SCROLLBARS_BOTH;
0N/A instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
0N/A add( "North", instructionsText );
0N/A
0N/A messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
0N/A add("Center", messageText);
0N/A
0N/A pack();
0N/A
0N/A// setVisible(true);
0N/A }// TestDialog()
0N/A
0N/A //DO NOT call this directly, go through Sysout
0N/A public void printInstructions( String[] instructions )
0N/A {
0N/A //Clear out any current instructions
0N/A instructionsText.setText( "" );
0N/A
0N/A //Go down array of instruction strings
0N/A
0N/A String printStr, remainingStr;
0N/A for( int i=0; i < instructions.length; i++ )
0N/A {
0N/A //chop up each into pieces maxSringLength long
0N/A remainingStr = instructions[ i ];
0N/A while( remainingStr.length() > 0 )
0N/A {
0N/A //if longer than max then chop off first max chars to print
0N/A if( remainingStr.length() >= maxStringLength )
0N/A {
0N/A //Try to chop on a word boundary
0N/A int posOfSpace = remainingStr.
0N/A lastIndexOf( ' ', maxStringLength - 1 );
0N/A
0N/A if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
0N/A
0N/A printStr = remainingStr.substring( 0, posOfSpace + 1 );
0N/A remainingStr = remainingStr.substring( posOfSpace + 1 );
0N/A }
0N/A //else just print
0N/A else
0N/A {
0N/A printStr = remainingStr;
0N/A remainingStr = "";
0N/A }
0N/A
0N/A instructionsText.append( printStr + "\n" );
0N/A
0N/A }// while
0N/A
0N/A }// for
0N/A
0N/A }//printInstructions()
0N/A
0N/A //DO NOT call this directly, go through Sysout
0N/A public void displayMessage( String messageIn )
0N/A {
0N/A messageText.append( messageIn + "\n" );
0N/A System.out.println(messageIn);
0N/A }
0N/A
0N/A}// TestDialog class