Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/apple/laf/ScreenMenu/ScreenMenuMemoryLeakTest.java
41152 views
1
/*
2
* Copyright (c) 2016, 2018, 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
/**
25
* @test
26
* @key headful
27
* @bug 8158325 8180821 8202878
28
* @summary Memory leak in com.apple.laf.ScreenMenu: removed JMenuItems are still referenced
29
* @requires (os.family == "mac")
30
* @library /javax/swing/regtesthelpers
31
* @build Util
32
* @run main/timeout=300/othervm -Xmx16m ScreenMenuMemoryLeakTest
33
*/
34
35
import java.awt.EventQueue;
36
import java.lang.ref.WeakReference;
37
import java.lang.reflect.InvocationTargetException;
38
import java.util.Objects;
39
40
import javax.swing.JFrame;
41
import javax.swing.JLabel;
42
import javax.swing.JMenu;
43
import javax.swing.JMenuBar;
44
import javax.swing.JMenuItem;
45
import javax.swing.WindowConstants;
46
47
public class ScreenMenuMemoryLeakTest {
48
49
private static WeakReference<JMenuItem> sMenuItem;
50
private static JFrame sFrame;
51
private static JMenu sMenu;
52
53
public static void main(String[] args) throws InvocationTargetException, InterruptedException {
54
EventQueue.invokeAndWait(new Runnable() {
55
@Override
56
public void run() {
57
System.setProperty("apple.laf.useScreenMenuBar", "true");
58
showUI();
59
}
60
});
61
62
EventQueue.invokeAndWait(new Runnable() {
63
@Override
64
public void run() {
65
removeMenuItemFromMenu();
66
}
67
});
68
69
Util.generateOOME();
70
JMenuItem menuItem = sMenuItem.get();
71
EventQueue.invokeAndWait(new Runnable() {
72
@Override
73
public void run() {
74
sFrame.dispose();
75
}
76
});
77
if (menuItem != null) {
78
throw new RuntimeException("The menu item should have been GC-ed");
79
}
80
}
81
82
private static void showUI() {
83
sFrame = new JFrame();
84
sFrame.add(new JLabel("Some dummy content"));
85
86
JMenuBar menuBar = new JMenuBar();
87
88
sMenu = new JMenu("Menu");
89
JMenuItem item = new JMenuItem("Item");
90
sMenu.add(item);
91
92
sMenuItem = new WeakReference<>(item);
93
94
menuBar.add(sMenu);
95
96
sFrame.setJMenuBar(menuBar);
97
98
sFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
99
sFrame.pack();
100
sFrame.setVisible(true);
101
}
102
103
private static void removeMenuItemFromMenu() {
104
JMenuItem menuItem = sMenuItem.get();
105
Objects.requireNonNull(menuItem, "The menu item should still be available at this point");
106
sMenu.remove(menuItem);
107
}
108
}
109
110