Path: blob/master/test/jdk/java/awt/Focus/ModalBlockedStealsFocusTest/ModalBlockedStealsFocusTest.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*/2223/*24@test25@key headful26@bug 642613227@summary Modal blocked window shouldn't steal focus when shown, or brought to front.28@library ../../regtesthelpers29@build Util30@run main ModalBlockedStealsFocusTest31*/3233import java.awt.*;34import java.awt.event.*;35import java.util.concurrent.atomic.AtomicBoolean;36import test.java.awt.regtesthelpers.Util;3738public class ModalBlockedStealsFocusTest {39Frame frame = new Frame("Blocked Frame");40Dialog dialog = new Dialog(frame, "Modal Dialog", Dialog.ModalityType.TOOLKIT_MODAL);41AtomicBoolean lostFocus = new AtomicBoolean(false);4243public static void main(String[] args) {44ModalBlockedStealsFocusTest app = new ModalBlockedStealsFocusTest();45app.start();46}4748public void start() {49if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {50System.out.println("The test is not for MToolkit.");51return;52}5354dialog.setBounds(800, 0, 200, 100);55frame.setBounds(800, 150, 200, 100);5657dialog.addWindowFocusListener(new WindowAdapter() {58public void windowLostFocus(WindowEvent e) {59System.out.println(e.toString());60synchronized (lostFocus) {61lostFocus.set(true);62lostFocus.notifyAll();63}64}65});6667new Thread(new Runnable() {68public void run() {69dialog.setVisible(true);70}71}).start();7273Util.waitTillShown(dialog);74try {75Robot robot = new Robot();76robot.waitForIdle();77}catch(Exception ex) {78ex.printStackTrace();79throw new RuntimeException("Unexpected failure");80}8182// Test 1. Show a modal blocked frame, check that it doesn't steal focus.8384frame.setVisible(true);8586if (Util.waitForCondition(lostFocus, 2000L)) {87throw new TestFailedException("the modal blocked frame stole focus on its showing!");88}8990// Test 2. Brought a modal blocked frame to front, check that it doesn't steal focus.9192frame.toFront();9394if (Util.waitForCondition(lostFocus, 2000L)) {95throw new TestFailedException("the modal blocked frame stole focus on its bringing to front!");96} else {97System.out.println("Test passed");98}99}100}101102class TestFailedException extends RuntimeException {103TestFailedException(String msg) {104super("Test failed: " + msg);105}106}107108109