Path: blob/master/test/jdk/java/awt/MenuBar/RemoveHelpMenu/RemoveHelpMenu.java
41155 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Frame;24import java.awt.Menu;25import java.awt.MenuBar;2627/**28* @test29* @key headful30* @bug 647536131* @author Sergey Bylokhov32*/33public final class RemoveHelpMenu {3435public static void main(final String[] args) {36final Frame frame = new Frame("RemoveHelpMenu Test");37try {38frame.pack();39// peer exists40test1(getMenuBar(frame));41test2(getMenuBar(frame));42test3(getMenuBar(frame));43test4(getMenuBar(frame));44} finally {45frame.dispose();46}47// peer is null48test1(getMenuBar(frame));49test2(getMenuBar(frame));50test3(getMenuBar(frame));51test4(getMenuBar(frame));52}5354private static MenuBar getMenuBar(final Frame frame) {55final MenuBar menuBar = new MenuBar();56frame.setMenuBar(menuBar);57return menuBar;58}5960private static void checkHelpMenu(final Menu menu, final boolean expected) {61final boolean actual = menu.toString().contains("isHelpMenu=true");62if (actual != expected) {63throw new RuntimeException("Incorrect menu type");64}65}6667private static void checkMenuCount(final MenuBar bar, final int expected) {68final int actual = bar.getMenuCount();69if (actual != expected) {70throw new RuntimeException("Incorrect menus count");71}72}7374private static void checkCurrentMenu(final MenuBar bar, final Menu menu) {75if (bar.getHelpMenu() != menu) {76throw new RuntimeException("Wrong HelpMenu");77}78}7980private static void test1(final MenuBar menuBar) {81checkCurrentMenu(menuBar, null);82checkMenuCount(menuBar, 0);83}8485private static void test2(final MenuBar menuBar) {86final Menu helpMenu = new Menu("Help Menu");87menuBar.setHelpMenu(helpMenu);88checkCurrentMenu(menuBar, helpMenu);89checkMenuCount(menuBar, 1);90checkHelpMenu(helpMenu, true);9192menuBar.remove(helpMenu);93checkCurrentMenu(menuBar, null);94checkMenuCount(menuBar, 0);95checkHelpMenu(helpMenu, false);96}9798private static void test3(final MenuBar menuBar) {99final Menu helpMenu1 = new Menu("Help Menu1");100final Menu helpMenu2 = new Menu("Help Menu2");101menuBar.setHelpMenu(helpMenu1);102checkCurrentMenu(menuBar, helpMenu1);103checkMenuCount(menuBar, 1);104checkHelpMenu(helpMenu1, true);105checkHelpMenu(helpMenu2, false);106107menuBar.setHelpMenu(helpMenu2);108checkCurrentMenu(menuBar, helpMenu2);109checkMenuCount(menuBar, 1);110checkHelpMenu(helpMenu1, false);111checkHelpMenu(helpMenu2, true);112113menuBar.remove(helpMenu2);114checkCurrentMenu(menuBar, null);115checkMenuCount(menuBar, 0);116checkHelpMenu(helpMenu1, false);117checkHelpMenu(helpMenu2, false);118}119120private static void test4(final MenuBar menuBar) {121final Menu helpMenu = new Menu("Help Menu");122menuBar.setHelpMenu(helpMenu);123checkCurrentMenu(menuBar, helpMenu);124checkMenuCount(menuBar, 1);125checkHelpMenu(helpMenu, true);126127menuBar.setHelpMenu(null);128checkCurrentMenu(menuBar, null);129checkMenuCount(menuBar, 0);130checkHelpMenu(helpMenu, false);131}132}133134135