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