Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/ShowFrameCheckForegroundTest/ShowFrameCheckForegroundTest.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 649297026@summary Tests that showing a toplvel in a not foreground Java process activates it.27@library ../../regtesthelpers28@build Util29@author Anton Tarasov: area=awt-focus30@run main ShowFrameCheckForegroundTest31*/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 ShowFrameCheckForegroundTest extends Applet {41Robot robot;42Frame nofocusFrame = new Frame("Non-focusable");43Frame frame = new Frame("Frame");44Dialog dialog1 = new Dialog(nofocusFrame, "Owned Dialog", false);45Dialog dialog2 = new Dialog((Frame)null, "Owned Dialog", false);46Window testToplevel = null;47Button testButton = new Button("button");48Button showButton = new Button("show");49Runnable action = new Runnable() {50public void run() {51robot.keyPress(KeyEvent.VK_SPACE);52robot.delay(50);53robot.keyRelease(KeyEvent.VK_SPACE);54}55};565758public static void main(String[] args) {59ShowFrameCheckForegroundTest app = new ShowFrameCheckForegroundTest();60app.init();61app.start();62}6364public void init() {65robot = Util.createRobot();66}6768public void start() {69showButton.addActionListener(new ActionListener() {70public void actionPerformed(ActionEvent e) {71testToplevel.setVisible(true);72}73});74nofocusFrame.add(showButton);75nofocusFrame.pack();76nofocusFrame.setFocusableWindowState(false);77nofocusFrame.setVisible(true);78Util.waitForIdle(robot);7980robot.delay(3000);8182// 1. Show the toplvel without clicking into the non-focusable frame.83test(frame, 1);84test(dialog1, 1);85test(dialog2, 1);8687// 2. Showing the toplvel via clicking into the non-focusable frame.88test(frame, 2);89test(dialog1, 2);90test(dialog2, 2);9192System.out.println("Test passed.");93}9495private void test(Window toplevel, int stage) {96toplevel.add(testButton);97toplevel.pack();98toplevel.setLocation(200, 0);99100switch (stage) {101case 1:102toplevel.setVisible(true);103break;104case 2:105testToplevel = toplevel;106Util.clickOnComp(showButton, robot);107break;108}109Util.waitForIdle(robot);110111if (!Util.trackActionPerformed(testButton, action, 2000, false)) {112throw new TestFailedException("Stage " + stage + ". The toplevel " + toplevel + " wasn't made foreground on showing");113}114System.out.println("Stage " + stage + ". Toplevel " + toplevel + " - passed");115toplevel.dispose();116}117}118119class TestFailedException extends RuntimeException {120TestFailedException(String msg) {121super("Test failed: " + msg);122}123}124125/****************************************************126* Standard Test Machinery127* DO NOT modify anything below -- it's a standard128* chunk of code whose purpose is to make user129* interaction uniform, and thereby make it simpler130* to read and understand someone else's test.131****************************************************/132133/**134* This is part of the standard test machinery.135* It creates a dialog (with the instructions), and is the interface136* for sending text messages to the user.137* To print the instructions, send an array of strings to Sysout.createDialog138* WithInstructions method. Put one line of instructions per array entry.139* To display a message for the tester to see, simply call Sysout.println140* with the string to be displayed.141* This mimics System.out.println but works within the test harness as well142* as standalone.143*/144145class Sysout {146static TestDialog dialog;147148public static void createDialogWithInstructions( String[] instructions ) {149dialog = new TestDialog( new Frame(), "Instructions" );150dialog.printInstructions( instructions );151dialog.setVisible(true);152println( "Any messages for the tester will display here." );153}154155public static void createDialog( ) {156dialog = new TestDialog( new Frame(), "Instructions" );157String[] defInstr = { "Instructions will appear here. ", "" } ;158dialog.printInstructions( defInstr );159dialog.setVisible(true);160println( "Any messages for the tester will display here." );161}162163164public static void printInstructions( String[] instructions ) {165dialog.printInstructions( instructions );166}167168169public static void println( String messageIn ) {170dialog.displayMessage( messageIn );171}172173}// Sysout class174175/**176* This is part of the standard test machinery. It provides a place for the177* test instructions to be displayed, and a place for interactive messages178* to the user to be displayed.179* To have the test instructions displayed, see Sysout.180* To have a message to the user be displayed, see Sysout.181* Do not call anything in this dialog directly.182*/183class TestDialog extends Dialog {184185TextArea instructionsText;186TextArea messageText;187int maxStringLength = 80;188189//DO NOT call this directly, go through Sysout190public TestDialog( Frame frame, String name ) {191super( frame, name );192int scrollBoth = TextArea.SCROLLBARS_BOTH;193instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );194add( "North", instructionsText );195196messageText = new TextArea( "", 5, maxStringLength, scrollBoth );197add("Center", messageText);198199pack();200201setVisible(true);202}// TestDialog()203204//DO NOT call this directly, go through Sysout205public void printInstructions( String[] instructions ) {206//Clear out any current instructions207instructionsText.setText( "" );208209//Go down array of instruction strings210211String printStr, remainingStr;212for( int i=0; i < instructions.length; i++ ) {213//chop up each into pieces maxSringLength long214remainingStr = instructions[ i ];215while( remainingStr.length() > 0 ) {216//if longer than max then chop off first max chars to print217if( remainingStr.length() >= maxStringLength ) {218//Try to chop on a word boundary219int posOfSpace = remainingStr.220lastIndexOf( ' ', maxStringLength - 1 );221222if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;223224printStr = remainingStr.substring( 0, posOfSpace + 1 );225remainingStr = remainingStr.substring( posOfSpace + 1 );226}227//else just print228else {229printStr = remainingStr;230remainingStr = "";231}232233instructionsText.append( printStr + "\n" );234235}// while236237}// for238239}//printInstructions()240241//DO NOT call this directly, go through Sysout242public void displayMessage( String messageIn ) {243messageText.append( messageIn + "\n" );244System.out.println(messageIn);245}246247}// TestDialog class248249250