Path: blob/master/test/jdk/javax/swing/JMenuItem/8139169/ScreenMenuBarInputTwice.java
41153 views
/*1* Copyright (c) 2015, 2017, 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* @key headful26* @bug 8139169 815839027* @summary verifies if TextArea gets input twice due to Apple's Screen Menubar28* @requires (os.family=="mac")29* @library ../../regtesthelpers30* @build Util31* @run main ScreenMenuBarInputTwice32*/3334import java.awt.BorderLayout;35import java.awt.Point;36import java.awt.Robot;37import java.awt.event.ActionEvent;38import java.awt.event.InputEvent;39import java.awt.event.KeyEvent;40import static java.awt.event.KeyEvent.VK_COMMA;41import static java.awt.event.KeyEvent.VK_META;42import static java.awt.event.KeyEvent.VK_SHIFT;43import javax.swing.AbstractAction;44import javax.swing.Action;45import javax.swing.JFrame;46import javax.swing.JMenu;47import javax.swing.JMenuBar;48import javax.swing.JMenuItem;49import javax.swing.JPanel;50import javax.swing.JScrollPane;51import javax.swing.JTextArea;52import javax.swing.KeyStroke;53import javax.swing.SwingUtilities;54import javax.swing.text.BadLocationException;5556public class ScreenMenuBarInputTwice {5758public static final String TEST_STRING = "Check string";5960private static Robot robot;61private static JFrame frame;62private static JPanel content;63private static JTextArea textArea;64private static JMenuBar menuBar;65private static JMenu menu;66private static JMenuItem menuItem;6768public static void main(String[] args) throws Exception {69robot = new Robot();70robot.setAutoDelay(200);71robot.setAutoWaitForIdle(true);72createUIWithSeperateMenuBar();73robot.waitForIdle();74robot.delay(500);75shortcutTestCase();76robot.waitForIdle();77robot.delay(250);78cleanUp();79robot.waitForIdle();80robot.delay(250);81createUIWithIntegratedMenuBar();82robot.waitForIdle();83robot.delay(500);84menuTestCase();85robot.waitForIdle();86robot.delay(250);87cleanUp();88}8990private static void createUIWithSeperateMenuBar() throws Exception {91SwingUtilities.invokeAndWait(new Runnable() {9293@Override94public void run() {95System.setProperty(96"com.apple.mrj.application.apple.menu.about.name",97"A test frame");98System.setProperty("apple.laf.useScreenMenuBar", "true");99frame = new JFrame("Text input twice check");100content = new JPanel(new BorderLayout());101textArea = new JTextArea();102content.add(new JScrollPane(textArea,103JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,104JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),105BorderLayout.CENTER);106menuBar = new JMenuBar();107frame.setJMenuBar(menuBar);108Action a = new AbstractAction("Insert some text") {109@Override110public void actionPerformed(ActionEvent arg0) {111try {112113textArea.getDocument()114.insertString(0, TEST_STRING, null);115} catch (BadLocationException e) {116frame.dispose();117throw new RuntimeException("Bad location: ", e);118}119}120};121KeyStroke keyStroke = KeyStroke.getKeyStroke(122"meta shift COMMA");123a.putValue(Action.ACCELERATOR_KEY, keyStroke);124textArea.getInputMap().put(keyStroke, "myAction");125textArea.getActionMap().put("myAction", a);126menu = new JMenu("The Menu");127menuItem = new JMenuItem(a);128menuItem.setAccelerator((KeyStroke) a.getValue(129Action.ACCELERATOR_KEY));130menu.add(menuItem);131menuBar.add(menu);132frame.getContentPane().add(content);133frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);134frame.setLocationRelativeTo(null);135frame.setSize(500, 500);136frame.setVisible(true);137frame.toFront();138}139});140}141142private static void createUIWithIntegratedMenuBar() throws Exception {143SwingUtilities.invokeAndWait(new Runnable() {144145@Override146public void run() {147System.setProperty(148"com.apple.mrj.application.apple.menu.about.name",149"A test frame");150System.setProperty("apple.laf.useScreenMenuBar", "false");151frame = new JFrame("Text input twice check");152content = new JPanel(new BorderLayout());153textArea = new JTextArea();154content.add(new JScrollPane(textArea,155JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,156JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),157BorderLayout.CENTER);158menuBar = new JMenuBar();159frame.setJMenuBar(menuBar);160Action a = new AbstractAction("Insert some text") {161@Override162public void actionPerformed(ActionEvent arg0) {163try {164165textArea.getDocument()166.insertString(0, TEST_STRING, null);167} catch (BadLocationException e) {168frame.dispose();169throw new RuntimeException("Bad location: ", e);170}171}172};173KeyStroke keyStroke = KeyStroke.getKeyStroke(174"meta shift COMMA");175a.putValue(Action.ACCELERATOR_KEY, keyStroke);176textArea.getInputMap().put(keyStroke, "myAction");177textArea.getActionMap().put("myAction", a);178menu = new JMenu("The Menu");179menuItem = new JMenuItem(a);180menuItem.setAccelerator((KeyStroke) a.getValue(181Action.ACCELERATOR_KEY));182menu.add(menuItem);183menuBar.add(menu);184frame.getContentPane().add(content);185frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);186frame.setSize(500, 500);187frame.setLocationRelativeTo(null);188frame.setVisible(true);189frame.toFront();190}191});192}193194private static void shortcutTestCase() throws Exception {195robot.keyPress(KeyEvent.VK_META);196robot.keyPress(KeyEvent.VK_SHIFT);197robot.keyPress(KeyEvent.VK_COMMA);198robot.keyRelease(VK_COMMA);199robot.keyRelease(VK_SHIFT);200robot.keyRelease(VK_META);201checkText(textArea.getText());202}203204private static void menuTestCase() throws Exception {205Point mousePoint;206mousePoint = Util.getCenterPoint(menu);207robot.mouseMove(mousePoint.x, mousePoint.y);208robot.mousePress(InputEvent.BUTTON1_MASK);209robot.mouseRelease(InputEvent.BUTTON1_MASK);210mousePoint = Util.getCenterPoint(menuItem);211robot.mouseMove(mousePoint.x, mousePoint.y);212robot.mousePress(InputEvent.BUTTON1_MASK);213robot.mouseRelease(InputEvent.BUTTON1_MASK);214checkText(textArea.getText());215}216217private static void checkText(String text) throws Exception {218SwingUtilities.invokeAndWait(new Runnable() {219@Override220public void run() {221if (TEST_STRING.equals(text)) {222textArea.setText("");223} else {224frame.dispose();225throw new RuntimeException("Failed. "226+ " Menu item shortcut invoked twice");227}228}229});230}231232private static void cleanUp() throws Exception {233SwingUtilities.invokeAndWait(new Runnable() {234@Override235public void run() {236frame.dispose();237}238});239}240}241242243