1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @key headful
* @bug 8146310
* @summary [macosx] setDefaultMenuBar does not initialize screen menu bar
* @author Alan Snyder
* @library /test/lib
* @run main/othervm TestNoScreenMenuBar
* @requires (os.family == "mac")
*/
import java.awt.AWTException;
import java.awt.Desktop;
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import jdk.test.lib.process.ProcessTools;
public class TestNoScreenMenuBar
{
static TestNoScreenMenuBar theTest;
private Robot robot;
private Process process;
private boolean isActionPerformed;
public TestNoScreenMenuBar()
{
try {
robot = new Robot();
robot.setAutoDelay(50);
} catch (AWTException ex) {
throw new RuntimeException(ex);
}
// activate another java application
openOtherApplication();
robot.delay(2000);
// The failure mode is installing the default menu bar while the application is inactive
Desktop desktop = Desktop.getDesktop();
desktop.setDefaultMenuBar(createMenuBar());
robot.delay(500);
desktop.requestForeground(true);
robot.delay(500);
}
JMenuBar createMenuBar()
{
JMenuBar mb = new JMenuBar();
// A very long name makes it more likely that the robot will hit the menu
JMenu menu = new JMenu("TestTestTestTestTestTestTestTestTestTest");
mb.add(menu);
JMenuItem item = new JMenuItem("TestTestTestTestTestTestTestTestTestTest");
item.addActionListener(ev -> {
isActionPerformed = true;
});
menu.add(item);
return mb;
}
void dispose()
{
closeOtherApplication();
Desktop.getDesktop().setDefaultMenuBar(null);
}
private void performMenuItemTest()
{
// Find the menu on the screen menu bar
// The location depends upon the application name which is the name of the first menu.
// Unfortunately, the application name can vary based on how the application is run.
// The work around is to make the menu and the menu item names very long.
int menuBarX = 250;
int menuBarY = 11;
int menuItemX = menuBarX;
int menuItemY = 34;
robot.mouseMove(menuBarX, menuBarY);
robot.delay(100);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.delay(100);
robot.mouseMove(menuItemX, menuItemY);
robot.delay(100);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
waitForAction();
}
private synchronized void waitForAction()
{
try {
for (int i = 0; i < 10; i++) {
if (isActionPerformed) {
return;
}
wait(100);
}
} catch (InterruptedException ex) {
}
throw new RuntimeException("Test failed: menu item action was not performed");
}
private void openOtherApplication() {
process = execute();
}
private void closeOtherApplication() {
if (process != null) {
process.destroyForcibly();
}
}
private Process execute() {
try {
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
TestNoScreenMenuBar.class.getSimpleName(), "mark");
return ProcessTools.startProcess("Other frame", pb);
} catch (IOException ex) {
throw new RuntimeException("Unable to execute command");
}
}
private static void runSwing(Runnable r)
{
try {
SwingUtilities.invokeAndWait(r);
} catch (InterruptedException e) {
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args)
{
if (!System.getProperty("os.name").contains("OS X")) {
System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
return;
}
if (args.length != 0) {
Frame frame = new Frame();
MenuBar mb = new MenuBar();
mb.add(new Menu("Hello"));
frame.setMenuBar(mb);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.toFront();
return;
}
System.setProperty("apple.laf.useScreenMenuBar", "true");
try {
runSwing(() -> theTest = new TestNoScreenMenuBar());
theTest.performMenuItemTest();
} finally {
if (theTest != null) {
runSwing(() -> theTest.dispose());
}
}
}
}
|