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