Path: blob/master/test/jdk/java/awt/Focus/NonFocusableBlockedOwnerTest/NonFocusableBlockedOwnerTest.java
41152 views
/*1* Copyright (c) 2005, 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 627232427@summary Modal excluded Window which decorated parent is blocked should be non-focusable.28@modules java.desktop/sun.awt29@run main NonFocusableBlockedOwnerTest30*/3132import java.awt.*;33import java.awt.event.*;34import java.lang.reflect.*;3536public class NonFocusableBlockedOwnerTest {37Robot robot;38Frame frame = new Frame("Modal Blocked Frame");39Dialog dialog = new Dialog(frame, "Modal Dialog", true);40Window excluded = new Window(frame);41Button button = new Button("button");4243public static void main(String[] args) {44NonFocusableBlockedOwnerTest app = new NonFocusableBlockedOwnerTest();45app.init();46app.start();47}4849public void init() {50try {51robot = new Robot();52} catch (AWTException e) {53throw new RuntimeException("Error: unable to create robot", e);54}55}5657public void start() {5859if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {60System.out.println("No testing on MToolkit.");61return;62}6364try {65EventQueue.invokeLater(new Runnable() {66public void run() {67frame.setSize(300, 200);68frame.setVisible(true);6970excluded.setSize(300, 200);71excluded.setLocation(0, 400);72excluded.setModalExclusionType(Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);73excluded.setLayout(new FlowLayout());74excluded.add(button);75excluded.setVisible(true);7677dialog.setSize(200, 100);78dialog.setLocation(0, 250);79dialog.setVisible(true);80}81});82} catch (Exception e) {83e.printStackTrace();84}8586waitTillShown(dialog);87clickOn(button);88if (frame == KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow()) {89throw new RuntimeException("Test failed!");90}91if (excluded == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow()) {92throw new RuntimeException("Test failed!");93}94if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {95throw new RuntimeException("Test failed!");96}97System.out.println("Test passed.");98}99100void clickOn(Component c) {101Point p = c.getLocationOnScreen();102Dimension d = c.getSize();103104System.out.println("Clicking " + c);105106if (c instanceof Frame) {107robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);108} else {109robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));110}111robot.mousePress(InputEvent.BUTTON1_MASK);112robot.mouseRelease(InputEvent.BUTTON1_MASK);113waitForIdle();114}115116void waitTillShown(Component c) {117while (true) {118try {119Thread.sleep(100);120c.getLocationOnScreen();121break;122} catch (InterruptedException e) {123throw new RuntimeException(e);124} catch (IllegalComponentStateException e) {}125}126}127void waitForIdle() {128try {129robot.waitForIdle();130EventQueue.invokeAndWait( new Runnable() {131public void run() {} // Dummy implementation132});133} catch(InterruptedException ie) {134System.out.println("waitForIdle, non-fatal exception caught:");135ie.printStackTrace();136} catch(InvocationTargetException ite) {137System.out.println("waitForIdle, non-fatal exception caught:");138ite.printStackTrace();139}140141// wait longer...142robot.delay(200);143}144}145146147