1 /*
   2  * Copyright (c) 2012, 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       6385277
  27  * @summary   Tests that override redirect window gets activated on click.
  28  * @author    anton.tarasov@sun.com: area=awt.focus
  29  * @library   ../../regtesthelpers
  30  * @build     Util
  31  * @run       main OverrideRedirectWindowActivationTest
  32  */
  33 import java.awt.*;
  34 import java.awt.event.*;
  35 import java.util.concurrent.Callable;
  36 import javax.swing.SwingUtilities;
  37 import sun.awt.SunToolkit;
  38 import test.java.awt.regtesthelpers.Util;
  39 
  40 public class OverrideRedirectWindowActivationTest {
  41 
  42     private static Frame frame;
  43     private static Window window;
  44     private static Button fbutton;
  45     private static Button wbutton;
  46     private static Label label;
  47     private static Robot robot;
  48     private static SunToolkit toolkit;
  49 
  50     public static void main(String[] args) throws Exception {
  51 
  52         if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {
  53             System.out.println("No testing on Motif. Test passed.");
  54             return;
  55         }
  56 
  57         toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  58         robot = new Robot();
  59         robot.setAutoDelay(50);
  60 
  61         Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
  62 
  63             public void eventDispatched(AWTEvent e) {
  64                 System.out.println(e);
  65             }
  66         }, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);
  67 
  68         createAndShowWindow();
  69         toolkit.realSync();
  70 
  71         createAndShowFrame();
  72         toolkit.realSync();
  73 
  74         // click on Frame
  75         clickOn(getClickPoint(frame));
  76 
  77         if (!frame.isFocused()) {
  78             throw new RuntimeException("Error: a frame couldn't be focused by click.");
  79         }
  80 
  81         //click on Label in Window
  82         clickOn(getClickPoint(label));
  83 
  84         if (!window.isFocused()) {
  85             throw new RuntimeException("Test failed: the window couldn't be activated by click!");
  86         }
  87 
  88         // bring focus back to the frame
  89         clickOn(getClickPoint(fbutton));
  90 
  91         if (!frame.isFocused()) {
  92             throw new RuntimeException("Error: a frame couldn't be focused by click.");
  93         }
  94 
  95         // Test 2. Verifies that clicking on a component of unfocusable Window
  96         //         won't activate it.
  97 
  98         window.setFocusableWindowState(false);
  99         toolkit.realSync();
 100 
 101 
 102         clickOn(getClickPoint(label));
 103 
 104         if (window.isFocused()) {
 105             throw new RuntimeException("Test failed: unfocusable window got activated by click!");
 106         }
 107         System.out.println("Test passed.");
 108 
 109     }
 110 
 111     private static void createAndShowWindow() {
 112 
 113         frame = new Frame("Test Frame");
 114         window = new Window(frame);
 115         wbutton = new Button("wbutton");
 116         label = new Label("label");
 117 
 118         window.setBounds(800, 200, 200, 100);
 119         window.setLayout(new FlowLayout());
 120         window.add(wbutton);
 121         window.add(label);
 122         window.setVisible(true);
 123 
 124     }
 125 
 126     private static void createAndShowFrame() {
 127         fbutton = new Button("fbutton");
 128 
 129         frame.setBounds(800, 0, 200, 100);
 130         frame.setLayout(new FlowLayout());
 131         frame.add(fbutton);
 132         frame.setVisible(true);
 133 
 134     }
 135 
 136     static void clickOn(Point point) {
 137 
 138         robot.mouseMove(point.x, point.y);
 139 
 140         robot.mousePress(InputEvent.BUTTON1_MASK);
 141         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 142 
 143         toolkit.realSync();
 144     }
 145 
 146     static Point getClickPoint(Component c) {
 147         Point p = c.getLocationOnScreen();
 148         Dimension d = c.getSize();
 149         return new Point(p.x + (int) (d.getWidth() / 2), p.y + (int) (d.getHeight() / 2));
 150     }
 151 
 152     static Point getClickPoint(Frame frame) {
 153         Point p = frame.getLocationOnScreen();
 154         Dimension d = frame.getSize();
 155         return new Point(p.x + (int) (d.getWidth() / 2), p.y + (frame.getInsets().top / 2));
 156     }
 157 }