1 /* 2 * Copyright (c) 2007, 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 6562853 7035459 27 @summary Tests that focus transfered directy to window w/o transfering it to frame. 28 @author Oleg Sukhodolsky: area=awt.focus 29 @library ../../regtesthelpers 30 @build Util 31 @run main TranserFocusToWindow 32 */ 33 34 import java.awt.Button; 35 import java.awt.Component; 36 import java.awt.Frame; 37 import java.awt.Robot; 38 import java.awt.Window; 39 40 import java.awt.event.WindowEvent; 41 import java.awt.event.WindowFocusListener; 42 43 import test.java.awt.regtesthelpers.Util; 44 45 public class TranserFocusToWindow 46 { 47 private static final int WIDTH = 300; 48 private static final int HEIGHT = 200; 49 50 public static void main(String[] args) { 51 Robot robot = Util.createRobot(); 52 Frame owner_frame = new Frame("Owner frame"); 53 owner_frame.setBounds(0, 0, WIDTH, HEIGHT); 54 owner_frame.setVisible(true); 55 Util.waitForIdle(robot); 56 57 Window window = new Window(owner_frame); 58 Button btn1 = new Button("button for focus"); 59 window.add(btn1); 60 window.pack(); 61 window.setLocation(0, HEIGHT + 100); 62 window.setVisible(true); 63 Util.waitForIdle(robot); 64 65 Frame another_frame = new Frame("Another frame"); 66 Button btn2 = new Button("button in a frame"); 67 another_frame.add(btn2); 68 another_frame.pack(); 69 another_frame.setLocation(WIDTH + 100, 0); 70 another_frame.setVisible(true); 71 Util.waitForIdle(robot); 72 73 owner_frame.addWindowFocusListener(new WindowFocusListener() { 74 public void windowLostFocus(WindowEvent we) { 75 System.out.println(we); 76 } 77 public void windowGainedFocus(WindowEvent we) { 78 System.out.println(we); 79 throw new RuntimeException("owner frame must not receive WINDWO_GAINED_FOCUS"); 80 } 81 }); 82 window.addWindowFocusListener(new WindowFocusListener() { 83 public void windowLostFocus(WindowEvent we) { 84 System.out.println(we); 85 } 86 public void windowGainedFocus(WindowEvent we) { 87 System.out.println(we); 88 } 89 }); 90 another_frame.addWindowFocusListener(new WindowFocusListener() { 91 public void windowLostFocus(WindowEvent we) { 92 System.out.println(we); 93 } 94 public void windowGainedFocus(WindowEvent we) { 95 System.out.println(we); 96 } 97 }); 98 99 Util.clickOnTitle(owner_frame, robot); 100 Util.waitForIdle(robot); 101 setFocus(btn1, robot); 102 setFocus(btn2, robot); 103 // we need this delay so WM can not treat two clicks on title as double click 104 robot.delay(500); 105 Util.clickOnTitle(owner_frame, robot); 106 Util.waitForIdle(robot); 107 108 System.out.println("test passed"); 109 } 110 111 private static void setFocus(final Component comp, final Robot r) { 112 if (comp.hasFocus()) { 113 return; 114 } 115 116 Util.clickOnComp(comp, r); 117 Util.waitForIdle(r); 118 119 if (!comp.hasFocus()) { 120 throw new RuntimeException("can not set focus on " + comp); 121 } 122 } 123 }