Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/MouseClickRequestFocusRaceTest/MouseClickRequestFocusRaceTest.java
47525 views
/*1* Copyright (c) 2006, 2014, 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/*24test25@bug 502801426@summary Focus request & mouse click performed nearly synchronously shouldn't lead to a focus race.27@author [email protected]: area=awt-focus28@run applet MouseClickRequestFocusRaceTest.html29*/3031import java.awt.*;32import javax.swing.*;33import java.awt.event.*;34import java.applet.Applet;3536public class MouseClickRequestFocusRaceTest extends Applet {37Robot robot;38JFrame frame1 = new JFrame("Frame-1") {39public String toString() { return "Frame-1";}40};41JFrame frame2 = new JFrame("Frame-2") {42public String toString() { return "Frame-2";}43};44JButton button1 = new JButton("button-1") {45public String toString() { return "button-1";}46};47JButton button2 = new JButton("button-2") {48public String toString() { return "button-2";}49};50JPopupMenu popup = new JPopupMenu();5152public static void main(String[] args) {53MouseClickRequestFocusRaceTest app = new MouseClickRequestFocusRaceTest();54app.init();55app.start();56}5758public void init() {59try {60robot = new Robot();61} catch (AWTException e) {62throw new RuntimeException("Error: unable to create robot", e);63}64// Create instructions for the user here, as well as set up65// the environment -- set the layout manager, add buttons,66// etc.67this.setLayout (new BorderLayout ());68Sysout.createDialogWithInstructions(new String[]69{"Automatic test. Simply wait until it is done."70});71}7273public void start() {74frame1.add(button1);75frame2.add(button2);76frame1.setBounds(0, 0, 200, 300);77frame2.setBounds(300, 0, 200, 300);78frame1.setLayout(new FlowLayout());79frame2.setLayout(new FlowLayout());8081popup.add(new JMenuItem("black"));82popup.add(new JMenuItem("yellow"));83popup.add(new JMenuItem("white"));8485frame1.add(popup);8687frame1.addMouseListener(new MouseAdapter() {88void popup(MouseEvent e) {89if (e.isPopupTrigger()) {90Point loc = button1.getLocation();91popup.show(button1, e.getX() - loc.x, e.getY() - loc.y);92}93}94public void mousePressed(MouseEvent e) {95popup(e);96}97public void mouseReleased(MouseEvent e) {98popup(e);99}100});101102frame2.addMouseListener(new MouseAdapter() {103public void mousePressed(MouseEvent e) {104button1.requestFocusInWindow();105}106});107108frame2.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);109110frame1.setVisible(true);111frame2.setVisible(true);112// ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();113robot.delay(1000);114115test();116}117118public void test() {119// Right click Frame-1120robot.mouseMove(frame1.getLocation().x + 100, frame1.getLocation().y + 200);121robot.mousePress(InputEvent.BUTTON3_MASK);122robot.delay(100);123robot.mouseRelease(InputEvent.BUTTON3_MASK);124125// ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();126robot.delay(1000);127128// Left click Frame-2129robot.mouseMove(frame2.getLocation().x + 100, frame1.getLocation().y + 200);130robot.mousePress(InputEvent.BUTTON1_MASK);131robot.delay(100);132robot.mouseRelease(InputEvent.BUTTON1_MASK);133134// ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();135robot.delay(1000);136137JComponent focusOwner = (JComponent)KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();138JFrame focusedWindow = (JFrame)KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();139140Sysout.println("focus owner: " + focusOwner);141Sysout.println("focused window: " + focusedWindow);142143// Verify that the focused window is the ancestor of the focus owner144if (!focusedWindow.isAncestorOf(focusOwner)) {145throw new TestFailedException("The focus owner is not in the focused window!");146}147148// Try to close native focused window149robot.keyPress(KeyEvent.VK_ALT);150robot.keyPress(KeyEvent.VK_F4);151robot.keyRelease(KeyEvent.VK_F4);152robot.keyRelease(KeyEvent.VK_ALT);153154// ((SunToolkit)Toolkit.getDefaultToolkit()).realSync();155robot.delay(1000);156157// Verify that the Java focused window really mapped the native focused window.158if (focusedWindow.isVisible()) {159throw new TestFailedException("The focused window is different on Java and on the native level.");160}161}162163class TestFailedException extends RuntimeException {164public TestFailedException(String cause) {165super("Test failed.");166Sysout.println(cause);167}168}169}170171/****************************************************172Standard Test Machinery173DO NOT modify anything below -- it's a standard174chunk of code whose purpose is to make user175interaction uniform, and thereby make it simpler176to read and understand someone else's test.177****************************************************/178179/**180This is part of the standard test machinery.181It creates a dialog (with the instructions), and is the interface182for sending text messages to the user.183To print the instructions, send an array of strings to Sysout.createDialog184WithInstructions method. Put one line of instructions per array entry.185To display a message for the tester to see, simply call Sysout.println186with the string to be displayed.187This mimics System.out.println but works within the test harness as well188as standalone.189*/190191class Sysout192{193static TestDialog dialog;194195public static void createDialogWithInstructions( String[] instructions )196{197dialog = new TestDialog( new Frame(), "Instructions" );198dialog.printInstructions( instructions );199// dialog.setVisible(true);200println( "Any messages for the tester will display here." );201}202203public static void createDialog( )204{205dialog = new TestDialog( new Frame(), "Instructions" );206String[] defInstr = { "Instructions will appear here. ", "" } ;207dialog.printInstructions( defInstr );208// dialog.setVisible(true);209println( "Any messages for the tester will display here." );210}211212213public static void printInstructions( String[] instructions )214{215dialog.printInstructions( instructions );216}217218219public static void println( String messageIn )220{221dialog.displayMessage( messageIn );222}223224}// Sysout class225226/**227This is part of the standard test machinery. It provides a place for the228test instructions to be displayed, and a place for interactive messages229to the user to be displayed.230To have the test instructions displayed, see Sysout.231To have a message to the user be displayed, see Sysout.232Do not call anything in this dialog directly.233*/234class TestDialog extends Dialog235{236237TextArea instructionsText;238TextArea messageText;239int maxStringLength = 80;240241//DO NOT call this directly, go through Sysout242public TestDialog( Frame frame, String name )243{244super( frame, name );245int scrollBoth = TextArea.SCROLLBARS_BOTH;246instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );247add( "North", instructionsText );248249messageText = new TextArea( "", 5, maxStringLength, scrollBoth );250add("Center", messageText);251252pack();253254// setVisible(true);255}// TestDialog()256257//DO NOT call this directly, go through Sysout258public void printInstructions( String[] instructions )259{260//Clear out any current instructions261instructionsText.setText( "" );262263//Go down array of instruction strings264265String printStr, remainingStr;266for( int i=0; i < instructions.length; i++ )267{268//chop up each into pieces maxSringLength long269remainingStr = instructions[ i ];270while( remainingStr.length() > 0 )271{272//if longer than max then chop off first max chars to print273if( remainingStr.length() >= maxStringLength )274{275//Try to chop on a word boundary276int posOfSpace = remainingStr.277lastIndexOf( ' ', maxStringLength - 1 );278279if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;280281printStr = remainingStr.substring( 0, posOfSpace + 1 );282remainingStr = remainingStr.substring( posOfSpace + 1 );283}284//else just print285else286{287printStr = remainingStr;288remainingStr = "";289}290291instructionsText.append( printStr + "\n" );292293}// while294295}// for296297}//printInstructions()298299//DO NOT call this directly, go through Sysout300public void displayMessage( String messageIn )301{302messageText.append( messageIn + "\n" );303System.out.println(messageIn);304}305306}// TestDialog class307308309