Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/AutoRequestFocusTest/AutoRequestFocusSetVisibleTest.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.setVisible() method.27@author anton.tarasov: area=awt.focus28@library ../../regtesthelpers29@build Util30@run main AutoRequestFocusSetVisibleTest31*/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 AutoRequestFocusSetVisibleTest extends Applet {41static Frame focusedFrame;42static Button focusOwner;43static Frame frame;44static Button frameButton;45static Frame frame2;46static Button frameButton2;47static Window window;48static Button winButton;49static Window ownedWindow;50static Button ownWinButton;51static Dialog ownedDialog;52static Button ownDlgButton;53static Dialog dialog;54static Button dlgButton;5556static String toolkitClassName;57static Robot robot = Util.createRobot();5859public static void main(String[] args) {60AutoRequestFocusSetVisibleTest app = new AutoRequestFocusSetVisibleTest();61app.init();62app.start();63}6465public void init() {66// Create instructions for the user here, as well as set up67// the environment -- set the layout manager, add buttons,68// etc.69this.setLayout (new BorderLayout ());70Sysout.createDialogWithInstructions(new String[]71{"This is an automatic test. Simply wait until it is done."72});73toolkitClassName = Toolkit.getDefaultToolkit().getClass().getName();74}7576void recreateGUI() {77if (focusedFrame != null) {78focusedFrame.dispose();79frame.dispose();80frame2.dispose();81window.dispose();82ownedWindow.dispose();83ownedDialog.dispose();84dialog.dispose();85}8687focusedFrame = new Frame("Base Frame");88focusOwner = new Button("button");8990frame = new Frame("Test Frame");91frameButton = new Button("button");9293frame2 = new Frame("Test Frame");94frameButton2 = new Button("button");9596window = new Window(focusedFrame);97winButton = new Button("button");9899ownedWindow = new Window(frame) {100/*101* When 'frame' is shown along with the 'ownedWindow'102* (i.e. showWithParent==true) then it can appear103* that the 'ownedWindow' is shown too early and104* it can't be focused due to its owner can't be105* yet activated. So, to avoid this race, we pospone106* a little the showing of the 'ownedWindow'.107*/108public void show() {109robot.delay(100);110super.show();111}112};113ownWinButton = new Button("button");114115ownedDialog = new Dialog(frame2);116ownDlgButton = new Button("button");117118dialog = new Dialog(focusedFrame, "Test Dialog");119dlgButton = new Button("button");120121focusedFrame.add(focusOwner);122focusedFrame.setBounds(100, 100, 300, 300);123124frame.setBounds(140, 140, 220, 220);125frame.add(frameButton);126127frame2.setBounds(140, 140, 220, 220);128frame2.add(frameButton2);129130window.setBounds(140, 140, 220, 220);131window.add(winButton);132133ownedWindow.setBounds(180, 180, 140, 140);134ownedWindow.add(ownWinButton);135136ownedDialog.setBounds(180, 180, 140, 140);137ownedDialog.add(ownDlgButton);138139dialog.setBounds(140, 140, 220, 220);140dialog.add(dlgButton);141}142143public void start() {144145///////////////////////////////////////////////////////146// 1. Show Frame with owned modal Dialog without delay.147// Check that the Dialog takes focus.148///////////////////////////////////////////////////////149150recreateGUI();151152Sysout.println("Stage 1 in progress...");153154dialog.setModal(true);155dialog.setAutoRequestFocus(false);156setVisible(focusedFrame, true);157158TestHelper.invokeLaterAndWait(new Runnable() {159public void run() {160dialog.setVisible(true);161}162}, robot);163164if (focusOwner.hasFocus()) {165throw new TestFailedException("the modal dialog must gain focus but it didn't!");166}167setVisible(dialog, false);168169//////////////////////////////////////////////////170// 2. Show Frame, activate, auto hide, auto show.171// Check that the Frame takes focus.172//////////////////////////////////////////////////173174recreateGUI();175176Sysout.println("Stage 2 in progress...");177178setVisible(focusedFrame, false);179180focusedFrame.setAutoRequestFocus(false);181setVisible(focusedFrame, true);182183Util.clickOnTitle(focusedFrame, robot);184Util.waitForIdle(robot);185186if (!focusedFrame.isFocused()) {187throw new Error("Test error: the frame couldn't be focused.");188}189190focusedFrame.setExtendedState(Frame.ICONIFIED);191Util.waitForIdle(robot);192focusedFrame.setExtendedState(Frame.NORMAL);193Util.waitForIdle(robot);194195if (!focusedFrame.isFocused()) {196throw new TestFailedException("the restored frame must gain focus but it didn't!");197}198199200////////////////////////201// 3.1 Show Frame normal.202////////////////////////203204recreateGUI();205206test("Stage 3.1 in progress...", frame, frameButton);207208209// 3.2. Show Frame maximized both.210/////////////////////////////////211212if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_BOTH)) {213System.out.println("Stage 3.2: Frame.MAXIMIZED_BOTH not supported. Skipping.");214} else {215frame.setExtendedState(Frame.MAXIMIZED_BOTH);216217test("Stage 3.2 in progress...", frame, frameButton);218}219220221// 3.3. Show Frame maximized vertically.222///////////////////////////////////////223224if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_VERT)) {225System.out.println("Stage 3.3: Frame.MAXIMIZED_VERT not supported. Skipping.");226} else {227frame.setExtendedState(Frame.MAXIMIZED_VERT);228229test("Stage 3.3 in progress...", frame, frameButton);230}231232233// 3.4. Show Frame maximized horizontally.234/////////////////////////////////////////235236if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.MAXIMIZED_HORIZ)) {237System.out.println("Stage 3.4: Frame.MAXIMIZED_HORIZ not supported. Skipping.");238} else {239frame.setExtendedState(Frame.MAXIMIZED_HORIZ);240241test("Stage 3.4 in progress...", frame, frameButton);242}243244245// 3.5. Show Frame iconified.246////////////////////////////247248if (!Toolkit.getDefaultToolkit().isFrameStateSupported(Frame.ICONIFIED)) {249System.out.println("Stage 3.5: Frame.ICONIFIED not supported. Skipping.");250} else {251frame.setExtendedState(Frame.ICONIFIED);252253test("Stage 3.5 in progress...", frame, frameButton);254}255256257///////////////////258// 4.1 Show Window.259///////////////////260recreateGUI();261test("Stage 4.1 in progress...", window, winButton);262263264// 4.2 Show Dialog.265//////////////////266267test("Stage 4.2 in progress...", dialog, dlgButton);268269270// 4.3. Show modal Dialog.271/////////////////////////272273dialog.setModal(true);274test("Stage 4.3 in progress...", dialog, dlgButton, true);275276277///////////////////////////////////278// 5.1 Show Frame with owned Window.279///////////////////////////////////280281// On Windows, an owned Window will not be focused on its showing282// if the owner is not currently active.283if ("sun.awt.windows.WToolkit".equals(toolkitClassName)) {284Sysout.println("Stage 5.1 - Skiping.");285} else {286setVisible(ownedWindow, true);287setVisible(frame, false); // 'ownedWindow' will be shown along with the owner.288289test("Stage 5.1 in progress...", frame, ownedWindow, ownWinButton, true);290}291292293// 5.2 Show Frame with owned Dialog.294///////////////////////////////////295296setVisible(ownedDialog, true);297setVisible(frame2, false); // 'ownedDialog' will be shown along with the owner.298299test("Stage 5.2 in progress...", frame2, ownedDialog, ownDlgButton, true);300301302///////////////////////////////////303// 6. Show unblocking modal Dialog.304///////////////////////////////////305306if ("sun.awt.motif.MToolkit".equals(toolkitClassName)) {307Sysout.println("Stage 6 - Skiping.");308} else {309Sysout.println("Stage 6 in progress...");310311// ---312// Testing the bug of activating invisible modal Dialog (awt_Window::SetAndActivateModalBlocker).313// Having some window not excluded from modality, so that it would be blocked.314Frame f = new Frame("Aux. Frame");315f.setSize(100, 100);316setVisible(f, true);317// ---318319setVisible(focusedFrame, true);320if (!focusOwner.hasFocus()) {321Util.clickOnComp(focusOwner, robot);322Util.waitForIdle(robot);323if (!focusOwner.hasFocus()) {324throw new Error("Test error: the frame couldn't be focused.");325}326}327328dialog.setModal(true);329dialog.setAutoRequestFocus(false);330focusedFrame.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);331332TestHelper.invokeLaterAndWait(new Runnable() {333public void run() {334dialog.setVisible(true);335}336}, robot);337338if (dialog.isFocused()) {339throw new TestFailedException("the unblocking dialog shouldn't gain focus but it did!");340}341setVisible(dialog, false);342}343344Sysout.println("Test passed.");345}346347/*348* @param msg notifies test stage number349* @param showWindow a window to show/test (if ownedWindow == null)350* @param ownedWindow an owned window to show/test, or null if showWindow should be tested351* @param clickButton a button of the window (owner or owned) expected to be on the top of stack order352* @param shouldFocusChange true the test window should gain focus353*/354void test(String msg, final Window showWindow, Window ownedWindow, final Button clickButton, boolean shouldFocusChange) {355Window testWindow = (ownedWindow == null ? showWindow : ownedWindow);356357Sysout.println(msg);358359if (showWindow.isVisible()) {360showWindow.dispose();361Util.waitForIdle(robot);362}363if (!focusedFrame.isVisible()) {364setVisible(focusedFrame, true);365}366if (!focusOwner.hasFocus()) {367Util.clickOnComp(focusOwner, robot);368Util.waitForIdle(robot);369if (!focusOwner.hasFocus()) {370throw new Error("Test error: the frame couldn't be focused.");371}372}373374//////////////////////////////////////////375// Test focus change on showing the window376//////////////////////////////////////////377378final Runnable showAction = new Runnable() {379public void run() {380showWindow.setAutoRequestFocus(false);381showWindow.setVisible(true);382}383};384385final Runnable trackerAction = new Runnable() {386public void run() {387if (showWindow instanceof Dialog && ((Dialog)showWindow).isModal()) {388TestHelper.invokeLaterAndWait(showAction, robot);389} else {390showAction.run();391}392}393};394395if (shouldFocusChange) {396trackerAction.run();397Util.waitForIdle(robot);398399if (!testWindow.isFocused()) {400throw new TestFailedException("the window must gain focus but it didn't!");401}402403} else if (TestHelper.trackFocusChangeFor(trackerAction, robot)) {404throw new TestFailedException("the window shouldn't gain focus but it did!");405}406407408////////////////////////////////////////////409// Test that the window was shown on the top.410// Test that it can be focused.411////////////////////////////////////////////412413if (!(testWindow instanceof Frame) ||414((Frame)testWindow).getExtendedState() != Frame.ICONIFIED)415{416boolean performed = Util.trackActionPerformed(clickButton, new Runnable() {417public void run() {418/*419* If 'showWindow' is not on the top then420* 'focusOwner' button completely overlaps 'clickButton'421* and we won't catch the action.422*/423Util.clickOnComp(clickButton, robot);424}425}, 1000, false);426427if (!performed) {428// In case of loosing ACTION_PERFORMED, try once more.429Sysout.println("(ACTION_EVENT was not generated. One more attemp.)");430performed = Util.trackActionPerformed(clickButton, new Runnable() {431public void run() {432Util.clickOnComp(clickButton, robot);433}434}, 1000, false);435436if (!performed) {437throw new TestFailedException("the window shown is not on the top!");438}439}440}441442recreateGUI();443}444445void test(String msg, final Window showWindow, Button clickButton) {446test(msg, showWindow, null, clickButton, false);447}448void test(String msg, final Window showWindow, Button clickButton, boolean shouldFocusChange) {449test(msg, showWindow, null, clickButton, shouldFocusChange);450}451void test(String msg, final Window showWindow, Window ownedWindow, Button clickButton) {452test(msg, showWindow, ownedWindow, clickButton, false);453}454455private static void setVisible(Window w, boolean b) {456w.setVisible(b);457try {458Util.waitForIdle(robot);459} catch (RuntimeException rte) { // InfiniteLoop460rte.printStackTrace();461}462robot.delay(200);463}464}465466class TestFailedException extends RuntimeException {467TestFailedException(String msg) {468super("Test failed: " + msg);469}470}471472/****************************************************473Standard Test Machinery474DO NOT modify anything below -- it's a standard475chunk of code whose purpose is to make user476interaction uniform, and thereby make it simpler477to read and understand someone else's test.478****************************************************/479480/**481This is part of the standard test machinery.482It creates a dialog (with the instructions), and is the interface483for sending text messages to the user.484To print the instructions, send an array of strings to Sysout.createDialog485WithInstructions method. Put one line of instructions per array entry.486To display a message for the tester to see, simply call Sysout.println487with the string to be displayed.488This mimics System.out.println but works within the test harness as well489as standalone.490*/491492class Sysout493{494static TestDialog dialog;495496public static void createDialogWithInstructions( String[] instructions )497{498dialog = new TestDialog( new Frame(), "Instructions" );499dialog.printInstructions( instructions );500// dialog.setVisible(true);501println( "Any messages for the tester will display here." );502}503504public static void createDialog( )505{506dialog = new TestDialog( new Frame(), "Instructions" );507String[] defInstr = { "Instructions will appear here. ", "" } ;508dialog.printInstructions( defInstr );509// dialog.setVisible(true);510println( "Any messages for the tester will display here." );511}512513514public static void printInstructions( String[] instructions )515{516dialog.printInstructions( instructions );517}518519520public static void println( String messageIn )521{522dialog.displayMessage( messageIn );523}524525}// Sysout class526527/**528This is part of the standard test machinery. It provides a place for the529test instructions to be displayed, and a place for interactive messages530to the user to be displayed.531To have the test instructions displayed, see Sysout.532To have a message to the user be displayed, see Sysout.533Do not call anything in this dialog directly.534*/535class TestDialog extends Dialog536{537538TextArea instructionsText;539TextArea messageText;540int maxStringLength = 80;541542//DO NOT call this directly, go through Sysout543public TestDialog( Frame frame, String name )544{545super( frame, name );546int scrollBoth = TextArea.SCROLLBARS_BOTH;547instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );548add( "North", instructionsText );549550messageText = new TextArea( "", 5, maxStringLength, scrollBoth );551add("Center", messageText);552553pack();554555// setVisible(true);556}// TestDialog()557558//DO NOT call this directly, go through Sysout559public void printInstructions( String[] instructions )560{561//Clear out any current instructions562instructionsText.setText( "" );563564//Go down array of instruction strings565566String printStr, remainingStr;567for( int i=0; i < instructions.length; i++ )568{569//chop up each into pieces maxSringLength long570remainingStr = instructions[ i ];571while( remainingStr.length() > 0 )572{573//if longer than max then chop off first max chars to print574if( remainingStr.length() >= maxStringLength )575{576//Try to chop on a word boundary577int posOfSpace = remainingStr.578lastIndexOf( ' ', maxStringLength - 1 );579580if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;581582printStr = remainingStr.substring( 0, posOfSpace + 1 );583remainingStr = remainingStr.substring( posOfSpace + 1 );584}585//else just print586else587{588printStr = remainingStr;589remainingStr = "";590}591592instructionsText.append( printStr + "\n" );593594}// while595596}// for597598}//printInstructions()599600//DO NOT call this directly, go through Sysout601public void displayMessage( String messageIn )602{603messageText.append( messageIn + "\n" );604System.out.println(messageIn);605}606607}// TestDialog class608609610