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