Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/apple/eawt/DefaultMenuBar/DefaultMenuBarTest.java
38855 views
/*1* Copyright (c) 2013, 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* @bug 800726726* @summary [macosx] com.apple.eawt.Application.setDefaultMenuBar is not working27* @requires (os.family == "mac")28* @author [email protected]29* @run main DefaultMenuBarTest30*/3132import java.awt.*;33import java.awt.event.*;34import javax.swing.*;35import java.lang.reflect.Method;363738public class DefaultMenuBarTest {39static KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.META_MASK);4041static volatile int listenerCallCounter = 0;42public static void main(String[] args) throws Exception {43if (!System.getProperty("os.name").contains("OS X")) {44System.out.println("This test is for MacOS only. Automatically passed on other platforms.");45return;46}4748System.setProperty("apple.laf.useScreenMenuBar", "true");49SwingUtilities.invokeAndWait(new Runnable() {50public void run() {51createAndShowGUI();52}53});5455Robot robot = new Robot();56robot.setAutoDelay(100);5758robot.keyPress(KeyEvent.VK_META);59robot.keyPress(ks.getKeyCode());60robot.keyRelease(ks.getKeyCode());61robot.keyRelease(KeyEvent.VK_META);6263robot.waitForIdle();6465if (listenerCallCounter != 1) {66throw new Exception("Test failed: ActionListener either wasn't called or was called more than once");67}68}6970private static void createAndShowGUI() {71JMenu menu = new JMenu("File");72JMenuItem newItem = new JMenuItem("Open");7374newItem.setAccelerator(ks);75newItem.addActionListener(76new ActionListener(){77public void actionPerformed(ActionEvent e) {78listenerCallCounter++;79}80}81);82menu.add(newItem);8384JMenuBar defaultMenu = new JMenuBar();85defaultMenu.add(menu);8687// Application.getApplication().setDefaultMenuBar(defaultMenu);88try {89Class appClass = Class.forName("com.apple.eawt.Application");90if (appClass != null) {91Method method = appClass.getMethod("getApplication");92if (method != null) {93Object app = method.invoke(null, new Object[]{});94if (app != null) {95method = appClass.getMethod("setDefaultMenuBar", new Class[]{JMenuBar.class});96if (method != null) {97method.invoke(app, new Object[]{defaultMenu});98}99}100}101}102} catch (Exception e) {103e.printStackTrace();104}105}106}107108109