Path: blob/master/test/jdk/java/awt/Modal/ToBack/ToBackDDFTest.java
41153 views
/*1* Copyright (c) 2007, 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*/2223import java.awt.*;24import static jdk.test.lib.Asserts.*;252627// DDF: Dialog->Dialog->Frame2829public class ToBackDDFTest {3031private volatile TestDialog leftDialog;32private volatile TestFrame rightFrame;33private volatile CustomDialog dialog;3435private static final int delay = 500;36private final ExtendedRobot robot;3738private Frame hiddenFrame;3940private volatile boolean setModal;4142private Dialog.ModalityType modalityType;4344private ToBackDDFTest(Dialog.ModalityType modType,45boolean modal) throws Exception {46modalityType = modType;47setModal = modal;4849robot = new ExtendedRobot();50EventQueue.invokeLater(this::createGUI);51}5253public ToBackDDFTest(Dialog.ModalityType modalityType) throws Exception {54this(modalityType, false);55}5657public ToBackDDFTest(boolean modal) throws Exception { this(null, modal); }5859private void createGUI() {6061hiddenFrame = new Frame();62leftDialog = new TestDialog(hiddenFrame);63leftDialog.setLocation(50, 50);64leftDialog.setBackground(Color.BLUE);65leftDialog.setVisible(true);6667dialog = new CustomDialog(leftDialog);6869if (modalityType == null) {70dialog.setModal(setModal);71modalityType = dialog.getModalityType();72} else if (modalityType != null) {73dialog.setModalityType(modalityType);74}7576dialog.setBackground(Color.WHITE);77dialog.setLocation(150, 50);7879rightFrame = new TestFrame();80rightFrame.setLocation(250, 50);81rightFrame.setBackground(Color.RED);8283if (modalityType == Dialog.ModalityType.APPLICATION_MODAL) {84rightFrame.setModalExclusionType(85Dialog.ModalExclusionType.APPLICATION_EXCLUDE);86} else if (modalityType == Dialog.ModalityType.TOOLKIT_MODAL) {87rightFrame.setModalExclusionType(88Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);89}9091dialog.setVisible(true);92}9394private void checkLeftDialogIsOverlapped(String msg) {9596Point p = leftDialog.getLocationOnScreen();97int x = p.x + (int)(leftDialog.getWidth() * 0.9);98int y = p.y + (int)(leftDialog.getHeight() * 0.9);99boolean f = robot.getPixelColor(x, y).equals(leftDialog.getBackground());100assertFalse(f, msg);101}102103private void checkRightFrameIsOverlaped(String msg) {104105Point p = rightFrame.getLocationOnScreen();106int x = p.x + (int)(rightFrame.getWidth() * 0.1);107int y = p.y + (int)(rightFrame.getHeight() * 0.9);108boolean f = robot.getPixelColor(x, y).equals(rightFrame.getBackground());109assertFalse(f, msg);110}111112public void doTest() throws Exception {113114try {115robot.waitForIdle(delay);116117dialog.clickOpenButton(robot);118robot.waitForIdle(delay);119120dialog.clickCloseButton(robot);121robot.waitForIdle(delay);122123EventQueue.invokeAndWait(() -> { dialog.toBack(); });124robot.waitForIdle(delay);125126String type = modalityType.toString().toLowerCase().replace('_', ' ');127128boolean isModeless = (modalityType == Dialog.ModalityType.MODELESS);129130final String msg1;131if (isModeless) {132msg1 = "The modeless dialog was overlapped by the " +133"parent dialog after calling toBack method.";134} else {135msg1 = "The " + type + " dialog was overlapped by the blocked dialog.";136}137EventQueue.invokeAndWait(() -> { checkLeftDialogIsOverlapped(msg1); });138139if (isModeless) {140EventQueue.invokeAndWait(() -> { dialog.toFront(); });141} else {142EventQueue.invokeAndWait(() -> { leftDialog.toFront(); });143}144robot.waitForIdle(delay);145146final String msg2 = "The dialog is still behind the right frame after " +147"calling toFront method for " + (isModeless ? "it." : "its parent.");148149EventQueue.invokeAndWait(() -> { checkRightFrameIsOverlaped(msg2); });150151final String msg3;152if (isModeless) {153msg3 = "The modeless dialog is still behind the parent dialog.";154} else {155msg3 = "The " + type + " dialog was overlapped by the blocked " +156"dialog after calling toFront method for the blocked dialog.";157}158EventQueue.invokeAndWait(() -> { checkLeftDialogIsOverlapped(msg3); });159160} finally {161EventQueue.invokeAndWait(this::closeAll);162}163}164165private void closeAll() {166if (dialog != null) { dialog.dispose(); }167if (leftDialog != null) { leftDialog.dispose(); }168if (rightFrame != null) { rightFrame.dispose(); }169if (hiddenFrame != null) { hiddenFrame.dispose(); }170}171172173class CustomDialog extends TestDialog {174175public CustomDialog(Dialog d) { super(d); }176177@Override178public void doOpenAction() {179if (rightFrame != null) { rightFrame.setVisible(true); }180}181}182}183184185