Path: blob/master/test/jdk/java/awt/Focus/ModalExcludedWindowClickTest/ModalExcludedWindowClickTest.java
41152 views
/*1* Copyright (c) 2005, 2018, 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@key headful26@bug 627184927@summary Tests that component in modal excluded Window which parent is blocked responses to mouse clicks.28@modules java.desktop/sun.awt29@run main ModalExcludedWindowClickTest30*/3132import java.awt.*;33import java.awt.event.*;34import java.lang.reflect.*;3536public class ModalExcludedWindowClickTest {37Robot robot;38Frame frame = new Frame("Frame");39Window w = new Window(frame);40Dialog d = new Dialog ((Dialog)null, "NullParentDialog", true);41Button button = new Button("Button");42boolean actionPerformed = false;4344public static void main (String args[]) {45ModalExcludedWindowClickTest app = new ModalExcludedWindowClickTest();46app.init();47app.start();48}4950public void init() {51try {52robot = new Robot();53} catch (AWTException e) {54throw new RuntimeException("Error: unable to create robot", e);55}56}5758public void start() {5960if ("sun.awt.motif.MToolkit".equals(Toolkit.getDefaultToolkit().getClass().getName())) {61System.out.println("No testing on MToolkit.");62return;63}6465button.addActionListener(new ActionListener() {66public void actionPerformed(ActionEvent e) {67actionPerformed = true;68System.out.println(e.paramString());69}70});7172EventQueue.invokeLater(new Runnable() {73public void run() {74frame.setSize(200, 200);75frame.setVisible(true);7677w.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);78w.add(button);79w.setSize(200, 200);80w.setLocation(230, 230);81w.setVisible(true);8283d.setSize(200, 200);84d.setLocation(0, 230);85d.setVisible(true);8687}88});8990waitTillShown(d);9192test();93}9495void test() {96clickOn(button);97waitForIdle();98if (!actionPerformed) {99throw new RuntimeException("Test failed!");100}101System.out.println("Test passed.");102}103104void clickOn(Component c) {105Point p = c.getLocationOnScreen();106Dimension d = c.getSize();107108System.out.println("Clicking " + c);109110if (c instanceof Frame) {111robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + ((Frame)c).getInsets().top/2);112} else {113robot.mouseMove(p.x + (int)(d.getWidth()/2), p.y + (int)(d.getHeight()/2));114}115waitForIdle();116robot.mousePress(InputEvent.BUTTON1_MASK);117robot.delay(50);118robot.mouseRelease(InputEvent.BUTTON1_MASK);119waitForIdle();120}121void waitTillShown(Component c) {122while (true) {123try {124Thread.sleep(100);125Point p = c.getLocationOnScreen();126if (p != null)127break;128} catch (InterruptedException e) {129throw new RuntimeException(e);130} catch (IllegalComponentStateException e) {}131}132}133void waitForIdle() {134try {135robot.waitForIdle();136EventQueue.invokeAndWait( new Runnable() {137public void run() {} // Dummy implementation138});139} catch(InterruptedException ie) {140System.out.println("waitForIdle, non-fatal exception caught:");141ie.printStackTrace();142} catch(InvocationTargetException ite) {143System.out.println("waitForIdle, non-fatal exception caught:");144ite.printStackTrace();145}146147// wait longer...148robot.delay(200);149}150}151152153