1 /* 2 * Copyright (c) 2006, 2018, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 test 26 @bug 6426132 27 @summary A Window should be initially focused on its showing (XAWT bug). 28 @author anton.tarasov@...: area=awt.focus 29 @run applet WindowInitialFocusTest.html 30 */ 31 32 import java.awt.*; 33 import java.awt.event.*; 34 import java.applet.Applet; 35 import java.util.concurrent.atomic.AtomicBoolean; 36 import test.java.awt.regtesthelpers.Util; 37 38 public class WindowInitialFocusTest extends Applet { 39 Frame frame = new Frame("Test Frame"); 40 Window window = new Window(frame); 41 Button button = new Button("button"); 42 AtomicBoolean focused = new AtomicBoolean(false); 43 Robot robot; 44 45 public static void main(String[] args) { 46 WindowInitialFocusTest app = new WindowInitialFocusTest(); 47 app.init(); 48 app.start(); 49 } 50 51 public void init() { 52 // Create instructions for the user here, as well as set up 53 // the environment -- set the layout manager, add buttons, 54 // etc. 55 this.setLayout (new BorderLayout ()); 56 } 57 58 public void start() { 59 frame.setBounds(800, 0, 200, 100); 60 window.setBounds(800, 200, 200, 100); 61 window.setLayout(new FlowLayout()); 62 window.add(button); 63 64 window.addWindowFocusListener(new WindowAdapter() { 65 public void windowGainedFocus(WindowEvent e) { 66 System.out.println(e.toString()); 67 synchronized (focused) { 68 focused.set(true); 69 focused.notifyAll(); 70 } 71 }}); 72 73 frame.setVisible(true); 74 try { 75 robot = new Robot(); 76 }catch(Exception ex) { 77 ex.printStackTrace(); 78 throw new RuntimeException("Unexpected failure"); 79 } 80 robot.waitForIdle(); 81 82 // Test 1. Show the window, check that it become focused. 83 84 window.setVisible(true); 85 robot.waitForIdle(); 86 87 if (!Util.waitForCondition(focused, 2000L)) { 88 throw new TestFailedException("the window didn't get focused on its showing!"); 89 } 90 91 // Test 2. Show unfocusable window, check that it doesn't become focused. 92 93 window.setVisible(false); 94 robot.waitForIdle(); 95 96 window.setFocusableWindowState(false); 97 focused.set(false); 98 99 window.setVisible(true); 100 robot.waitForIdle(); 101 102 if (Util.waitForCondition(focused, 2000L)) { 103 throw new TestFailedException("the unfocusable window got focused on its showing!"); 104 } else { 105 System.out.println("Test passed"); 106 } 107 } 108 } 109 110 class TestFailedException extends RuntimeException { 111 TestFailedException(String msg) { 112 super("Test failed: " + msg); 113 } 114 }