Path: blob/master/test/jdk/javax/swing/JLabel/4138746/JLabelMnemonicsTest.java
66646 views
/*1* Copyright (c) 2001, 2022, 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*/2223import java.awt.GridLayout;24import java.awt.Robot;25import java.awt.event.FocusAdapter;26import java.awt.event.FocusEvent;27import java.awt.event.KeyEvent;28import java.util.Arrays;29import java.util.List;30import java.util.concurrent.CountDownLatch;31import java.util.concurrent.TimeUnit;32import java.util.concurrent.atomic.AtomicBoolean;33import java.util.concurrent.atomic.AtomicInteger;34import java.util.stream.Collectors;35import javax.swing.AbstractButton;36import javax.swing.JButton;37import javax.swing.JFrame;38import javax.swing.JLabel;39import javax.swing.JPanel;40import javax.swing.SwingUtilities;41import javax.swing.UIManager;42import javax.swing.UIManager.LookAndFeelInfo;43import javax.swing.UnsupportedLookAndFeelException;4445import static javax.swing.UIManager.getInstalledLookAndFeels;4647/*48* @test49* @key headful50* @bug 413874651* @summary This testcase tests RFE-4138746 request, verifies the case-sensitive52* setting of Mnemonics to AbstractButton & JLabel.53* @run main JLabelMnemonicsTest54*/55public class JLabelMnemonicsTest {5657private static JButton button1;58private static JButton button2;59private static JLabel label1;60private static JLabel label2;61private static JPanel panel;62private static Robot robot;63private static boolean result;64private static CountDownLatch focusGainedLatch;65private static JFrame frame;6667public static void main(String[] args) throws Exception {68final boolean isMac =69System.getProperty("os.name").toLowerCase().contains("os x");7071robot = new Robot();72robot.setAutoWaitForIdle(true);73robot.setAutoDelay(200);74List<String> lafs = Arrays.stream(getInstalledLookAndFeels())75.map(LookAndFeelInfo::getClassName)76.collect(Collectors.toList());77for (final String laf : lafs) {78try {79result = true;80focusGainedLatch = new CountDownLatch(1);81AtomicBoolean lafSetSuccess = new AtomicBoolean(false);82SwingUtilities.invokeAndWait(() -> {83lafSetSuccess.set(setLookAndFeel(laf));84if (lafSetSuccess.get()) {85createUI();86}87});88if (!lafSetSuccess.get()) {89continue;90}91robot.waitForIdle();9293// Verifier 1: Verifies if getDisplayedMnemonicIndex() returns the94// right index set with setDisplayedMnemonicIndex method for JButton95if (getDisplayedMnemonicIndex(button1) == 5 &&96getDisplayedMnemonicIndex(button2) == 0) {97System.out.println("Verifier 1 Passed");98} else {99System.out.println(100"Verifier 1 Failed, testing JButton failed");101result = false;102}103104// Verifier 2: Verifies that, on setting displayedMnemonicIndex to105// -1, the component can be still accessed with the mnemonic set106if (isMac) {107hitKeys(KeyEvent.VK_ALT, KeyEvent.VK_CONTROL,108KeyEvent.VK_V);109} else {110hitKeys(KeyEvent.VK_ALT, KeyEvent.VK_V);111}112if (focusGainedLatch.await(3, TimeUnit.SECONDS)) {113System.out.println("Verifier 2 Passed");114} else {115System.out.println(116"Verifier 2 Failed, Waited too long, but Button3 " +117"has not yet gained focus in " + laf);118result = false;119}120121// Verifier 3: Testing JLabel: Verifies if122// getDisplayedMnemonicIndex() returns the right123// index set with setDisplayedMnemonicIndex method for JLabel124if (getDisplayedMnemonicIndex(label1) == 5 &&125getDisplayedMnemonicIndex(label2) == 0) {126System.out.println("Verifier 3 Passed");127} else {128System.out.println("Verifier 3, testing JLabel Failed");129result = false;130}131132if (result) {133System.out.println("Test Passed in " + laf);134} else {135throw new RuntimeException(136"Test Failed, as one or more verifiers failed in " +137laf);138}139} finally {140SwingUtilities.invokeAndWait(JLabelMnemonicsTest::disposeFrame);141}142}143}144145private static int getDisplayedMnemonicIndex(JLabel jLabel)146throws Exception {147final AtomicInteger index = new AtomicInteger();148SwingUtilities.invokeAndWait(149() -> index.set(jLabel.getDisplayedMnemonicIndex()));150return index.get();151}152153private static int getDisplayedMnemonicIndex(AbstractButton button)154throws Exception {155final AtomicInteger index = new AtomicInteger();156SwingUtilities.invokeAndWait(157() -> index.set(button.getDisplayedMnemonicIndex()));158return index.get();159}160161private static void hitKeys(int... keys) {162for (int key : keys) {163robot.keyPress(key);164}165166for (int i = keys.length - 1; i >= 0; i--) {167robot.keyRelease(keys[i]);168}169}170171private static void createUI() {172frame = new JFrame();173panel = new JPanel();174panel.setLayout(new GridLayout(2, 3));175176button1 = new JButton("Save As");177button1.setMnemonic(KeyEvent.VK_A);178button1.setDisplayedMnemonicIndex(5);179panel.add(button1);180181button2 = new JButton("save AS");182button2.setMnemonic(KeyEvent.VK_S);183panel.add(button2);184185JButton button3 = new JButton("Save As");186button3.setMnemonic(KeyEvent.VK_V);187button3.setDisplayedMnemonicIndex(-1);188panel.add(button3);189button3.addFocusListener(new FocusAdapter() {190public void focusGained(FocusEvent fe) {191System.out.println("FocusGained on Button3");192focusGainedLatch.countDown();193}194});195196label1 = new JLabel("Save As");197label1.setDisplayedMnemonic(KeyEvent.VK_A);198label1.setDisplayedMnemonicIndex(5);199panel.add(label1);200201label2 = new JLabel("save AS");202label2.setDisplayedMnemonic(KeyEvent.VK_S);203panel.add(label2);204205frame.add(panel);206frame.setLocationRelativeTo(null);207frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);208frame.pack();209frame.setVisible(true);210}211212private static boolean setLookAndFeel(String lafName) {213try {214UIManager.setLookAndFeel(lafName);215} catch (UnsupportedLookAndFeelException ignored) {216System.out.println("Ignoring Unsupported L&F: " + lafName);217return false;218} catch (ClassNotFoundException | InstantiationException219| IllegalAccessException e) {220throw new RuntimeException(e);221}222return true;223}224225private static void disposeFrame() {226if (frame != null) {227frame.dispose();228frame = null;229}230}231232}233234235