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