Path: blob/master/test/jdk/java/awt/Dialog/NestedDialogs/Modal/NestedModalDialogTest.java
41171 views
/*1* Copyright (c) 2011, 2020, 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 8160266 822579026* @key headful27* @summary See <rdar://problem/3429130>: Events: actionPerformed() method not28* called when it is button is clicked (system load related)29* @run main NestedModalDialogTest30*/3132//////////////////////////////////////////////////////////////////////////////33// NestedModalDialogTest.java34// The test launches a parent frame. From this parent frame it launches a modal35// dialog. From the modal dialog it launches a second modal dialog with a text36// field in it and tries to write into the text field. The test succeeds if you37// are successfully able to write into this second Nested Modal Dialog38//////////////////////////////////////////////////////////////////////////////39// classes necessary for this test4041import java.awt.Button;42import java.awt.Component;43import java.awt.Dialog;44import java.awt.Frame;45import java.awt.GridBagLayout;46import java.awt.Panel;47import java.awt.Point;48import java.awt.Rectangle;49import java.awt.Robot;50import java.awt.TextField;51import java.awt.event.ActionEvent;52import java.awt.event.InputEvent;53import java.awt.event.KeyEvent;5455public class NestedModalDialogTest {56private static Frame frame;57private static IntermediateDialog interDiag;58private static TextDialog txtDiag;5960// Global variables so the robot thread can locate things.61private static Button[] robot_button = new Button[2];62private static TextField robot_text = null;63private static Robot robot = null;6465private static void blockTillDisplayed(Component comp) {66Point p = null;67while (p == null) {68try {69p = comp.getLocationOnScreen();70} catch (IllegalStateException e) {71try {72Thread.sleep(500);73} catch (InterruptedException ie) {74}75}76}77}7879private static void clickOnComp(Component comp) {80Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());81robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);82robot.waitForIdle();83robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);84robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);85robot.waitForIdle();86}8788public void testModalDialogs() throws Exception {89try {90robot = new Robot();91robot.setAutoDelay(100);9293// launch first frame with firstButton94frame = new StartFrame();95blockTillDisplayed(frame);96clickOnComp(robot_button[0]);9798// Dialog must be created and onscreen before we proceed.99blockTillDisplayed(interDiag);100clickOnComp(robot_button[1]);101102// Again, the Dialog must be created and onscreen before we proceed.103blockTillDisplayed(robot_text);104clickOnComp(robot_text);105106robot.keyPress(KeyEvent.VK_SHIFT);107robot.keyPress(KeyEvent.VK_H);108robot.keyRelease(KeyEvent.VK_H);109robot.keyRelease(KeyEvent.VK_SHIFT);110robot.waitForIdle();111112robot.keyPress(KeyEvent.VK_E);113robot.keyRelease(KeyEvent.VK_E);114robot.waitForIdle();115116robot.keyPress(KeyEvent.VK_L);117robot.keyRelease(KeyEvent.VK_L);118robot.waitForIdle();119120robot.keyPress(KeyEvent.VK_L);121robot.keyRelease(KeyEvent.VK_L);122robot.waitForIdle();123124robot.keyPress(KeyEvent.VK_O);125robot.keyRelease(KeyEvent.VK_O);126robot.waitForIdle();127} finally {128if (frame != null) {129frame.dispose();130}131if (interDiag != null) {132interDiag.dispose();133}134if (txtDiag != null) {135txtDiag.dispose();136}137}138}139140//////////////////// Start Frame ///////////////////141/**142* Launches the first frame with a button in it143*/144class StartFrame extends Frame {145146/**147* Constructs a new instance.148*/149public StartFrame() {150super("First Frame");151setLayout(new GridBagLayout());152setLocation(375, 200);153setSize(271, 161);154Button but = new Button("Make Intermediate");155but.addActionListener(new java.awt.event.ActionListener() {156public void actionPerformed(ActionEvent e) {157interDiag = new IntermediateDialog(StartFrame.this);158interDiag.setSize(300, 200);159160// may need listener to watch this move.161interDiag.setLocation(getLocationOnScreen());162interDiag.pack();163interDiag.setVisible(true);164}165});166Panel pan = new Panel();167pan.add(but);168add(pan);169setVisible(true);170robot_button[0] = but;171}172}173174///////////////////////////// MODAL DIALOGS /////////////////////////////175/* A Dialog that launches a sub-dialog */176class IntermediateDialog extends Dialog {177178Dialog m_parent;179180public IntermediateDialog(Frame parent) {181super(parent, "Intermediate Modal", true /*Modal*/);182m_parent = this;183Button but = new Button("Make Text");184but.addActionListener(new java.awt.event.ActionListener() {185public void actionPerformed(ActionEvent e) {186txtDiag = new TextDialog(m_parent);187txtDiag.setSize(300, 100);188txtDiag.setVisible(true);189}190});191Panel pan = new Panel();192pan.add(but);193add(pan);194pack();195196// The robot needs to know about us, so set global197robot_button[1] = but;198}199}200201/* A Dialog that just holds a text field */202class TextDialog extends Dialog {203204public TextDialog(Dialog parent) {205super(parent, "Modal Dialog", true /*Modal*/);206TextField txt = new TextField("", 10);207Panel pan = new Panel();208pan.add(txt);209add(pan);210pack();211212// The robot needs to know about us, so set global213robot_text = txt;214}215}216217public static void main(String[] args) throws RuntimeException, Exception {218try {219new NestedModalDialogTest().testModalDialogs();220} catch (Exception e) {221throw new RuntimeException("NestedModalDialogTest object creation "222+ "failed");223}224}225}226227228