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