Path: blob/master/test/jdk/java/awt/Frame/InvisibleOwner/InvisibleOwner.java
41153 views
/*1* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24@test25@key headful26@bug 715417727@summary An invisible owner frame should never become visible28@author [email protected]: area=awt.toplevel29@library ../../regtesthelpers30@build Util31@run main InvisibleOwner32*/3334import java.awt.*;35import java.awt.event.*;36import java.util.*;37import test.java.awt.regtesthelpers.Util;3839public class InvisibleOwner {40private static volatile boolean invisibleOwnerClicked = false;41private static volatile boolean backgroundClicked = false;4243private static final int F_X = 40, F_Y = 40, F_W = 200, F_H = 200;4445public static void main(String[] args) throws AWTException {46// A background frame to compare a pixel color against47Frame helperFrame = new Frame("Background frame");48helperFrame.setBackground(Color.BLUE);49helperFrame.setBounds(F_X - 10, F_Y - 10, F_W + 20, F_H + 20);50helperFrame.addMouseListener(new MouseAdapter() {51@Override52public void mouseClicked(MouseEvent ev) {53backgroundClicked= true;54}55});56helperFrame.setVisible(true);5758// An owner frame that should stay invisible59Frame frame = new Frame("Invisible Frame");60frame.setBackground(Color.GREEN);61frame.setLocation(F_X, F_Y);62frame.setSize(F_W, F_H);63frame.addMouseListener(new MouseAdapter() {64@Override65public void mouseClicked(MouseEvent ev) {66invisibleOwnerClicked = true;67}68});6970// An owned window71final Window window = new Window(frame);72window.setBackground(Color.RED);73window.setSize(200, 200);74window.setLocation(300, 300);75window.setVisible(true);76try { Thread.sleep(1000); } catch (Exception ex) {}7778Robot robot = new Robot();7980// Clicking the owned window shouldn't make its owner visible81Util.clickOnComp(window, robot);82try { Thread.sleep(500); } catch (Exception ex) {}838485// 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.8889// 1. Check the color at the center of the owner frame90Color c = robot.getPixelColor(F_X + F_W / 2, F_Y + F_H / 2);91System.err.println("Pixel color: " + c);92if (c == null) {93throw new RuntimeException("Robot.getPixelColor() failed");94}95if (c.equals(frame.getBackground())) {96throw new RuntimeException("The invisible frame has become visible");97}98if (!c.equals(helperFrame.getBackground())) {99throw new RuntimeException("The background helper frame has been covered by something unexpected");100}101102// 2. Try to click it103robot.mouseMove(F_X + F_W / 2, F_Y + F_H / 2);104robot.mousePress(InputEvent.BUTTON1_MASK);105robot.mouseRelease(InputEvent.BUTTON1_MASK);106try { Thread.sleep(500); } catch (Exception ex) {}107108// Cleanup109window.dispose();110frame.dispose();111helperFrame.dispose();112113// Final checks114if (invisibleOwnerClicked) {115throw new RuntimeException("An invisible owner frame got clicked. Looks like it became visible.");116}117if (!backgroundClicked) {118throw new RuntimeException("The background helper frame hasn't been clciked");119}120}121}122123124