1 /*
2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @key headful
27  * @bug 8007006
28  * @requires (os.family == "mac")
29  * @summary [macosx] Closing subwindow loses main window menus.
30  * @library /test/lib
31  * @build jdk.test.lib.Platform
32  * @run main bug8007006
33  */
34 
35 import java.awt.*;
36 import java.awt.event.*;
37 
38 import jdk.test.lib.Platform;
39 
40 public class bug8007006 {
41     private static Frame frame1;
42     private static Frame frame2;
43     private static volatile boolean isActionPerformed;
44 
main(String[] args)45     public static void main(String[] args) throws Exception {
46         if (!Platform.isOSX()) {
47             System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
48             return;
49         }
50 
51         System.setProperty("apple.laf.useScreenMenuBar", "true");
52 
53         Robot robot = new Robot();
54         robot.setAutoDelay(300);
55 
56         createAndShowGUI();
57         robot.waitForIdle();
58         frame2.dispose();
59         robot.waitForIdle();
60 
61         performMenuItemTest(robot);
62 
63         frame1.dispose();
64         if (!isActionPerformed) {
65             throw new Exception("Test failed: menu item action was not performed");
66         }
67     }
68 
createAndShowGUI()69     private static void createAndShowGUI() {
70         frame1 = new Frame("Frame 1");
71         frame1.setMenuBar(createMenuBar());
72         frame1.setSize(200, 200);
73 
74         frame2 = new Frame("Frame 2");
75         frame2.setMenuBar(createMenuBar());
76         frame2.setSize(200, 200);
77 
78         frame1.setVisible(true);
79         frame2.setVisible(true);
80     }
81 
createMenuBar()82     private static MenuBar createMenuBar() {
83         // A very long name makes it more likely that the robot will hit the
84         // menu
85         Menu menu = new Menu("TestTestTestTestTestTestTestTestTestTest");
86         MenuItem item = new MenuItem("TestTestTestTestTestTestTestTestTestTest");
87         item.addActionListener(new ActionListener() {
88             @Override
89             public void actionPerformed(ActionEvent ev) {
90                 isActionPerformed = true;
91             }
92         });
93         menu.add(item);
94         MenuBar mb = new MenuBar();
95         mb.add(menu);
96         return mb;
97     }
98 
performMenuItemTest(Robot robot)99     private static void performMenuItemTest(Robot robot) {
100         // Find the menu on the screen menu bar
101         // The location depends upon the application name which is the name
102         // of the first menu.
103         // Unfortunately, the application name can vary based on how the
104         // application is run.
105         // The work around is to make the menu and the menu item names very
106         // long.
107         int menuBarX = 250;
108         int menuBarY = 11;
109         int menuItemX = menuBarX;
110         int menuItemY = 34;
111         robot.mouseMove(menuBarX, menuBarY);
112         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
113         robot.mouseMove(menuItemX, menuItemY);
114         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
115         robot.waitForIdle();
116         waitForAction();
117     }
118 
waitForAction()119     private static void waitForAction() {
120         try {
121             for (int i = 0; i < 10; i++) {
122                 if (isActionPerformed) {
123                     return;
124                 }
125                 Thread.sleep(100);
126             }
127         } catch (InterruptedException ex) {
128         }
129     }
130 }
131