1 /*
   2  * Copyright (c) 2013, 2016, 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.Color;
  25 import java.awt.Dialog;
  26 import java.awt.Frame;
  27 import java.awt.Rectangle;
  28 import java.awt.Robot;
  29 import java.awt.Toolkit;
  30 import java.awt.event.InputEvent;
  31 
  32 /**
  33  * @test
  34  * @key headful
  35  * @bug 8008728
  36  * @summary [macosx] Swing. JDialog. Modal dialog goes to background
  37  * @author Alexandr Scherbatiy
  38  * @library ../../../../lib/testlibrary
  39  * @build ExtendedRobot
  40  * @run main ModalDialogOrderingTest
  41  */
  42 public class ModalDialogOrderingTest {
  43 
  44     private static final Color DIALOG_COLOR = Color.GREEN;
  45     private static final Color FRAME_COLOR = Color.BLUE;
  46 
  47     public static void main(String[] args) {
  48 
  49         final Frame frame = new Frame("Test");
  50         frame.setSize(400, 400);
  51         frame.setBackground(FRAME_COLOR);
  52         frame.setVisible(true);
  53 
  54         final Dialog modalDialog = new Dialog(null, true);
  55         modalDialog.setTitle("Modal Dialog");
  56         modalDialog.setSize(400, 200);
  57         modalDialog.setBackground(DIALOG_COLOR);
  58         modalDialog.setModal(true);
  59 
  60         new Thread(new Runnable() {
  61 
  62             @Override
  63             public void run() {
  64                 runTest(modalDialog, frame);
  65             }
  66         }).start();
  67 
  68         modalDialog.setVisible(true);
  69     }
  70 
  71     private static void runTest(Dialog dialog, Frame frame) {
  72         try {
  73             ExtendedRobot robot = new ExtendedRobot();
  74             robot.setAutoDelay(50);
  75             robot.mouseMove(300, 300);
  76 
  77             while (!dialog.isVisible()) {
  78                 robot.waitForIdle(1000);
  79             }
  80 
  81             Rectangle dialogBounds = dialog.getBounds();
  82             Rectangle frameBounds = frame.getBounds();
  83 
  84             int y1 = dialogBounds.y + dialogBounds.height;
  85             int y2 = frameBounds.y + frameBounds.height;
  86 
  87             int clickX = frameBounds.x + frameBounds.width / 2;
  88             int clickY = y1 + (y2 - y1) / 2;
  89 
  90             robot.mouseMove(clickX, clickY);
  91             robot.mousePress(InputEvent.BUTTON1_MASK);
  92             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  93             robot.waitForIdle(1000);
  94 
  95             int colorX = dialogBounds.x + dialogBounds.width / 2;
  96             int colorY = dialogBounds.y + dialogBounds.height / 2;
  97 
  98             Color color = robot.getPixelColor(colorX, colorY);
  99 
 100 
 101             if (!DIALOG_COLOR.equals(color)) {
 102                 throw new RuntimeException("The frame is on top"
 103                         + " of the modal dialog!");
 104             }else{
 105                 frame.dispose();
 106                 dialog.dispose();
 107             }
 108         } catch (Exception ex) {
 109             throw new RuntimeException(ex);
 110         }
 111     }
 112 }