1 /*
   2  * Copyright (c) 2015, 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  * @key headful
  27  * @summary setAlwaysOnTop doesn't behave correctly in Linux/Solaris under
  28  *          certain scenarios
  29  * @bug 8021961
  30  * @author Semyon Sadetsky
  31  * @run main ChildAlwaysOnTopTest
  32  */
  33 
  34 import javax.swing.*;
  35 import java.awt.*;
  36 
  37 public class ChildAlwaysOnTopTest {
  38 
  39     private static Window win1;
  40     private static Window win2;
  41     private static Point point;
  42 
  43     public static void main(String[] args) throws Exception {
  44         if( Toolkit.getDefaultToolkit().isAlwaysOnTopSupported() ) {
  45 
  46 
  47             test(null);
  48 
  49             Window f = new Frame();
  50             f.setBackground(Color.darkGray);
  51             f.setSize(500, 500);
  52             try {
  53                 test(f);
  54             } finally {
  55                 f.dispose();
  56             }
  57 
  58             f = new Frame();
  59             f.setBackground(Color.darkGray);
  60             f.setSize(500, 500);
  61             f.setVisible(true);
  62             f = new Dialog((Frame)f);
  63             try {
  64                 test(f);
  65             } finally {
  66                 ((Frame)f.getParent()).dispose();
  67             }
  68         }
  69         System.out.println("ok");
  70     }
  71 
  72     public static void test(Window parent) throws Exception {
  73         SwingUtilities.invokeAndWait(new Runnable() {
  74             @Override
  75             public void run() {
  76                 win1 = parent == null ? new JDialog() : new JDialog(parent);
  77                 win1.setName("top");
  78                 win2 = parent == null ? new JDialog() : new JDialog(parent);
  79                 win2.setName("behind");
  80                 win1.setSize(200, 200);
  81                 Panel panel = new Panel();
  82                 panel.setBackground(Color.GREEN);
  83                 win1.add(panel);
  84                 panel = new Panel();
  85                 panel.setBackground(Color.RED);
  86                 win2.add(panel);
  87                 win1.setAlwaysOnTop(true);
  88                 win2.setAlwaysOnTop(false);
  89                 win1.setVisible(true);
  90             }
  91         });
  92 
  93         Robot robot = new Robot();
  94         robot.delay(500);
  95         robot.waitForIdle();
  96 
  97         SwingUtilities.invokeAndWait(new Runnable() {
  98             @Override
  99             public void run() {
 100                 point = win1.getLocationOnScreen();
 101                 win2.setBounds(win1.getBounds());
 102                 win2.setVisible(true);
 103             }
 104         });
 105 
 106         robot.delay(500);
 107         robot.waitForIdle();
 108 
 109         Color color = robot.getPixelColor(point.x + 100, point.y + 100);
 110         if(!color.equals(Color.GREEN)) {
 111             win1.dispose();
 112             win2.dispose();
 113             throw new RuntimeException("alawaysOnTop window is sent back by " +
 114                     "another child window setVisible(). " + color);
 115         }
 116 
 117         SwingUtilities.invokeAndWait(new Runnable() {
 118             @Override
 119             public void run() {
 120                 win2.toFront();
 121                 if (parent != null) {
 122                     parent.setLocation(win1.getLocation());
 123                     parent.toFront();
 124                 }
 125             }
 126         });
 127 
 128         robot.delay(500);
 129         robot.waitForIdle();
 130 
 131         color = robot.getPixelColor(point.x + 100, point.y + 100);
 132         if(!color.equals(Color.GREEN)) {
 133             win1.dispose();
 134             win2.dispose();
 135             throw new RuntimeException("alawaysOnTop window is sent back by " +
 136                     "another child window toFront(). " + color);
 137         }
 138 
 139         SwingUtilities.invokeAndWait(new Runnable() {
 140             @Override
 141             public void run() {
 142                 win1.setAlwaysOnTop(false);
 143                 if (parent != null) {
 144                     parent.setVisible(false);
 145                     parent.setVisible(true);
 146                 }
 147                 win2.toFront();
 148             }
 149         });
 150 
 151         robot.delay(500);
 152         robot.waitForIdle();
 153 
 154         color = robot.getPixelColor(point.x + 100, point.y + 100);
 155         if(!color.equals(Color.RED)) {
 156             throw new RuntimeException("Failed to unset alawaysOnTop " + color);
 157         }
 158 
 159         win1.dispose();
 160         win2.dispose();
 161     }
 162 }