blob: fa26504f548335f7cd7e0aa8b9578bc38f9affad [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 test
26 @bug 6396785
27 @summary Action key pressed on a button should be swallowed.
28 @author anton.tarasov@...: area=awt.focus
29 @run applet ButtonActionKeyTest.html
30*/
31
32import java.awt.*;
33import java.awt.event.*;
34import javax.swing.*;
35import java.applet.Applet;
36import java.util.concurrent.atomic.AtomicBoolean;
37import java.lang.reflect.InvocationTargetException;
38import test.java.awt.regtesthelpers.Util;
39
40public class ButtonActionKeyTest extends Applet {
41 Robot robot;
42 JFrame frame = new JFrame("Frame");
43 JButton button = new JButton("button");
44 JTextField text = new JTextField("text");
45 AtomicBoolean gotEvent = new AtomicBoolean(false);
46
47 public static void main(String[] args) {
48 ButtonActionKeyTest app = new ButtonActionKeyTest();
49 app.init();
50 app.start();
51 }
52
53 public void init() {
54 robot = Util.createRobot();
55
56 // Create instructions for the user here, as well as set up
57 // the environment -- set the layout manager, add buttons,
58 // etc.
59 this.setLayout (new BorderLayout ());
60 Sysout.createDialogWithInstructions(new String[]
61 {"This is an automatic test. Simply wait until it is done."
62 });
63 }
64
65 public void start() {
66 frame.setLayout(new FlowLayout());
67 frame.add(button);
68 frame.add(text);
69 frame.pack();
70
71 button.getInputMap().put(KeyStroke.getKeyStroke("A"), "GO!");
72 button.getActionMap().put("GO!", new AbstractAction() {
73 public void actionPerformed(ActionEvent e) {
74 Sysout.println("Action performed!");
75 text.requestFocusInWindow();
76 }
77 });
78
79 text.addKeyListener(new KeyAdapter() {
80 public void keyTyped(KeyEvent e) {
81 if (e.getKeyChar() == 'a') {
82 Sysout.println(e.toString());
83 synchronized (gotEvent) {
84 gotEvent.set(true);
85 gotEvent.notifyAll();
86 }
87 }
88 }
89 });
90
91 frame.setVisible(true);
92 Util.waitForIdle(robot);
93
94 Util.clickOnComp(button, robot);
95 Util.waitForIdle(robot);
96
97 if (!button.isFocusOwner()) {
98 throw new Error("Test error: a button didn't gain focus.");
99 }
100
101 robot.keyPress(KeyEvent.VK_A);
102 robot.delay(20);
103 robot.keyRelease(KeyEvent.VK_A);
104
105 if (Util.waitForCondition(gotEvent, 2000)) {
106 throw new TestFailedException("an action key went into the text field!");
107 }
108
109 Sysout.println("Test passed.");
110 }
111}
112
113class TestFailedException extends RuntimeException {
114 TestFailedException(String msg) {
115 super("Test failed: " + msg);
116 }
117}
118
119/****************************************************
120 Standard Test Machinery
121 DO NOT modify anything below -- it's a standard
122 chunk of code whose purpose is to make user
123 interaction uniform, and thereby make it simpler
124 to read and understand someone else's test.
125 ****************************************************/
126
127/**
128 This is part of the standard test machinery.
129 It creates a dialog (with the instructions), and is the interface
130 for sending text messages to the user.
131 To print the instructions, send an array of strings to Sysout.createDialog
132 WithInstructions method. Put one line of instructions per array entry.
133 To display a message for the tester to see, simply call Sysout.println
134 with the string to be displayed.
135 This mimics System.out.println but works within the test harness as well
136 as standalone.
137 */
138
139class Sysout
140{
141 static TestDialog dialog;
142
143 public static void createDialogWithInstructions( String[] instructions )
144 {
145 dialog = new TestDialog( new Frame(), "Instructions" );
146 dialog.printInstructions( instructions );
147// dialog.setVisible(true);
148 println( "Any messages for the tester will display here." );
149 }
150
151 public static void createDialog( )
152 {
153 dialog = new TestDialog( new Frame(), "Instructions" );
154 String[] defInstr = { "Instructions will appear here. ", "" } ;
155 dialog.printInstructions( defInstr );
156// dialog.setVisible(true);
157 println( "Any messages for the tester will display here." );
158 }
159
160
161 public static void printInstructions( String[] instructions )
162 {
163 dialog.printInstructions( instructions );
164 }
165
166
167 public static void println( String messageIn )
168 {
169 dialog.displayMessage( messageIn );
170 }
171
172}// Sysout class
173
174/**
175 This is part of the standard test machinery. It provides a place for the
176 test instructions to be displayed, and a place for interactive messages
177 to the user to be displayed.
178 To have the test instructions displayed, see Sysout.
179 To have a message to the user be displayed, see Sysout.
180 Do not call anything in this dialog directly.
181 */
182class TestDialog extends Dialog
183{
184
185 TextArea instructionsText;
186 TextArea messageText;
187 int maxStringLength = 80;
188
189 //DO NOT call this directly, go through Sysout
190 public TestDialog( Frame frame, String name )
191 {
192 super( frame, name );
193 int scrollBoth = TextArea.SCROLLBARS_BOTH;
194 instructionsText = new TextArea( "", 15, maxStringLength, scrollBoth );
195 add( "North", instructionsText );
196
197 messageText = new TextArea( "", 5, maxStringLength, scrollBoth );
198 add("Center", messageText);
199
200 pack();
201
202// setVisible(true);
203 }// TestDialog()
204
205 //DO NOT call this directly, go through Sysout
206 public void printInstructions( String[] instructions )
207 {
208 //Clear out any current instructions
209 instructionsText.setText( "" );
210
211 //Go down array of instruction strings
212
213 String printStr, remainingStr;
214 for( int i=0; i < instructions.length; i++ )
215 {
216 //chop up each into pieces maxSringLength long
217 remainingStr = instructions[ i ];
218 while( remainingStr.length() > 0 )
219 {
220 //if longer than max then chop off first max chars to print
221 if( remainingStr.length() >= maxStringLength )
222 {
223 //Try to chop on a word boundary
224 int posOfSpace = remainingStr.
225 lastIndexOf( ' ', maxStringLength - 1 );
226
227 if( posOfSpace <= 0 ) posOfSpace = maxStringLength - 1;
228
229 printStr = remainingStr.substring( 0, posOfSpace + 1 );
230 remainingStr = remainingStr.substring( posOfSpace + 1 );
231 }
232 //else just print
233 else
234 {
235 printStr = remainingStr;
236 remainingStr = "";
237 }
238
239 instructionsText.append( printStr + "\n" );
240
241 }// while
242
243 }// for
244
245 }//printInstructions()
246
247 //DO NOT call this directly, go through Sysout
248 public void displayMessage( String messageIn )
249 {
250 messageText.append( messageIn + "\n" );
251 System.out.println(messageIn);
252 }
253
254}// TestDialog class