Path: blob/master/test/jdk/java/awt/Focus/DisposedWindow/DisposeDialogNotActivateOwnerTest/DisposeDialogNotActivateOwnerTest.java
41154 views
/*1* Copyright (c) 2006, 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 6386592 816076627@summary Tests that disposing a dialog doesn't activate its invisible owner.28*/2930import java.awt.AWTException;31import java.awt.Button;32import java.awt.Component;33import java.awt.Dialog;34import java.awt.Dimension;35import java.awt.Frame;36import java.awt.Point;37import java.awt.Robot;38import java.awt.event.FocusAdapter;39import java.awt.event.FocusEvent;40import java.awt.event.InputEvent;4142public class DisposeDialogNotActivateOwnerTest {43Robot robot;44Frame frame;45Frame dialogInvisibleOwner;46Dialog dialog;47Button frameButton;48static volatile boolean buttonReceivedFocus = false;4950public static void main(String[] args) {51DisposeDialogNotActivateOwnerTest test =52new DisposeDialogNotActivateOwnerTest();53test.performTest();54test.dispose();55}5657public DisposeDialogNotActivateOwnerTest() {58try {59robot = new Robot();60} catch (AWTException e) {61throw new RuntimeException("Error: unable to create robot");62}6364robot.setAutoDelay(200);65dialogInvisibleOwner = new Frame("Dialog Invisible Owner Frame");66dialog = new Dialog(dialogInvisibleOwner, "Owned Dialog");6768frame = new Frame("A Frame");69frameButton = new Button("button");70frameButton.addFocusListener(new FocusAdapter() {71public void focusGained(FocusEvent e) {72buttonReceivedFocus = true;73}74});75frame.setBounds(0, 0, 400, 200);76frame.add(frameButton);77dialog.setBounds(100, 50, 200, 100);78}7980public void performTest() {81frame.setVisible(true);82robot.delay(200);83robot.waitForIdle();84clickOnTitle(frame);85robot.delay(200);86robot.waitForIdle();87robot.delay(200);88if (!frame.isFocused()) {89dispose();90throw new RuntimeException("Error: frame didn't get initial focus");91}9293dialog.setVisible(true);94robot.delay(200);95robot.waitForIdle();96robot.delay(200);97if (!dialog.isFocused()) {98dispose();99throw new RuntimeException("Error: dialog didn't get initial focus");100}101102dialog.dispose();103robot.waitForIdle();104robot.delay(200);105if (!buttonReceivedFocus) {106dispose();107throw new RuntimeException(108"Test failed: Dialog activates invisible owner when disposed!");109}110}111112public void dispose() {113frame.dispose();114dialog.dispose();115dialogInvisibleOwner.dispose();116}117118void clickOnTitle(Component c) {119Point p = c.getLocationOnScreen();120Dimension d = c.getSize();121robot.mouseMove(p.x + (int)(d.getWidth() / 2),122p.y + ((Frame)c).getInsets().top / 2);123robot.mousePress(InputEvent.BUTTON1_MASK);124robot.mouseRelease(InputEvent.BUTTON1_MASK);125}126}127128129