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 import sun.awt.SunToolkit;
  33 
  34 /**
  35  * @test
  36  * @key headful
  37  * @bug 8008728
  38  * @summary [macosx] Swing. JDialog. Modal dialog goes to background
  39  * @author Alexandr Scherbatiy
  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             Robot robot = new Robot();
  74             robot.setAutoDelay(50);
  75             robot.mouseMove(300, 300);
  76 
  77             while (!dialog.isVisible()) {
  78                 sleep();
  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             sleep();
  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             dialog.dispose();
 101             frame.dispose();
 102 
 103             if (!DIALOG_COLOR.equals(color)) {
 104                 throw new RuntimeException("The frame is on top"
 105                         + " of the modal dialog!");
 106             }
 107         } catch (Exception ex) {
 108             throw new RuntimeException(ex);
 109         }
 110     }
 111 
 112     private static void sleep() {
 113         try {
 114             Thread.sleep(1000);
 115         } catch (InterruptedException ignored) {
 116         }
 117         ((SunToolkit) Toolkit.getDefaultToolkit()).realSync();
 118     }
 119 }