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 import static jdk.testlibrary.Asserts.*;
  26 
  27 
  28 // DDF: Dialog->Dialog->Frame
  29 
  30 public class ToBackDDFTest {
  31 
  32     private volatile TestDialog leftDialog;
  33     private volatile TestFrame  rightFrame;
  34     private volatile CustomDialog dialog;
  35 
  36     private static final int delay = 500;
  37     private final ExtendedRobot robot;
  38 
  39     private Frame hiddenFrame;
  40 
  41     private volatile boolean setModal;
  42 
  43     private Dialog.ModalityType modalityType;
  44 
  45     private ToBackDDFTest(Dialog.ModalityType modType,
  46                           boolean             modal) throws Exception {
  47         modalityType = modType;
  48         setModal = modal;
  49 
  50         robot = new ExtendedRobot();
  51         EventQueue.invokeLater(this::createGUI);
  52     }
  53 
  54     public ToBackDDFTest(Dialog.ModalityType modalityType) throws Exception {
  55         this(modalityType, false);
  56     }
  57 
  58     public ToBackDDFTest(boolean modal) throws Exception { this(null, modal); }
  59 
  60     private void createGUI() {
  61 
  62         hiddenFrame = new Frame();
  63         leftDialog = new TestDialog(hiddenFrame);
  64         leftDialog.setLocation(50, 50);
  65         leftDialog.setBackground(Color.BLUE);
  66         leftDialog.setVisible(true);
  67 
  68         dialog = new CustomDialog(leftDialog);
  69 
  70         if (modalityType == null) {
  71             dialog.setModal(setModal);
  72             modalityType = dialog.getModalityType();
  73         } else if (modalityType != null) {
  74             dialog.setModalityType(modalityType);
  75         }
  76 
  77         dialog.setBackground(Color.WHITE);
  78         dialog.setLocation(150, 50);
  79 
  80         rightFrame = new TestFrame();
  81         rightFrame.setLocation(250, 50);
  82         rightFrame.setBackground(Color.RED);
  83 
  84         if (modalityType == Dialog.ModalityType.APPLICATION_MODAL) {
  85             rightFrame.setModalExclusionType(
  86                 Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
  87         } else if (modalityType == Dialog.ModalityType.TOOLKIT_MODAL) {
  88             rightFrame.setModalExclusionType(
  89                 Dialog.ModalExclusionType.TOOLKIT_EXCLUDE);
  90         }
  91 
  92         dialog.setVisible(true);
  93     }
  94 
  95     private void checkLeftDialogIsOverlapped(String msg) {
  96 
  97         Point p = leftDialog.getLocationOnScreen();
  98         int x = p.x + (int)(leftDialog.getWidth()  * 0.9);
  99         int y = p.y + (int)(leftDialog.getHeight() * 0.9);
 100         boolean f = robot.getPixelColor(x, y).equals(leftDialog.getBackground());
 101         assertFalse(f, msg);
 102     }
 103 
 104     private void checkRightFrameIsOverlaped(String msg) {
 105 
 106         Point p = rightFrame.getLocationOnScreen();
 107         int x = p.x + (int)(rightFrame.getWidth()  * 0.1);
 108         int y = p.y + (int)(rightFrame.getHeight() * 0.9);
 109         boolean f = robot.getPixelColor(x, y).equals(rightFrame.getBackground());
 110         assertFalse(f, msg);
 111     }
 112 
 113     public void doTest() throws Exception {
 114 
 115         try {
 116             robot.waitForIdle(delay);
 117 
 118             dialog.clickOpenButton(robot);
 119             robot.waitForIdle(delay);
 120 
 121             dialog.clickCloseButton(robot);
 122             robot.waitForIdle(delay);
 123 
 124             EventQueue.invokeAndWait(() -> { dialog.toBack(); });
 125             robot.waitForIdle(delay);
 126 
 127             String type = modalityType.toString().toLowerCase().replace('_', ' ');
 128 
 129             boolean isModeless = (modalityType == Dialog.ModalityType.MODELESS);
 130 
 131             final String msg1;
 132             if (isModeless) {
 133                 msg1 = "The modeless dialog was overlapped by the " +
 134                     "parent dialog after calling toBack method.";
 135             } else {
 136                 msg1 = "The " + type + " dialog was overlapped by the blocked dialog.";
 137             }
 138             EventQueue.invokeAndWait(() -> { checkLeftDialogIsOverlapped(msg1); });
 139 
 140             if (isModeless) {
 141                 EventQueue.invokeAndWait(() -> { dialog.toFront(); });
 142             } else {
 143                 EventQueue.invokeAndWait(() -> { leftDialog.toFront(); });
 144             }
 145             robot.waitForIdle(delay);
 146 
 147             final String msg2 = "The dialog is still behind the right frame after " +
 148                 "calling toFront method for " + (isModeless ? "it." : "its parent.");
 149 
 150             EventQueue.invokeAndWait(() -> { checkRightFrameIsOverlaped(msg2); });
 151 
 152             final String msg3;
 153             if (isModeless) {
 154                 msg3 = "The modeless dialog is still behind the parent dialog.";
 155             } else {
 156                 msg3 = "The " + type + " dialog was overlapped by the blocked " +
 157                 "dialog after calling toFront method for the blocked dialog.";
 158             }
 159             EventQueue.invokeAndWait(() -> { checkLeftDialogIsOverlapped(msg3); });
 160 
 161         } finally {
 162             EventQueue.invokeAndWait(this::closeAll);
 163         }
 164     }
 165 
 166     private void closeAll() {
 167         if (dialog      != null) {      dialog.dispose(); }
 168         if (leftDialog  != null) {  leftDialog.dispose(); }
 169         if (rightFrame  != null) {  rightFrame.dispose(); }
 170         if (hiddenFrame != null) { hiddenFrame.dispose(); }
 171     }
 172 
 173 
 174     class CustomDialog extends TestDialog {
 175 
 176         public CustomDialog(Dialog d) { super(d); }
 177 
 178         @Override
 179         public void doOpenAction() {
 180             if (rightFrame != null) { rightFrame.setVisible(true); }
 181         }
 182     }
 183 }