jdk/test/com/apple/eawt/DefaultMenuBar/DefaultMenuBarTest.java
author leonidr
Thu, 01 Aug 2013 01:26:57 +0400
changeset 19172 718ea16b9de2
child 31448 1066345d2a8a
permissions -rw-r--r--
8021815: Add regression test for JDK-8007267 Reviewed-by: serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19172
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
     1
/*
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
     4
 *
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
     8
 *
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    13
 * accompanied this code).
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    14
 *
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    18
 *
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    21
 * questions.
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    22
 */
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    23
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    24
/*
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    25
 * @test
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    26
 * @bug 8007267
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    27
 * @summary [macosx] com.apple.eawt.Application.setDefaultMenuBar is not working
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    28
 * @author leonid.romanov@oracle.com
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    29
 * @run main DefaultMenuBarTest
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    30
 */
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    31
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    32
import java.awt.*;
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    33
import java.awt.event.*;
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    34
import javax.swing.*;
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    35
import sun.awt.*;
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    36
import java.lang.reflect.Method;
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    37
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    38
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    39
public class DefaultMenuBarTest {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    40
    static KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.META_MASK);
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    41
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    42
    static volatile int listenerCallCounter = 0;
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    43
    public static void main(String[] args) throws Exception {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    44
        if (sun.awt.OSInfo.getOSType() != sun.awt.OSInfo.OSType.MACOSX) {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    45
            System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    46
            return;
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    47
        }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    48
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    49
        System.setProperty("apple.laf.useScreenMenuBar", "true");
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    50
        SwingUtilities.invokeAndWait(new Runnable() {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    51
            public void run() {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    52
                createAndShowGUI();
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    53
            }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    54
        });
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    55
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    56
        SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    57
        Robot robot = new Robot();
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    58
        robot.setAutoDelay(100);
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    59
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    60
        robot.keyPress(KeyEvent.VK_META);
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    61
        robot.keyPress(ks.getKeyCode());
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    62
        robot.keyRelease(ks.getKeyCode());
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    63
        robot.keyRelease(KeyEvent.VK_META);
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    64
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    65
        toolkit.realSync();
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    66
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    67
        if (listenerCallCounter != 1) {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    68
            throw new Exception("Test failed: ActionListener either wasn't called or was called more than once");
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    69
        }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    70
    }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    71
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    72
    private static void createAndShowGUI() {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    73
        JMenu menu = new JMenu("File");
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    74
        JMenuItem newItem = new JMenuItem("Open");
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    75
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    76
        newItem.setAccelerator(ks);
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    77
        newItem.addActionListener(
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    78
            new ActionListener(){
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    79
                public void actionPerformed(ActionEvent e) {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    80
                    listenerCallCounter++;
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    81
                }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    82
            }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    83
        );
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    84
        menu.add(newItem);
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    85
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    86
        JMenuBar defaultMenu = new JMenuBar();
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    87
        defaultMenu.add(menu);
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    88
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    89
        // Application.getApplication().setDefaultMenuBar(defaultMenu);
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    90
        try {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    91
            Class appClass = Class.forName("com.apple.eawt.Application");
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    92
            if (appClass != null) {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    93
                Method method = appClass.getMethod("getApplication");
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    94
                if (method != null) {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    95
                    Object app = method.invoke(null, new Object[]{});
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    96
                    if (app != null) {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    97
                        method = appClass.getMethod("setDefaultMenuBar", new Class[]{JMenuBar.class});
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    98
                        if (method != null) {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
    99
                            method.invoke(app, new Object[]{defaultMenu});
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
   100
                        }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
   101
                    }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
   102
                }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
   103
            }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
   104
        } catch (Exception e) {
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
   105
            e.printStackTrace();
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
   106
        }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
   107
    }
718ea16b9de2 8021815: Add regression test for JDK-8007267
leonidr
parents:
diff changeset
   108
}