test/jdk/java/awt/MenuBar/8007006/bug8007006.java
changeset 52540 a00f1403dbe9
parent 51637 e9177e7749e7
equal deleted inserted replaced
52539:b0dcecb339c4 52540:a00f1403dbe9
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @key headful
    26  * @key headful
    27  * @bug 8007006
    27  * @bug 8007006
       
    28  * @requires (os.family == "mac")
    28  * @summary [macosx] Closing subwindow loses main window menus.
    29  * @summary [macosx] Closing subwindow loses main window menus.
    29  * @author Leonid Romanov
       
    30  * @library /test/lib
    30  * @library /test/lib
    31  * @build ExtendedRobot jdk.test.lib.Platform
    31  * @build jdk.test.lib.Platform
    32  * @run main bug8007006
    32  * @run main bug8007006
    33  */
    33  */
    34 
    34 
    35 import java.awt.*;
    35 import java.awt.*;
    36 import java.awt.event.*;
    36 import java.awt.event.*;
    38 import jdk.test.lib.Platform;
    38 import jdk.test.lib.Platform;
    39 
    39 
    40 public class bug8007006 {
    40 public class bug8007006 {
    41     private static Frame frame1;
    41     private static Frame frame1;
    42     private static Frame frame2;
    42     private static Frame frame2;
       
    43     private static volatile boolean isActionPerformed;
    43 
    44 
    44     public static void main(String[] args) throws Exception {
    45     public static void main(String[] args) throws Exception {
    45         if (!Platform.isOSX()) {
    46         if (!Platform.isOSX()) {
    46             System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
    47             System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
    47             return;
    48             return;
    48         }
    49         }
    49 
    50 
    50         System.setProperty("apple.laf.useScreenMenuBar", "true");
    51         System.setProperty("apple.laf.useScreenMenuBar", "true");
    51 
    52 
    52         ExtendedRobot robot = new ExtendedRobot();
    53         Robot robot = new Robot();
    53         robot.setAutoDelay(50);
    54         robot.setAutoDelay(300);
    54 
    55 
    55         createAndShowGUI();
    56         createAndShowGUI();
    56         robot.waitForIdle(1500);
    57         robot.waitForIdle();
    57 
       
    58         frame2.dispose();
    58         frame2.dispose();
    59 
       
    60         robot.waitForIdle(1500);
       
    61 
       
    62 
       
    63         // open "Apple" menu (the leftmost one)
       
    64         robot.keyPress(KeyEvent.VK_META);
       
    65         robot.keyPress(KeyEvent.VK_SHIFT);
       
    66         robot.keyPress(KeyEvent.VK_SLASH);
       
    67         robot.keyRelease(KeyEvent.VK_SLASH);
       
    68         robot.keyRelease(KeyEvent.VK_SHIFT);
       
    69         robot.keyRelease(KeyEvent.VK_META);
       
    70 
       
    71         // Select our menu
       
    72         robot.keyPress(KeyEvent.VK_LEFT);
       
    73         robot.keyRelease(KeyEvent.VK_LEFT);
       
    74 
       
    75         // Select menu item
       
    76         robot.keyPress(KeyEvent.VK_DOWN);
       
    77         robot.keyRelease(KeyEvent.VK_DOWN);
       
    78         robot.keyPress(KeyEvent.VK_ENTER);
       
    79         robot.keyRelease(KeyEvent.VK_ENTER);
       
    80 
       
    81         robot.waitForIdle();
    59         robot.waitForIdle();
    82 
    60 
    83         MenuBar mbar = frame1.getMenuBar();
    61         performMenuItemTest(robot);
    84         Menu menu = mbar.getMenu(0);
       
    85         CheckboxMenuItem item = (CheckboxMenuItem)menu.getItem(0);
       
    86         boolean isChecked = item.getState();
       
    87 
    62 
    88         frame1.dispose();
    63         frame1.dispose();
    89 
    64         if (!isActionPerformed) {
    90         if (isChecked) {
    65             throw new Exception("Test failed: menu item action was not performed");
    91             throw new Exception("Test failed: menu item remained checked");
       
    92         }
    66         }
    93     }
    67     }
    94 
    68 
    95     private static void createAndShowGUI() {
    69     private static void createAndShowGUI() {
    96         frame1 = new Frame("Frame 1");
    70         frame1 = new Frame("Frame 1");
   104         frame1.setVisible(true);
    78         frame1.setVisible(true);
   105         frame2.setVisible(true);
    79         frame2.setVisible(true);
   106     }
    80     }
   107 
    81 
   108     private static MenuBar createMenuBar() {
    82     private static MenuBar createMenuBar() {
   109         MenuBar mbar = new MenuBar();
    83         // A very long name makes it more likely that the robot will hit the
   110         Menu menu = new Menu("Menu");
    84         // menu
   111         MenuItem item = new CheckboxMenuItem("Checked", true);
    85         Menu menu = new Menu("TestTestTestTestTestTestTestTestTestTest");
   112 
    86         MenuItem item = new MenuItem("TestTestTestTestTestTestTestTestTestTest");
       
    87         item.addActionListener(new ActionListener() {
       
    88             @Override
       
    89             public void actionPerformed(ActionEvent ev) {
       
    90                 isActionPerformed = true;
       
    91             }
       
    92         });
   113         menu.add(item);
    93         menu.add(item);
   114         mbar.add(menu);
    94         MenuBar mb = new MenuBar();
   115 
    95         mb.add(menu);
   116         return mbar;
    96         return mb;
   117     }
    97     }
   118 
    98 
       
    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 
       
   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     }
   119 }
   130 }