|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2024, 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 | + * @bug 4167811 |
| 27 | + * @summary tests that shortcuts work for Checkbox menu items |
| 28 | + * @library /java/awt/regtesthelpers |
| 29 | + * @build PassFailJFrame |
| 30 | + * @run main/manual CheckMenuShortcut |
| 31 | +*/ |
| 32 | + |
| 33 | +import java.awt.CheckboxMenuItem; |
| 34 | +import java.awt.Dimension; |
| 35 | +import java.awt.Frame; |
| 36 | +import java.awt.Insets; |
| 37 | +import java.awt.Menu; |
| 38 | +import java.awt.MenuBar; |
| 39 | +import java.awt.MenuItem; |
| 40 | +import java.awt.MenuShortcut; |
| 41 | +import java.awt.Panel; |
| 42 | +import java.awt.Rectangle; |
| 43 | +import java.awt.TextArea; |
| 44 | +import java.awt.event.ActionEvent; |
| 45 | +import java.awt.event.ActionListener; |
| 46 | +import java.awt.event.ItemEvent; |
| 47 | +import java.awt.event.ItemListener; |
| 48 | +import java.awt.event.KeyEvent; |
| 49 | + |
| 50 | +public class CheckMenuShortcut implements ActionListener, ItemListener { |
| 51 | + |
| 52 | + static final String INSTRUCTIONS = """ |
| 53 | + A window that contains a text area will be displayed. |
| 54 | + The window will have a menu labeled 'Window Menu'. Click on the menu to see its items. |
| 55 | +
|
| 56 | + The two menu items should have shortcuts which in order are : Ctrl-A, Ctrl-I. |
| 57 | + On macOS these will be Command-A, Command-I. |
| 58 | +
|
| 59 | + If the second item only has the label 'checkbox item' and no shortcut |
| 60 | + ie none of Ctrl-I or Ctrl-i, or Command-I or Command-i on macOS painted on it, the test FAILS. |
| 61 | +
|
| 62 | + The same second item - labeled 'checkbox item' is in fact a Checkbox menu item. |
| 63 | + The menu item should NOT be checked (eg no tick mark). |
| 64 | +
|
| 65 | + Dismiss the menu by clicking inside the window, do not select any of menu items. |
| 66 | + After that press Ctrl-i, (Command-i on macOS). |
| 67 | +
|
| 68 | + After that click on the menu again. If the second menu item 'checkbox item' is now |
| 69 | + checked, the test PASSES, if it is not checked, the test FAILS. |
| 70 | + """; |
| 71 | + |
| 72 | + public static void main(String[] args) throws Exception { |
| 73 | + PassFailJFrame.builder() |
| 74 | + .title("CheckboxMenuItem Shortcut Test Instructions") |
| 75 | + .instructions(INSTRUCTIONS) |
| 76 | + .columns(60) |
| 77 | + .logArea() |
| 78 | + .testUI(CheckMenuShortcut::createUI) |
| 79 | + .build() |
| 80 | + .awaitAndCheck(); |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | + static Frame createUI() { |
| 85 | + |
| 86 | + MenuBar mainMenu; |
| 87 | + Menu menu; |
| 88 | + MenuItem action; |
| 89 | + CheckboxMenuItem item; |
| 90 | + TextArea pane; |
| 91 | + |
| 92 | + boolean isMac = System.getProperty("os.name").startsWith("Mac"); |
| 93 | + String ctrlA = (isMac) ? "Command-A" : "Ctrl-A"; |
| 94 | + String ctrlI = (isMac) ? "Command-I" : "Ctrl-I"; |
| 95 | + |
| 96 | + CheckMenuShortcut cms = new CheckMenuShortcut(); |
| 97 | + Frame frame = new Frame("CheckMenuShortcut"); |
| 98 | + |
| 99 | + mainMenu = new MenuBar(); |
| 100 | + menu = new Menu("Window Menu"); |
| 101 | + |
| 102 | + action = new MenuItem("action"); |
| 103 | + action.setShortcut(new MenuShortcut(KeyEvent.VK_A, false)); |
| 104 | + action.addActionListener(cms); |
| 105 | + action.setActionCommand("action"); |
| 106 | + menu.add(action); |
| 107 | + |
| 108 | + item = new CheckboxMenuItem("checkbox item", false); |
| 109 | + item.setShortcut(new MenuShortcut(KeyEvent.VK_I,false)); |
| 110 | + item.addItemListener(cms); |
| 111 | + item.addActionListener(cms); |
| 112 | + menu.add(item); |
| 113 | + |
| 114 | + mainMenu.add(menu); |
| 115 | + |
| 116 | + frame.setMenuBar(mainMenu); |
| 117 | + |
| 118 | + pane = new TextArea(ctrlA + " -- action menu test\n", 10, 40, TextArea.SCROLLBARS_VERTICAL_ONLY); |
| 119 | + Dimension mySize = frame.getSize(); |
| 120 | + Insets myIns = frame.getInsets(); |
| 121 | + pane.setBounds(new Rectangle(mySize.width - myIns.left - myIns.right, |
| 122 | + mySize.height - myIns.top - myIns.bottom)); |
| 123 | + pane.setLocation(myIns.left,myIns.top); |
| 124 | + frame.add(pane); |
| 125 | + |
| 126 | + pane.append(ctrlI + " -- item menu test\n"); |
| 127 | + |
| 128 | + frame.pack(); |
| 129 | + return frame; |
| 130 | + } |
| 131 | + |
| 132 | + public void itemStateChanged(ItemEvent evt) { |
| 133 | + PassFailJFrame.log("Got item: " + evt.getItem() + "\n"); |
| 134 | + } |
| 135 | + |
| 136 | + public void actionPerformed(ActionEvent evt) { |
| 137 | + PassFailJFrame.log("Got action: " + evt.getActionCommand() + "\n"); |
| 138 | + } |
| 139 | +} |
0 commit comments