1 /*
2  * Copyright (c) 2014, 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
26  * @library ../../regtesthelpers
27  * @build Util
28  * @bug 8036819
29  * @summary JAB: mnemonics not read for textboxes
30  * @author Vivi An
31  * @run main bug8036819
32  */
33 
34 import javax.swing.*;
35 import javax.swing.event.*;
36 import java.awt.event.*;
37 import java.awt.*;
38 import javax.accessibility.*;
39 
40 public class bug8036819 {
41 
42     public static volatile Boolean passed = false;
43 
main(String args[])44     public static void main(String args[]) throws Throwable {
45         SwingUtilities.invokeAndWait(new Runnable() {
46             public void run() {
47                 createAndShowGUI();
48             }
49         });
50 
51 
52         Robot robo = new Robot();
53         robo.setAutoDelay(300);
54         robo.waitForIdle();
55 
56         // Using mnemonic key to focus on the textfield
57         Util.hitMnemonics(robo, KeyEvent.VK_P);
58         robo.waitForIdle();
59 
60         if (!passed){
61             throw new RuntimeException("Test failed.");
62         }
63     }
64 
createAndShowGUI()65     private static void createAndShowGUI() {
66         JFrame mainFrame = new JFrame("bug 8036819");
67 
68         JLabel usernameLabel = new JLabel("Username: ");
69         JTextField usernameField = new JTextField(20);
70         usernameLabel.setDisplayedMnemonic(KeyEvent.VK_U);
71         usernameLabel.setLabelFor(usernameField);
72 
73         JLabel pwdLabel = new JLabel("Password: ");
74         JTextField pwdField = new JTextField(20);
75         pwdLabel.setDisplayedMnemonic(KeyEvent.VK_P);
76         pwdLabel.setLabelFor(pwdField);
77 
78         pwdField.addKeyListener(
79             new KeyListener(){
80                 @Override
81                 public void keyPressed(KeyEvent keyEvent) {
82                 }
83 
84                 @Override
85                 public void keyTyped(KeyEvent keyEvent) {
86                 }
87 
88                 @Override
89                 public void keyReleased(KeyEvent keyEvent){
90                     JComponent comp = (JComponent) pwdField;
91                     AccessibleContext ac = comp.getAccessibleContext();
92                     AccessibleExtendedComponent aec = (AccessibleExtendedComponent)ac.getAccessibleComponent();
93                     AccessibleKeyBinding akb = aec.getAccessibleKeyBinding();
94                     if (akb != null){
95                          int count = akb.getAccessibleKeyBindingCount();
96                         if (count != 1){
97                             passed = false;
98                             return;
99                         }
100 
101                         // there is 1 accessible key for the text field
102                         System.out.println("Retrieved AccessibleKeyBinding for textfield " + count);
103 
104                         // the key code is KeyEvent.VK_P
105                         Object o = akb.getAccessibleKeyBinding(0);
106                         if (o instanceof KeyStroke){
107                             javax.swing.KeyStroke key = (javax.swing.KeyStroke)o;
108                             System.out.println("keystroke is " + key.getKeyCode());
109                             if (key.getKeyCode() == KeyEvent.VK_P)
110                                 passed = true;
111                         }
112                     }
113                 }
114             }
115         );
116 
117         mainFrame.getContentPane().add(usernameLabel);
118         mainFrame.getContentPane().add(usernameField);
119         mainFrame.getContentPane().add(pwdLabel);
120         mainFrame.getContentPane().add(pwdField);
121 
122         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
123         mainFrame.setLayout(new FlowLayout(FlowLayout.LEFT));
124 
125         mainFrame.setSize(200, 200);
126         mainFrame.setLocation(200, 200);
127         mainFrame.setVisible(true);
128         mainFrame.toFront();
129     }
130  }
131