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.toFront() method.
0N/A @author anton.tarasov: area=awt.focus
0N/A @library ../../regtesthelpers
0N/A @build Util
0N/A @run main AutoRequestFocusToFrontTest
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 AutoRequestFocusToFrontTest extends Applet {
0N/A static boolean haveDelays;
0N/A
0N/A static Frame auxFrame;
0N/A static Frame frame;
0N/A static Button frameButton;
0N/A static Frame frame2;
0N/A static Button frameButton2;
0N/A static Frame frame3;
0N/A static Button frameButton3;
0N/A static Window window;
0N/A static Button winButton;
0N/A static Dialog dialog;
0N/A static Button dlgButton;
0N/A static Window ownedWindow;
0N/A static Button ownWinButton;
0N/A static Dialog ownedDialog;
0N/A static Button ownDlgButton;
0N/A static Dialog modalDialog;
0N/A static Button modalDlgButton;
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
0N/A if (args.length != 0) {
0N/A haveDelays = "delay".equals(args[0]) ? true : false;
0N/A }
0N/A
0N/A AutoRequestFocusToFrontTest app = new AutoRequestFocusToFrontTest();
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 static void recreateGUI() {
0N/A if (auxFrame != null) {
0N/A auxFrame.dispose();
0N/A frame.dispose();
0N/A frame2.dispose();
0N/A frame3.dispose();
0N/A window.dispose();
0N/A dialog.dispose();
0N/A ownedWindow.dispose();
0N/A ownedDialog.dispose();
0N/A modalDialog.dispose();
0N/A }
0N/A
0N/A auxFrame = new Frame("Auxiliary Frame");
0N/A
0N/A frame = new Frame("Test Frame");
0N/A frameButton = new Button("button");
0N/A
0N/A frame2 = new Frame("Test Frame 2");
0N/A frameButton2 = new Button("button");
0N/A
0N/A frame3 = new Frame("Test Frame 3");
0N/A frameButton3 = new Button("button");
0N/A
0N/A window = new Window(null);
0N/A winButton = new Button("button");
0N/A dialog = new Dialog((Frame)null, "Test Dialog");
0N/A dlgButton = new Button("button");
0N/A
0N/A ownedWindow = new Window(frame);
0N/A ownWinButton = new Button("button");
0N/A
0N/A ownedDialog = new Dialog(frame2, "Test Owned Dialog");
0N/A ownDlgButton = new Button("button");
0N/A
0N/A modalDialog = new Dialog(frame3, "Test Modal Dialog");
0N/A modalDlgButton = new Button("button");
0N/A
0N/A auxFrame.setBounds(100, 100, 300, 300);
0N/A
0N/A frame.setBounds(120, 120, 260, 260);
0N/A frame.add(frameButton);
0N/A
0N/A frame2.setBounds(120, 120, 260, 260);
0N/A frame2.add(frameButton2);
0N/A
0N/A frame3.setBounds(120, 120, 260, 260);
0N/A frame3.add(frameButton3);
0N/A
0N/A window.setBounds(120, 120, 260, 260);
0N/A window.add(winButton);
0N/A
0N/A dialog.setBounds(120, 120, 260, 260);
0N/A dialog.add(dlgButton);
0N/A
0N/A ownedWindow.setBounds(140, 140, 220, 220);
0N/A ownedWindow.add(ownWinButton);
0N/A
0N/A ownedDialog.setBounds(140, 140, 220, 220);
0N/A ownedDialog.add(ownDlgButton);
0N/A
0N/A modalDialog.setBounds(140, 140, 220, 220);
0N/A modalDialog.add(modalDlgButton);
0N/A modalDialog.setModal(true);
0N/A }
0N/A
0N/A public void start() {
0N/A // 1. Simple Frame.
0N/A //////////////////
0N/A
0N/A recreateGUI();
0N/A Test.setWindows(frame, null, null);
0N/A Test.test("Test stage 1 in progress", frameButton);
0N/A
0N/A
0N/A // 2. Ownerless Window.
0N/A //////////////////////
0N/A
0N/A recreateGUI();
0N/A Test.setWindows(window, null, null);
0N/A Test.test("Test stage 2 in progress", winButton);
0N/A
0N/A
0N/A // 3. Ownerless Dialog.
0N/A //////////////////////
0N/A
0N/A recreateGUI();
0N/A Test.setWindows(dialog, null, null);
0N/A Test.test("Test stage 3 in progress", dlgButton);
0N/A
0N/A
0N/A // 4.1. Owner Frame (with owned Window).
0N/A ///////////////////////////////////////
0N/A
0N/A recreateGUI();
0N/A Test.setWindows(frame, null, new Window[] {ownedWindow, frame});
0N/A Test.test("Test stage 4.1 in progress", ownWinButton);
0N/A
0N/A
0N/A // 4.2. Owned Window (with owner Frame).
0N/A ///////////////////////////////////////
0N/A
0N/A recreateGUI();
0N/A Test.setWindows(ownedWindow, null, new Window[] {ownedWindow, frame});
0N/A Test.test("Test stage 4.2 in progress", ownWinButton);
0N/A
0N/A
0N/A // 5.1. Owner Frame (with owned Dialog).
0N/A ///////////////////////////////////////
0N/A
0N/A recreateGUI();
0N/A Test.setWindows(frame2, null, new Window[] {ownedDialog, frame2});
0N/A Test.test("Test stage 5.1 in progress", ownDlgButton);
0N/A
0N/A
0N/A // 5.2. Owned Dialog (with owner Frame).
0N/A ///////////////////////////////////////
0N/A
0N/A recreateGUI();
0N/A Test.setWindows(ownedDialog, null, new Window[] {ownedDialog, frame2});
0N/A Test.test("Test stage 5.2 in progress", ownDlgButton);
0N/A
0N/A
0N/A ////////////////////////////////////////////////
0N/A // 6.1. Owned modal Dialog (with owner Frame).
0N/A // Focused frame is excluded from modality.
0N/A ////////////////////////////////////////////////
0N/A
0N/A if (!"sun.awt.motif.MToolkit".equals(toolkitClassName)) {
0N/A recreateGUI();
0N/A auxFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
0N/A
0N/A Test.setWindows(modalDialog, modalDialog, new Window[] {modalDialog, frame3});
0N/A Test.test("Test stage 6.1 in progress", modalDlgButton);
0N/A }
0N/A
0N/A
0N/A // 6.2. Owner Frame (with owned modal Dialog).
0N/A // Focused frame is excluded from modality.
0N/A ////////////////////////////////////////////////
0N/A
0N/A if (!"sun.awt.motif.MToolkit".equals(toolkitClassName)) {
0N/A recreateGUI();
0N/A auxFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
0N/A
0N/A Test.setWindows(frame3, modalDialog, new Window[] {modalDialog, frame3});
0N/A Test.test("Test stage 6.2 in progress", modalDlgButton, true);
0N/A }
0N/A
0N/A ///////////////////////////////////////////////////
0N/A // 7. Calling setVisible(true) for the shown Frame.
0N/A ///////////////////////////////////////////////////
0N/A
0N/A recreateGUI();
0N/A Test.setWindows(frame, null, null);
0N/A Test.setTestSetVisible();
0N/A Test.test("Test stage 7 in progress", frameButton);
0N/A
0N/A
0N/A Sysout.println("Test passed.");
0N/A }
0N/A
0N/A static class Test {
0N/A static Window testWindow; // a window to move to front with autoRequestFocus set
0N/A static Window focusWindow; // a window to gain focus
0N/A static Window[] showWindows; // windows to show, or null if only testWindow should be shown
0N/A
0N/A static boolean testSetVisible;
0N/A
0N/A static void setWindows(Window _testWindow, Window _focusWindow, Window[] _showWindows) {
0N/A testWindow = _testWindow;
0N/A focusWindow = _focusWindow;
0N/A showWindows = _showWindows;
0N/A }
0N/A static void setTestSetVisible() {
0N/A testSetVisible = true;
0N/A }
0N/A
0N/A /*
0N/A * @param msg notifies test stage number
0N/A * @param testButton a button of the window (owner or owned) that is to be on the top of stack order
0N/A * @param shouldFocusChange true for modal dialogs
0N/A */
0N/A static void test(String msg, final Button testButton, boolean shouldFocusChange) {
0N/A Sysout.println(msg);
0N/A
0N/A showWindows(testWindow, showWindows, true);
0N/A
0N/A pause(100);
0N/A
0N/A /////////////////////////////////////////////////////////
0N/A // Test that calling toFront() doesn't cause focus change
0N/A // when 'autoRequestFocus' is false.
0N/A /////////////////////////////////////////////////////////
0N/A
0N/A Runnable action = new Runnable() {
0N/A public void run() {
0N/A testWindow.setAutoRequestFocus(false);
0N/A if (testSetVisible) {
0N/A setVisible(testWindow, true);
0N/A } else {
0N/A toFront(testWindow);
0N/A }
0N/A }
0N/A };
0N/A
0N/A if (shouldFocusChange) {
0N/A action.run();
0N/A Util.waitForIdle(robot);
0N/A if (!focusWindow.isFocused()) {
0N/A throw new TestFailedException("the window must gain focus on moving to front but it didn't!");
0N/A }
0N/A } else if (TestHelper.trackFocusChangeFor(action, robot)) {
0N/A throw new TestFailedException("the window shouldn't gain focus on moving to front but it did!");
0N/A }
0N/A
0N/A pause(100);
0N/A
0N/A ///////////////////////////////////////////////////////
0N/A // Test that the window (or its owned window) is on top.
0N/A ///////////////////////////////////////////////////////
0N/A
0N/A // The latest versions of Metacity (e.g. 2.16) have problems with moving a window to the front.
0N/A if (Util.getWMID() != Util.METACITY_WM) {
0N/A
0N/A boolean performed = Util.trackActionPerformed(testButton, new Runnable() {
0N/A public void run() {
0N/A Util.clickOnComp(testButton, robot);
0N/A }
0N/A }, 1000, false);
0N/A
0N/A if (!performed) {
0N/A // For the case when the robot failed to trigger ACTION_EVENT.
0N/A Sysout.println("(ACTION_EVENT was not generated. One more attemp.)");
0N/A performed = Util.trackActionPerformed(testButton, new Runnable() {
0N/A public void run() {
0N/A Util.clickOnComp(testButton, robot);
0N/A }
0N/A }, 1000, false);
0N/A if (!performed) {
0N/A throw new TestFailedException("the window moved to front is not on the top!");
0N/A }
0N/A }
0N/A }
0N/A
0N/A showWindows(testWindow, showWindows, false);
0N/A
0N/A
0N/A /////////////////////////////////////////////////
0N/A // Test that calling toFront() focuses the window
0N/A // when 'autoRequestFocus' is true.
0N/A /////////////////////////////////////////////////
0N/A
0N/A // Skip this stage for unfocusable window
0N/A if (!testWindow.isFocusableWindow()) {
0N/A return;
0N/A }
0N/A
0N/A showWindows(testWindow, showWindows, true);
0N/A
0N/A pause(100);
0N/A
0N/A boolean gained = Util.trackWindowGainedFocus(testWindow, new Runnable() {
0N/A public void run() {
0N/A testWindow.setAutoRequestFocus(true);
0N/A if (testSetVisible) {
0N/A setVisible(testWindow, true);
0N/A } else {
0N/A toFront(testWindow);
0N/A }
0N/A }
0N/A }, 1000, false);
0N/A
0N/A // Either the window or its owned window must be focused
0N/A if (!gained && !testButton.hasFocus()) {
0N/A throw new TestFailedException("the window should gain focus automatically but it didn't!");
0N/A }
0N/A
0N/A showWindows(testWindow, showWindows, false);
0N/A }
0N/A
0N/A static void test(String msg, Button testButton) {
0N/A test(msg, testButton, false);
0N/A }
0N/A
0N/A private static void showWindows(Window win, Window[] wins, final boolean visible) {
0N/A pause(100);
0N/A
0N/A if (wins == null) {
0N/A wins = new Window[] {win}; // operate with 'win'
0N/A }
0N/A for (final Window w: wins) {
0N/A if (visible) {
0N/A if ((w instanceof Dialog) && ((Dialog)w).isModal()) {
0N/A TestHelper.invokeLaterAndWait(new Runnable() {
0N/A public void run() {
0N/A w.setVisible(true);
0N/A }
0N/A }, robot);
0N/A } else {
0N/A setVisible(w, true);
0N/A }
0N/A } else {
0N/A w.dispose();
0N/A }
0N/A }
0N/A setVisible(auxFrame, visible);
0N/A
0N/A if (visible) {
0N/A if (!auxFrame.isFocused()) {
0N/A Util.clickOnTitle(auxFrame, robot);
0N/A Util.waitForIdle(robot);
0N/A if (!auxFrame.isFocused()) {
0N/A throw new Error("Test error: the frame couldn't be focused.");
0N/A }
0N/A }
0N/A }
0N/A }
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 private static void toFront(Window w) {
0N/A w.toFront();
0N/A Util.waitForIdle(robot);
0N/A robot.delay(200);
0N/A }
0N/A
0N/A private static void pause(int msec) {
0N/A if (haveDelays) {
0N/A robot.delay(msec);
0N/A }
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