Path: blob/master/test/jdk/java/awt/MenuBar/TestNoScreenMenuBar.java
41149 views
/*1* Copyright (c) 2018, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/**26* @test27* @key headful28* @bug 814631029* @summary [macosx] setDefaultMenuBar does not initialize screen menu bar30* @author Alan Snyder31* @library /test/lib32* @run main/othervm TestNoScreenMenuBar33* @requires (os.family == "mac")34*/3536import java.awt.AWTException;37import java.awt.Desktop;38import java.awt.Frame;39import java.awt.Menu;40import java.awt.MenuBar;41import java.awt.Robot;42import java.awt.event.InputEvent;43import java.io.IOException;44import java.lang.reflect.InvocationTargetException;4546import javax.swing.JMenu;47import javax.swing.JMenuBar;48import javax.swing.JMenuItem;49import javax.swing.SwingUtilities;5051import jdk.test.lib.process.ProcessTools;5253public class TestNoScreenMenuBar54{55static TestNoScreenMenuBar theTest;56private Robot robot;57private Process process;58private boolean isActionPerformed;5960public TestNoScreenMenuBar()61{62try {63robot = new Robot();64robot.setAutoDelay(50);65} catch (AWTException ex) {66throw new RuntimeException(ex);67}6869// activate another java application70openOtherApplication();71robot.delay(2000);7273// The failure mode is installing the default menu bar while the application is inactive74Desktop desktop = Desktop.getDesktop();75desktop.setDefaultMenuBar(createMenuBar());7677robot.delay(500);78desktop.requestForeground(true);79robot.delay(500);80}8182JMenuBar createMenuBar()83{84JMenuBar mb = new JMenuBar();85// A very long name makes it more likely that the robot will hit the menu86JMenu menu = new JMenu("TestTestTestTestTestTestTestTestTestTest");87mb.add(menu);88JMenuItem item = new JMenuItem("TestTestTestTestTestTestTestTestTestTest");89item.addActionListener(ev -> {90isActionPerformed = true;91});92menu.add(item);93return mb;94}9596void dispose()97{98closeOtherApplication();99Desktop.getDesktop().setDefaultMenuBar(null);100}101102private void performMenuItemTest()103{104// Find the menu on the screen menu bar105// The location depends upon the application name which is the name of the first menu.106// Unfortunately, the application name can vary based on how the application is run.107// The work around is to make the menu and the menu item names very long.108109int menuBarX = 250;110int menuBarY = 11;111int menuItemX = menuBarX;112int menuItemY = 34;113114robot.mouseMove(menuBarX, menuBarY);115robot.delay(100);116robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);117robot.delay(100);118robot.mouseMove(menuItemX, menuItemY);119robot.delay(100);120robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);121robot.waitForIdle();122123waitForAction();124}125126private synchronized void waitForAction()127{128try {129for (int i = 0; i < 10; i++) {130if (isActionPerformed) {131return;132}133wait(100);134}135} catch (InterruptedException ex) {136}137throw new RuntimeException("Test failed: menu item action was not performed");138}139140private void openOtherApplication() {141process = execute();142}143144private void closeOtherApplication() {145if (process != null) {146process.destroyForcibly();147}148}149150private Process execute() {151try {152ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(153TestNoScreenMenuBar.class.getSimpleName(), "mark");154return ProcessTools.startProcess("Other frame", pb);155} catch (IOException ex) {156throw new RuntimeException("Unable to execute command");157}158}159160private static void runSwing(Runnable r)161{162try {163SwingUtilities.invokeAndWait(r);164} catch (InterruptedException e) {165} catch (InvocationTargetException e) {166throw new RuntimeException(e);167}168}169170public static void main(String[] args)171{172if (!System.getProperty("os.name").contains("OS X")) {173System.out.println("This test is for MacOS only. Automatically passed on other platforms.");174return;175}176if (args.length != 0) {177Frame frame = new Frame();178MenuBar mb = new MenuBar();179mb.add(new Menu("Hello"));180frame.setMenuBar(mb);181frame.setSize(300, 300);182frame.setLocationRelativeTo(null);183frame.setVisible(true);184frame.toFront();185return;186}187System.setProperty("apple.laf.useScreenMenuBar", "true");188try {189runSwing(() -> theTest = new TestNoScreenMenuBar());190theTest.performMenuItemTest();191} finally {192if (theTest != null) {193runSwing(() -> theTest.dispose());194}195}196}197}198199200