1 /*
   2  * Copyright (c) 2008, 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       6182359
  28   @summary   Tests that Window having non-focusable owner can't be a focus owner.
  29   @library   ../../regtesthelpers
  30   @build     Util
  31   @run       main NonfocusableOwnerTest
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import test.java.awt.regtesthelpers.Util;
  37 
  38 public class NonfocusableOwnerTest {
  39     Robot robot = Util.createRobot();
  40     Frame frame;
  41     Dialog dialog;
  42     Window window1;
  43     Window window2;
  44     Button button = new Button("button");
  45 
  46     public static void main(String[] args) {
  47         NonfocusableOwnerTest test = new NonfocusableOwnerTest();
  48         test.start();
  49     }
  50 
  51     public void start() {
  52         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  53                 public void eventDispatched(AWTEvent e) {
  54                     System.out.println(e.toString());
  55                 }
  56             }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK | WindowEvent.WINDOW_EVENT_MASK);
  57 
  58         frame = new Frame("Frame");
  59         frame.setName("Frame-owner");
  60         frame.setBounds(100, 0, 100, 100);
  61         dialog = new Dialog(frame, "Dialog");
  62         dialog.setName("Dialog-owner");
  63         dialog.setBounds(100, 0, 100, 100);
  64 
  65         window1 = new Window(frame);
  66         window1.setName("1st child");
  67         window1.setBounds(100, 300, 100, 100);
  68         window2 = new Window(window1);
  69         window2.setName("2nd child");
  70         window2.setBounds(100, 500, 100, 100);
  71 
  72         test1(frame, window1);
  73         test2(frame, window1, window2);
  74         test3(frame, window1, window2);
  75 
  76         window1 = new Window(dialog);
  77         window1.setBounds(100, 300, 100, 100);
  78         window1.setName("1st child");
  79         window2 = new Window(window1);
  80         window2.setName("2nd child");
  81         window2.setBounds(100, 500, 100, 100);
  82 
  83         test1(dialog, window1);
  84         test2(dialog, window1, window2);
  85         test3(dialog, window1, window2);
  86 
  87         System.out.println("Test passed.");
  88     }
  89 
  90     void test1(Window owner, Window child) {
  91         System.out.println("* * * STAGE 1 * * *\nWindow owner: " + owner);
  92 
  93         owner.setFocusableWindowState(false);
  94         owner.setVisible(true);
  95 
  96         child.add(button);
  97         child.setVisible(true);
  98 
  99         Util.waitTillShown(child);
 100 
 101         Util.clickOnComp(button, robot);
 102         if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
 103             throw new RuntimeException("Test Failed.");
 104         }
 105         child.dispose();
 106         owner.dispose();
 107     }
 108 
 109     void test2(Window owner, Window child1, Window child2) {
 110         System.out.println("* * * STAGE 2 * * *\nWindow nowner: " + owner);
 111 
 112         owner.setFocusableWindowState(false);
 113         owner.setVisible(true);
 114 
 115         child1.setFocusableWindowState(true);
 116         child1.setVisible(true);
 117 
 118         child2.add(button);
 119         child2.setVisible(true);
 120 
 121         Util.waitTillShown(child2);
 122 
 123         Util.clickOnComp(button, robot);
 124         if (button == KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
 125             throw new RuntimeException("Test failed.");
 126         }
 127         child2.dispose();
 128         child1.dispose();
 129         owner.dispose();
 130     }
 131 
 132     void test3(Window owner, Window child1, Window child2) {
 133         System.out.println("* * * STAGE 3 * * *\nWidow owner: " + owner);
 134 
 135         owner.setFocusableWindowState(true);
 136         owner.setVisible(true);
 137 
 138         child1.setFocusableWindowState(false);
 139         child1.setVisible(true);
 140 
 141         child2.setFocusableWindowState(true);
 142         child2.add(button);
 143         child2.setVisible(true);
 144 
 145         Util.waitTillShown(child2);
 146 
 147         Util.clickOnComp(button, robot);
 148         System.err.println("focus owner: " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
 149         if (button != KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner()) {
 150             throw new RuntimeException("Test failed.");
 151         }
 152         child1.dispose();
 153         child2.dispose();
 154         owner.dispose();
 155     }
 156 }