1 /*
   2  * Copyright (c) 2013, 2017, 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  * @summary [macosx] Closing subwindow loses main window menus.
  29  * @author Leonid Romanov
  30  * @run main bug8007006
  31  */
  32 
  33 import sun.awt.SunToolkit;
  34 import java.awt.*;
  35 import java.awt.event.*;
  36 
  37 public class bug8007006 {
  38     private static Frame frame1;
  39     private static Frame frame2;
  40 
  41     public static void main(String[] args) throws Exception {
  42         if (sun.awt.OSInfo.getOSType() != sun.awt.OSInfo.OSType.MACOSX) {
  43             System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
  44             return;
  45         }
  46 
  47         System.setProperty("apple.laf.useScreenMenuBar", "true");
  48 
  49         createAndShowGUI();
  50         sleep(1500);
  51 
  52         frame2.dispose();
  53         sleep(1500);
  54 
  55         SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
  56 
  57         Robot robot = new Robot();
  58         robot.setAutoDelay(50);
  59 
  60         // open "Apple" menu (the leftmost one)
  61         robot.keyPress(KeyEvent.VK_META);
  62         robot.keyPress(KeyEvent.VK_SHIFT);
  63         robot.keyPress(KeyEvent.VK_SLASH);
  64         robot.keyRelease(KeyEvent.VK_SLASH);
  65         robot.keyRelease(KeyEvent.VK_SHIFT);
  66         robot.keyRelease(KeyEvent.VK_META);
  67 
  68         // Select our menu
  69         robot.keyPress(KeyEvent.VK_LEFT);
  70         robot.keyRelease(KeyEvent.VK_LEFT);
  71 
  72         // Select menu item
  73         robot.keyPress(KeyEvent.VK_DOWN);
  74         robot.keyRelease(KeyEvent.VK_DOWN);
  75         robot.keyPress(KeyEvent.VK_ENTER);
  76         robot.keyRelease(KeyEvent.VK_ENTER);
  77 
  78         sleep(0);
  79 
  80         MenuBar mbar = frame1.getMenuBar();
  81         Menu menu = mbar.getMenu(0);
  82         CheckboxMenuItem item = (CheckboxMenuItem)menu.getItem(0);
  83         boolean isChecked = item.getState();
  84 
  85         frame1.dispose();
  86 
  87         if (isChecked) {
  88             throw new Exception("Test failed: menu item remained checked");
  89         }
  90     }
  91 
  92     private static void createAndShowGUI() {
  93         frame1 = new Frame("Frame 1");
  94         frame1.setMenuBar(createMenuBar());
  95         frame1.setSize(200, 200);
  96 
  97         frame2 = new Frame("Frame 2");
  98         frame2.setMenuBar(createMenuBar());
  99         frame2.setSize(200, 200);
 100 
 101         frame1.setVisible(true);
 102         frame2.setVisible(true);
 103     }
 104 
 105     private static MenuBar createMenuBar() {
 106         MenuBar mbar = new MenuBar();
 107         Menu menu = new Menu("Menu");
 108         MenuItem item = new CheckboxMenuItem("Checked", true);
 109 
 110         menu.add(item);
 111         mbar.add(menu);
 112 
 113         return mbar;
 114     }
 115 
 116     private static void sleep(int ms) {
 117         SunToolkit tk = (SunToolkit)Toolkit.getDefaultToolkit();
 118         tk.realSync();
 119 
 120         try {
 121             Thread.sleep(ms);
 122         } catch (Exception ignore) {
 123         }
 124     }
 125 }