Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/accessibility/6714324/TabbedPaneMemLeak.java
41149 views
1
/*
2
* Copyright (c) 2018, 2020, 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
/* @test
25
* @key headful
26
* @bug 6714324
27
* @summary tests if removing a Tab from JTabbedComponent, clears the reference
28
* to the Page (AccessibleContext) object.
29
* @modules java.desktop/java.awt:open
30
* @run main TabbedPaneMemLeak
31
*/
32
import javax.accessibility.Accessible;
33
import javax.accessibility.AccessibleContext;
34
import javax.swing.JTabbedPane;
35
import javax.swing.JComponent;
36
import javax.swing.JPanel;
37
import javax.swing.JSlider;
38
import javax.swing.SwingUtilities;
39
40
import java.awt.Component;
41
import java.lang.reflect.Field;
42
import java.util.Hashtable;
43
44
public class TabbedPaneMemLeak
45
{
46
private static void checkAccessibleParent(Component component) {
47
//Use reflection to check the value of accessibleContext, since directly calling getAccessibleContext()
48
//creates one, if not already present.
49
try {
50
Field field =
51
component.getClass().getSuperclass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField(
52
"accessibleContext");
53
field.setAccessible(true);
54
AccessibleContext ctx = (AccessibleContext)field.get(component);
55
if (ctx != null) {
56
Field accessibleParentField = field.getType().getDeclaredField("accessibleParent");
57
accessibleParentField.setAccessible(true);
58
Accessible parent = (Accessible)accessibleParentField.get(ctx);
59
if (parent != null) {
60
throw new RuntimeException("Test failed: AccessibleContext added on the wrong codepath.");
61
}
62
}
63
} catch (NoSuchFieldException | IllegalAccessException e) {
64
throw new RuntimeException("Test failed: Unable to fetch AccessibleContext");
65
}
66
67
}
68
69
public static void main(String[] args) throws Exception
70
{
71
SwingUtilities.invokeAndWait(() -> {
72
JTabbedPane tabbedPane = new JTabbedPane();
73
if (tabbedPane.getAccessibleContext() != null) { // Ensure that the JTabbedPane has an AccessibleContext
74
JComponent component = new JPanel();
75
System.out.println(component.getAccessibleContext().getAccessibleParent()); // null
76
tabbedPane.addTab("Component", component);
77
System.out.println(component.getAccessibleContext().getAccessibleParent()); // JTabbedPane$Page
78
79
JComponent component1 = new JPanel();
80
JComponent component2 = new JPanel();
81
82
tabbedPane.addTab("Component1", component1);
83
tabbedPane.setComponentAt(1, component2);
84
85
if (component1.getAccessibleContext().getAccessibleParent() != null) {
86
throw new RuntimeException("Test failed: Parent AccessibleContext not cleared from the child component");
87
}
88
89
tabbedPane.removeAll(); // Could also be tabbedPane.remove(component) or tabbedPane.removeTabAt(0)
90
if (component.getAccessibleContext().getAccessibleParent() != null) {
91
throw new RuntimeException("Test failed: Parent AccessibleContext not cleared from the child " +
92
"component");
93
}
94
95
JSlider slider = new JSlider(0, 10);
96
Hashtable<Integer, JComponent> labels = slider.createStandardLabels(5, 2);
97
98
JComponent labelComp = labels.get(labels.keys().nextElement());
99
100
tabbedPane.add(labelComp);
101
102
checkAccessibleParent(labelComp);
103
104
tabbedPane.remove(labelComp);
105
106
checkAccessibleParent(labelComp);
107
}
108
});
109
}
110
}
111
112