Path: blob/master/test/jdk/java/awt/Focus/AutoRequestFocusTest/AutoRequestFocusToFrontTest.java
41152 views
/*1* Copyright (c) 2007, 2018, 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@key headful26@bug 618706627@summary Tests the Window.autoRequestFocus property for the Window.toFront() method.28@library /java/awt/patchlib ../../regtesthelpers29@build java.desktop/java.awt.Helper30@build Util31@run main AutoRequestFocusToFrontTest32*/3334import java.awt.*;35import test.java.awt.regtesthelpers.Util;3637public class AutoRequestFocusToFrontTest {38static boolean haveDelays;3940static Frame auxFrame;41static Frame frame;42static Button frameButton;43static Frame frame2;44static Button frameButton2;45static Frame frame3;46static Button frameButton3;47static Window window;48static Button winButton;49static Dialog dialog;50static Button dlgButton;51static Window ownedWindow;52static Button ownWinButton;53static Dialog ownedDialog;54static Button ownDlgButton;55static Dialog modalDialog;56static Button modalDlgButton;5758static String toolkitClassName;59static Robot robot = Util.createRobot();6061public static void main(String[] args) {6263if (args.length != 0) {64haveDelays = "delay".equals(args[0]) ? true : false;65}6667AutoRequestFocusToFrontTest app = new AutoRequestFocusToFrontTest();68app.init();69app.start();70}7172public void init() {73toolkitClassName = Toolkit.getDefaultToolkit().getClass().getName();74}7576static void recreateGUI() {77if (auxFrame != null) {78auxFrame.dispose();79frame.dispose();80frame2.dispose();81frame3.dispose();82window.dispose();83dialog.dispose();84ownedWindow.dispose();85ownedDialog.dispose();86modalDialog.dispose();87}8889auxFrame = new Frame("Auxiliary Frame");9091frame = new Frame("Test Frame");92frameButton = new Button("button");9394frame2 = new Frame("Test Frame 2");95frameButton2 = new Button("button");9697frame3 = new Frame("Test Frame 3");98frameButton3 = new Button("button");99100window = new Window(null);101winButton = new Button("button");102dialog = new Dialog((Frame)null, "Test Dialog");103dlgButton = new Button("button");104105ownedWindow = new Window(frame);106ownWinButton = new Button("button");107108ownedDialog = new Dialog(frame2, "Test Owned Dialog");109ownDlgButton = new Button("button");110111modalDialog = new Dialog(frame3, "Test Modal Dialog");112modalDlgButton = new Button("button");113114auxFrame.setBounds(100, 100, 300, 300);115116frame.setBounds(120, 120, 260, 260);117frame.add(frameButton);118119frame2.setBounds(120, 120, 260, 260);120frame2.add(frameButton2);121122frame3.setBounds(120, 120, 260, 260);123frame3.add(frameButton3);124125window.setBounds(120, 120, 260, 260);126window.add(winButton);127128dialog.setBounds(120, 120, 260, 260);129dialog.add(dlgButton);130131ownedWindow.setBounds(140, 140, 220, 220);132ownedWindow.add(ownWinButton);133134ownedDialog.setBounds(140, 140, 220, 220);135ownedDialog.add(ownDlgButton);136137modalDialog.setBounds(140, 140, 220, 220);138modalDialog.add(modalDlgButton);139modalDialog.setModal(true);140}141142public void start() {143// 1. Simple Frame.144//////////////////145146recreateGUI();147Test.setWindows(frame, null, null);148Test.test("Test stage 1 in progress", frameButton);149150151// 2. Ownerless Window.152//////////////////////153154recreateGUI();155Test.setWindows(window, null, null);156Test.test("Test stage 2 in progress", winButton);157158159// 3. Ownerless Dialog.160//////////////////////161162recreateGUI();163Test.setWindows(dialog, null, null);164Test.test("Test stage 3 in progress", dlgButton);165166167// 4.1. Owner Frame (with owned Window).168///////////////////////////////////////169170recreateGUI();171Test.setWindows(frame, null, new Window[] {ownedWindow, frame});172Test.test("Test stage 4.1 in progress", ownWinButton);173174175// 4.2. Owned Window (with owner Frame).176///////////////////////////////////////177178recreateGUI();179Test.setWindows(ownedWindow, null, new Window[] {ownedWindow, frame});180Test.test("Test stage 4.2 in progress", ownWinButton);181182183// 5.1. Owner Frame (with owned Dialog).184///////////////////////////////////////185186recreateGUI();187Test.setWindows(frame2, null, new Window[] {ownedDialog, frame2});188Test.test("Test stage 5.1 in progress", ownDlgButton);189190191// 5.2. Owned Dialog (with owner Frame).192///////////////////////////////////////193194recreateGUI();195Test.setWindows(ownedDialog, null, new Window[] {ownedDialog, frame2});196Test.test("Test stage 5.2 in progress", ownDlgButton);197198199////////////////////////////////////////////////200// 6.1. Owned modal Dialog (with owner Frame).201// Focused frame is excluded from modality.202////////////////////////////////////////////////203204if (!"sun.awt.motif.MToolkit".equals(toolkitClassName)) {205recreateGUI();206auxFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);207208Test.setWindows(modalDialog, modalDialog, new Window[] {modalDialog, frame3});209Test.test("Test stage 6.1 in progress", modalDlgButton);210}211212213// 6.2. Owner Frame (with owned modal Dialog).214// Focused frame is excluded from modality.215////////////////////////////////////////////////216217if (!"sun.awt.motif.MToolkit".equals(toolkitClassName)) {218recreateGUI();219auxFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);220221Test.setWindows(frame3, modalDialog, new Window[] {modalDialog, frame3});222Test.test("Test stage 6.2 in progress", modalDlgButton, true);223}224225///////////////////////////////////////////////////226// 7. Calling setVisible(true) for the shown Frame.227///////////////////////////////////////////////////228229recreateGUI();230Test.setWindows(frame, null, null);231Test.setTestSetVisible();232Test.test("Test stage 7 in progress", frameButton);233234235System.out.println("Test passed.");236}237238static class Test {239static Window testWindow; // a window to move to front with autoRequestFocus set240static Window focusWindow; // a window to gain focus241static Window[] showWindows; // windows to show, or null if only testWindow should be shown242243static boolean testSetVisible;244245static void setWindows(Window _testWindow, Window _focusWindow, Window[] _showWindows) {246testWindow = _testWindow;247focusWindow = _focusWindow;248showWindows = _showWindows;249}250static void setTestSetVisible() {251testSetVisible = true;252}253254/*255* @param msg notifies test stage number256* @param testButton a button of the window (owner or owned) that is to be on the top of stack order257* @param shouldFocusChange true for modal dialogs258*/259static void test(String msg, final Button testButton, boolean shouldFocusChange) {260System.out.println(msg);261262showWindows(testWindow, showWindows, true);263264pause(100);265266/////////////////////////////////////////////////////////267// Test that calling toFront() doesn't cause focus change268// when 'autoRequestFocus' is false.269/////////////////////////////////////////////////////////270271Runnable action = new Runnable() {272public void run() {273testWindow.setAutoRequestFocus(false);274if (testSetVisible) {275setVisible(testWindow, true);276} else {277toFront(testWindow);278}279}280};281282if (shouldFocusChange) {283action.run();284Util.waitForIdle(robot);285if (!focusWindow.isFocused()) {286throw new TestFailedException("the window must gain focus on moving to front but it didn't!");287}288} else if (TestHelper.trackFocusChangeFor(action, robot)) {289throw new TestFailedException("the window shouldn't gain focus on moving to front but it did!");290}291292pause(100);293294///////////////////////////////////////////////////////295// Test that the window (or its owned window) is on top.296///////////////////////////////////////////////////////297298// The latest versions of Metacity (e.g. 2.16) have problems with moving a window to the front.299if (Util.getWMID() != Util.METACITY_WM) {300301boolean performed = Util.trackActionPerformed(testButton, new Runnable() {302public void run() {303Util.clickOnComp(testButton, robot);304}305}, 1000, false);306307if (!performed) {308// For the case when the robot failed to trigger ACTION_EVENT.309System.out.println("(ACTION_EVENT was not generated. One more attemp.)");310performed = Util.trackActionPerformed(testButton, new Runnable() {311public void run() {312Util.clickOnComp(testButton, robot);313}314}, 1000, false);315if (!performed) {316throw new TestFailedException("the window moved to front is not on the top!");317}318}319}320321showWindows(testWindow, showWindows, false);322323324/////////////////////////////////////////////////325// Test that calling toFront() focuses the window326// when 'autoRequestFocus' is true.327/////////////////////////////////////////////////328329// Skip this stage for unfocusable window330if (!testWindow.isFocusableWindow()) {331return;332}333334showWindows(testWindow, showWindows, true);335336pause(100);337338boolean gained = Util.trackWindowGainedFocus(testWindow, new Runnable() {339public void run() {340testWindow.setAutoRequestFocus(true);341if (testSetVisible) {342setVisible(testWindow, true);343} else {344toFront(testWindow);345}346}347}, 1000, false);348349// Either the window or its owned window must be focused350if (!gained && !testButton.hasFocus()) {351throw new TestFailedException("the window should gain focus automatically but it didn't!");352}353354showWindows(testWindow, showWindows, false);355}356357static void test(String msg, Button testButton) {358test(msg, testButton, false);359}360361private static void showWindows(Window win, Window[] wins, final boolean visible) {362pause(100);363364if (wins == null) {365wins = new Window[] {win}; // operate with 'win'366}367for (final Window w: wins) {368if (visible) {369if ((w instanceof Dialog) && ((Dialog)w).isModal()) {370TestHelper.invokeLaterAndWait(new Runnable() {371public void run() {372w.setVisible(true);373}374}, robot);375} else {376setVisible(w, true);377}378} else {379w.dispose();380}381}382setVisible(auxFrame, visible);383384if (visible) {385if (!auxFrame.isFocused()) {386Util.clickOnTitle(auxFrame, robot);387Util.waitForIdle(robot);388if (!auxFrame.isFocused()) {389throw new Error("Test error: the frame couldn't be focused.");390}391}392}393}394}395396private static void setVisible(Window w, boolean b) {397w.setVisible(b);398try {399Util.waitForIdle(robot);400} catch (RuntimeException rte) { // InfiniteLoop401rte.printStackTrace();402}403robot.delay(200);404}405406private static void toFront(Window w) {407w.toFront();408Util.waitForIdle(robot);409robot.delay(200);410}411412private static void pause(int msec) {413if (haveDelays) {414robot.delay(msec);415}416}417}418419class TestFailedException extends RuntimeException {420TestFailedException(String msg) {421super("Test failed: " + msg);422}423}424425426427