Path: blob/master/test/jdk/java/awt/Menu/WrongParentAfterRemoveMenu/WrongParentAfterRemoveMenu.java
41153 views
/*1* Copyright (c) 2016, 2018, 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;26import java.awt.PopupMenu;27import java.awt.Window;2829/**30* @test31* @bug 8165769 819800132* @key headful33*/34public final class WrongParentAfterRemoveMenu {3536public static void main(final String[] args) {37testMenuBar();38testComponent();39testFrame();40}4142private static void testFrame() {43// peer exists44Frame frame = new Frame();45try {46frame.pack();47PopupMenu popupMenu = new PopupMenu();48frame.add(popupMenu);49checkParent(popupMenu, frame);50frame.remove(popupMenu);51checkParent(popupMenu, null);52} finally {53frame.dispose();54}55// peer is null56frame = new Frame();57PopupMenu popupMenu = new PopupMenu();58frame.add(popupMenu);59checkParent(popupMenu, frame);60frame.remove(popupMenu);61checkParent(popupMenu, null);62}6364private static void testComponent() {65// peer exists66Window w = new Window(null);67try {68w.pack();69PopupMenu popupMenu = new PopupMenu();70w.add(popupMenu);71checkParent(popupMenu, w);72w.remove(popupMenu);73checkParent(popupMenu, null);74} finally {75w.dispose();76}77// peer is null78w = new Window(null);79PopupMenu popupMenu = new PopupMenu();80w.add(popupMenu);81checkParent(popupMenu, w);82w.remove(popupMenu);83checkParent(popupMenu, null);84}8586private static void testMenuBar() {87// peer exists88MenuBar mb = new MenuBar();89try {90mb.addNotify();91Menu m1 = new Menu();92Menu m2 = new Menu();93m1.add(m2);94mb.add(m1);95checkParent(m1, mb);96checkParent(m2, m1);97m1.remove(m2);98checkParent(m2, null);99mb.remove(m1);100checkParent(m1, null);101} finally {102mb.removeNotify();103}104// peer is null105mb = new MenuBar();106Menu m1 = new Menu();107Menu m2 = new Menu();108m1.add(m2);109mb.add(m1);110checkParent(m1, mb);111checkParent(m2, m1);112m1.remove(m2);113checkParent(m2, null);114mb.remove(m1);115checkParent(m1, null);116}117118private static void checkParent(final Menu menu, final Object parent) {119if (menu.getParent() != parent) {120System.err.println("Expected: " + parent);121System.err.println("Actual: " + menu.getParent());122throw new RuntimeException("Wrong parent");123}124}125}126127128