Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTextField/8036819/bug8036819.java
38853 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @library ../../regtesthelpers26* @build Util27* @bug 803681928* @summary JAB: mnemonics not read for textboxes29* @author Vivi An30* @run main bug803681931*/3233import javax.swing.*;34import javax.swing.event.*;35import java.awt.event.*;36import java.awt.*;37import javax.accessibility.*;3839public class bug8036819 {4041public static volatile Boolean passed = false;4243public static void main(String args[]) throws Throwable {44SwingUtilities.invokeAndWait(new Runnable() {45public void run() {46createAndShowGUI();47}48});495051Robot robo = new Robot();52robo.setAutoDelay(300);53robo.waitForIdle();5455// Using mnemonic key to focus on the textfield56Util.hitMnemonics(robo, KeyEvent.VK_P);57robo.waitForIdle();5859if (!passed){60throw new RuntimeException("Test failed.");61}62}6364private static void createAndShowGUI() {65JFrame mainFrame = new JFrame("bug 8036819");6667JLabel usernameLabel = new JLabel("Username: ");68JTextField usernameField = new JTextField(20);69usernameLabel.setDisplayedMnemonic(KeyEvent.VK_U);70usernameLabel.setLabelFor(usernameField);7172JLabel pwdLabel = new JLabel("Password: ");73JTextField pwdField = new JTextField(20);74pwdLabel.setDisplayedMnemonic(KeyEvent.VK_P);75pwdLabel.setLabelFor(pwdField);7677pwdField.addKeyListener(78new KeyListener(){79@Override80public void keyPressed(KeyEvent keyEvent) {81}8283@Override84public void keyTyped(KeyEvent keyEvent) {85}8687@Override88public void keyReleased(KeyEvent keyEvent){89JComponent comp = (JComponent) pwdField;90AccessibleContext ac = comp.getAccessibleContext();91AccessibleExtendedComponent aec = (AccessibleExtendedComponent)ac.getAccessibleComponent();92AccessibleKeyBinding akb = aec.getAccessibleKeyBinding();93if (akb != null){94int count = akb.getAccessibleKeyBindingCount();95if (count != 1){96passed = false;97return;98}99100// there is 1 accessible key for the text field101System.out.println("Retrieved AccessibleKeyBinding for textfield " + count);102103// the key code is KeyEvent.VK_P104Object o = akb.getAccessibleKeyBinding(0);105if (o instanceof KeyStroke){106javax.swing.KeyStroke key = (javax.swing.KeyStroke)o;107System.out.println("keystroke is " + key.getKeyCode());108if (key.getKeyCode() == KeyEvent.VK_P)109passed = true;110}111}112}113}114);115116mainFrame.getContentPane().add(usernameLabel);117mainFrame.getContentPane().add(usernameField);118mainFrame.getContentPane().add(pwdLabel);119mainFrame.getContentPane().add(pwdField);120121mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);122mainFrame.setLayout(new FlowLayout(FlowLayout.LEFT));123124mainFrame.setSize(200, 200);125mainFrame.setLocation(200, 200);126mainFrame.setVisible(true);127mainFrame.toFront();128}129}130131132