Path: blob/master/test/jdk/java/awt/Focus/ModalDialogActivationTest/ModalDialogActivationTest.java
41152 views
/*1* Copyright (c) 2016, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.awt.EventQueue;26import java.awt.Frame;27import java.awt.event.WindowAdapter;28import java.awt.event.WindowEvent;29import javax.swing.JDialog;30import javax.swing.JFrame;3132/*33@test34@key headful35@bug 8160570 816601536@summary Tests that a modal dialog receives WINDOW_ACTIVATED37& WINDOW_GAINED_FOCUS on first show.38*/3940public class ModalDialogActivationTest {41static final Object lock = new Object();42static volatile boolean activated;43static volatile boolean focused;4445public static void main(String[] args) throws InterruptedException {46EventQueue.invokeLater(() -> runGUI());4748long time = System.currentTimeMillis();49synchronized (lock) {50while (!activated || !focused) {51lock.wait(5000);52if (System.currentTimeMillis() - time >= 5000) break;53}54}55if (!activated || !focused) {56throw new RuntimeException("Test FAILED: activated: " + activated57+ ", focused: " + focused);58}59System.out.println("Test PASSED");60}6162static void runGUI() {63JFrame f = new JFrame("frame");64final JDialog d = new MyModalDialog(f, "dialog");65d.addWindowListener(new WindowAdapter() {66@Override67public void windowActivated(WindowEvent e) {68synchronized (lock) {69activated = true;70lock.notifyAll();71}72}73});74d.addWindowFocusListener(new WindowAdapter() {75@Override76public void windowGainedFocus(WindowEvent e) {77synchronized (lock) {78focused = true;79lock.notifyAll();80}81}82});83f.setVisible(true);84d.setVisible(true);85}8687static class MyModalDialog extends JDialog {88public MyModalDialog(Frame owner, String title) {89super(owner, title, true);90}9192@Override93public boolean getFocusableWindowState() {94try {95// let Toolkit thread go ahead96Thread.sleep(100);97} catch (InterruptedException ignore) {98}99return super.getFocusableWindowState();100}101}102}103104105