Path: blob/master/test/jdk/java/awt/Frame/DisposeParentGC/DisposeParentGC.java
41153 views
/*1* Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.*;24import java.awt.image.BufferedImage;25import java.lang.ref.PhantomReference;26import java.lang.ref.ReferenceQueue;27import java.util.ArrayList;28import java.util.Vector;2930/*31* @test32* @key headful33* @summary Display a dialog with a parent, the dialog contains all awt components34* added to it & each components are setted with different cursors types.35* Dispose the parent & collect GC. Garbage collection should happen36* @author Dmitriy Ermashov ([email protected])37* @library /lib/client38* @build ExtendedRobot39* @run main/othervm -Xmx20m DisposeParentGC40*/4142public class DisposeParentGC {43Frame parentFrame;44ExtendedRobot robot;4546ArrayList<PhantomReference<Dialog>> refs = new ArrayList<PhantomReference<Dialog>>();47ReferenceQueue<Dialog> que = new ReferenceQueue<>();4849public static void main(String []args) throws Exception {50new DisposeParentGC().doTest();51}5253DisposeParentGC() throws Exception {54robot = new ExtendedRobot();55EventQueue.invokeAndWait(this::initGui);56}5758void initGui(){59parentFrame = new Frame("Parent Frame");60parentFrame.setLayout(new FlowLayout());6162for (int i = 1; i <= 3; i++)63createDialog(i);6465parentFrame.setLocation(250, 20);66parentFrame.pack();67parentFrame.setVisible(true);68}6970public void doTest() throws Exception{71robot.waitForIdle();7273parentFrame.dispose();74robot.waitForIdle();7576Vector garbage = new Vector();77while (true) {78try {79garbage.add(new byte[1000]);80} catch (OutOfMemoryError er) {81break;82}83}84garbage = null;8586int count = 1;87for (; count <= 3; count++)88if(que.remove(5000) == null)89break;9091if (count < 3)92throw new RuntimeException("Count = "+count+". GC didn't collect the objects after the parent is disposed!");93}9495public void createDialog(int number) {96Dialog child = new Dialog(parentFrame);97child.setTitle("Dialog " + number);98child.setLayout(new FlowLayout());99child.setLocation(20, 140 * number);100101Button button = new Button("Press Me") ;102TextArea textArea = new TextArea(5,5);103TextField textField = new TextField(10);104Choice choice = new Choice();105choice.add("One");106choice.add("Two");107choice.add("Three");108choice.add("Four");109choice.add("Five");110List list = new List();111list.add("One");112list.add("Two");113list.add("Three");114list.add("Four");115list.add("Five");116Checkbox checkBox = new Checkbox("Hai");117Scrollbar scrollBar = new Scrollbar(Scrollbar.VERTICAL,0,1,0,200);118CheckboxGroup checkboxGroup = new CheckboxGroup();119Checkbox radioButton = new Checkbox("Hello" ,true, checkboxGroup);120Canvas canvas = new Canvas();121Label label = new Label("I am label!");122Cursor customCursor = null;123124child.setLayout(new FlowLayout());125canvas.setSize(100,100);126canvas.setBackground(Color.red);127128button.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));129label.setCursor(new Cursor(Cursor.TEXT_CURSOR));130choice.setCursor(new Cursor(Cursor.WAIT_CURSOR));131list.setCursor(new Cursor(Cursor.HAND_CURSOR));132checkBox.setCursor(new Cursor(Cursor.MOVE_CURSOR));133radioButton.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));134scrollBar.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));135canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));136textField.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));137138/* create a custom cursor */139Toolkit toolkit = Toolkit.getDefaultToolkit();140Dimension d = toolkit.getBestCursorSize(32,32);141int color = toolkit.getMaximumCursorColors();142143if(!d.equals(new Dimension(0,0)) && color != 0 )144customCursor = toolkit.createCustomCursor(new BufferedImage( 16, 16, BufferedImage.TYPE_INT_RGB ), new Point(10, 10), "custom cursor.");145else146System.err.println("Platform doesn't support to create a custom cursor.");147148textArea.setCursor(customCursor);149child.add(label);150child.add(button);151child.add(choice);152child.add(list);153child.add(checkBox);154child.add(radioButton);155child.add(scrollBar);156child.add(canvas);157child.add(textArea);158child.add(textField);159child.add(button);160child.revalidate();161162child.pack();163child.setVisible(true);164refs.add(new PhantomReference<Dialog>(child, que));165}166}167168169