1 /*
   2  * Copyright (c) 2017, 2018, 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 /**
  25  * @test
  26  * @bug 8190230 8196360
  27  * @summary [macosx] Order of overlapping of modal dialogs is wrong
  28  * @key headful
  29  * @run main SiblingChildOrderTest
  30  */
  31 
  32 import java.awt.Color;
  33 import java.awt.Robot;
  34 import javax.swing.JDialog;
  35 import javax.swing.JFrame;
  36 import javax.swing.SwingUtilities;
  37 
  38 public class SiblingChildOrderTest
  39 {
  40     static Color[] colors = new Color[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW};
  41     static int[] x = new int[]{200, 150, 100, 50};
  42     static int[] y = new int[]{200, 150, 100, 50};
  43     static JDialog[] dlgs = new JDialog[4];
  44     private static JFrame frame;
  45 
  46     public static void main(String args[]) throws Exception {
  47         SwingUtilities.invokeAndWait(() -> {
  48             frame = new JFrame("FRAME");
  49             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  50             frame.setUndecorated(true);
  51             frame.setBounds(50,50, 400, 400);
  52             frame.setVisible(true);
  53         });
  54 
  55         for (int i = 0; i < colors.length; i++) {
  56             int finalI = i;
  57             SwingUtilities.invokeLater(() -> {
  58                 dlgs[finalI] = new JDialog(frame, "DLG " + finalI, true);
  59                 dlgs[finalI].getContentPane().setBackground(colors[finalI]);
  60                 dlgs[finalI].setBounds(x[finalI], y[finalI], 200, 200);
  61                 dlgs[finalI].setUndecorated(true);
  62                 dlgs[finalI].setVisible(true);
  63             });
  64         }
  65 
  66         Robot robot = new Robot();
  67         robot.waitForIdle();
  68         robot.delay(1000);
  69 
  70         for (int i = 0; i < colors.length; i++) {
  71             Color c = robot.getPixelColor(x[i] + 190, y[i] + 190);
  72         if (!compareColor(colors[i], c, 5)) {
  73                 throw new RuntimeException("Expected " + colors[i] + " got " + c);
  74             }
  75         }
  76 
  77         for (int i = 0; i < colors.length; i++) {
  78             SwingUtilities.invokeLater(dlgs[i]::dispose);
  79         }
  80         SwingUtilities.invokeLater(frame::dispose);
  81     }
  82 
  83     private static boolean compareColor(Color c1, Color c2, int threshold) {
  84         if (c1.equals(c2)) {
  85             return true;
  86         } else {
  87             return ((Math.abs(c1.getRed() - c2.getRed()) < threshold ) &&
  88                     (Math.abs(c1.getGreen() - c2.getGreen()) < threshold ) &&
  89                     (Math.abs(c1.getBlue() - c2.getBlue()) < threshold ));
  90         }
  91     }
  92 }