Path: blob/master/test/jdk/java/awt/Modal/ModalFocusTransferTests/FocusTransferFWDTest.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// FWD: Frame -> Window -> Dialog29public class FocusTransferFWDTest {3031class CustomFrame extends TestFrame {3233@Override34public void doOpenAction() {35if (window != null) {36window.setVisible(true);37}38}3940@Override41public void doCloseAction() {42this.dispose();43}44}4546class CustomWindow extends TestWindow {4748public CustomWindow(Frame f) {49super(f);50}5152@Override53public void doOpenAction() {54if (dialog != null) {55dialog.setVisible(true);56}57}5859@Override60public void doCloseAction() {61this.dispose();62}63}6465class CustomDialog extends TestDialog {6667public CustomDialog(Frame f) {68super(f);69}7071public CustomDialog(Dialog d) {72super(d);73}7475@Override76public void doCloseAction() {77this.dispose();78}79}8081private TestDialog dialog;82private TestFrame frame;83private TestWindow window;8485private Frame parentFrame;86private Dialog parentDialog;8788private static final int delay = 1000;8990private final ExtendedRobot robot;9192private final Dialog.ModalityType modalityType;9394public enum DialogParent {NULL_DIALOG, NULL_FRAME, HIDDEN_DIALOG, HIDDEN_FRAME};95private DialogParent dialogParent;9697FocusTransferFWDTest(Dialog.ModalityType modType,98DialogParent dlgParent) throws Exception {99100modalityType = modType;101dialogParent = dlgParent;102103robot = new ExtendedRobot();104EventQueue.invokeLater(this::createGUI);105}106107private void createGUI() {108109frame = new CustomFrame();110frame.setLocation(50, 50);111112switch (dialogParent) {113case NULL_DIALOG:114dialog = new CustomDialog((Dialog) null);115break;116case NULL_FRAME:117dialog = new CustomDialog((Frame) null);118break;119case HIDDEN_DIALOG:120parentDialog = new Dialog((Frame) null);121dialog = new CustomDialog(parentDialog);122break;123case HIDDEN_FRAME:124parentFrame = new Frame();125dialog = new CustomDialog(parentFrame);126break;127}128129assertTrue(dialog != null, "error: null dialog");130131if (modalityType != null) {132dialog.setModalityType(modalityType);133}134135dialog.setLocation(250, 50);136window = new CustomWindow(frame);137window.setLocation(450, 50);138frame.setVisible(true);139}140141private void closeAll() {142if (dialog != null) { dialog.dispose(); }143if ( frame != null) { frame.dispose(); }144if (window != null) { window.dispose(); }145146if (parentDialog != null) { parentDialog.dispose(); }147if (parentFrame != null) { parentFrame.dispose(); }148}149150public void doTest() throws Exception {151152robot.waitForIdle(delay);153154try {155156frame.checkCloseButtonFocusGained(true);157158frame.clickOpenButton(robot);159robot.waitForIdle(delay);160161window.checkCloseButtonFocusGained(true);162frame.checkOpenButtonFocusLost(true);163164window.clickOpenButton(robot);165robot.waitForIdle(delay);166167dialog.checkCloseButtonFocusGained(true);168window.checkOpenButtonFocusLost(true);169170window.openGained.reset();171172dialog.clickCloseButton(robot);173robot.waitForIdle(delay);174175window.checkOpenButtonFocusGained(true);176177frame.openGained.reset();178179window.clickCloseButton(robot);180robot.waitForIdle(delay);181182frame.checkOpenButtonFocusGained(true);183184frame.clickCloseButton(robot);185robot.waitForIdle(delay);186187} catch (Exception e) {188189// make screenshot before exit190Rectangle rect = new Rectangle(0, 0, 650, 250);191java.awt.image.BufferedImage img = robot.createScreenCapture(rect);192javax.imageio.ImageIO.write(img, "jpg", new java.io.File("NOK.jpg"));193194throw e;195}196197robot.waitForIdle(delay);198EventQueue.invokeAndWait(this::closeAll);199}200}201202203