Path: blob/master/test/jdk/java/awt/Modal/ModalFocusTransferTests/FocusTransferDWFTest.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.*;2425import static jdk.test.lib.Asserts.*;2627// DWF: Dialog -> Window -> Frame28public class FocusTransferDWFTest {2930class CustomDialog extends TestDialog {3132public CustomDialog(Frame f) {33super(f);34}3536@Override37public void doOpenAction() {38if (window != null) {39window.setVisible(true);40}41}4243@Override44public void doCloseAction() {45this.dispose();46}47}4849class CustomFrame extends TestFrame {5051@Override52public void doCloseAction() {53this.dispose();54}55}5657class CustomWindow extends TestWindow {5859public CustomWindow(Dialog d) {60super(d);61}6263@Override64public void doOpenAction() {65if (frame != null) {66frame.setVisible(true);67}68}6970@Override71public void doCloseAction() {72this.dispose();73}74}7576private TestDialog dialog;77private TestFrame frame;78private TestWindow window;7980private static final int delay = 1000;8182private final ExtendedRobot robot;8384private Dialog.ModalityType modalityType;8586FocusTransferDWFTest(Dialog.ModalityType modType) throws Exception {8788modalityType = modType;8990robot = new ExtendedRobot();91EventQueue.invokeLater(this::createGUI);92}9394private void createGUI() {9596frame = new CustomFrame();97frame.setLocation(50, 50);9899dialog = new CustomDialog((Frame) null);100if (modalityType == null) {101modalityType = Dialog.ModalityType.MODELESS;102} else {103dialog.setModalityType(modalityType);104}105dialog.setLocation(250, 50);106107window = new CustomWindow(dialog);108window.setLocation(450, 50);109dialog.setVisible(true);110}111112private void closeAll() {113if (dialog != null) { dialog.dispose(); }114if ( frame != null) { frame.dispose(); }115if (window != null) { window.dispose(); }116}117118public void doTest() throws Exception {119120robot.waitForIdle(delay);121122try {123124dialog.checkCloseButtonFocusGained(true);125126dialog.clickOpenButton(robot);127robot.waitForIdle(delay);128129window.checkCloseButtonFocusGained(true);130dialog.checkOpenButtonFocusLost(true);131132window.clickOpenButton(robot);133robot.waitForIdle(delay);134135switch (modalityType) {136case APPLICATION_MODAL:137frame.checkCloseButtonFocusGained(false, 10);138window.checkOpenButtonFocusLost(false, 10);139140frame.closeGained.reset();141142dialog.clickCloseButton(robot);143robot.waitForIdle(delay);144145frame.checkCloseButtonFocusGained(true);146assertFalse(window.isVisible(), "window shouldn't be visible");147148break;149150case DOCUMENT_MODAL:151case MODELESS:152frame.checkCloseButtonFocusGained(true);153window.checkOpenButtonFocusLost(true);154155window.openGained.reset();156157frame.clickCloseButton(robot);158robot.waitForIdle(delay);159160window.checkOpenButtonFocusGained(true);161162dialog.openGained.reset();163window.clickCloseButton(robot);164robot.waitForIdle(delay);165166dialog.checkOpenButtonFocusGained(true);167168break;169}170171} catch (Exception e) {172173// make screenshot before exit174Rectangle rect = new Rectangle(0, 0, 650, 250);175java.awt.image.BufferedImage img = robot.createScreenCapture(rect);176javax.imageio.ImageIO.write(img, "jpg", new java.io.File("NOK.jpg"));177178throw e;179}180181robot.waitForIdle(delay);182183EventQueue.invokeAndWait(this::closeAll);184}185}186187188