Path: blob/master/test/jdk/java/awt/Modal/ToBack/ToBackFDFTest.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*/222324import java.awt.*;25import static jdk.test.lib.Asserts.*;262728// FDF: Frame->Dialog->Frame2930public class ToBackFDFTest {3132private volatile CustomDialog dialog;33private volatile TestFrame leftFrame, rightFrame;3435private static final int delay = 500;36private final ExtendedRobot robot;3738private Dialog hiddenDialog;39private Frame hiddenFrame;4041public enum DialogOwner {HIDDEN_DIALOG, NULL_DIALOG, HIDDEN_FRAME, NULL_FRAME, FRAME};4243private DialogOwner owner;44private volatile boolean setModal;4546private Dialog.ModalityType modalityType;4748private ToBackFDFTest(Dialog.ModalityType modType,49boolean modal,50DialogOwner o) throws Exception {51modalityType = modType;52setModal = modal;53owner = o;5455robot = new ExtendedRobot();56EventQueue.invokeLater(this::createGUI);57}5859public ToBackFDFTest(Dialog.ModalityType modalityType,60DialogOwner o) throws Exception {61this(modalityType, false, o);62}6364public ToBackFDFTest(boolean modal, DialogOwner o) throws Exception {65this(null, modal, o);66}6768private void createGUI() {6970leftFrame = new TestFrame();71leftFrame.setLocation(50, 50);72leftFrame.setBackground(Color.BLUE);73leftFrame.setVisible(true);7475switch (owner) {76case HIDDEN_DIALOG:77hiddenDialog = new Dialog((Frame) null);78dialog = new CustomDialog(hiddenDialog);79break;80case NULL_DIALOG:81dialog = new CustomDialog((Dialog) null);82break;83case HIDDEN_FRAME:84hiddenFrame = new Frame();85dialog = new CustomDialog(hiddenFrame);86break;87case NULL_FRAME:88dialog = new CustomDialog((Frame) null);89break;90case FRAME:91dialog = new CustomDialog(leftFrame);92break;93}9495if (modalityType == null) {96dialog.setModal(setModal);97modalityType = dialog.getModalityType();98} else if (modalityType != null) {99dialog.setModalityType(modalityType);100}101102dialog.setBackground(Color.WHITE);103dialog.setLocation(150, 50);104105rightFrame = new TestFrame();106rightFrame.setLocation(250, 50);107rightFrame.setBackground(Color.RED);108109if (modalityType == Dialog.ModalityType.APPLICATION_MODAL) {110rightFrame.setModalExclusionType(111Dialog.ModalExclusionType.APPLICATION_EXCLUDE);112} else if (modalityType == Dialog.ModalityType.TOOLKIT_MODAL) {113rightFrame.setModalExclusionType(114Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);115}116117dialog.setVisible(true);118}119120private void checkIfLeftOnTop(boolean refState, String msg) {121122Point p = leftFrame.getLocationOnScreen();123int x = p.x + (int)(leftFrame.getWidth() * 0.9);124int y = p.y + (int)(leftFrame.getHeight() * 0.9);125boolean f = robot.getPixelColor(x, y).equals(leftFrame.getBackground());126assertEQ(refState, f, msg);127}128129private void checkIfRightOnTop(boolean refState, String msg) {130131Point p = rightFrame.getLocationOnScreen();132int x = p.x + (int)(rightFrame.getWidth() * 0.1);133int y = p.y + (int)(rightFrame.getHeight() * 0.9);134boolean f = robot.getPixelColor(x, y).equals(rightFrame.getBackground());135assertEQ(refState, f, msg);136}137138private void Test() throws Exception {139140String type =141dialog.getModalityType().toString().toLowerCase().replace('_', ' ');142143final String msg1 = "The " + type + "dialog was " +144"overlapped by the blocked frame.";145EventQueue.invokeAndWait(() -> { checkIfLeftOnTop(false, msg1); });146147EventQueue.invokeAndWait(() -> { leftFrame.toFront(); });148robot.waitForIdle(delay);149150final String msg2 = "The dialog is still overlapped by the right frame" +151" after calling toFront method for the blocked (left) frame.";152EventQueue.invokeAndWait(() -> { checkIfRightOnTop(false, msg2); });153154final String msg3 = "The " + type + " dialog was overlapped by the " +155"blocked frame after calling toFront method for the blocked frame.";156EventQueue.invokeAndWait(() -> { checkIfLeftOnTop(false, msg3); });157158159if (owner == DialogOwner.FRAME) { return; }160161EventQueue.invokeAndWait(() -> { leftFrame.toBack(); });162robot.waitForIdle(delay);163164final String msg4 = "Calling toBack " +165"for the blocked frame pushed the blocking dialog to back.";166EventQueue.invokeAndWait(() -> { checkIfRightOnTop(false, msg4); });167168final String msg5 = "The " + type + " dialog was overlapped " +169"by the blocked frame after toBack was called for the left frame.";170EventQueue.invokeAndWait(() -> { checkIfLeftOnTop(false, msg5); });171}172173private void docTest() throws Exception {174175if (owner == DialogOwner.FRAME) { Test(); }176else {177178final String msg1 = "toBack was called for the dialog.";179EventQueue.invokeAndWait(() -> { checkIfLeftOnTop(true, msg1); });180EventQueue.invokeAndWait(() -> { checkIfRightOnTop(true, msg1); });181182EventQueue.invokeAndWait(() -> { dialog.toFront(); });183robot.waitForIdle(delay);184185final String msg2 = "Dialog still behind " +186"the right frame even after calling toFront method.";187EventQueue.invokeAndWait(() -> { checkIfRightOnTop(false, msg2); });188final String msg3 = "The document modal dialog " +189"gone behind the blocked left frame.";190EventQueue.invokeAndWait(() -> { checkIfLeftOnTop(false, msg3); });191192EventQueue.invokeAndWait(() -> { leftFrame.toBack(); });193robot.waitForIdle(delay);194195final String msg4 = "Calling toBack for the left " +196"frame pushed the document modal dialog to back.";197EventQueue.invokeAndWait(() -> { checkIfRightOnTop(false, msg4); });198final String msg5 = "The document modal dialog " +199"was pushed behind the left frame when toBack called for the frame.";200EventQueue.invokeAndWait(() -> { checkIfRightOnTop(false, msg5); });201}202}203204private void modelessTest() throws Exception {205206if (owner == DialogOwner.FRAME) {207final String msg = "The modeless dialog was " +208"pushed behind the parent left frame after toBack call.";209EventQueue.invokeAndWait(() -> { checkIfLeftOnTop(false, msg); });210} else {211final String msg =212"Dialog should not overlap the frame after calling toBack.";213EventQueue.invokeAndWait(() -> { checkIfLeftOnTop(true, msg); });214EventQueue.invokeAndWait(() -> { checkIfRightOnTop(true, msg); });215}216217EventQueue.invokeAndWait(() -> { dialog.toFront(); });218robot.waitForIdle(delay);219220final String msg1 = "The frames should not overlap the dialog " +221"after calling toFront for it.";222EventQueue.invokeAndWait(() -> { checkIfLeftOnTop(false, msg1); });223EventQueue.invokeAndWait(() -> { checkIfRightOnTop(false, msg1); });224225if (owner == DialogOwner.FRAME) { return; }226227EventQueue.invokeAndWait(() -> { leftFrame.toBack(); });228robot.waitForIdle(delay);229230final String msg2 = "Calling toBack method for the " +231"left frame pushed the modeless dialog to back.";232EventQueue.invokeAndWait(() -> { checkIfRightOnTop(false, msg2); });233final String msg3 = "The modeless dialog was pushed " +234"behind the left frame after toBack was called for the frame.";235EventQueue.invokeAndWait(() -> { checkIfLeftOnTop(false, msg3); });236}237238public void doTest() throws Exception {239240try {241robot.waitForIdle(delay);242243dialog.clickOpenButton(robot);244robot.waitForIdle(delay);245246dialog.clickCloseButton(robot);247robot.waitForIdle(delay);248249EventQueue.invokeAndWait(() -> { dialog.toBack(); });250robot.waitForIdle(delay);251252switch (modalityType) {253case APPLICATION_MODAL:254case TOOLKIT_MODAL:255Test();256break;257case DOCUMENT_MODAL:258docTest();259break;260case MODELESS:261modelessTest();262break;263}264} finally {265EventQueue.invokeAndWait(this::closeAll);266}267}268269private void closeAll() {270if (dialog != null) { dialog.dispose(); }271if (leftFrame != null) { leftFrame.dispose(); }272if (rightFrame != null) { rightFrame.dispose(); }273if (hiddenDialog != null) { hiddenDialog.dispose(); }274if (hiddenFrame != null) { hiddenFrame.dispose(); }275}276277278class CustomDialog extends TestDialog {279280public CustomDialog(Dialog d) { super(d); }281public CustomDialog(Frame f) { super(f); }282283@Override284public void doOpenAction() {285if (rightFrame != null) {286rightFrame.setVisible(true);287}288}289}290}291292293