1 /*
   2  * Copyright (c) 2010, 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   @key headful
  27   @bug 6829546, 8197808
  28   @summary tests that an always-on-top modal dialog doesn't make any windows always-on-top
  29   @author artem.ananiev: area=awt.modal
  30   @library ../../regtesthelpers
  31   @build Util
  32   @run main MakeWindowAlwaysOnTop
  33 */
  34 
  35 import java.awt.Frame;
  36 import java.awt.Dialog;
  37 import java.awt.EventQueue;
  38 import java.awt.Color;
  39 import java.awt.Robot;
  40 import java.awt.Point;
  41 import java.awt.event.InputEvent;
  42 import test.java.awt.regtesthelpers.Util;
  43 
  44 public class MakeWindowAlwaysOnTop
  45 {
  46     private static Frame f;
  47     private static Dialog d;
  48 
  49     public static void main(String[] args) throws Exception
  50     {
  51         Robot r = Util.createRobot();
  52         Util.waitForIdle(r);
  53 
  54         // Frame
  55         f = new Frame("Test frame");
  56         f.setBounds(100, 100, 400, 300);
  57         f.setBackground(Color.RED);
  58         f.setVisible(true);
  59         r.delay(100);
  60         Util.waitForIdle(r);
  61 
  62         // Dialog
  63         d = new Dialog(null, "Modal dialog", Dialog.ModalityType.APPLICATION_MODAL);
  64         d.setBounds(500, 500, 160, 160);
  65         d.setAlwaysOnTop(true);
  66         EventQueue.invokeLater(() ->  d.setVisible(true) );
  67         // Wait until the dialog is shown
  68         EventQueue.invokeAndWait(() -> { /* Empty */ });
  69         r.delay(100);
  70         Util.waitForIdle(r);
  71 
  72         // Click on the frame to trigger modality
  73         Point p = f.getLocationOnScreen();
  74         r.mouseMove(p.x + f.getWidth() / 2, p.y + f.getHeight() / 2);
  75         Util.waitForIdle(r);
  76         r.mousePress(InputEvent.BUTTON1_MASK);
  77         Util.waitForIdle(r);
  78         r.mouseRelease(InputEvent.BUTTON1_MASK);
  79         Util.waitForIdle(r);
  80 
  81         r.delay(100);
  82         Util.waitForIdle(r);
  83 
  84         // Dispose dialog
  85         d.dispose();
  86         r.delay(100);
  87         Util.waitForIdle(r);
  88 
  89         // Show another frame at the same location
  90         Frame t = new Frame("Check");
  91         t.setBounds(100, 100, 400, 300);
  92         t.setBackground(Color.BLUE);
  93         t.setVisible(true);
  94         r.delay(100);
  95         Util.waitForIdle(r);
  96 
  97         // Bring it above the first frame
  98         t.toFront();
  99 
 100         r.delay(200);
 101         Util.waitForIdle(r);
 102 
 103 
 104         Color c = r.getPixelColor(p.x + f.getWidth() / 2, p.y + f.getHeight() / 2);
 105         System.out.println("Color = " + c);
 106 
 107         String exceptionMessage = null;
 108         // If the color is RED, then the first frame is now always-on-top
 109         if (Color.RED.equals(c)) {
 110             exceptionMessage = "Test FAILED: the frame is always-on-top";
 111         } else if (!Color.BLUE.equals(c)) {
 112             exceptionMessage = "Test FAILED: unknown window is on top of the frame";
 113         }
 114 
 115         // Dispose all the windows
 116         t.dispose();
 117         f.dispose();
 118 
 119         if (exceptionMessage != null) {
 120             throw new RuntimeException(exceptionMessage);
 121         } else {
 122             System.out.println("Test PASSED");
 123         }
 124     }
 125 }