1 /*
   2  * Copyright (c) 2004, 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        5090325
  28   @summary    Tests that Window's child can be focused on XAWT.
  29   @run        main ChildWindowFocusTest
  30 */
  31 
  32 import java.awt.*;
  33 import java.awt.event.*;
  34 
  35 public class ChildWindowFocusTest {
  36     Robot robot;
  37     Frame frame = new Frame("Owner");
  38     Button button0 = new Button("button-0");
  39     TextField text0 = new TextField("text-0");
  40     TextField text1 = new TextField("text-1");
  41     Window win1 = new TestWindow(frame, text0, 110);
  42     Window win2 = new TestWindow(win1, text1, 220);
  43     Frame outerFrame = new Frame("Outer");
  44     Button button1 = new Button("button-1");
  45     int shift;
  46 
  47     public static void main(final String[] args) {
  48         ChildWindowFocusTest app = new ChildWindowFocusTest();
  49         app.init();
  50         app.start();
  51     }
  52 
  53     public void init() {
  54         try {
  55             robot = new Robot();
  56         } catch (AWTException e) {
  57             throw new RuntimeException("Error: unable to create robot", e);
  58         }
  59         shift = 100;
  60     }
  61 
  62     public void start() {
  63 
  64         frame.setBounds(0, 50, 400, 100);
  65         frame.setLayout(new FlowLayout());
  66         frame.add(button0);
  67 
  68         outerFrame.setBounds(0, 390, 400, 100);
  69         outerFrame.setLayout(new FlowLayout());
  70         outerFrame.add(button1);
  71 
  72         adjustAndShow(new Component[] {frame, win1, win2, outerFrame});
  73         robot.waitForIdle();
  74 
  75         test();
  76     }
  77 
  78     void adjustAndShow(Component[] comps) {
  79         for (Component comp: comps) {
  80             comp.setLocation(shift, (int)comp.getLocation().getY());
  81             comp.setVisible(true);
  82             robot.waitForIdle();
  83         }
  84     }
  85 
  86     void test() {
  87         clickOnCheckFocusOwner(button0);
  88         clickOnCheckFocusOwner(text1);
  89         clickOnCheckFocusOwner(button1);
  90         clickOn(frame);
  91         checkFocusOwner(text1);
  92         clickOnCheckFocusOwner(text0);
  93         clickOnCheckFocusOwner(button1);
  94         clickOn(frame);
  95         checkFocusOwner(text0);
  96 
  97         System.out.println("Test passed.");
  98     }
  99 
 100     void clickOnCheckFocusOwner(Component c) {
 101         clickOn(c);
 102         if (!checkFocusOwner(c)) {
 103             throw new RuntimeException("Test failed: couldn't focus <" + c + "> by mouse click!");
 104         }
 105     }
 106 
 107     boolean checkFocusOwner(Component comp) {
 108         return (comp == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
 109     }
 110 
 111     void clickOn(Component c) {
 112         Point p = c.getLocationOnScreen();
 113         Dimension d = c.getSize();
 114 
 115         System.out.println("Clicking " + c);
 116 
 117         if (c instanceof Frame) {
 118             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);
 119         } else {
 120             robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));
 121         }
 122         robot.delay(50);
 123         robot.mousePress(InputEvent.BUTTON1_MASK);
 124         robot.delay(50);
 125         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 126         robot.waitForIdle();
 127     }
 128 
 129 }
 130 
 131 class TestWindow extends Window {
 132     TestWindow(Window owner, Component comp, int x) {
 133         super(owner);
 134         setBackground(Color.blue);
 135         setLayout(new FlowLayout());
 136         add(comp);
 137         comp.setBackground(Color.yellow);
 138         setBounds(0, x, 100, 100);
 139     }
 140 }