Path: blob/master/test/jdk/javax/accessibility/6714324/TabbedPaneMemLeak.java
41149 views
/*1* Copyright (c) 2018, 2020, 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*/2223/* @test24* @key headful25* @bug 671432426* @summary tests if removing a Tab from JTabbedComponent, clears the reference27* to the Page (AccessibleContext) object.28* @modules java.desktop/java.awt:open29* @run main TabbedPaneMemLeak30*/31import javax.accessibility.Accessible;32import javax.accessibility.AccessibleContext;33import javax.swing.JTabbedPane;34import javax.swing.JComponent;35import javax.swing.JPanel;36import javax.swing.JSlider;37import javax.swing.SwingUtilities;3839import java.awt.Component;40import java.lang.reflect.Field;41import java.util.Hashtable;4243public class TabbedPaneMemLeak44{45private static void checkAccessibleParent(Component component) {46//Use reflection to check the value of accessibleContext, since directly calling getAccessibleContext()47//creates one, if not already present.48try {49Field field =50component.getClass().getSuperclass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField(51"accessibleContext");52field.setAccessible(true);53AccessibleContext ctx = (AccessibleContext)field.get(component);54if (ctx != null) {55Field accessibleParentField = field.getType().getDeclaredField("accessibleParent");56accessibleParentField.setAccessible(true);57Accessible parent = (Accessible)accessibleParentField.get(ctx);58if (parent != null) {59throw new RuntimeException("Test failed: AccessibleContext added on the wrong codepath.");60}61}62} catch (NoSuchFieldException | IllegalAccessException e) {63throw new RuntimeException("Test failed: Unable to fetch AccessibleContext");64}6566}6768public static void main(String[] args) throws Exception69{70SwingUtilities.invokeAndWait(() -> {71JTabbedPane tabbedPane = new JTabbedPane();72if (tabbedPane.getAccessibleContext() != null) { // Ensure that the JTabbedPane has an AccessibleContext73JComponent component = new JPanel();74System.out.println(component.getAccessibleContext().getAccessibleParent()); // null75tabbedPane.addTab("Component", component);76System.out.println(component.getAccessibleContext().getAccessibleParent()); // JTabbedPane$Page7778JComponent component1 = new JPanel();79JComponent component2 = new JPanel();8081tabbedPane.addTab("Component1", component1);82tabbedPane.setComponentAt(1, component2);8384if (component1.getAccessibleContext().getAccessibleParent() != null) {85throw new RuntimeException("Test failed: Parent AccessibleContext not cleared from the child component");86}8788tabbedPane.removeAll(); // Could also be tabbedPane.remove(component) or tabbedPane.removeTabAt(0)89if (component.getAccessibleContext().getAccessibleParent() != null) {90throw new RuntimeException("Test failed: Parent AccessibleContext not cleared from the child " +91"component");92}9394JSlider slider = new JSlider(0, 10);95Hashtable<Integer, JComponent> labels = slider.createStandardLabels(5, 2);9697JComponent labelComp = labels.get(labels.keys().nextElement());9899tabbedPane.add(labelComp);100101checkAccessibleParent(labelComp);102103tabbedPane.remove(labelComp);104105checkAccessibleParent(labelComp);106}107});108}109}110111112