Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JComboBox/TestMemLeakComboBox.java
41149 views
1
/*
2
* Copyright (c) 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 6542439
27
* @summary Verifies memory leak in BasicComboBoxUI and MetalComboBoxButton
28
* @run main TestMemLeakComboBox
29
*/
30
import java.awt.Graphics;
31
import java.awt.Component;
32
import java.awt.Container;
33
import java.awt.Dimension;
34
import java.awt.FlowLayout;
35
import javax.swing.JFrame;
36
import javax.swing.JPanel;
37
import javax.swing.JComboBox;
38
import javax.swing.CellRendererPane;
39
import javax.swing.SwingUtilities;
40
import javax.swing.UIManager;
41
import javax.swing.UnsupportedLookAndFeelException;
42
43
public class TestMemLeakComboBox {
44
private static JFrame frame;
45
private static String failed = null;
46
47
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
48
try {
49
UIManager.setLookAndFeel(laf.getClassName());
50
} catch (UnsupportedLookAndFeelException ignored) {
51
System.out.println("Unsupported L&F: " + laf.getClassName());
52
} catch (ClassNotFoundException | InstantiationException
53
| IllegalAccessException e) {
54
throw new RuntimeException(e);
55
}
56
}
57
58
private static class MyPanel extends JPanel {
59
public void paint(Graphics g) {
60
super.paint(g);
61
for (Component child : getComponents()) {
62
verifyChild(child);
63
}
64
}
65
66
private void verifyChild(Component c) {
67
if (c instanceof JComboBox) {
68
for (Component child : ((Container)c).getComponents()) {
69
if (child instanceof CellRendererPane &&
70
((CellRendererPane)child).getComponentCount() > 0) {
71
failed = new String("CellRendererPane still has children for: " + c);
72
}
73
}
74
}
75
}
76
}
77
78
public static void main(String[] args) throws Exception {
79
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
80
System.out.println("Testing l&f : " + laf.getClassName());
81
SwingUtilities.invokeAndWait(() -> setLookAndFeel(laf));
82
test();
83
if (failed != null) {
84
throw new RuntimeException(failed);
85
}
86
}
87
}
88
89
private static void test() throws Exception {
90
try {
91
SwingUtilities.invokeAndWait(() -> {
92
frame = new JFrame();
93
JPanel panel = new MyPanel();
94
panel.setPreferredSize(new Dimension(100, 100));
95
panel.setLayout(new FlowLayout());
96
panel.add(new JComboBox(new String[]{"one", "two", "three"}));
97
98
frame.add(panel);
99
frame.pack();
100
frame.setVisible(true);
101
});
102
} finally {
103
if (frame != null) {
104
SwingUtilities.invokeAndWait(() -> frame.dispose());
105
}
106
}
107
}
108
}
109
110