test/jdk/java/awt/MenuBar/DefaultMenuBarDispose.java
author egahlin
Wed, 03 Jul 2019 22:51:44 +0200
branchJEP-349-branch
changeset 57449 099789ceff7d
parent 49103 24c72ffb7660
permissions -rw-r--r--
Provide implementation for all added API methods
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49103
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
     1
/*
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
     4
 *
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    10
 *
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    15
 * accompanied this code).
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    16
 *
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    20
 *
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    23
 * questions.
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    24
 */
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    25
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    26
/**
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    27
 * @test
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    28
 * @key headful
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    29
 * @bug 8196322
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    30
 * @summary [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    31
 * @author Alan Snyder
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    32
 * @run main/othervm DefaultMenuBarDispose
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    33
 * @requires (os.family == "mac")
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    34
 */
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    35
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    36
import java.awt.Desktop;
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    37
import java.lang.reflect.InvocationTargetException;
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    38
import javax.swing.JMenuBar;
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    39
import javax.swing.SwingUtilities;
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    40
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    41
public class DefaultMenuBarDispose
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    42
{
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    43
    public DefaultMenuBarDispose()
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    44
    {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    45
        Thread watcher = new Thread(() -> {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    46
            try {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    47
                synchronized (this) {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    48
                    wait(5000);
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    49
                }
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    50
                throw new RuntimeException("Test failed: failed to exit");
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    51
            } catch (InterruptedException ex) {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    52
            }
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    53
        });
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    54
        watcher.setDaemon(true);
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    55
        watcher.start();
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    56
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    57
        runSwing(() ->
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    58
            {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    59
                JMenuBar mb = new JMenuBar();
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    60
                Desktop.getDesktop().setDefaultMenuBar(mb);
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    61
                Desktop.getDesktop().setDefaultMenuBar(null);
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    62
            }
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    63
        );
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    64
    }
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    65
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    66
    private static void runSwing(Runnable r)
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    67
    {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    68
        try {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    69
            SwingUtilities.invokeAndWait(r);
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    70
        } catch (InterruptedException e) {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    71
        } catch (InvocationTargetException e) {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    72
            throw new RuntimeException(e);
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    73
        }
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    74
    }
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    75
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    76
    public static void main(String[] args)
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    77
    {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    78
        if (!System.getProperty("os.name").contains("OS X")) {
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    79
            System.out.println("This test is for MacOS only. Automatically passed on other platforms.");
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    80
            return;
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    81
        }
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    82
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    83
        System.setProperty("apple.laf.useScreenMenuBar", "true");
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    84
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    85
        new DefaultMenuBarDispose();
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    86
    }
24c72ffb7660 8196322: [macosx] When the screen menu bar is used, clearing the default menu bar should permit AWT shutdown
serb
parents:
diff changeset
    87
}