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 import java.awt.*;
  25 
  26 import static jdk.testlibrary.Asserts.*;
  27 
  28 // DWF: Dialog -> Window -> Frame
  29 public class FocusTransferDWFTest {
  30 
  31     class CustomDialog extends TestDialog {
  32 
  33         public CustomDialog(Frame f) {
  34             super(f);
  35         }
  36 
  37         @Override
  38         public void doOpenAction() {
  39             if (window != null) {
  40                 window.setVisible(true);
  41             }
  42         }
  43 
  44         @Override
  45         public void doCloseAction() {
  46             this.dispose();
  47         }
  48     }
  49 
  50     class CustomFrame extends TestFrame {
  51 
  52         @Override
  53         public void doCloseAction() {
  54             this.dispose();
  55         }
  56     }
  57 
  58     class CustomWindow extends TestWindow {
  59 
  60         public CustomWindow(Dialog d) {
  61             super(d);
  62         }
  63 
  64         @Override
  65         public void doOpenAction() {
  66             if (frame != null) {
  67                 frame.setVisible(true);
  68             }
  69         }
  70 
  71         @Override
  72         public void doCloseAction() {
  73             this.dispose();
  74         }
  75     }
  76 
  77     private TestDialog dialog;
  78     private TestFrame  frame;
  79     private TestWindow window;
  80 
  81     private static final int delay = 1000;
  82 
  83     private final ExtendedRobot robot;
  84 
  85     private Dialog.ModalityType modalityType;
  86 
  87     FocusTransferDWFTest(Dialog.ModalityType modType) throws Exception {
  88 
  89         modalityType = modType;
  90 
  91         robot = new ExtendedRobot();
  92         EventQueue.invokeLater(this::createGUI);
  93     }
  94 
  95     private void createGUI() {
  96 
  97         frame = new CustomFrame();
  98         frame.setLocation(50, 50);
  99 
 100         dialog = new CustomDialog((Frame) null);
 101         if (modalityType == null) {
 102             modalityType = Dialog.ModalityType.MODELESS;
 103         } else {
 104             dialog.setModalityType(modalityType);
 105         }
 106         dialog.setLocation(250, 50);
 107 
 108         window = new CustomWindow(dialog);
 109         window.setLocation(450, 50);
 110         dialog.setVisible(true);
 111     }
 112 
 113     private void closeAll() {
 114         if (dialog != null) { dialog.dispose(); }
 115         if ( frame != null) {  frame.dispose(); }
 116         if (window != null) { window.dispose(); }
 117     }
 118 
 119     public void doTest() throws Exception {
 120 
 121         robot.waitForIdle(delay);
 122 
 123         try {
 124 
 125             dialog.checkCloseButtonFocusGained(true);
 126 
 127             dialog.clickOpenButton(robot);
 128             robot.waitForIdle(delay);
 129 
 130             window.checkCloseButtonFocusGained(true);
 131             dialog.checkOpenButtonFocusLost(true);
 132 
 133             window.clickOpenButton(robot);
 134             robot.waitForIdle(delay);
 135 
 136             switch (modalityType) {
 137                 case APPLICATION_MODAL:
 138                     frame.checkCloseButtonFocusGained(false, 10);
 139                     window.checkOpenButtonFocusLost(false, 10);
 140 
 141                     frame.closeGained.reset();
 142 
 143                     dialog.clickCloseButton(robot);
 144                     robot.waitForIdle(delay);
 145 
 146                     frame.checkCloseButtonFocusGained(true);
 147                     assertFalse(window.isVisible(), "window shouldn't be visible");
 148 
 149                     break;
 150 
 151                 case DOCUMENT_MODAL:
 152                 case MODELESS:
 153                     frame.checkCloseButtonFocusGained(true);
 154                     window.checkOpenButtonFocusLost(true);
 155 
 156                     window.openGained.reset();
 157 
 158                     frame.clickCloseButton(robot);
 159                     robot.waitForIdle(delay);
 160 
 161                     window.checkOpenButtonFocusGained(true);
 162 
 163                     dialog.openGained.reset();
 164                     window.clickCloseButton(robot);
 165                     robot.waitForIdle(delay);
 166 
 167                     dialog.checkOpenButtonFocusGained(true);
 168 
 169                     break;
 170             }
 171 
 172         } catch (Exception e) {
 173 
 174             // make screenshot before exit
 175             Rectangle rect = new Rectangle(0, 0, 650, 250);
 176             java.awt.image.BufferedImage img = robot.createScreenCapture(rect);
 177             javax.imageio.ImageIO.write(img, "jpg", new java.io.File("NOK.jpg"));
 178 
 179             throw e;
 180         }
 181 
 182         robot.waitForIdle(delay);
 183 
 184         EventQueue.invokeAndWait(this::closeAll);
 185     }
 186 }