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