1 /*
   2  * Copyright (c) 2013, 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  * @bug 8008728
  35  * @summary [macosx] Swing. JDialog. Modal dialog goes to background
  36  * @author Alexandr Scherbatiy
  37  * @library ../../../../lib/testlibrary
  38  * @build ExtendedRobot
  39  * @run main ModalDialogOrderingTest
  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(400, 400);
  50         frame.setBackground(FRAME_COLOR);
  51         frame.setVisible(true);
  52 
  53         final Dialog modalDialog = new Dialog(null, true);
  54         modalDialog.setTitle("Modal Dialog");
  55         modalDialog.setSize(400, 200);
  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             ExtendedRobot robot = new ExtendedRobot();
  73             robot.setAutoDelay(50);
  74             robot.mouseMove(300, 300);
  75 
  76             while (!dialog.isVisible()) {
  77                 robot.waitForIdle(1000);
  78             }
  79 
  80             Rectangle dialogBounds = dialog.getBounds();
  81             Rectangle frameBounds = frame.getBounds();
  82 
  83             int y1 = dialogBounds.y + dialogBounds.height;
  84             int y2 = frameBounds.y + frameBounds.height;
  85 
  86             int clickX = frameBounds.x + frameBounds.width / 2;
  87             int clickY = y1 + (y2 - y1) / 2;
  88 
  89             robot.mouseMove(clickX, clickY);
  90             robot.mousePress(InputEvent.BUTTON1_MASK);
  91             robot.mouseRelease(InputEvent.BUTTON1_MASK);
  92             robot.waitForIdle(1000);
  93 
  94             int colorX = dialogBounds.x + dialogBounds.width / 2;
  95             int colorY = dialogBounds.y + dialogBounds.height / 2;
  96 
  97             Color color = robot.getPixelColor(colorX, colorY);
  98 
  99 
 100             if (!DIALOG_COLOR.equals(color)) {
 101                 throw new RuntimeException("The frame is on top"
 102                         + " of the modal dialog!");
 103             }else{
 104                 frame.dispose();
 105                 dialog.dispose();
 106             }
 107         } catch (Exception ex) {
 108             throw new RuntimeException(ex);
 109         }
 110     }
 111 }