1 /* 2 * Copyright (c) 2014, 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 import java.awt.*; 25 import java.awt.image.BufferedImage; 26 import java.lang.ref.PhantomReference; 27 import java.lang.ref.ReferenceQueue; 28 import java.util.ArrayList; 29 import java.util.Vector; 30 31 /* 32 * @test 33 * @summary Display a dialog with a parent, the dialog contains all awt components 34 * added to it & each components are setted with different cursors types. 35 * Dispose the parent & collect GC. Garbage collection should happen 36 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com) 37 * @library ../../../../lib/testlibrary 38 * @build ExtendedRobot 39 * @run main/othervm -Xmx20m DisposeParentGC 40 */ 41 42 public class DisposeParentGC { 43 Frame parentFrame; 44 ExtendedRobot robot; 45 46 ArrayList<PhantomReference<Dialog>> refs = new ArrayList<PhantomReference<Dialog>>(); 47 ReferenceQueue<Dialog> que = new ReferenceQueue<>(); 48 49 public static void main(String []args) throws Exception { 50 new DisposeParentGC().doTest(); 51 } 52 53 DisposeParentGC() throws Exception { 54 robot = new ExtendedRobot(); 55 EventQueue.invokeAndWait(this::initGui); 56 } 57 58 void initGui(){ 59 parentFrame = new Frame("Parent Frame"); 60 parentFrame.setLayout(new FlowLayout()); 61 62 for (int i = 1; i <= 3; i++) 63 createDialog(i); 64 65 parentFrame.setLocation(250, 20); 66 parentFrame.pack(); 67 parentFrame.setVisible(true); 68 } 69 70 public void doTest() throws Exception{ 71 robot.waitForIdle(); 72 73 parentFrame.dispose(); 74 robot.waitForIdle(); 75 76 Vector garbage = new Vector(); 77 while (true) { 78 try { 79 garbage.add(new byte[1000]); 80 } catch (OutOfMemoryError er) { 81 break; 82 } 83 } 84 garbage = null; 85 86 int count = 1; 87 for (; count <= 3; count++) 88 if(que.remove(5000) == null) 89 break; 90 91 if (count < 3) 92 throw new RuntimeException("Count = "+count+". GC didn't collect the objects after the parent is disposed!"); 93 } 94 95 public void createDialog(int number) { 96 Dialog child = new Dialog(parentFrame); 97 child.setTitle("Dialog " + number); 98 child.setLayout(new FlowLayout()); 99 child.setLocation(20, 140 * number); 100 101 Button button = new Button("Press Me") ; 102 TextArea textArea = new TextArea(5,5); 103 TextField textField = new TextField(10); 104 Choice choice = new Choice(); 105 choice.add("One"); 106 choice.add("Two"); 107 choice.add("Three"); 108 choice.add("Four"); 109 choice.add("Five"); 110 List list = new List(); 111 list.add("One"); 112 list.add("Two"); 113 list.add("Three"); 114 list.add("Four"); 115 list.add("Five"); 116 Checkbox checkBox = new Checkbox("Hai"); 117 Scrollbar scrollBar = new Scrollbar(Scrollbar.VERTICAL,0,1,0,200); 118 CheckboxGroup checkboxGroup = new CheckboxGroup(); 119 Checkbox radioButton = new Checkbox("Hello" ,true, checkboxGroup); 120 Canvas canvas = new Canvas(); 121 Label label = new Label("I am label!"); 122 Cursor customCursor = null; 123 124 child.setLayout(new FlowLayout()); 125 canvas.setSize(100,100); 126 canvas.setBackground(Color.red); 127 128 button.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); 129 label.setCursor(new Cursor(Cursor.TEXT_CURSOR)); 130 choice.setCursor(new Cursor(Cursor.WAIT_CURSOR)); 131 list.setCursor(new Cursor(Cursor.HAND_CURSOR)); 132 checkBox.setCursor(new Cursor(Cursor.MOVE_CURSOR)); 133 radioButton.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR)); 134 scrollBar.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR)); 135 canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); 136 textField.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 137 138 /* create a custom cursor */ 139 Toolkit toolkit = Toolkit.getDefaultToolkit(); 140 Dimension d = toolkit.getBestCursorSize(32,32); 141 int color = toolkit.getMaximumCursorColors(); 142 143 if(!d.equals(new Dimension(0,0)) && color != 0 ) 144 customCursor = toolkit.createCustomCursor(new BufferedImage( 16, 16, BufferedImage.TYPE_INT_RGB ), new Point(10, 10), "custom cursor."); 145 else 146 System.err.println("Platform doesn't support to create a custom cursor."); 147 148 textArea.setCursor(customCursor); 149 child.add(label); 150 child.add(button); 151 child.add(choice); 152 child.add(list); 153 child.add(checkBox); 154 child.add(radioButton); 155 child.add(scrollBar); 156 child.add(canvas); 157 child.add(textArea); 158 child.add(textField); 159 child.add(button); 160 child.revalidate(); 161 162 child.pack(); 163 child.setVisible(true); 164 refs.add(new PhantomReference<Dialog>(child, que)); 165 } 166 }