11/*
2- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2121 * questions.
2222 */
2323
24- import java .awt .*;
25- import java .awt .event .*;
26- import javax .swing .*;
24+ import java .awt .Robot ;
25+ import java .awt .event .ActionEvent ;
26+ import java .awt .event .ActionListener ;
27+ import java .awt .event .KeyEvent ;
28+ import javax .swing .JFrame ;
29+ import javax .swing .JMenu ;
30+ import javax .swing .JMenuBar ;
31+ import javax .swing .JMenuItem ;
32+ import javax .swing .SwingUtilities ;
2733
2834/**
2935 * @test
3743 */
3844public class bug4515762 {
3945
40- private static volatile boolean actionExpected = false ;
41- private static volatile boolean actionRecieved = false ;
46+ private static volatile boolean actionExpected ;
47+ private static volatile boolean actionRecieved ;
4248 private static JFrame frame ;
4349
4450 /**
@@ -99,18 +105,18 @@ public void actionPerformed(ActionEvent evt) {
99105 return menuItem ;
100106 }
101107
102- public static void checkAction () {
108+ public static void checkAction (String str ) {
103109 if (actionRecieved == true ) {
104110 actionRecieved = false ;
105111 } else {
106- throw new RuntimeException ("Action has not been received" );
112+ throw new RuntimeException ("Action has not been received: " + str );
107113 }
108114 }
109115
110116 public static void main (String [] args ) throws Throwable {
111117 try {
112118 Robot robot = new Robot ();
113- robot .setAutoDelay (250 );
119+ robot .setAutoDelay (100 );
114120
115121 SwingUtilities .invokeAndWait (new Runnable () {
116122
@@ -119,21 +125,25 @@ public void run() {
119125 frame = new JFrame ("Test" );
120126 frame .setJMenuBar (createMenuBar ());
121127 frame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
128+ frame .setLocationRelativeTo (null );
122129 frame .pack ();
123130 frame .setVisible (true );
124131 frame .toFront ();
125132 }
126133 });
127134
128135 robot .waitForIdle ();
136+ robot .delay (1000 );
129137
130138 Util .hitMnemonics (robot , KeyEvent .VK_D );
131139 robot .waitForIdle ();
132140
133141 // Press the S key many times (should not cause an action peformed)
134142 int TIMES = 5 ;
135143 for (int i = 0 ; i < TIMES ; i ++) {
136- Util .hitKeys (robot , KeyEvent .VK_S );
144+ robot .keyPress (KeyEvent .VK_S );
145+ robot .keyRelease (KeyEvent .VK_S );
146+ robot .waitForIdle ();
137147 }
138148 robot .waitForIdle ();
139149
@@ -146,7 +156,7 @@ public void run() {
146156 robot .keyRelease (KeyEvent .VK_S );
147157 robot .waitForIdle ();
148158
149- checkAction ();
159+ checkAction ("pressing VK_S" );
150160
151161 Util .hitMnemonics (robot , KeyEvent .VK_U );
152162 robot .waitForIdle ();
@@ -155,28 +165,31 @@ public void run() {
155165 robot .keyRelease (KeyEvent .VK_M );
156166 robot .waitForIdle ();
157167
158- checkAction ();
168+ checkAction ("pressing VK_M" );
159169
160170 Util .hitMnemonics (robot , KeyEvent .VK_U );
161171 robot .waitForIdle ();
162- Util .hitKeys (robot , KeyEvent .VK_T );
172+ robot .keyPress (KeyEvent .VK_T );
173+ robot .keyRelease (KeyEvent .VK_T );
163174 robot .waitForIdle ();
164175
165- checkAction ();
176+ checkAction ("pressing VK_T" );
166177
167178 Util .hitMnemonics (robot , KeyEvent .VK_U );
168179 robot .waitForIdle ();
169- Util .hitKeys (robot , KeyEvent .VK_W );
180+ robot .keyPress (KeyEvent .VK_W );
181+ robot .keyRelease (KeyEvent .VK_W );
170182 robot .waitForIdle ();
171183
172- checkAction ();
184+ checkAction ("pressing VK_W" );
173185
174186 Util .hitMnemonics (robot , KeyEvent .VK_U );
175187 robot .waitForIdle ();
176- Util .hitKeys (robot , KeyEvent .VK_U );
188+ robot .keyPress (KeyEvent .VK_U );
189+ robot .keyRelease (KeyEvent .VK_U );
177190 robot .waitForIdle ();
178191
179- checkAction ();
192+ checkAction ("pressing VK_U" );
180193 } finally {
181194 if (frame != null ) SwingUtilities .invokeAndWait (() -> frame .dispose ());
182195 }
0 commit comments