1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 import javax.swing.*; 27 import java.awt.*; 28 import java.awt.event.*; 29 30 /* 31 @test 32 @bug 8160570 33 @summary Tests that a modal dialog receives WINDOW_ACTIVATED & WINDOW_GAINED_FOCUS on first show. 34 */ 35 public class ModalDialogActivationTest { 36 static final Object lock = new Object(); 37 static volatile boolean activated; 38 static volatile boolean focused; 39 40 public static void main(String[] args) throws InterruptedException { 41 EventQueue.invokeLater(() -> runGUI()); 42 43 long time = System.currentTimeMillis(); 44 synchronized (lock) { 45 while (!activated || !focused) { 46 lock.wait(5000); 47 if (System.currentTimeMillis() - time >= 5000) break; 48 } 49 } 50 if (!activated || !focused) { 51 throw new RuntimeException("Test FAILED: activated: " + activated + ", focused: " + focused); 52 } 53 System.out.println("Test PASSED"); 54 } 55 56 static void runGUI() { 57 JFrame f = new JFrame("frame"); 58 JDialog d = new MyModalDialog(f, "dialog"); 59 d.addWindowListener(new WindowAdapter() { 60 @Override 61 public void windowActivated(WindowEvent e) { 62 synchronized (lock) { 63 activated = true; 64 lock.notifyAll(); 65 } 66 } 67 }); 68 d.addWindowFocusListener(new WindowAdapter() { 69 @Override 70 public void windowGainedFocus(WindowEvent e) { 71 synchronized (lock) { 72 focused = true; 73 lock.notifyAll(); 74 } 75 } 76 }); 77 f.setVisible(true); 78 d.setVisible(true); 79 } 80 81 static class MyModalDialog extends JDialog { 82 public MyModalDialog(Frame owner, String title) { 83 super(owner, title, true); 84 } 85 86 @Override 87 public boolean getFocusableWindowState() { 88 try { 89 // let Toolkit thread go ahead 90 Thread.sleep(100); 91 } catch (InterruptedException ignore) { 92 } 93 return super.getFocusableWindowState(); 94 } 95 } 96 }