1 /*
   2  * Copyright (c) 2012, 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 7154177
  27   @summary An invisible owner frame should never become visible
  28   @author anthony.petrov@oracle.com: area=awt.toplevel
  29   @library ../../regtesthelpers
  30   @build Util
  31   @run main InvisibleOwner
  32 */
  33 
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 import java.util.*;
  37 import test.java.awt.regtesthelpers.Util;
  38 
  39 public class InvisibleOwner {
  40     private static volatile boolean invisibleOwnerClicked = false;
  41     private static volatile boolean backgroundClicked = false;
  42 
  43     private static final int F_X = 40, F_Y = 40, F_W = 200, F_H = 200;
  44 
  45     public static void main(String[] args) throws AWTException {
  46         // A background frame to compare a pixel color against
  47         Frame helperFrame = new Frame("Background frame");
  48         helperFrame.setBackground(Color.BLUE);
  49         helperFrame.setBounds(F_X - 10, F_Y - 10, F_W + 20, F_H + 20);
  50         helperFrame.addMouseListener(new MouseAdapter() {
  51             @Override
  52             public void mouseClicked(MouseEvent ev) {
  53                 backgroundClicked= true;
  54             }
  55         });
  56         helperFrame.setVisible(true);
  57 
  58         // An owner frame that should stay invisible
  59         Frame frame = new Frame("Invisible Frame");
  60         frame.setBackground(Color.GREEN);
  61         frame.setLocation(F_X, F_Y);
  62         frame.setSize(F_W, F_H);
  63         frame.addMouseListener(new MouseAdapter() {
  64             @Override
  65             public void mouseClicked(MouseEvent ev) {
  66                 invisibleOwnerClicked = true;
  67             }
  68         });
  69 
  70         // An owned window
  71         final Window window = new Window(frame);
  72         window.setBackground(Color.RED);
  73         window.setSize(200, 200);
  74         window.setLocation(300, 300);
  75         window.setVisible(true);
  76         try { Thread.sleep(1000); } catch (Exception ex) {}
  77 
  78         Robot robot = new Robot();
  79 
  80         // Clicking the owned window shouldn't make its owner visible
  81         Util.clickOnComp(window, robot);
  82         try { Thread.sleep(500); } catch (Exception ex) {}
  83 
  84 
  85         // Assume the location and size are applied to the frame as expected.
  86         // This should work fine on the Mac. We can't call getLocationOnScreen()
  87         // since from Java perspective the frame is invisible anyway.
  88 
  89         // 1. Check the color at the center of the owner frame
  90         Color c = robot.getPixelColor(F_X + F_W / 2, F_Y + F_H / 2);
  91         System.err.println("Pixel color: " + c);
  92         if (c == null) {
  93             throw new RuntimeException("Robot.getPixelColor() failed");
  94         }
  95         if (c.equals(frame.getBackground())) {
  96             throw new RuntimeException("The invisible frame has become visible");
  97         }
  98         if (!c.equals(helperFrame.getBackground())) {
  99             throw new RuntimeException("The background helper frame has been covered by something unexpected");
 100         }
 101 
 102         // 2. Try to click it
 103         robot.mouseMove(F_X + F_W / 2, F_Y + F_H / 2);
 104         robot.mousePress(InputEvent.BUTTON1_MASK);
 105         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 106         try { Thread.sleep(500); } catch (Exception ex) {}
 107 
 108         // Cleanup
 109         window.dispose();
 110         frame.dispose();
 111         helperFrame.dispose();
 112 
 113         // Final checks
 114         if (invisibleOwnerClicked) {
 115             throw new RuntimeException("An invisible owner frame got clicked. Looks like it became visible.");
 116         }
 117         if (!backgroundClicked) {
 118             throw new RuntimeException("The background helper frame hasn't been clciked");
 119         }
 120     }
 121 }