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