1 /* 2 * Copyright (c) 2015, 2016, 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 /* @test 25 * @bug 8139169 8158390 26 * @summary verifies if TextArea gets input twice due to Apple's Screen Menubar 27 * @requires (os.family=="mac") 28 * @library ../../regtesthelpers 29 * @build Util 30 * @run main ScreenMenuBarInputTwice 31 */ 32 import java.awt.BorderLayout; 33 import java.awt.Point; 34 import java.awt.Robot; 35 import java.awt.event.ActionEvent; 36 import java.awt.event.InputEvent; 37 import java.awt.event.KeyEvent; 38 import static java.awt.event.KeyEvent.VK_COMMA; 39 import static java.awt.event.KeyEvent.VK_META; 40 import static java.awt.event.KeyEvent.VK_SHIFT; 41 import javax.swing.AbstractAction; 42 import javax.swing.Action; 43 import javax.swing.JFrame; 44 import javax.swing.JMenu; 45 import javax.swing.JMenuBar; 46 import javax.swing.JMenuItem; 47 import javax.swing.JPanel; 48 import javax.swing.JScrollPane; 49 import javax.swing.JTextArea; 50 import javax.swing.KeyStroke; 51 import javax.swing.SwingUtilities; 52 import javax.swing.text.BadLocationException; 53 54 public class ScreenMenuBarInputTwice { 55 56 public static final String TEST_STRING = "Check string"; 57 58 private static Robot robot; 59 private static JFrame frame; 60 private static JPanel content; 61 private static JTextArea textArea; 62 private static JMenuBar menuBar; 63 private static JMenu menu; 64 private static JMenuItem menuItem; 65 66 public static void main(String[] args) throws Exception { 67 robot = new Robot(); 68 robot.setAutoDelay(200); 69 robot.setAutoWaitForIdle(true); 70 createUIWithSeperateMenuBar(); 71 shortcutTestCase(); 72 cleanUp(); 73 createUIWithIntegratedMenuBar(); 74 menuTestCase(); 75 cleanUp(); 76 } 77 78 private static void createUIWithSeperateMenuBar() throws Exception { 79 SwingUtilities.invokeAndWait(new Runnable() { 80 81 @Override 82 public void run() { 83 System.setProperty( 84 "com.apple.mrj.application.apple.menu.about.name", 85 "A test frame"); 86 System.setProperty("apple.laf.useScreenMenuBar", "true"); 87 frame = new JFrame("Text input twice check"); 88 content = new JPanel(new BorderLayout()); 89 textArea = new JTextArea(); 90 content.add(new JScrollPane(textArea, 91 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 92 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), 93 BorderLayout.CENTER); 94 menuBar = new JMenuBar(); 95 frame.setJMenuBar(menuBar); 96 Action a = new AbstractAction("Insert some text") { 97 @Override 98 public void actionPerformed(ActionEvent arg0) { 99 try { 100 101 textArea.getDocument() 102 .insertString(0, TEST_STRING, null); 103 } catch (BadLocationException e) { 104 frame.dispose(); 105 throw new RuntimeException("Bad location: ", e); 106 } 107 } 108 }; 109 KeyStroke keyStroke = KeyStroke.getKeyStroke( 110 "meta shift COMMA"); 111 a.putValue(Action.ACCELERATOR_KEY, keyStroke); 112 textArea.getInputMap().put(keyStroke, "myAction"); 113 textArea.getActionMap().put("myAction", a); 114 menu = new JMenu("The Menu"); 115 menuItem = new JMenuItem(a); 116 menuItem.setAccelerator((KeyStroke) a.getValue( 117 Action.ACCELERATOR_KEY)); 118 menu.add(menuItem); 119 menuBar.add(menu); 120 frame.getContentPane().add(content); 121 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 122 frame.setLocationRelativeTo(null); 123 frame.setSize(500, 500); 124 frame.setVisible(true); 125 frame.toFront(); 126 } 127 }); 128 } 129 130 private static void createUIWithIntegratedMenuBar() throws Exception { 131 SwingUtilities.invokeAndWait(new Runnable() { 132 133 @Override 134 public void run() { 135 System.setProperty( 136 "com.apple.mrj.application.apple.menu.about.name", 137 "A test frame"); 138 System.setProperty("apple.laf.useScreenMenuBar", "false"); 139 frame = new JFrame("Text input twice check"); 140 content = new JPanel(new BorderLayout()); 141 textArea = new JTextArea(); 142 content.add(new JScrollPane(textArea, 143 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 144 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), 145 BorderLayout.CENTER); 146 menuBar = new JMenuBar(); 147 frame.setJMenuBar(menuBar); 148 Action a = new AbstractAction("Insert some text") { 149 @Override 150 public void actionPerformed(ActionEvent arg0) { 151 try { 152 153 textArea.getDocument() 154 .insertString(0, TEST_STRING, null); 155 } catch (BadLocationException e) { 156 frame.dispose(); 157 throw new RuntimeException("Bad location: ", e); 158 } 159 } 160 }; 161 KeyStroke keyStroke = KeyStroke.getKeyStroke( 162 "meta shift COMMA"); 163 a.putValue(Action.ACCELERATOR_KEY, keyStroke); 164 textArea.getInputMap().put(keyStroke, "myAction"); 165 textArea.getActionMap().put("myAction", a); 166 menu = new JMenu("The Menu"); 167 menuItem = new JMenuItem(a); 168 menuItem.setAccelerator((KeyStroke) a.getValue( 169 Action.ACCELERATOR_KEY)); 170 menu.add(menuItem); 171 menuBar.add(menu); 172 frame.getContentPane().add(content); 173 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 174 frame.setSize(500, 500); 175 frame.setLocationRelativeTo(null); 176 frame.setVisible(true); 177 frame.toFront(); 178 } 179 }); 180 } 181 182 private static void shortcutTestCase() throws Exception { 183 robot.keyPress(KeyEvent.VK_META); 184 robot.keyPress(KeyEvent.VK_SHIFT); 185 robot.keyPress(KeyEvent.VK_COMMA); 186 robot.keyRelease(VK_COMMA); 187 robot.keyRelease(VK_SHIFT); 188 robot.keyRelease(VK_META); 189 checkText(textArea.getText()); 190 } 191 192 private static void menuTestCase() throws Exception { 193 Point mousePoint; 194 mousePoint = Util.getCenterPoint(menu); 195 robot.mouseMove(mousePoint.x, mousePoint.y); 196 robot.mousePress(InputEvent.BUTTON1_MASK); 197 robot.mouseRelease(InputEvent.BUTTON1_MASK); 198 mousePoint = Util.getCenterPoint(menuItem); 199 robot.mouseMove(mousePoint.x, mousePoint.y); 200 robot.mousePress(InputEvent.BUTTON1_MASK); 201 robot.mouseRelease(InputEvent.BUTTON1_MASK); 202 checkText(textArea.getText()); 203 } 204 205 private static void checkText(String text) throws Exception { 206 SwingUtilities.invokeAndWait(new Runnable() { 207 @Override 208 public void run() { 209 if (TEST_STRING.equals(text)) { 210 textArea.setText(""); 211 } else { 212 frame.dispose(); 213 throw new RuntimeException("Failed. " 214 + " Menu item shortcut invoked twice"); 215 } 216 } 217 }); 218 } 219 220 private static void cleanUp() throws Exception { 221 SwingUtilities.invokeAndWait(new Runnable() { 222 @Override 223 public void run() { 224 frame.dispose(); 225 } 226 }); 227 } 228 }