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