1 /*
   2  * Copyright (c) 2011, 2016 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24  /*
  25  @test 8155740
  26  @summary See <rdar://problem/3429130>: Events: actionPerformed() method not
  27           called when it is button is clicked (system load related)
  28  @summary com.apple.junit.java.awt.Frame
  29  @library ../../../regtesthelpers
  30  @build VisibilityValidator
  31  @build RobotUtilities
  32  @build Waypoint
  33  @run main NestedModalDialogTest
  34  */
  35 //////////////////////////////////////////////////////////////////////////////
  36 //  NestedModalDialogTest.java
  37 // The test launches a parent frame. From this parent frame it launches a modal
  38 // dialog. From the modal dialog it launches a second modal dialog with a text
  39 // field in it and tries to write into the text field. The test succeeds if you
  40 // are successfully able to write into this second Nested Modal Dialog
  41 //////////////////////////////////////////////////////////////////////////////
  42 // classes necessary for this test
  43 
  44 import java.awt.*;
  45 import java.awt.event.*;
  46 import java.util.Enumeration;
  47 
  48 import test.java.awt.regtesthelpers.Waypoint;
  49 import test.java.awt.regtesthelpers.VisibilityValidator;
  50 import test.java.awt.regtesthelpers.RobotUtilities;
  51 
  52 public class NestedModalDialogTest {
  53 
  54     Waypoint[] event_checkpoint = new Waypoint[3];
  55     VisibilityValidator[] win_checkpoint = new VisibilityValidator[2];
  56 
  57     IntermediateDialog interDiag;
  58     TextDialog txtDiag;
  59 
  60     // Global variables so the robot thread can locate things.
  61     Button[] robot_button = new Button[2];
  62     TextField robot_text = null;
  63 
  64     /*
  65      * @throws InterruptedException
  66      * @throws WaypointException
  67      */
  68     public void testModalDialogs() throws Exception {
  69         Frame frame = null;
  70         String result = "";
  71 
  72         event_checkpoint[0] = new Waypoint(); // "-Launch 1-"
  73         event_checkpoint[1] = new Waypoint(); // "-Launch 2-"
  74 
  75         // Thread.currentThread().setName("NestedModalDialogTest Thread");
  76         // launch first frame with firstButton
  77         frame = new StartFrame();
  78         VisibilityValidator.setVisibleAndConfirm(frame);
  79         RobotUtilities.click(robot_button[0]);
  80 
  81         // Dialog must be created and onscreen before we proceed.
  82         //   The event_checkpoint waits for the Dialog to be created.
  83         //   The win_checkpoint waits for the Dialog to be visible.
  84         event_checkpoint[0].requireClear("TestFrame actionPerformed() never "
  85                 + "called, see <rdar://problem/3429130>");
  86         win_checkpoint[0].requireVisible();
  87         RobotUtilities.click(robot_button[1]);
  88 
  89         // Again, the Dialog must be created and onscreen before we proceed.
  90         //   The event_checkpoint waits for the Dialog to be created.
  91         //   The win_checkpoint waits for the Dialog to be visible.
  92         event_checkpoint[1].requireClear("IntermediateDialog actionPerformed() "
  93                 + "never called, see <rdar://problem/3429130>");
  94         win_checkpoint[1].requireVisible();
  95         RobotUtilities.click(robot_text);
  96 
  97         // I'm really not sure whether the click is needed for focus
  98         // but since it's asynchronous, as is the actually gaining of focus
  99         // we might as well do our best
 100         try {
 101             EventQueue.invokeAndWait(new Runnable() {
 102                 public void run() {
 103                 }
 104             });
 105         } catch (Exception e) {
 106         }
 107 
 108         RobotUtilities.pressKey(KeyEvent.VK_SHIFT);
 109         RobotUtilities.typeKey(KeyEvent.VK_H);
 110         RobotUtilities.releaseKey(KeyEvent.VK_SHIFT);
 111         RobotUtilities.typeKey(KeyEvent.VK_E);
 112         RobotUtilities.typeKey(KeyEvent.VK_L);
 113         RobotUtilities.typeKey(KeyEvent.VK_L);
 114         RobotUtilities.typeKey(KeyEvent.VK_O);
 115 
 116         //
 117         // NOTE THAT WE MAY HAVE MORE SYNCHRONIZATION WORK TO DO HERE.
 118         // CURRENTLY THERE IS NO GUARANTEE THAT THE KEYEVENT THAT THAT
 119         // TYPES THE 'O' HAS BEEN PROCESSED BEFORE WE GET THE RESULT
 120         //
 121         // This is a (lame) attempt at waiting for the last typeKey events to
 122         // propagate. It's not quite right because robot uses
 123         // CGRemoteOperations, which are asynchronous. But that's why I put in
 124         // the pause
 125         try {
 126             EventQueue.invokeAndWait(new Runnable() {
 127                 public void run() {
 128                 }
 129             });
 130         } catch (Exception e) {
 131         }
 132 
 133         // Need to call this before the dialog that robot_text is in is disposed
 134         result = robot_text.getText();
 135 
 136         Thread.sleep(50); // shouldn't need this, but pause adds stability
 137         // Click Close box of modal dialog with textField
 138         RobotUtilities.clickAt(txtDiag, 14, 10);
 139 
 140         Thread.sleep(50); // shouldn't need this, but pause adds stability
 141         // Click Close box of intermediate modal dialog
 142         RobotUtilities.clickAt(interDiag, 14, 10);
 143 
 144         Thread.sleep(50); // shouldn't need this, but pause adds stability
 145         // Click Close box of intermediate modal dialog
 146         RobotUtilities.clickAt(frame, 14, 10);
 147 
 148         String expected = "Hello";
 149     }
 150 
 151     //////////////////// Start Frame ///////////////////
 152     /**
 153      * Launches the first frame with a button in it
 154      */
 155     class StartFrame extends Frame {
 156 
 157         /**
 158          * Constructs a new instance.
 159          */
 160         public StartFrame() {
 161             super("First Frame");
 162             setLayout(new GridBagLayout());
 163             setLocation(375, 200);
 164             setSize(271, 161);
 165             Button but = new Button("Make Intermediate");
 166             but.addActionListener(new java.awt.event.ActionListener() {
 167                 public void actionPerformed(ActionEvent e) {
 168                     interDiag = new IntermediateDialog(StartFrame.this);
 169                     win_checkpoint[0] = new VisibilityValidator(interDiag);
 170                     interDiag.setSize(300, 200);
 171 
 172                     // may need listener to watch this move.
 173                     interDiag.setLocation(getLocationOnScreen());
 174                     interDiag.pack();
 175                     event_checkpoint[0].clear();
 176                     interDiag.setVisible(true);
 177                 }
 178             });
 179             Panel pan = new Panel();
 180             pan.add(but);
 181             add(pan);
 182             robot_button[0] = but;
 183             addWindowListener(new WindowAdapter() {
 184                 public void windowClosing(WindowEvent e) {
 185                     setVisible(false);
 186                     dispose();
 187                 }
 188             });
 189         }
 190     }
 191 
 192     ///////////////////////////// MODAL DIALOGS /////////////////////////////
 193     /* A Dialog that launches a sub-dialog */
 194     class IntermediateDialog extends Dialog {
 195 
 196         Dialog m_parent;
 197 
 198         public IntermediateDialog(Frame parent) {
 199             super(parent, "Intermediate Modal", true /*Modal*/);
 200             m_parent = this;
 201             Button but = new Button("Make Text");
 202             but.addActionListener(new java.awt.event.ActionListener() {
 203                 public void actionPerformed(ActionEvent e) {
 204                     txtDiag = new TextDialog(m_parent);
 205                     win_checkpoint[1] = new VisibilityValidator(txtDiag);
 206                     txtDiag.setSize(300, 100);
 207                     event_checkpoint[1].clear();
 208                     txtDiag.setVisible(true);
 209                 }
 210             });
 211             Panel pan = new Panel();
 212             pan.add(but);
 213             add(pan);
 214             pack();
 215             addWindowListener(new WindowAdapter() {
 216                 public void windowClosing(WindowEvent e) {
 217                     setVisible(false);
 218                     dispose();
 219                 }
 220             });
 221 
 222             // The robot needs to know about us, so set global
 223             robot_button[1] = but;
 224         }
 225     }
 226 
 227     /* A Dialog that just holds a text field */
 228     class TextDialog extends Dialog {
 229 
 230         public TextDialog(Dialog parent) {
 231             super(parent, "Modal Dialog", true /*Modal*/);
 232             TextField txt = new TextField("", 10);
 233             Panel pan = new Panel();
 234             pan.add(txt);
 235             add(pan);
 236             pack();
 237             addWindowListener(new WindowAdapter() {
 238                 public void windowClosing(WindowEvent e) {
 239                     setVisible(false);
 240                     dispose();
 241                 }
 242             });
 243 
 244             // The robot needs to know about us, so set global
 245             robot_text = txt;
 246         }
 247     }
 248 
 249     public static void main(String[] args) throws RuntimeException, Exception {
 250         try {
 251             new NestedModalDialogTest().testModalDialogs();
 252         } catch (Exception e) {
 253             throw new RuntimeException("NestedModalDialogTest object creation "
 254                     + "failed");
 255         }
 256     }
 257 }