Path: blob/master/test/jdk/java/awt/Focus/ModalDialogInitialFocusTest/ModalDialogInitialFocusTest.java
51659 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*/2223/*24@test25@key headful26@bug 638275027@summary Tests that modal dialog doesn't request extra initial focus on show.28@run main ModalDialogInitialFocusTest29*/3031import java.awt.*;32import java.awt.event.*;3334public class ModalDialogInitialFocusTest {35Robot robot;3637Dialog dialog = new Dialog((Window)null, "Test Dialog", Dialog.ModalityType.TOOLKIT_MODAL);38Button button = new Button("button");3940volatile static boolean passed = true;4142public static void main(String[] args) {43ModalDialogInitialFocusTest app = new ModalDialogInitialFocusTest();44app.init();45app.start();46}4748public void init() {49try {50robot = new Robot();51} catch (AWTException e) {52throw new RuntimeException("Error: unable to create robot", e);53}54}5556public void start() {5758dialog.setLayout(new FlowLayout());59dialog.add(button);60dialog.setBounds(800, 0, 100, 100);6162dialog.addFocusListener(new FocusAdapter() {63// The only expected FOCUS_GAINED is on the button.64public void focusGained(FocusEvent e) {65passed = false;66}67});6869test();70}7172void test() {73new Thread(new Runnable() {74public void run() {75dialog.setVisible(true);76}77}).start();7879waitTillShown(dialog);8081robot.waitForIdle();8283dialog.dispose();8485if (passed) {86System.out.println("Test passed.");87} else {88throw new RuntimeException("Test failed: dialog requests extra focus on show!");89}90}9192void waitTillShown(Component c) {93while (true) {94try {95Thread.sleep(100);96c.getLocationOnScreen();97break;98} catch (InterruptedException ie) {99ie.printStackTrace();100break;101} catch (IllegalComponentStateException e) {102}103}104}105}106107108