Path: blob/master/test/jdk/java/awt/Focus/AutoRequestFocusTest/AutoRequestFocusSetVisibleTest.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.setVisible() method.28@library ../../regtesthelpers29@build Util30@run main AutoRequestFocusSetVisibleTest31*/3233import java.awt.*;34import test.java.awt.regtesthelpers.Util;3536public class AutoRequestFocusSetVisibleTest {37static Frame focusedFrame;38static Button focusOwner;39static Frame frame;40static Button frameButton;41static Frame frame2;42static Button frameButton2;43static Window window;44static Button winButton;45static Window ownedWindow;46static Button ownWinButton;47static Dialog ownedDialog;48static Button ownDlgButton;49static Dialog dialog;50static Button dlgButton;5152static String toolkitClassName;53static Robot robot = Util.createRobot();5455public static void main(String[] args) {56AutoRequestFocusSetVisibleTest app = new AutoRequestFocusSetVisibleTest();57app.init();58app.start();59}6061public void init() {62toolkitClassName = Toolkit.getDefaultToolkit().getClass().getName();63}6465void recreateGUI() {66if (focusedFrame != null) {67focusedFrame.dispose();68frame.dispose();69frame2.dispose();70window.dispose();71ownedWindow.dispose();72ownedDialog.dispose();73dialog.dispose();74}7576focusedFrame = new Frame("Base Frame");77focusOwner = new Button("button");7879frame = new Frame("Test Frame");80frameButton = new Button("button");8182frame2 = new Frame("Test Frame");83frameButton2 = new Button("button");8485window = new Window(focusedFrame);86winButton = new Button("button");8788ownedWindow = new Window(frame) {89/*90* When 'frame' is shown along with the 'ownedWindow'91* (i.e. showWithParent==true) then it can appear92* that the 'ownedWindow' is shown too early and93* it can't be focused due to its owner can't be94* yet activated. So, to avoid this race, we pospone95* a little the showing of the 'ownedWindow'.96*/97public void show() {98robot.delay(100);99super.show();100}101};102ownWinButton = new Button("button");103104ownedDialog = new Dialog(frame2);105ownDlgButton = new Button("button");106107dialog = new Dialog(focusedFrame, "Test Dialog");108dlgButton = new Button("button");109110focusedFrame.add(focusOwner);111focusedFrame.setBounds(100, 100, 300, 300);112113frame.setBounds(140, 140, 220, 220);114frame.add(frameButton);115116frame2.setBounds(140, 140, 220, 220);117frame2.add(frameButton2);118119window.setBounds(140, 140, 220, 220);120window.add(winButton);121122ownedWindow.setBounds(180, 180, 140, 140);123ownedWindow.add(ownWinButton);124125ownedDialog.setBounds(180, 180, 140, 140);126ownedDialog.add(ownDlgButton);127128dialog.setBounds(140, 140, 220, 220);129dialog.add(dlgButton);130}131132public void start() {133134///////////////////////////////////////////////////////135// 1. Show Frame with owned modal Dialog without delay.136// Check that the Dialog takes focus.137///////////////////////////////////////////////////////138139recreateGUI();140141System.out.println("Stage 1 in progress...");142143dialog.setModal(true);144dialog.setAutoRequestFocus(false);145setVisible(focusedFrame, true);146147TestHelper.invokeLaterAndWait(new Runnable() {148public void run() {149dialog.setVisible(true);150}151}, robot);152153if (focusOwner.hasFocus()) {154throw new TestFailedException("the modal dialog must gain focus but it didn't!");155}156setVisible(dialog, false);157158//////////////////////////////////////////////////159// 2. Show Frame, activate, auto hide, auto show.160// Check that the Frame takes focus.161//////////////////////////////////////////////////162163recreateGUI();164165System.out.println("Stage 2 in progress...");166167setVisible(focusedFrame, false);168169focusedFrame.setAutoRequestFocus(false);170setVisible(focusedFrame, true);171172Util.clickOnTitle(focusedFrame, robot);173Util.waitForIdle(robot);174175if (!focusedFrame.isFocused()) {176throw new Error("Test error: the frame couldn't be focused.");177}178179focusedFrame.setExtendedState(Frame.ICONIFIED);180Util.waitForIdle(robot);181focusedFrame.setExtendedState(Frame.NORMAL);182Util.waitForIdle(robot);183184if (!focusedFrame.isFocused()) {185throw new TestFailedException("the restored frame must gain focus but it didn't!");186}187188189////////////////////////190// 3.1 Show Frame normal.191////////////////////////192193recreateGUI();194195test("Stage 3.1 in progress...", frame, frameButton);196197198// 3.2. Show Frame maximized both.199/////////////////////////////////200201if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {202System.out.println("Stage 3.2: Frame.MAXIMIZED_BOTH not supported. Skipping.");203} else {204frame.setExtendedState(Frame.MAXIMIZED_BOTH);205206test("Stage 3.2 in progress...", frame, frameButton);207}208209210// 3.3. Show Frame maximized vertically.211///////////////////////////////////////212213if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_VERT)) {214System.out.println("Stage 3.3: Frame.MAXIMIZED_VERT not supported. Skipping.");215} else {216frame.setExtendedState(Frame.MAXIMIZED_VERT);217218test("Stage 3.3 in progress...", frame, frameButton);219}220221222// 3.4. Show Frame maximized horizontally.223/////////////////////////////////////////224225if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_HORIZ)) {226System.out.println("Stage 3.4: Frame.MAXIMIZED_HORIZ not supported. Skipping.");227} else {228frame.setExtendedState(Frame.MAXIMIZED_HORIZ);229230test("Stage 3.4 in progress...", frame, frameButton);231}232233234// 3.5. Show Frame iconified.235////////////////////////////236237if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.ICONIFIED)) {238System.out.println("Stage 3.5: Frame.ICONIFIED not supported. Skipping.");239} else {240frame.setExtendedState(Frame.ICONIFIED);241242test("Stage 3.5 in progress...", frame, frameButton);243}244245246///////////////////247// 4.1 Show Window.248///////////////////249recreateGUI();250test("Stage 4.1 in progress...", window, winButton);251252253// 4.2 Show Dialog.254//////////////////255256test("Stage 4.2 in progress...", dialog, dlgButton);257258259// 4.3. Show modal Dialog.260/////////////////////////261262dialog.setModal(true);263test("Stage 4.3 in progress...", dialog, dlgButton, true);264265266///////////////////////////////////267// 5.1 Show Frame with owned Window.268///////////////////////////////////269270// On Windows, an owned Window will not be focused on its showing271// if the owner is not currently active.272if ("sun.awt.windows.WToolkit".equals(toolkitClassName)) {273System.out.println("Stage 5.1 - Skiping.");274} else {275setVisible(ownedWindow, true);276setVisible(frame, false); // 'ownedWindow' will be shown along with the owner.277278test("Stage 5.1 in progress...", frame, ownedWindow, ownWinButton, true);279}280281282// 5.2 Show Frame with owned Dialog.283///////////////////////////////////284285setVisible(ownedDialog, true);286setVisible(frame2, false); // 'ownedDialog' will be shown along with the owner.287288test("Stage 5.2 in progress...", frame2, ownedDialog, ownDlgButton, true);289290291///////////////////////////////////292// 6. Show unblocking modal Dialog.293///////////////////////////////////294295if ("sun.awt.motif.MToolkit".equals(toolkitClassName)) {296System.out.println("Stage 6 - Skiping.");297} else {298System.out.println("Stage 6 in progress...");299300// ---301// Testing the bug of activating invisible modal Dialog (awt_Window::SetAndActivateModalBlocker).302// Having some window not excluded from modality, so that it would be blocked.303Frame f = new Frame("Aux. Frame");304f.setSize(100, 100);305setVisible(f, true);306// ---307308setVisible(focusedFrame, true);309if (!focusOwner.hasFocus()) {310Util.clickOnComp(focusOwner, robot);311Util.waitForIdle(robot);312if (!focusOwner.hasFocus()) {313throw new Error("Test error: the frame couldn't be focused.");314}315}316317dialog.setModal(true);318dialog.setAutoRequestFocus(false);319focusedFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);320321TestHelper.invokeLaterAndWait(new Runnable() {322public void run() {323dialog.setVisible(true);324}325}, robot);326327if (dialog.isFocused()) {328throw new TestFailedException("the unblocking dialog shouldn't gain focus but it did!");329}330setVisible(dialog, false);331}332333System.out.println("Test passed.");334}335336/*337* @param msg notifies test stage number338* @param showWindow a window to show/test (if ownedWindow == null)339* @param ownedWindow an owned window to show/test, or null if showWindow should be tested340* @param clickButton a button of the window (owner or owned) expected to be on the top of stack order341* @param shouldFocusChange true the test window should gain focus342*/343void test(String msg, final Window showWindow, Window ownedWindow, final Button clickButton, boolean shouldFocusChange) {344Window testWindow = (ownedWindow == null ? showWindow : ownedWindow);345346System.out.println(msg);347348if (showWindow.isVisible()) {349showWindow.dispose();350Util.waitForIdle(robot);351}352if (!focusedFrame.isVisible()) {353setVisible(focusedFrame, true);354}355if (!focusOwner.hasFocus()) {356Util.clickOnComp(focusOwner, robot);357Util.waitForIdle(robot);358if (!focusOwner.hasFocus()) {359throw new Error("Test error: the frame couldn't be focused.");360}361}362363//////////////////////////////////////////364// Test focus change on showing the window365//////////////////////////////////////////366367final Runnable showAction = new Runnable() {368public void run() {369showWindow.setAutoRequestFocus(false);370showWindow.setVisible(true);371}372};373374final Runnable trackerAction = new Runnable() {375public void run() {376if (showWindow instanceof Dialog && ((Dialog)showWindow).isModal()) {377TestHelper.invokeLaterAndWait(showAction, robot);378} else {379showAction.run();380}381}382};383384if (shouldFocusChange) {385trackerAction.run();386Util.waitForIdle(robot);387388if (!testWindow.isFocused()) {389throw new TestFailedException("the window must gain focus but it didn't!");390}391392} else if (TestHelper.trackFocusChangeFor(trackerAction, robot)) {393throw new TestFailedException("the window shouldn't gain focus but it did!");394}395396397////////////////////////////////////////////398// Test that the window was shown on the top.399// Test that it can be focused.400////////////////////////////////////////////401402if (!(testWindow instanceof Frame) ||403((Frame)testWindow).getExtendedState() != Frame.ICONIFIED)404{405boolean performed = Util.trackActionPerformed(clickButton, new Runnable() {406public void run() {407/*408* If 'showWindow' is not on the top then409* 'focusOwner' button completely overlaps 'clickButton'410* and we won't catch the action.411*/412Util.clickOnComp(clickButton, robot);413}414}, 1000, false);415416if (!performed) {417// In case of loosing ACTION_PERFORMED, try once more.418System.out.println("(ACTION_EVENT was not generated. One more attemp.)");419performed = Util.trackActionPerformed(clickButton, new Runnable() {420public void run() {421Util.clickOnComp(clickButton, robot);422}423}, 1000, false);424425if (!performed) {426throw new TestFailedException("the window shown is not on the top!");427}428}429}430431recreateGUI();432}433434void test(String msg, final Window showWindow, Button clickButton) {435test(msg, showWindow, null, clickButton, false);436}437void test(String msg, final Window showWindow, Button clickButton, boolean shouldFocusChange) {438test(msg, showWindow, null, clickButton, shouldFocusChange);439}440void test(String msg, final Window showWindow, Window ownedWindow, Button clickButton) {441test(msg, showWindow, ownedWindow, clickButton, false);442}443444private static void setVisible(Window w, boolean b) {445w.setVisible(b);446try {447Util.waitForIdle(robot);448} catch (RuntimeException rte) { // InfiniteLoop449rte.printStackTrace();450}451robot.delay(200);452}453}454455class TestFailedException extends RuntimeException {456TestFailedException(String msg) {457super("Test failed: " + msg);458}459}460461462