提交 2e2faa5c 编写于 作者: S serb

6475361: Attempting to remove help menu from java.awt.MenuBar throws NullPointerException

Reviewed-by: azvegint, ant
上级 736effa1
/* /*
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -181,7 +181,7 @@ public class MenuBar extends MenuComponent implements MenuContainer, Accessible ...@@ -181,7 +181,7 @@ public class MenuBar extends MenuComponent implements MenuContainer, Accessible
* removed from the menu bar, and replaced with the specified menu. * removed from the menu bar, and replaced with the specified menu.
* @param m the menu to be set as the help menu * @param m the menu to be set as the help menu
*/ */
public void setHelpMenu(Menu m) { public void setHelpMenu(final Menu m) {
synchronized (getTreeLock()) { synchronized (getTreeLock()) {
if (helpMenu == m) { if (helpMenu == m) {
return; return;
...@@ -189,11 +189,11 @@ public class MenuBar extends MenuComponent implements MenuContainer, Accessible ...@@ -189,11 +189,11 @@ public class MenuBar extends MenuComponent implements MenuContainer, Accessible
if (helpMenu != null) { if (helpMenu != null) {
remove(helpMenu); remove(helpMenu);
} }
if (m.parent != this) {
add(m);
}
helpMenu = m; helpMenu = m;
if (m != null) { if (m != null) {
if (m.parent != this) {
add(m);
}
m.isHelpMenu = true; m.isHelpMenu = true;
m.parent = this; m.parent = this;
MenuBarPeer peer = (MenuBarPeer)this.peer; MenuBarPeer peer = (MenuBarPeer)this.peer;
...@@ -242,7 +242,7 @@ public class MenuBar extends MenuComponent implements MenuContainer, Accessible ...@@ -242,7 +242,7 @@ public class MenuBar extends MenuComponent implements MenuContainer, Accessible
* @param index the position of the menu to be removed. * @param index the position of the menu to be removed.
* @see java.awt.MenuBar#add(java.awt.Menu) * @see java.awt.MenuBar#add(java.awt.Menu)
*/ */
public void remove(int index) { public void remove(final int index) {
synchronized (getTreeLock()) { synchronized (getTreeLock()) {
Menu m = getMenu(index); Menu m = getMenu(index);
menus.removeElementAt(index); menus.removeElementAt(index);
...@@ -252,6 +252,10 @@ public class MenuBar extends MenuComponent implements MenuContainer, Accessible ...@@ -252,6 +252,10 @@ public class MenuBar extends MenuComponent implements MenuContainer, Accessible
m.parent = null; m.parent = null;
peer.delMenu(index); peer.delMenu(index);
} }
if (helpMenu == m) {
helpMenu = null;
m.isHelpMenu = false;
}
} }
} }
......
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Frame;
import java.awt.Menu;
import java.awt.MenuBar;
/**
* @test
* @bug 6475361
* @author Sergey Bylokhov
*/
public final class RemoveHelpMenu {
public static void main(final String[] args) {
final Frame frame = new Frame("RemoveHelpMenu Test");
try {
frame.pack();
// peer exists
test1(getMenuBar(frame));
test2(getMenuBar(frame));
test3(getMenuBar(frame));
test4(getMenuBar(frame));
} finally {
frame.dispose();
}
// peer is null
test1(getMenuBar(frame));
test2(getMenuBar(frame));
test3(getMenuBar(frame));
test4(getMenuBar(frame));
}
private static MenuBar getMenuBar(final Frame frame) {
final MenuBar menuBar = new MenuBar();
frame.setMenuBar(menuBar);
return menuBar;
}
private static void checkHelpMenu(final Menu menu, final boolean expected) {
final boolean actual = menu.toString().contains("isHelpMenu=true");
if (actual != expected) {
throw new RuntimeException("Incorrect menu type");
}
}
private static void checkMenuCount(final MenuBar bar, final int expected) {
final int actual = bar.getMenuCount();
if (actual != expected) {
throw new RuntimeException("Incorrect menus count");
}
}
private static void checkCurrentMenu(final MenuBar bar, final Menu menu) {
if (bar.getHelpMenu() != menu) {
throw new RuntimeException("Wrong HelpMenu");
}
}
private static void test1(final MenuBar menuBar) {
checkCurrentMenu(menuBar, null);
checkMenuCount(menuBar, 0);
}
private static void test2(final MenuBar menuBar) {
final Menu helpMenu = new Menu("Help Menu");
menuBar.setHelpMenu(helpMenu);
checkCurrentMenu(menuBar, helpMenu);
checkMenuCount(menuBar, 1);
checkHelpMenu(helpMenu, true);
menuBar.remove(helpMenu);
checkCurrentMenu(menuBar, null);
checkMenuCount(menuBar, 0);
checkHelpMenu(helpMenu, false);
}
private static void test3(final MenuBar menuBar) {
final Menu helpMenu1 = new Menu("Help Menu1");
final Menu helpMenu2 = new Menu("Help Menu2");
menuBar.setHelpMenu(helpMenu1);
checkCurrentMenu(menuBar, helpMenu1);
checkMenuCount(menuBar, 1);
checkHelpMenu(helpMenu1, true);
checkHelpMenu(helpMenu2, false);
menuBar.setHelpMenu(helpMenu2);
checkCurrentMenu(menuBar, helpMenu2);
checkMenuCount(menuBar, 1);
checkHelpMenu(helpMenu1, false);
checkHelpMenu(helpMenu2, true);
menuBar.remove(helpMenu2);
checkCurrentMenu(menuBar, null);
checkMenuCount(menuBar, 0);
checkHelpMenu(helpMenu1, false);
checkHelpMenu(helpMenu2, false);
}
private static void test4(final MenuBar menuBar) {
final Menu helpMenu = new Menu("Help Menu");
menuBar.setHelpMenu(helpMenu);
checkCurrentMenu(menuBar, helpMenu);
checkMenuCount(menuBar, 1);
checkHelpMenu(helpMenu, true);
menuBar.setHelpMenu(null);
checkCurrentMenu(menuBar, null);
checkMenuCount(menuBar, 0);
checkHelpMenu(helpMenu, false);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册