Path: blob/master/test/jdk/java/awt/Modal/ModalFocusTransferTests/FocusTransferDialogsTest.java
41153 views
/*1* Copyright (c) 2007, 2014, 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.*;252627public class FocusTransferDialogsTest {2829class CustomDialog1 extends TestDialog {3031public CustomDialog1(Frame f) {32super(f);33}3435@Override36public void doOpenAction() {37if (dialog2 != null) {38dialog2.setVisible(true);39}40}4142@Override43public void doCloseAction() {44this.dispose();45}46}4748class CustomDialog2 extends TestDialog {4950public CustomDialog2(Dialog d) {51super(d);52}5354@Override55public void doOpenAction() {56if (dialog3 != null) {57dialog3.setVisible(true);58}59}6061@Override62public void doCloseAction() {63this.dispose();64}65}6667class CustomDialog3 extends TestDialog {6869public CustomDialog3(Frame f) {70super(f);71}7273public CustomDialog3(Dialog d) {74super(d);75}7677@Override78public void doCloseAction() {79this.dispose();80}81}828384private TestDialog dialog1, dialog2, dialog3;85private Frame parentFrame;8687private static final int delay = 1000;88private final ExtendedRobot robot;89private Dialog.ModalityType modalityType;9091FocusTransferDialogsTest(Dialog.ModalityType modType) throws Exception {9293modalityType = modType;94robot = new ExtendedRobot();95EventQueue.invokeLater(this::createGUI);96}9798private void createGUI() {99100dialog1 = new CustomDialog1((Frame) null);101dialog1.setTitle("Dialog1");102dialog1.setLocation(50, 50);103104if (modalityType != null) {105dialog1.setModalityType(modalityType);106} else {107modalityType = Dialog.ModalityType.MODELESS;108}109110dialog2 = new CustomDialog2(dialog1);111dialog2.setTitle("Dialog2");112dialog2.setLocation(250, 50);113114parentFrame = new Frame();115dialog3 = new CustomDialog3(parentFrame);116dialog3.setTitle("Dialog3");117dialog3.setLocation(450, 50);118119dialog1.setVisible(true);120}121122private void closeAll() {123if (dialog1 != null) { dialog1.dispose(); }124if (dialog2 != null) { dialog2.dispose(); }125if (dialog3 != null) { dialog3.dispose(); }126if (parentFrame != null) { parentFrame.dispose(); }127}128129public void doTest() throws Exception {130131robot.waitForIdle(delay);132133try {134135dialog1.checkCloseButtonFocusGained(true);136137dialog1.clickOpenButton(robot);138robot.waitForIdle(delay);139140dialog2.checkCloseButtonFocusGained(true);141dialog1.checkOpenButtonFocusLost(true);142143dialog1.openGained.reset();144dialog2.clickOpenButton(robot);145robot.waitForIdle(delay);146147switch (modalityType) {148case APPLICATION_MODAL:149150dialog3.checkCloseButtonFocusGained(false, 10);151dialog2.checkOpenButtonFocusLost(true);152153dialog1.checkCloseButtonFocusGained(true);154dialog3.closeGained.reset();155156dialog1.clickCloseButton(robot);157robot.waitForIdle(delay);158159dialog3.checkCloseButtonFocusGained(true);160161break;162163case DOCUMENT_MODAL:164case MODELESS:165166dialog3.checkCloseButtonFocusGained(true);167dialog2.checkOpenButtonFocusLost(true);168169dialog1.openGained.reset();170171dialog2.clickCloseButton(robot);172robot.waitForIdle(delay);173174dialog1.checkOpenButtonFocusGained(true);175176break;177}178179} catch (Exception e) {180181// make screenshot before exit182Rectangle rect = new Rectangle(0, 0, 650, 250);183java.awt.image.BufferedImage img = robot.createScreenCapture(rect);184javax.imageio.ImageIO.write(img, "jpg", new java.io.File("NOK.jpg"));185186throw e;187}188189robot.waitForIdle(delay);190EventQueue.invokeAndWait(this::closeAll);191}192}193194195