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