Path: blob/master/test/jdk/java/awt/Dialog/NestedDialogs/Modeless/NestedModelessDialogTest.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 NestedModelessDialogTest30*/3132/////////////////////////////////////////////////////////////////////////////33// NestedModelessDialogTest.java34// The test launches a parent frame. From this parent frame it launches a modal35// dialog. From the modal dialog it launches a modeless 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 Nested Modeless 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 NestedModelessDialogTest {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;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}8788/**89* Get called by test harness90*91* @throws Exception92*/93public void testModelessDialogs() throws Exception {94try {95robot = new Robot();96robot.setAutoDelay(100);9798// launch first frame with fistButton99frame = new StartFrame();100robot.waitForIdle();101blockTillDisplayed(frame);102clickOnComp(robot_button[0]);103104// Dialog must be created and onscreen before we proceed.105blockTillDisplayed(interDiag);106clickOnComp(robot_button[1]);107108// Again, the Dialog must be created and onscreen before we proceed.109blockTillDisplayed(robot_text);110clickOnComp(robot_text);111112robot.keyPress(KeyEvent.VK_SHIFT);113robot.keyPress(KeyEvent.VK_H);114robot.keyRelease(KeyEvent.VK_H);115robot.keyRelease(KeyEvent.VK_SHIFT);116robot.waitForIdle();117118robot.keyPress(KeyEvent.VK_E);119robot.keyRelease(KeyEvent.VK_E);120robot.waitForIdle();121122robot.keyPress(KeyEvent.VK_L);123robot.keyRelease(KeyEvent.VK_L);124robot.waitForIdle();125126robot.keyPress(KeyEvent.VK_L);127robot.keyRelease(KeyEvent.VK_L);128robot.waitForIdle();129130robot.keyPress(KeyEvent.VK_O);131robot.keyRelease(KeyEvent.VK_O);132robot.waitForIdle();133} finally {134if (frame != null) {135frame.dispose();136}137if (interDiag != null) {138interDiag.dispose();139}140if (txtDiag != null) {141txtDiag.dispose();142}143}144}145146//////////////////// Start Frame ///////////////////147/**148* Launches the first frame with a button in it149*/150class StartFrame extends Frame {151152/**153* Constructs a new instance.154*/155public StartFrame() {156super("First Frame");157setLayout(new GridBagLayout());158setLocation(375, 200);159setSize(271, 161);160Button but = new Button("Make Intermediate");161but.addActionListener(new java.awt.event.ActionListener() {162public void actionPerformed(ActionEvent e) {163interDiag = new IntermediateDialog(StartFrame.this);164interDiag.setSize(300, 200);165166// may need listener to watch this move.167interDiag.setLocation(getLocationOnScreen());168interDiag.pack();169interDiag.setVisible(true);170}171});172Panel pan = new Panel();173pan.add(but);174add(pan);175setVisible(true);176robot_button[0] = but;177}178}179180///////////////////////////// VARIOUS DIALOGS //////////////////////////181/* A Dialog that launches a sub-dialog */182class IntermediateDialog extends Dialog {183184Dialog m_parent;185186public IntermediateDialog(Frame parent) {187super(parent, "Intermediate Modal", true /*Modal*/);188m_parent = this;189Button but = new Button("Make Text");190but.addActionListener(new java.awt.event.ActionListener() {191public void actionPerformed(ActionEvent e) {192txtDiag = new TextDialog(m_parent);193txtDiag.setSize(300, 100);194txtDiag.setVisible(true);195}196});197Panel pan = new Panel();198pan.add(but);199add(pan);200pack();201202// The robot needs to know about us, so set global203robot_button[1] = but;204}205}206207/* A Dialog that just holds a text field */208class TextDialog extends Dialog {209210public TextDialog(Dialog parent) {211super(parent, "Modeless Dialog", false /*Modeless*/);212TextField txt = new TextField("", 10);213Panel pan = new Panel();214pan.add(txt);215add(pan);216pack();217218// The robot needs to know about us, so set global219robot_text = txt;220}221}222223public static void main(String[] args) throws RuntimeException {224try {225new NestedModelessDialogTest().testModelessDialogs();226} catch (Exception e) {227throw new RuntimeException("NestedModelessDialogTest object "228+ "creation failed");229}230}231}232233234