author | ssadetsky |
Mon, 16 Oct 2017 08:06:56 -0700 | |
changeset 47387 | 4d711a58bb3b |
parent 47216 | 71c04702a3d5 |
child 50144 | 2a85d59e7f8b |
permissions | -rw-r--r-- |
39539
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
1 |
/* |
46151
5fa789776f7d
8185500: [TESTBUG] Add keywords headful/printer in java/awt and javax tests.
goetz
parents:
45028
diff
changeset
|
2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
39539
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
4 |
* |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
8 |
* |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
13 |
* accompanied this code). |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
14 |
* |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
18 |
* |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
21 |
* questions. |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
22 |
*/ |
46151
5fa789776f7d
8185500: [TESTBUG] Add keywords headful/printer in java/awt and javax tests.
goetz
parents:
45028
diff
changeset
|
23 |
|
5fa789776f7d
8185500: [TESTBUG] Add keywords headful/printer in java/awt and javax tests.
goetz
parents:
45028
diff
changeset
|
24 |
/** |
39539
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
25 |
* @test |
46151
5fa789776f7d
8185500: [TESTBUG] Add keywords headful/printer in java/awt and javax tests.
goetz
parents:
45028
diff
changeset
|
26 |
* @key headful |
47387
4d711a58bb3b
8180821: [macosx]com/apple/laf/ScreenMenu/ScreenMenuMemoryLeakTest.java: The menu item should have been GC-ed
ssadetsky
parents:
47216
diff
changeset
|
27 |
* @bug 8158325 8180821 |
39539
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
28 |
* @summary Memory leak in com.apple.laf.ScreenMenu: removed JMenuItems are still referenced |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
29 |
* @requires (os.family == "mac") |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
30 |
* @run main/timeout=300/othervm -Xmx16m ScreenMenuMemoryLeakTest |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
31 |
*/ |
46151
5fa789776f7d
8185500: [TESTBUG] Add keywords headful/printer in java/awt and javax tests.
goetz
parents:
45028
diff
changeset
|
32 |
|
39539
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
33 |
import java.awt.EventQueue; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
34 |
import java.lang.ref.WeakReference; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
35 |
import java.lang.reflect.InvocationTargetException; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
36 |
import java.util.Objects; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
37 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
38 |
import javax.swing.JFrame; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
39 |
import javax.swing.JLabel; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
40 |
import javax.swing.JMenu; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
41 |
import javax.swing.JMenuBar; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
42 |
import javax.swing.JMenuItem; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
43 |
import javax.swing.WindowConstants; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
44 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
45 |
public class ScreenMenuMemoryLeakTest { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
46 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
47 |
private static byte[] sBytes; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
48 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
49 |
private static WeakReference<JMenuItem> sMenuItem; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
50 |
private static JFrame sFrame; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
51 |
private static JMenu sMenu; |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
52 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
53 |
public static void main(String[] args) throws InvocationTargetException, InterruptedException { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
54 |
EventQueue.invokeAndWait(new Runnable() { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
55 |
@Override |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
56 |
public void run() { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
57 |
System.setProperty("apple.laf.useScreenMenuBar", "true"); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
58 |
showUI(); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
59 |
} |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
60 |
}); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
61 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
62 |
EventQueue.invokeAndWait(new Runnable() { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
63 |
@Override |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
64 |
public void run() { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
65 |
removeMenuItemFromMenu(); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
66 |
} |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
67 |
}); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
68 |
System.gc(); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
69 |
System.runFinalization(); |
47387
4d711a58bb3b
8180821: [macosx]com/apple/laf/ScreenMenu/ScreenMenuMemoryLeakTest.java: The menu item should have been GC-ed
ssadetsky
parents:
47216
diff
changeset
|
70 |
Thread.sleep(1000); |
39539
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
71 |
JMenuItem menuItem = sMenuItem.get(); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
72 |
EventQueue.invokeAndWait(new Runnable() { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
73 |
@Override |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
74 |
public void run() { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
75 |
sFrame.dispose(); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
76 |
} |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
77 |
}); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
78 |
if (menuItem != null) { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
79 |
throw new RuntimeException("The menu item should have been GC-ed"); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
80 |
} |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
81 |
} |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
82 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
83 |
private static void showUI() { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
84 |
sFrame = new JFrame(); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
85 |
sFrame.add(new JLabel("Some dummy content")); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
86 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
87 |
JMenuBar menuBar = new JMenuBar(); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
88 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
89 |
sMenu = new JMenu("Menu"); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
90 |
JMenuItem item = new JMenuItem("Item"); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
91 |
sMenu.add(item); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
92 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
93 |
sMenuItem = new WeakReference<>(item); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
94 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
95 |
menuBar.add(sMenu); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
96 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
97 |
sFrame.setJMenuBar(menuBar); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
98 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
99 |
sFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
100 |
sFrame.pack(); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
101 |
sFrame.setVisible(true); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
102 |
} |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
103 |
|
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
104 |
private static void removeMenuItemFromMenu() { |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
105 |
JMenuItem menuItem = sMenuItem.get(); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
106 |
Objects.requireNonNull(menuItem, "The menu item should still be available at this point"); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
107 |
sMenu.remove(menuItem); |
c6d88f43892c
8158325: [macosx] Memory leak in com.apple.laf.ScreenMenu
alexsch
parents:
diff
changeset
|
108 |
} |
45028
b0ea3c0bfb81
8179889: Fix typographic errors in copyright headers
ihse
parents:
39539
diff
changeset
|
109 |
} |