Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Focus/SimpleWindowActivationTest/SimpleWindowActivationTest.java
47525 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 638527726* @summary Tests that override redirect window gets activated on click.27* @author [email protected]: area=awt.focus28* @library ../../regtesthelpers29* @build Util30* @run main SimpleWindowActivationTest31*/32import java.awt.*;33import java.awt.event.*;34import java.util.concurrent.Callable;35import javax.swing.SwingUtilities;36import test.java.awt.regtesthelpers.Util;3738public class SimpleWindowActivationTest {3940private static Frame frame;41private static Window window;42private static Button fbutton;43private static Button wbutton;44private static Label label;45private static Robot robot;4647public static void main(String[] args) throws Exception {4849if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {50System.out.println("No testing on Motif. Test passed.");51return;52}5354robot = new Robot();55robot.setAutoDelay(50);5657Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {5859public void eventDispatched(AWTEvent e) {60System.out.println(e);61}62}, FocusEvent.FOCUS_EVENT_MASK | WindowEvent.WINDOW_FOCUS_EVENT_MASK);6364createAndShowWindow();65robot.waitForIdle();6667createAndShowFrame();68robot.waitForIdle();6970// click on Frame71clickOn(getClickPoint(frame));7273if (!frame.isFocused()) {74throw new RuntimeException("Error: a frame couldn't be focused by click.");75}7677//click on Label in Window78clickOn(getClickPoint(label));7980if (!window.isFocused()) {81throw new RuntimeException("Test failed: the window couldn't be activated by click!");82}8384// bring focus back to the frame85clickOn(getClickPoint(fbutton));8687if (!frame.isFocused()) {88throw new RuntimeException("Error: a frame couldn't be focused by click.");89}9091// Test 2. Verifies that clicking on a component of unfocusable Window92// won't activate it.9394window.setFocusableWindowState(false);95robot.waitForIdle();969798clickOn(getClickPoint(label));99100if (window.isFocused()) {101throw new RuntimeException("Test failed: unfocusable window got activated by click!");102}103System.out.println("Test passed.");104105}106107private static void createAndShowWindow() {108109frame = new Frame("Test Frame");110window = new Window(frame);111wbutton = new Button("wbutton");112label = new Label("label");113114window.setBounds(800, 200, 300, 100);115window.setLayout(new FlowLayout());116window.add(wbutton);117window.add(label);118window.setVisible(true);119120}121122private static void createAndShowFrame() {123fbutton = new Button("fbutton");124125frame.setBounds(800, 0, 300, 100);126frame.setLayout(new FlowLayout());127frame.add(fbutton);128frame.setVisible(true);129130}131132static void clickOn(Point point) {133134robot.mouseMove(point.x, point.y);135robot.waitForIdle();136137robot.mousePress(InputEvent.BUTTON1_MASK);138robot.mouseRelease(InputEvent.BUTTON1_MASK);139140robot.waitForIdle();141}142143static Point getClickPoint(Component c) {144Point p = c.getLocationOnScreen();145Dimension d = c.getSize();146return new Point(p.x + (int) (d.getWidth() / 2), p.y + (int) (d.getHeight() / 2));147}148149static Point getClickPoint(Frame frame) {150Point p = frame.getLocationOnScreen();151Dimension d = frame.getSize();152return new Point(p.x + (int) (d.getWidth() / 2), p.y + (frame.getInsets().top / 2));153}154}155156157