jdk/test/java/awt/Modal/ModalFocusTransferTests/FocusTransferWDFTest.java
changeset 25759 625be49758c1
equal deleted inserted replaced
25758:7b1757bad3fa 25759:625be49758c1
       
     1 /*
       
     2  * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 
       
    25 import java.awt.*;
       
    26 import static jdk.testlibrary.Asserts.*;
       
    27 
       
    28 // WDF: Window -> Dialog -> Frame
       
    29 public class FocusTransferWDFTest {
       
    30 
       
    31     class CustomDialog extends TestDialog {
       
    32 
       
    33         public CustomDialog(Frame f) {
       
    34             super(f);
       
    35         }
       
    36 
       
    37         public CustomDialog(Dialog d) {
       
    38             super(d);
       
    39         }
       
    40 
       
    41         @Override
       
    42         public void doOpenAction() {
       
    43             if (frame != null) {
       
    44                 frame.setVisible(true);
       
    45             }
       
    46         }
       
    47 
       
    48         @Override
       
    49         public void doCloseAction() {
       
    50             this.dispose();
       
    51         }
       
    52     }
       
    53 
       
    54     class CustomFrame extends TestFrame {
       
    55 
       
    56         @Override
       
    57         public void doCloseAction() {
       
    58             this.dispose();
       
    59         }
       
    60     }
       
    61 
       
    62     class CustomWindow extends TestWindow {
       
    63 
       
    64         public CustomWindow(Frame f) {
       
    65             super(f);
       
    66         }
       
    67 
       
    68         @Override
       
    69         public void doOpenAction() {
       
    70             if (dialog != null) {
       
    71                 dialog.setVisible(true);
       
    72             }
       
    73         }
       
    74     }
       
    75 
       
    76 
       
    77     private TestDialog dialog;
       
    78     private TestFrame  frame;
       
    79     private TestWindow window;
       
    80 
       
    81     private Frame  parentFrame;
       
    82 
       
    83     private static final int delay = 1000;
       
    84 
       
    85     private final ExtendedRobot robot;
       
    86 
       
    87     private Dialog.ModalityType modalityType;
       
    88 
       
    89     public enum DialogParent {FRAME, NULL_DIALOG};
       
    90     private DialogParent dialogParent;
       
    91 
       
    92     public enum WindowParent {FRAME, NEW_FRAME};
       
    93     private WindowParent windowParent;
       
    94 
       
    95 
       
    96     FocusTransferWDFTest(Dialog.ModalityType modType,
       
    97                          DialogParent        dlgParent,
       
    98                          WindowParent        winParent) throws Exception {
       
    99 
       
   100         modalityType = modType;
       
   101         dialogParent = dlgParent;
       
   102         windowParent = winParent;
       
   103 
       
   104         robot = new ExtendedRobot();
       
   105         EventQueue.invokeLater( this::createGUI );
       
   106     }
       
   107 
       
   108     private void createGUI() {
       
   109 
       
   110         frame = new CustomFrame();
       
   111         frame.setLocation(50, 50);
       
   112 
       
   113         switch (dialogParent) {
       
   114             case FRAME:
       
   115                 dialog = new CustomDialog(frame);
       
   116                 break;
       
   117             case NULL_DIALOG:
       
   118                 dialog = new CustomDialog((Dialog) null);
       
   119                 break;
       
   120         }
       
   121         assertTrue(dialog != null, "error: null dialog");
       
   122 
       
   123         if (modalityType == null) {
       
   124             modalityType = Dialog.ModalityType.MODELESS;
       
   125         } else {
       
   126             dialog.setModalityType(modalityType);
       
   127         }
       
   128 
       
   129         dialog.setLocation(250, 50);
       
   130 
       
   131         switch (windowParent) {
       
   132             case FRAME:
       
   133                 window = new CustomWindow(frame);
       
   134                 break;
       
   135             case NEW_FRAME:
       
   136                 parentFrame = new Frame();
       
   137                 window = new CustomWindow(parentFrame);
       
   138                 break;
       
   139         }
       
   140         assertTrue(window != null, "error: null window");
       
   141 
       
   142         window.setLocation(450, 50);
       
   143         window.setVisible(true);
       
   144     }
       
   145 
       
   146     private void closeAll() {
       
   147         if (dialog != null) { dialog.dispose(); }
       
   148         if ( frame != null) {  frame.dispose(); }
       
   149         if (window != null) { window.dispose(); }
       
   150 
       
   151         if (parentFrame  != null) {  parentFrame.dispose(); }
       
   152     }
       
   153 
       
   154     private void ModalTest() throws Exception {
       
   155         frame.checkCloseButtonFocusGained(false, 10);
       
   156         dialog.checkOpenButtonFocusLost(false, 10);
       
   157 
       
   158         dialog.clickCloseButton(robot);
       
   159         robot.waitForIdle(delay);
       
   160 
       
   161         frame.checkCloseButtonFocusGained(true);
       
   162 
       
   163         window.openGained.reset();
       
   164 
       
   165         frame.clickCloseButton(robot);
       
   166         robot.waitForIdle(delay);
       
   167     }
       
   168 
       
   169     public void doTest() throws Exception {
       
   170 
       
   171         try {
       
   172 
       
   173             robot.waitForIdle(delay);
       
   174 
       
   175             window.checkCloseButtonFocusGained(false, 10);
       
   176 
       
   177             window.clickOpenButton(robot);
       
   178             robot.waitForIdle(delay);
       
   179 
       
   180             dialog.checkCloseButtonFocusGained(true);
       
   181             window.checkOpenButtonFocusLost(false, 10);
       
   182 
       
   183             dialog.clickOpenButton(robot);
       
   184             robot.waitForIdle(delay);
       
   185 
       
   186             switch (modalityType) {
       
   187                 case APPLICATION_MODAL:
       
   188                     ModalTest();
       
   189                     if (windowParent == WindowParent.FRAME) {
       
   190                         assertFalse(window.isVisible(),
       
   191                             "window shouldn't be visible");
       
   192                     } else { // WindowParent.NEW_FRAME
       
   193                         window.checkOpenButtonFocusGained(false, 10);
       
   194                     }
       
   195 
       
   196                     break;
       
   197 
       
   198                 case DOCUMENT_MODAL:
       
   199                     if (dialogParent == DialogParent.FRAME) {
       
   200                         ModalTest();
       
   201                         if (windowParent == WindowParent.FRAME) { // 10
       
   202                             assertFalse(window.isVisible(),
       
   203                                 "window shouldn't be visible");
       
   204                         } else { // WindowParent.NEW_FRAME
       
   205                             window.checkOpenButtonFocusGained(false, 10);
       
   206                         }
       
   207                     } else { // DialogParent.NULL_DIALOG
       
   208                         frame.checkCloseButtonFocusGained(true);
       
   209                         dialog.checkOpenButtonFocusLost(true);
       
   210 
       
   211                         dialog.openGained.reset();
       
   212 
       
   213                         frame.clickCloseButton(robot);
       
   214                         robot.waitForIdle(delay);
       
   215 
       
   216                         dialog.checkOpenButtonFocusGained(true);
       
   217 
       
   218                         window.openGained.reset();
       
   219 
       
   220                         dialog.clickCloseButton(robot);
       
   221                         robot.waitForIdle(delay);
       
   222 
       
   223                         window.checkOpenButtonFocusGained(false, 10);
       
   224                     }
       
   225                     break;
       
   226 
       
   227                 case MODELESS:
       
   228 
       
   229                     frame.checkCloseButtonFocusGained(true);
       
   230                     dialog.checkOpenButtonFocusLost(true);
       
   231 
       
   232                     dialog.openGained.reset();
       
   233 
       
   234                     frame.clickCloseButton(robot);
       
   235                     robot.waitForIdle(delay);
       
   236 
       
   237                     if (dialogParent == DialogParent.NULL_DIALOG) {
       
   238                         dialog.checkOpenButtonFocusGained(true);
       
   239 
       
   240                         window.openGained.reset();
       
   241 
       
   242                         dialog.clickCloseButton(robot);
       
   243                         robot.waitForIdle(delay);
       
   244 
       
   245                         window.checkOpenButtonFocusGained(false, 10);
       
   246                     } else {
       
   247                         assertFalse(dialog.isVisible(),
       
   248                             "dialog shouldn't be visible");
       
   249 
       
   250                         if (windowParent == WindowParent.FRAME) {
       
   251                             assertFalse(window.isVisible(),
       
   252                                 "window shouldn't be visible");
       
   253                         }
       
   254                     }
       
   255 
       
   256                     break;
       
   257             }
       
   258 
       
   259         } catch (Exception e) {
       
   260 
       
   261             // make screenshot before exit
       
   262             Rectangle rect = new Rectangle(0, 0, 650, 250);
       
   263             java.awt.image.BufferedImage img = robot.createScreenCapture(rect);
       
   264             javax.imageio.ImageIO.write(img, "jpg", new java.io.File("NOK.jpg"));
       
   265 
       
   266             throw e;
       
   267         }
       
   268 
       
   269         robot.waitForIdle(delay);
       
   270         EventQueue.invokeAndWait(this::closeAll);
       
   271     }
       
   272 }