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
  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     public static void main(String[] args) {
  48         Robot robot = Util.createRobot();
  49         Frame owner_frame = new Frame("Owner frame");
  50         owner_frame.setBounds(0, 0, 200, 200);
  51         owner_frame.setVisible(true);
  52         Util.waitForIdle(robot);
  53 
  54         Window window = new Window(owner_frame);
  55         Button btn1 = new Button("button for focus");
  56         window.add(btn1);
  57         window.pack();
  58         window.setLocation(0, 300);
  59         window.setVisible(true);
  60         Util.waitForIdle(robot);
  61 
  62         Frame another_frame = new Frame("Another frame");
  63         Button btn2 = new Button("button in a frame");
  64         another_frame.add(btn2);
  65         another_frame.pack();
  66         another_frame.setLocation(300, 0);
  67         another_frame.setVisible(true);
  68         Util.waitForIdle(robot);
  69 
  70         Util.clickOnTitle(owner_frame, robot);
  71         Util.waitForIdle(robot);
  72 
  73         setFocus(btn1, robot);
  74 
  75         setFocus(btn2, robot);
  76 
  77         owner_frame.addWindowFocusListener(new WindowFocusListener() {
  78                 public void windowLostFocus(WindowEvent we) {
  79                     System.out.println(we);
  80                 }
  81                 public void windowGainedFocus(WindowEvent we) {
  82                     System.out.println(we);
  83                     throw new RuntimeException("owner frame must not receive WINDWO_GAINED_FOCUS");
  84                 }
  85             });
  86         window.addWindowFocusListener(new WindowFocusListener() {
  87                 public void windowLostFocus(WindowEvent we) {
  88                     System.out.println(we);
  89                 }
  90                 public void windowGainedFocus(WindowEvent we) {
  91                     System.out.println(we);
  92                 }
  93             });
  94         another_frame.addWindowFocusListener(new WindowFocusListener() {
  95                 public void windowLostFocus(WindowEvent we) {
  96                     System.out.println(we);
  97                 }
  98                 public void windowGainedFocus(WindowEvent we) {
  99                     System.out.println(we);
 100                 }
 101             });
 102 




 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 }
--- EOF ---