Path: blob/master/test/jdk/java/awt/Focus/MouseClickRequestFocusRaceTest/MouseClickRequestFocusRaceTest.java
41152 views
/*1* Copyright (c) 2006, 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*/2223import java.awt.AWTException;24import java.awt.FlowLayout;25import java.awt.KeyboardFocusManager;26import java.awt.Point;27import java.awt.Robot;28import java.awt.event.InputEvent;29import java.awt.event.KeyEvent;30import java.awt.event.MouseAdapter;31import java.awt.event.MouseEvent;3233import javax.swing.JButton;34import javax.swing.JComponent;35import javax.swing.JFrame;36import javax.swing.JMenuItem;37import javax.swing.JPopupMenu;38import javax.swing.WindowConstants;3940import jdk.test.lib.Platform;4142/**43* @test44* @key headful45* @bug 502801446* @summary Focus request & mouse click being performed nearly synchronously47* shouldn't break the focus subsystem48* @author [email protected]: area=awt-focus49* @library /test/lib50* @build jdk.test.lib.Platform51* @run main MouseClickRequestFocusRaceTest52*/53public class MouseClickRequestFocusRaceTest {54static Robot robot;55static JFrame frame1 = new JFrame("Frame-1") {56public String toString() { return "Frame-1";}57};58static JFrame frame2 = new JFrame("Frame-2") {59public String toString() { return "Frame-2";}60};61static JButton button1 = new JButton("button-1") {62public String toString() { return "button-1";}63};64static JButton button2 = new JButton("button-2") {65public String toString() { return "button-2";}66};67static JPopupMenu popup = new JPopupMenu();6869public static void main(String[] args) {70try {71robot = new Robot();72robot.setAutoWaitForIdle(true);73robot.setAutoDelay(100);74} catch (AWTException e) {75throw new RuntimeException("Error: unable to create robot", e);76}77frame1.add(button1);78frame2.add(button2);79frame1.setBounds(0, 0, 200, 300);80frame2.setBounds(300, 0, 200, 300);81frame1.setLayout(new FlowLayout());82frame2.setLayout(new FlowLayout());8384popup.add(new JMenuItem("black"));85popup.add(new JMenuItem("yellow"));86popup.add(new JMenuItem("white"));8788frame1.add(popup);8990frame1.addMouseListener(new MouseAdapter() {91void popup(MouseEvent e) {92if (e.isPopupTrigger()) {93Point loc = button1.getLocation();94popup.show(button1, e.getX() - loc.x, e.getY() - loc.y);95}96}97public void mousePressed(MouseEvent e) {98popup(e);99}100public void mouseReleased(MouseEvent e) {101popup(e);102}103});104105frame2.addMouseListener(new MouseAdapter() {106public void mousePressed(MouseEvent e) {107button1.requestFocusInWindow();108}109});110111frame2.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);112113frame1.setVisible(true);114frame2.setVisible(true);115116robot.delay(1000);117try {118test();119} finally {120frame1.dispose();121frame2.dispose();122}123}124125public static void test() {126// Right click Frame-1127robot.mouseMove(frame1.getLocation().x + 100, frame1.getLocation().y + 200);128robot.mousePress(InputEvent.BUTTON3_MASK);129robot.mouseRelease(InputEvent.BUTTON3_MASK);130131robot.delay(1000);132133// Left click Frame-2134robot.mouseMove(frame2.getLocation().x + 100, frame1.getLocation().y + 200);135robot.mousePress(InputEvent.BUTTON1_MASK);136robot.mouseRelease(InputEvent.BUTTON1_MASK);137138robot.delay(1000);139140JComponent focusOwner = (JComponent)KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();141JFrame focusedWindow = (JFrame)KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();142143System.out.println("focus owner: " + focusOwner);144System.out.println("focused window: " + focusedWindow);145146// Verify that the focused window is the ancestor of the focus owner147if (!focusedWindow.isAncestorOf(focusOwner)) {148throw new RuntimeException("The focus owner is not in the focused window!");149}150151if (!Platform.isOSX()) {152// Try to close native focused window153robot.keyPress(KeyEvent.VK_ALT);154robot.keyPress(KeyEvent.VK_F4);155robot.keyRelease(KeyEvent.VK_F4);156robot.keyRelease(KeyEvent.VK_ALT);157robot.delay(1000);158// Verify that the Java focused window really mapped the native focused window.159if (focusedWindow.isVisible()) {160throw new RuntimeException("The focused window is different on Java and on the native level.");161}162} else {163// Try to move native focus to previous window164robot.keyPress(KeyEvent.VK_CONTROL);165robot.keyPress(KeyEvent.VK_F4);166robot.keyRelease(KeyEvent.VK_F4);167robot.keyRelease(KeyEvent.VK_CONTROL);168robot.delay(1000);169// Verify that the Java focused window really mapped the native focused window.170if (focusedWindow.isFocused()) {171throw new RuntimeException("The focused window is different on Java and on the native level.");172}173}174}175}176177178