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