Path: blob/master/test/jdk/java/awt/Modal/ModalFocusTransferTests/FocusTransferWDFTest.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.*;2627// WDF: Window -> Dialog -> Frame28public class FocusTransferWDFTest {2930class CustomDialog extends TestDialog {3132public CustomDialog(Frame f) {33super(f);34}3536public CustomDialog(Dialog d) {37super(d);38}3940@Override41public void doOpenAction() {42if (frame != null) {43frame.setVisible(true);44}45}4647@Override48public void doCloseAction() {49this.dispose();50}51}5253class CustomFrame extends TestFrame {5455@Override56public void doCloseAction() {57this.dispose();58}59}6061class CustomWindow extends TestWindow {6263public CustomWindow(Frame f) {64super(f);65}6667@Override68public void doOpenAction() {69if (dialog != null) {70dialog.setVisible(true);71}72}73}747576private TestDialog dialog;77private TestFrame frame;78private TestWindow window;7980private Frame parentFrame;8182private static final int delay = 1000;8384private final ExtendedRobot robot;8586private Dialog.ModalityType modalityType;8788public enum DialogParent {FRAME, NULL_DIALOG};89private DialogParent dialogParent;9091public enum WindowParent {FRAME, NEW_FRAME};92private WindowParent windowParent;939495FocusTransferWDFTest(Dialog.ModalityType modType,96DialogParent dlgParent,97WindowParent winParent) throws Exception {9899modalityType = modType;100dialogParent = dlgParent;101windowParent = winParent;102103robot = new ExtendedRobot();104EventQueue.invokeLater( this::createGUI );105}106107private void createGUI() {108109frame = new CustomFrame();110frame.setLocation(50, 50);111112switch (dialogParent) {113case FRAME:114dialog = new CustomDialog(frame);115break;116case NULL_DIALOG:117dialog = new CustomDialog((Dialog) null);118break;119}120assertTrue(dialog != null, "error: null dialog");121122if (modalityType == null) {123modalityType = Dialog.ModalityType.MODELESS;124} else {125dialog.setModalityType(modalityType);126}127128dialog.setLocation(250, 50);129130switch (windowParent) {131case FRAME:132window = new CustomWindow(frame);133break;134case NEW_FRAME:135parentFrame = new Frame();136window = new CustomWindow(parentFrame);137break;138}139assertTrue(window != null, "error: null window");140141window.setLocation(450, 50);142window.setVisible(true);143}144145private void closeAll() {146if (dialog != null) { dialog.dispose(); }147if ( frame != null) { frame.dispose(); }148if (window != null) { window.dispose(); }149150if (parentFrame != null) { parentFrame.dispose(); }151}152153private void ModalTest() throws Exception {154frame.checkCloseButtonFocusGained(false, 10);155dialog.checkOpenButtonFocusLost(false, 10);156157dialog.clickCloseButton(robot);158robot.waitForIdle(delay);159160frame.checkCloseButtonFocusGained(true);161162window.openGained.reset();163164frame.clickCloseButton(robot);165robot.waitForIdle(delay);166}167168public void doTest() throws Exception {169170try {171172robot.waitForIdle(delay);173174window.checkCloseButtonFocusGained(false, 10);175176window.clickOpenButton(robot);177robot.waitForIdle(delay);178179dialog.checkCloseButtonFocusGained(true);180window.checkOpenButtonFocusLost(false, 10);181182dialog.clickOpenButton(robot);183robot.waitForIdle(delay);184185switch (modalityType) {186case APPLICATION_MODAL:187ModalTest();188if (windowParent == WindowParent.FRAME) {189assertFalse(window.isVisible(),190"window shouldn't be visible");191} else { // WindowParent.NEW_FRAME192window.checkOpenButtonFocusGained(false, 10);193}194195break;196197case DOCUMENT_MODAL:198if (dialogParent == DialogParent.FRAME) {199ModalTest();200if (windowParent == WindowParent.FRAME) { // 10201assertFalse(window.isVisible(),202"window shouldn't be visible");203} else { // WindowParent.NEW_FRAME204window.checkOpenButtonFocusGained(false, 10);205}206} else { // DialogParent.NULL_DIALOG207frame.checkCloseButtonFocusGained(true);208dialog.checkOpenButtonFocusLost(true);209210dialog.openGained.reset();211212frame.clickCloseButton(robot);213robot.waitForIdle(delay);214215dialog.checkOpenButtonFocusGained(true);216217window.openGained.reset();218219dialog.clickCloseButton(robot);220robot.waitForIdle(delay);221222window.checkOpenButtonFocusGained(false, 10);223}224break;225226case MODELESS:227228frame.checkCloseButtonFocusGained(true);229dialog.checkOpenButtonFocusLost(true);230231dialog.openGained.reset();232233frame.clickCloseButton(robot);234robot.waitForIdle(delay);235236if (dialogParent == DialogParent.NULL_DIALOG) {237dialog.checkOpenButtonFocusGained(true);238239window.openGained.reset();240241dialog.clickCloseButton(robot);242robot.waitForIdle(delay);243244window.checkOpenButtonFocusGained(false, 10);245} else {246assertFalse(dialog.isVisible(),247"dialog shouldn't be visible");248249if (windowParent == WindowParent.FRAME) {250assertFalse(window.isVisible(),251"window shouldn't be visible");252}253}254255break;256}257258} catch (Exception e) {259260// make screenshot before exit261Rectangle rect = new Rectangle(0, 0, 650, 250);262java.awt.image.BufferedImage img = robot.createScreenCapture(rect);263javax.imageio.ImageIO.write(img, "jpg", new java.io.File("NOK.jpg"));264265throw e;266}267268robot.waitForIdle(delay);269EventQueue.invokeAndWait(this::closeAll);270}271}272273274