Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Frame/InvisibleOwner/InvisibleOwner.java
38828 views
/*1* Copyright (c) 2012, 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@bug 715417726@summary An invisible owner frame should never become visible27@author [email protected]: area=awt.toplevel28@library ../../regtesthelpers29@build Util30@run main InvisibleOwner31*/3233import java.awt.*;34import java.awt.event.*;35import java.util.*;36import test.java.awt.regtesthelpers.Util;3738public class InvisibleOwner {39private static volatile boolean invisibleOwnerClicked = false;40private static volatile boolean backgroundClicked = false;4142private static final int F_X = 40, F_Y = 40, F_W = 200, F_H = 200;4344public static void main(String[] args) throws AWTException {45// A background frame to compare a pixel color against46Frame helperFrame = new Frame("Background frame");47helperFrame.setBackground(Color.BLUE);48helperFrame.setBounds(F_X - 10, F_Y - 10, F_W + 20, F_H + 20);49helperFrame.addMouseListener(new MouseAdapter() {50@Override51public void mouseClicked(MouseEvent ev) {52backgroundClicked= true;53}54});55helperFrame.setVisible(true);5657// An owner frame that should stay invisible58Frame frame = new Frame("Invisible Frame");59frame.setBackground(Color.GREEN);60frame.setLocation(F_X, F_Y);61frame.setSize(F_W, F_H);62frame.addMouseListener(new MouseAdapter() {63@Override64public void mouseClicked(MouseEvent ev) {65invisibleOwnerClicked = true;66}67});6869// An owned window70final Window window = new Window(frame);71window.setBackground(Color.RED);72window.setSize(200, 200);73window.setLocation(300, 300);74window.setVisible(true);75try { Thread.sleep(1000); } catch (Exception ex) {}7677Robot robot = new Robot();7879// Clicking the owned window shouldn't make its owner visible80Util.clickOnComp(window, robot);81try { Thread.sleep(500); } catch (Exception ex) {}828384// Assume the location and size are applied to the frame as expected.85// This should work fine on the Mac. We can't call getLocationOnScreen()86// since from Java perspective the frame is invisible anyway.8788// 1. Check the color at the center of the owner frame89Color c = robot.getPixelColor(F_X + F_W / 2, F_Y + F_H / 2);90System.err.println("Pixel color: " + c);91if (c == null) {92throw new RuntimeException("Robot.getPixelColor() failed");93}94if (c.equals(frame.getBackground())) {95throw new RuntimeException("The invisible frame has become visible");96}97if (!c.equals(helperFrame.getBackground())) {98throw new RuntimeException("The background helper frame has been covered by something unexpected");99}100101// 2. Try to click it102robot.mouseMove(F_X + F_W / 2, F_Y + F_H / 2);103robot.mousePress(InputEvent.BUTTON1_MASK);104robot.mouseRelease(InputEvent.BUTTON1_MASK);105try { Thread.sleep(500); } catch (Exception ex) {}106107// Cleanup108window.dispose();109frame.dispose();110helperFrame.dispose();111112// Final checks113if (invisibleOwnerClicked) {114throw new RuntimeException("An invisible owner frame got clicked. Looks like it became visible.");115}116if (!backgroundClicked) {117throw new RuntimeException("The background helper frame hasn't been clciked");118}119}120}121122123