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