Path: blob/master/test/jdk/com/apple/eawt/DefaultMenuBar/DefaultMenuBarTest.java
41153 views
/*1* Copyright (c) 2013, 2017, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @key headful26* @bug 800726727* @summary [macosx] com.apple.eawt.Application.setDefaultMenuBar is not working28* @requires (os.family == "mac")29* @author [email protected]30* @modules java.desktop/sun.awt31* java.desktop/com.apple.eawt32* @run main DefaultMenuBarTest33*/3435import java.awt.*;36import java.awt.event.*;37import javax.swing.*;38import java.lang.reflect.Method;394041public class DefaultMenuBarTest {42static KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.META_MASK);4344static volatile int listenerCallCounter = 0;45public static void main(String[] args) throws Exception {46if (!System.getProperty("os.name").contains("OS X")) {47System.out.println("This test is for MacOS only. Automatically passed on other platforms.");48return;49}5051System.setProperty("apple.laf.useScreenMenuBar", "true");52SwingUtilities.invokeAndWait(new Runnable() {53public void run() {54createAndShowGUI();55}56});5758Robot robot = new Robot();59robot.setAutoDelay(100);6061robot.keyPress(KeyEvent.VK_META);62robot.keyPress(ks.getKeyCode());63robot.keyRelease(ks.getKeyCode());64robot.keyRelease(KeyEvent.VK_META);6566robot.waitForIdle();6768if (listenerCallCounter != 1) {69throw new Exception("Test failed: ActionListener either wasn't called or was called more than once");70}71}7273private static void createAndShowGUI() {74JMenu menu = new JMenu("File");75JMenuItem newItem = new JMenuItem("Open");7677newItem.setAccelerator(ks);78newItem.addActionListener(79new ActionListener(){80public void actionPerformed(ActionEvent e) {81listenerCallCounter++;82}83}84);85menu.add(newItem);8687JMenuBar defaultMenu = new JMenuBar();88defaultMenu.add(menu);8990// Application.getApplication().setDefaultMenuBar(defaultMenu);91try {92Class appClass = Class.forName("com.apple.eawt.Application");93if (appClass != null) {94Method method = appClass.getMethod("getApplication");95if (method != null) {96Object app = method.invoke(null, new Object[]{});97if (app != null) {98method = appClass.getMethod("setDefaultMenuBar", new Class[]{JMenuBar.class});99if (method != null) {100method.invoke(app, new Object[]{defaultMenu});101}102}103}104}105} catch (Exception e) {106e.printStackTrace();107}108}109}110111112