1 /* 2 * Copyright (c) 2002, 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 @key headful 27 @bug 4092033 4529626 28 @summary Tests that toFront makes window focused unless it is non-focusable 29 @library ../../regtesthelpers 30 @build Util 31 @run main ToFrontFocus 32 */ 33 34 import java.awt.*; 35 import java.awt.event.*; 36 import test.java.awt.regtesthelpers.Util; 37 38 public class ToFrontFocus { 39 Frame cover, focus_frame, nonfocus_frame; 40 Button focus_button, nonfocus_button; 41 volatile boolean focus_gained = false, nonfocus_gained = false; 42 43 public static void main(final String[] args) { 44 ToFrontFocus app = new ToFrontFocus(); 45 app.init(); 46 app.start(); 47 } 48 49 public void init() 50 { 51 cover = new Frame("Cover frame"); 52 cover.setBounds(100, 100, 200, 200); 53 focus_frame = new Frame("Focusable frame"); 54 focus_frame.setBounds(150, 100, 250, 150); 55 nonfocus_frame = new Frame("Non-focusable frame"); 56 nonfocus_frame.setFocusableWindowState(false); 57 nonfocus_frame.setBounds(150, 150, 250, 200); 58 focus_button = new Button("Button in focusable frame"); 59 focus_button.addFocusListener(new FocusAdapter() { 60 public void focusGained(FocusEvent e) { 61 focus_gained = true; 62 } 63 }); 64 nonfocus_button = new Button("Button in non-focusable frame"); 65 nonfocus_button.addFocusListener(new FocusAdapter() { 66 public void focusGained(FocusEvent e) { 67 nonfocus_gained = true; 68 } 69 }); 70 }//End init() 71 72 public void start () 73 { 74 Util.waitForIdle(null); 75 76 focus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() { 77 public Component getInitialComponent(Window w) { 78 return null; 79 } 80 }); 81 focus_frame.setVisible(true); 82 nonfocus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() { 83 public Component getInitialComponent(Window w) { 84 return null; 85 } 86 }); 87 nonfocus_frame.setVisible(true); 88 cover.setVisible(true); 89 90 Util.waitForIdle(null); 91 92 // So events are no generated at the creation add buttons here. 93 focus_frame.add(focus_button); 94 focus_frame.pack(); 95 focus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() { 96 public Component getInitialComponent(Window w) { 97 return focus_button; 98 } 99 }); 100 nonfocus_frame.add(nonfocus_button); 101 nonfocus_frame.pack(); 102 nonfocus_frame.setFocusTraversalPolicy(new DefaultFocusTraversalPolicy() { 103 public Component getInitialComponent(Window w) { 104 return nonfocus_button; 105 } 106 }); 107 108 System.err.println("------------ Starting test ------------"); 109 // Make frame focused - focus_gained will be genereated for button. 110 focus_frame.toFront(); 111 // focus_gained should not be generated 112 nonfocus_frame.toFront(); 113 114 // Wait for events. 115 Util.waitForIdle(null); 116 117 if (!focus_gained || nonfocus_gained) { 118 throw new RuntimeException("ToFront doesn't work as expected"); 119 } 120 }// start() 121 122 }// class ToFrontFocus