Path: blob/master/test/jdk/java/awt/MenuBar/8007006/bug8007006.java
41154 views
/*1* Copyright (c) 2013, 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.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 800700627* @requires (os.family == "mac")28* @summary [macosx] Closing subwindow loses main window menus.29* @library /test/lib30* @build jdk.test.lib.Platform31* @run main bug800700632*/3334import java.awt.*;35import java.awt.event.*;3637import jdk.test.lib.Platform;3839public class bug8007006 {40private static Frame frame1;41private static Frame frame2;42private static volatile boolean isActionPerformed;4344public static void main(String[] args) throws Exception {45if (!Platform.isOSX()) {46System.out.println("This test is for MacOS only. Automatically passed on other platforms.");47return;48}4950System.setProperty("apple.laf.useScreenMenuBar", "true");5152Robot robot = new Robot();53robot.setAutoDelay(300);5455createAndShowGUI();56robot.waitForIdle();57frame2.dispose();58robot.waitForIdle();5960performMenuItemTest(robot);6162frame1.dispose();63if (!isActionPerformed) {64throw new Exception("Test failed: menu item action was not performed");65}66}6768private static void createAndShowGUI() {69frame1 = new Frame("Frame 1");70frame1.setMenuBar(createMenuBar());71frame1.setSize(200, 200);7273frame2 = new Frame("Frame 2");74frame2.setMenuBar(createMenuBar());75frame2.setSize(200, 200);7677frame1.setVisible(true);78frame2.setVisible(true);79}8081private static MenuBar createMenuBar() {82// A very long name makes it more likely that the robot will hit the83// menu84Menu menu = new Menu("TestTestTestTestTestTestTestTestTestTest");85MenuItem item = new MenuItem("TestTestTestTestTestTestTestTestTestTest");86item.addActionListener(new ActionListener() {87@Override88public void actionPerformed(ActionEvent ev) {89isActionPerformed = true;90}91});92menu.add(item);93MenuBar mb = new MenuBar();94mb.add(menu);95return mb;96}9798private static void performMenuItemTest(Robot robot) {99// Find the menu on the screen menu bar100// The location depends upon the application name which is the name101// of the first menu.102// Unfortunately, the application name can vary based on how the103// application is run.104// The work around is to make the menu and the menu item names very105// long.106int menuBarX = 250;107int menuBarY = 11;108int menuItemX = menuBarX;109int menuItemY = 34;110robot.mouseMove(menuBarX, menuBarY);111robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);112robot.mouseMove(menuItemX, menuItemY);113robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);114robot.waitForIdle();115waitForAction();116}117118private static void waitForAction() {119try {120for (int i = 0; i < 10; i++) {121if (isActionPerformed) {122return;123}124Thread.sleep(100);125}126} catch (InterruptedException ex) {127}128}129}130131132