1 /* 2 * Copyright (c) 2016, 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 import java.awt.PopupMenu; 28 import java.awt.Window; 29 30 /** 31 * @test 32 * @bug 8165769 33 * @key headful 34 */ 35 public final class WrongParentAfterRemoveMenu { 36 37 public static void main(final String[] args) { 38 testMenuBar(); 39 testComponent(); 40 testFrame(); 41 } 42 43 private static void testFrame() { 44 // peer exists 45 Frame frame = new Frame(); 46 try { 47 frame.pack(); 48 PopupMenu popupMenu = new PopupMenu(); 49 frame.add(popupMenu); 50 checkParent(popupMenu, frame); 51 frame.remove(popupMenu); 52 checkParent(popupMenu, null); 53 } finally { 54 frame.dispose(); 55 } 56 // peer is null 57 frame = new Frame(); 58 PopupMenu popupMenu = new PopupMenu(); 59 frame.add(popupMenu); 60 checkParent(popupMenu, frame); 61 frame.remove(popupMenu); 62 checkParent(popupMenu, null); 63 } 64 65 private static void testComponent() { 66 // peer exists 67 Window w = new Window(null); 68 try { 69 w.pack(); 70 PopupMenu popupMenu = new PopupMenu(); 71 w.add(popupMenu); 72 checkParent(popupMenu, w); 73 w.remove(popupMenu); 74 checkParent(popupMenu, null); 75 } finally { 76 w.dispose(); 77 } 78 // peer is null 79 w = new Window(null); 80 PopupMenu popupMenu = new PopupMenu(); 81 w.add(popupMenu); 82 checkParent(popupMenu, w); 83 w.remove(popupMenu); 84 checkParent(popupMenu, null); 85 } 86 87 private static void testMenuBar() { 88 // peer exists 89 MenuBar mb = new MenuBar(); 90 try { 91 mb.addNotify(); 92 Menu m1 = new Menu(); 93 Menu m2 = new Menu(); 94 m1.add(m2); 95 mb.add(m1); 96 checkParent(m1, mb); 97 checkParent(m2, m1); 98 m1.remove(m2); 99 checkParent(m2, null); 100 mb.remove(m1); 101 checkParent(m1, null); 102 } finally { 103 mb.removeNotify(); 104 } 105 // peer is null 106 mb = new MenuBar(); 107 Menu m1 = new Menu(); 108 Menu m2 = new Menu(); 109 m1.add(m2); 110 mb.add(m1); 111 checkParent(m1, mb); 112 checkParent(m2, m1); 113 m1.remove(m2); 114 checkParent(m2, null); 115 mb.remove(m1); 116 checkParent(m1, null); 117 } 118 119 private static void checkParent(final Menu menu, final Object parent) { 120 if (menu.getParent() != parent) { 121 System.err.println("Expected: " + parent); 122 System.err.println("Actual: " + menu.getParent()); 123 throw new RuntimeException("Wrong parent"); 124 } 125 } 126 }