Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/AutoRequestFocusTest/AutoRequestFocusToFrontTest.java
47525 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@bug 618706626@summary Tests the Window.autoRequestFocus property for the Window.toFront() method.27@author anton.tarasov: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main AutoRequestFocusToFrontTest31*/3233import java.awt.*;34import java.awt.event.*;35import java.applet.Applet;36import java.util.concurrent.atomic.AtomicBoolean;37import java.lang.reflect.InvocationTargetException;38import test.java.awt.regtesthelpers.Util;3940public class AutoRequestFocusToFrontTest extends Applet {41static boolean haveDelays;4243static Frame auxFrame;44static Frame frame;45static Button frameButton;46static Frame frame2;47static Button frameButton2;48static Frame frame3;49static Button frameButton3;50static Window window;51static Button winButton;52static Dialog dialog;53static Button dlgButton;54static Window ownedWindow;55static Button ownWinButton;56static Dialog ownedDialog;57static Button ownDlgButton;58static Dialog modalDialog;59static Button modalDlgButton;6061static String toolkitClassName;62static Robot robot = Util.createRobot();6364public static void main(String[] args) {6566if (args.length != 0) {67haveDelays = "delay".equals(args[0]) ? true : false;68}6970AutoRequestFocusToFrontTest app = new AutoRequestFocusToFrontTest();71app.init();72app.start();73}7475public void init() {76// Create instructions for the user here, as well as set up77// the environment -- set the layout manager, add buttons,78// etc.79this.setLayout (new BorderLayout ());80Sysout.createDialogWithInstructions(new String[]81{"This is an automatic test. Simply wait until it is done."82});83toolkitClassName = Toolkit.getDefaultToolkit().getClass().getName();84}8586static void recreateGUI() {87if (auxFrame != null) {88auxFrame.dispose();89frame.dispose();90frame2.dispose();91frame3.dispose();92window.dispose();93dialog.dispose();94ownedWindow.dispose();95ownedDialog.dispose();96modalDialog.dispose();97}9899auxFrame = new Frame("Auxiliary Frame");100101frame = new Frame("Test Frame");102frameButton = new Button("button");103104frame2 = new Frame("Test Frame 2");105frameButton2 = new Button("button");106107frame3 = new Frame("Test Frame 3");108frameButton3 = new Button("button");109110window = new Window(null);111winButton = new Button("button");112dialog = new Dialog((Frame)null, "Test Dialog");113dlgButton = new Button("button");114115ownedWindow = new Window(frame);116ownWinButton = new Button("button");117118ownedDialog = new Dialog(frame2, "Test Owned Dialog");119ownDlgButton = new Button("button");120121modalDialog = new Dialog(frame3, "Test Modal Dialog");122modalDlgButton = new Button("button");123124auxFrame.setBounds(100, 100, 300, 300);125126frame.setBounds(120, 120, 260, 260);127frame.add(frameButton);128129frame2.setBounds(120, 120, 260, 260);130frame2.add(frameButton2);131132frame3.setBounds(120, 120, 260, 260);133frame3.add(frameButton3);134135window.setBounds(120, 120, 260, 260);136window.add(winButton);137138dialog.setBounds(120, 120, 260, 260);139dialog.add(dlgButton);140141ownedWindow.setBounds(140, 140, 220, 220);142ownedWindow.add(ownWinButton);143144ownedDialog.setBounds(140, 140, 220, 220);145ownedDialog.add(ownDlgButton);146147modalDialog.setBounds(140, 140, 220, 220);148modalDialog.add(modalDlgButton);149modalDialog.setModal(true);150}151152public void start() {153// 1. Simple Frame.154//////////////////155156recreateGUI();157Test.setWindows(frame, null, null);158Test.test("Test stage 1 in progress", frameButton);159160161// 2. Ownerless Window.162//////////////////////163164recreateGUI();165Test.setWindows(window, null, null);166Test.test("Test stage 2 in progress", winButton);167168169// 3. Ownerless Dialog.170//////////////////////171172recreateGUI();173Test.setWindows(dialog, null, null);174Test.test("Test stage 3 in progress", dlgButton);175176177// 4.1. Owner Frame (with owned Window).178///////////////////////////////////////179180recreateGUI();181Test.setWindows(frame, null, new Window[] {ownedWindow, frame});182Test.test("Test stage 4.1 in progress", ownWinButton);183184185// 4.2. Owned Window (with owner Frame).186///////////////////////////////////////187188recreateGUI();189Test.setWindows(ownedWindow, null, new Window[] {ownedWindow, frame});190Test.test("Test stage 4.2 in progress", ownWinButton);191192193// 5.1. Owner Frame (with owned Dialog).194///////////////////////////////////////195196recreateGUI();197Test.setWindows(frame2, null, new Window[] {ownedDialog, frame2});198Test.test("Test stage 5.1 in progress", ownDlgButton);199200201// 5.2. Owned Dialog (with owner Frame).202///////////////////////////////////////203204recreateGUI();205Test.setWindows(ownedDialog, null, new Window[] {ownedDialog, frame2});206Test.test("Test stage 5.2 in progress", ownDlgButton);207208209////////////////////////////////////////////////210// 6.1. Owned modal Dialog (with owner Frame).211// Focused frame is excluded from modality.212////////////////////////////////////////////////213214if (!"sun.awt.motif.MToolkit".equals(toolkitClassName)) {215recreateGUI();216auxFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);217218Test.setWindows(modalDialog, modalDialog, new Window[] {modalDialog, frame3});219Test.test("Test stage 6.1 in progress", modalDlgButton);220}221222223// 6.2. Owner Frame (with owned modal Dialog).224// Focused frame is excluded from modality.225////////////////////////////////////////////////226227if (!"sun.awt.motif.MToolkit".equals(toolkitClassName)) {228recreateGUI();229auxFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);230231Test.setWindows(frame3, modalDialog, new Window[] {modalDialog, frame3});232Test.test("Test stage 6.2 in progress", modalDlgButton, true);233}234235///////////////////////////////////////////////////236// 7. Calling setVisible(true) for the shown Frame.237///////////////////////////////////////////////////238239recreateGUI();240Test.setWindows(frame, null, null);241Test.setTestSetVisible();242Test.test("Test stage 7 in progress", frameButton);243244245Sysout.println("Test passed.");246}247248static class Test {249static Window testWindow; // a window to move to front with autoRequestFocus set250static Window focusWindow; // a window to gain focus251static Window[] showWindows; // windows to show, or null if only testWindow should be shown252253static boolean testSetVisible;254255static void setWindows(Window _testWindow, Window _focusWindow, Window[] _showWindows) {256testWindow = _testWindow;257focusWindow = _focusWindow;258showWindows = _showWindows;259}260static void setTestSetVisible() {261testSetVisible = true;262}263264/*265* @param msg notifies test stage number266* @param testButton a button of the window (owner or owned) that is to be on the top of stack order267* @param shouldFocusChange true for modal dialogs268*/269static void test(String msg, final Button testButton, boolean shouldFocusChange) {270Sysout.println(msg);271272showWindows(testWindow, showWindows, true);273274pause(100);275276/////////////////////////////////////////////////////////277// Test that calling toFront() doesn't cause focus change278// when 'autoRequestFocus' is false.279/////////////////////////////////////////////////////////280281Runnable action = new Runnable() {282public void run() {283testWindow.setAutoRequestFocus(false);284if (testSetVisible) {285setVisible(testWindow, true);286} else {287toFront(testWindow);288}289}290};291292if (shouldFocusChange) {293action.run();294Util.waitForIdle(robot);295if (!focusWindow.isFocused()) {296throw new TestFailedException("the window must gain focus on moving to front but it didn't!");297}298} else if (TestHelper.trackFocusChangeFor(action, robot)) {299throw new TestFailedException("the window shouldn't gain focus on moving to front but it did!");300}301302pause(100);303304///////////////////////////////////////////////////////305// Test that the window (or its owned window) is on top.306///////////////////////////////////////////////////////307308// The latest versions of Metacity (e.g. 2.16) have problems with moving a window to the front.309if (Util.getWMID() != Util.METACITY_WM) {310311boolean performed = Util.trackActionPerformed(testButton, new Runnable() {312public void run() {313Util.clickOnComp(testButton, robot);314}315}, 1000, false);316317if (!performed) {318// For the case when the robot failed to trigger ACTION_EVENT.319Sysout.println("(ACTION_EVENT was not generated. One more attemp.)");320performed = Util.trackActionPerformed(testButton, new Runnable() {321public void run() {322Util.clickOnComp(testButton, robot);323}324}, 1000, false);325if (!performed) {326throw new TestFailedException("the window moved to front is not on the top!");327}328}329}330331showWindows(testWindow, showWindows, false);332333334/////////////////////////////////////////////////335// Test that calling toFront() focuses the window336// when 'autoRequestFocus' is true.337/////////////////////////////////////////////////338339// Skip this stage for unfocusable window340if (!testWindow.isFocusableWindow()) {341return;342}343344showWindows(testWindow, showWindows, true);345346pause(100);347348boolean gained = Util.trackWindowGainedFocus(testWindow, new Runnable() {349public void run() {350testWindow.setAutoRequestFocus(true);351if (testSetVisible) {352setVisible(testWindow, true);353} else {354toFront(testWindow);355}356}357}, 1000, false);358359// Either the window or its owned window must be focused360if (!gained && !testButton.hasFocus()) {361throw new TestFailedException("the window should gain focus automatically but it didn't!");362}363364showWindows(testWindow, showWindows, false);365}366367static void test(String msg, Button testButton) {368test(msg, testButton, false);369}370371private static void showWindows(Window win, Window[] wins, final boolean visible) {372pause(100);373374if (wins == null) {375wins = new Window[] {win}; // operate with 'win'376}377for (final Window w: wins) {378if (visible) {379if ((w instanceof Dialog) && ((Dialog)w).isModal()) {380TestHelper.invokeLaterAndWait(new Runnable() {381public void run() {382w.setVisible(true);383}384}, robot);385} else {386setVisible(w, true);387}388} else {389w.dispose();390}391}392setVisible(auxFrame, visible);393394if (visible) {395if (!auxFrame.isFocused()) {396Util.clickOnTitle(auxFrame, robot);397Util.waitForIdle(robot);398if (!auxFrame.isFocused()) {399throw new Error("Test error: the frame couldn't be focused.");400}401}402}403}404}405406private static void setVisible(Window w, boolean b) {407w.setVisible(b);408try {409Util.waitForIdle(robot);410} catch (RuntimeException rte) { // InfiniteLoop411rte.printStackTrace();412}413robot.delay(200);414}415416private static void toFront(Window w) {417w.toFront();418Util.waitForIdle(robot);419robot.delay(200);420}421422private static void pause(int msec) {423if (haveDelays) {424robot.delay(msec);425}426}427}428429class TestFailedException extends RuntimeException {430TestFailedException(String msg) {431super("Test failed: " + msg);432}433}434435/****************************************************436Standard Test Machinery437DO NOT modify anything below -- it's a standard438chunk of code whose purpose is to make user439interaction uniform, and thereby make it simpler440to read and understand someone else's test.441****************************************************/442443/**444This is part of the standard test machinery.445It creates a dialog (with the instructions), and is the interface446for sending text messages to the user.447To print the instructions, send an array of strings to Sysout.createDialog448WithInstructions method. Put one line of instructions per array entry.449To display a message for the tester to see, simply call Sysout.println450with the string to be displayed.451This mimics System.out.println but works within the test harness as well452as standalone.453*/454455class Sysout456{457static TestDialog dialog;458459public static void createDialogWithInstructions( String[] instructions )460{461dialog = new TestDialog( new Frame(), "Instructions" );462dialog.printInstructions( instructions );463// dialog.setVisible(true);464println( "Any messages for the tester will display here." );465}466467public static void createDialog( )468{469dialog = new TestDialog( new Frame(), "Instructions" );470String[] defInstr = { "Instructions will appear here. ", "" } ;471dialog.printInstructions( defInstr );472// dialog.setVisible(true);473println( "Any messages for the tester will display here." );474}475476477public static void printInstructions( String[] instructions )478{479dialog.printInstructions( instructions );480}481482483public static void println( String messageIn )484{485dialog.displayMessage( messageIn );486}487488}// Sysout class489490/**491This is part of the standard test machinery. It provides a place for the492test instructions to be displayed, and a place for interactive messages493to the user to be displayed.494To have the test instructions displayed, see Sysout.495To have a message to the user be displayed, see Sysout.496Do not call anything in this dialog directly.497*/498class TestDialog extends Dialog499{500501TextArea instructionsText;502TextArea messageText;503int maxStringLength = 80;504505//DO NOT call this directly, go through Sysout506public TestDialog( Frame frame, String name )507{508super( frame, name );509int scrollBoth = TextArea.SCROLLBARS_BOTH;510instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );511add( "North", instructionsText );512513messageText = new TextArea( "", 5, maxStringLength, scrollBoth );514add("Center", messageText);515516pack();517518// setVisible(true);519}// TestDialog()520521//DO NOT call this directly, go through Sysout522public void printInstructions( String[] instructions )523{524//Clear out any current instructions525instructionsText.setText( "" );526527//Go down array of instruction strings528529String printStr, remainingStr;530for( int i=0; i < instructions.length; i++ )531{532//chop up each into pieces maxSringLength long533remainingStr = instructions[ i ];534while( remainingStr.length() > 0 )535{536//if longer than max then chop off first max chars to print537if( remainingStr.length() >= maxStringLength )538{539//Try to chop on a word boundary540int posOfSpace = remainingStr.541lastIndexOf( ' ', maxStringLength - 1 );542543if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;544545printStr = remainingStr.substring( 0, posOfSpace + 1 );546remainingStr = remainingStr.substring( posOfSpace + 1 );547}548//else just print549else550{551printStr = remainingStr;552remainingStr = "";553}554555instructionsText.append( printStr + "\n" );556557}// while558559}// for560561}//printInstructions()562563//DO NOT call this directly, go through Sysout564public void displayMessage( String messageIn )565{566messageText.append( messageIn + "\n" );567System.out.println(messageIn);568}569570}// TestDialog class571572573