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 Verify that disposed frames are collected with GC 34 * @author Dmitriy Ermashov (dmitriy.ermashov@oracle.com) 35 * @library ../../../../lib/testlibrary 36 * @build ExtendedRobot 37 * @run main FramesGC 38 */ 39 40 41 public class FramesGC { 42 43 ExtendedRobot robot; 44 ArrayList<PhantomReference<Frame>> refs = new ArrayList<PhantomReference<Frame>>(); 45 ReferenceQueue<Frame> que = new ReferenceQueue<Frame>(); 46 47 public static void main(String []args) throws Exception { 48 new FramesGC().doTest(); 49 } 50 51 FramesGC() throws Exception{ 52 robot = new ExtendedRobot(); 53 } 54 55 void doTest() throws Exception { 56 for( int i = 1; i <= 3; i++) { 57 final int j = i; 58 EventQueue.invokeAndWait(() -> { 59 createFrame(j); 60 }); 61 } 62 robot.waitForIdle(); 63 64 for (Frame f : Frame.getFrames()) 65 f.dispose(); 66 67 robot.waitForIdle(); 68 69 Vector garbage = new Vector(); 70 while (true) { 71 try { 72 garbage.add(new byte[1000]); 73 } catch (OutOfMemoryError er) { 74 break; 75 } 76 } 77 garbage = null; 78 79 int count = 1; 80 for(; count <= 3; count++) 81 if(que.remove(5000) == null) 82 break; 83 84 System.out.println("Total no of instances eligible for GC = " + count); 85 if(count < 3) 86 throw new RuntimeException("Count = "+count+". Test failed!"); 87 88 } 89 90 void createFrame(int i){ 91 Frame frame = new Frame("Frame " + i); 92 93 Button button=new Button("Press Me"); 94 TextArea textArea=new TextArea(5,5); 95 TextField textField=new TextField(10); 96 Choice choice=new Choice(); 97 choice.add("One"); 98 choice.add("Two"); 99 choice.add("Three"); 100 choice.add("Four"); 101 choice.add("Five"); 102 List list = new List(); 103 list.add("One"); 104 list.add("Two"); 105 list.add("Three"); 106 list.add("Four"); 107 list.add("Five"); 108 Checkbox checkBox= new Checkbox("Hai"); 109 Scrollbar scrollBar=new Scrollbar(Scrollbar.VERTICAL,0,1,0,200); 110 CheckboxGroup checkboxGroup=new CheckboxGroup(); 111 Checkbox radioButton=new Checkbox("Hello" ,true, checkboxGroup); 112 Canvas canvas=new Canvas(); 113 canvas.setSize(100, 100); 114 canvas.setBackground(java.awt.Color.red); 115 Label label=new Label("I am label.!"); 116 Cursor customCursor=null; 117 118 frame.setLayout(new java.awt.FlowLayout()); 119 120 button.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); 121 label.setCursor(new Cursor(Cursor.TEXT_CURSOR)); 122 choice.setCursor(new Cursor(Cursor.WAIT_CURSOR)); 123 list.setCursor(new Cursor(Cursor.HAND_CURSOR)); 124 checkBox.setCursor(new Cursor(Cursor.MOVE_CURSOR)); 125 radioButton.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR)); 126 scrollBar.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR)); 127 canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR)); 128 textField.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); 129 130 /* create a custom cursor */ 131 Toolkit toolkit = Toolkit.getDefaultToolkit(); 132 Dimension d = toolkit.getBestCursorSize(32,32); 133 int color = toolkit.getMaximumCursorColors(); 134 135 if(!d.equals(new Dimension(0,0)) && color != 0 ) 136 customCursor = toolkit.createCustomCursor(new BufferedImage( 16, 16, BufferedImage.TYPE_INT_RGB ), new Point(10, 10), "custom cursor."); 137 else 138 System.err.println("Platform doesn't support to create a custom cursor."); 139 140 textArea.setCursor(customCursor); 141 frame.add(label); 142 frame.add(button); 143 frame.add(choice); 144 frame.add(list); 145 frame.add(checkBox); 146 frame.add(radioButton); 147 frame.add(scrollBar); 148 frame.add(canvas); 149 frame.add(textArea); 150 frame.add(textField); 151 frame.add(button); 152 153 frame.setLocation(20, 140 * i); 154 frame.pack(); 155 frame.setVisible(true); 156 refs.add(new PhantomReference<Frame>(frame, que)); 157 } 158 159 }