RemoveHelpMenu.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2015, 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
24import java.awt.Frame;
25import java.awt.Menu;
26import java.awt.MenuBar;
27
28/**
29 * @test
30 * @key headful
31 * @bug 6475361
32 * @author Sergey Bylokhov
33 */
34public final class RemoveHelpMenu {
35
36    public static void main(final String[] args) {
37        final Frame frame = new Frame("RemoveHelpMenu Test");
38        try {
39            frame.pack();
40            // peer exists
41            test1(getMenuBar(frame));
42            test2(getMenuBar(frame));
43            test3(getMenuBar(frame));
44            test4(getMenuBar(frame));
45        } finally {
46            frame.dispose();
47        }
48        // peer is null
49        test1(getMenuBar(frame));
50        test2(getMenuBar(frame));
51        test3(getMenuBar(frame));
52        test4(getMenuBar(frame));
53    }
54
55    private static MenuBar getMenuBar(final Frame frame) {
56        final MenuBar menuBar = new MenuBar();
57        frame.setMenuBar(menuBar);
58        return menuBar;
59    }
60
61    private static void checkHelpMenu(final Menu menu, final boolean expected) {
62        final boolean actual = menu.toString().contains("isHelpMenu=true");
63        if (actual != expected) {
64            throw new RuntimeException("Incorrect menu type");
65        }
66    }
67
68    private static void checkMenuCount(final MenuBar bar, final int expected) {
69        final int actual = bar.getMenuCount();
70        if (actual != expected) {
71            throw new RuntimeException("Incorrect menus count");
72        }
73    }
74
75    private static void checkCurrentMenu(final MenuBar bar, final Menu menu) {
76        if (bar.getHelpMenu() != menu) {
77            throw new RuntimeException("Wrong HelpMenu");
78        }
79    }
80
81    private static void test1(final MenuBar menuBar) {
82        checkCurrentMenu(menuBar, null);
83        checkMenuCount(menuBar, 0);
84    }
85
86    private static void test2(final MenuBar menuBar) {
87        final Menu helpMenu = new Menu("Help Menu");
88        menuBar.setHelpMenu(helpMenu);
89        checkCurrentMenu(menuBar, helpMenu);
90        checkMenuCount(menuBar, 1);
91        checkHelpMenu(helpMenu, true);
92
93        menuBar.remove(helpMenu);
94        checkCurrentMenu(menuBar, null);
95        checkMenuCount(menuBar, 0);
96        checkHelpMenu(helpMenu, false);
97    }
98
99    private static void test3(final MenuBar menuBar) {
100        final Menu helpMenu1 = new Menu("Help Menu1");
101        final Menu helpMenu2 = new Menu("Help Menu2");
102        menuBar.setHelpMenu(helpMenu1);
103        checkCurrentMenu(menuBar, helpMenu1);
104        checkMenuCount(menuBar, 1);
105        checkHelpMenu(helpMenu1, true);
106        checkHelpMenu(helpMenu2, false);
107
108        menuBar.setHelpMenu(helpMenu2);
109        checkCurrentMenu(menuBar, helpMenu2);
110        checkMenuCount(menuBar, 1);
111        checkHelpMenu(helpMenu1, false);
112        checkHelpMenu(helpMenu2, true);
113
114        menuBar.remove(helpMenu2);
115        checkCurrentMenu(menuBar, null);
116        checkMenuCount(menuBar, 0);
117        checkHelpMenu(helpMenu1, false);
118        checkHelpMenu(helpMenu2, false);
119    }
120
121    private static void test4(final MenuBar menuBar) {
122        final Menu helpMenu = new Menu("Help Menu");
123        menuBar.setHelpMenu(helpMenu);
124        checkCurrentMenu(menuBar, helpMenu);
125        checkMenuCount(menuBar, 1);
126        checkHelpMenu(helpMenu, true);
127
128        menuBar.setHelpMenu(null);
129        checkCurrentMenu(menuBar, null);
130        checkMenuCount(menuBar, 0);
131        checkHelpMenu(helpMenu, false);
132    }
133}
134