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