Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Frame/DisposeParentGC/DisposeParentGC.java
38828 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* @summary Display a dialog with a parent, the dialog contains all awt components33* added to it & each components are setted with different cursors types.34* Dispose the parent & collect GC. Garbage collection should happen35* @author Dmitriy Ermashov ([email protected])36* @library ../../../../lib/testlibrary37* @build ExtendedRobot38* @run main/othervm -Xmx20m DisposeParentGC39*/4041public class DisposeParentGC {42Frame parentFrame;43ExtendedRobot robot;4445ArrayList<PhantomReference<Dialog>> refs = new ArrayList<PhantomReference<Dialog>>();46ReferenceQueue<Dialog> que = new ReferenceQueue<>();4748public static void main(String []args) throws Exception {49new DisposeParentGC().doTest();50}5152DisposeParentGC() throws Exception {53robot = new ExtendedRobot();54EventQueue.invokeAndWait(this::initGui);55}5657void initGui(){58parentFrame = new Frame("Parent Frame");59parentFrame.setLayout(new FlowLayout());6061for (int i = 1; i <= 3; i++)62createDialog(i);6364parentFrame.setLocation(250, 20);65parentFrame.pack();66parentFrame.setVisible(true);67}6869public void doTest() throws Exception{70robot.waitForIdle();7172parentFrame.dispose();73robot.waitForIdle();7475Vector garbage = new Vector();76while (true) {77try {78garbage.add(new byte[1000]);79} catch (OutOfMemoryError er) {80break;81}82}83garbage = null;8485int count = 1;86for (; count <= 3; count++)87if(que.remove(5000) == null)88break;8990if (count < 3)91throw new RuntimeException("Count = "+count+". GC didn't collect the objects after the parent is disposed!");92}9394public void createDialog(int number) {95Dialog child = new Dialog(parentFrame);96child.setTitle("Dialog " + number);97child.setLayout(new FlowLayout());98child.setLocation(20, 140 * number);99100Button button = new Button("Press Me") ;101TextArea textArea = new TextArea(5,5);102TextField textField = new TextField(10);103Choice choice = new Choice();104choice.add("One");105choice.add("Two");106choice.add("Three");107choice.add("Four");108choice.add("Five");109List list = new List();110list.add("One");111list.add("Two");112list.add("Three");113list.add("Four");114list.add("Five");115Checkbox checkBox = new Checkbox("Hai");116Scrollbar scrollBar = new Scrollbar(Scrollbar.VERTICAL,0,1,0,200);117CheckboxGroup checkboxGroup = new CheckboxGroup();118Checkbox radioButton = new Checkbox("Hello" ,true, checkboxGroup);119Canvas canvas = new Canvas();120Label label = new Label("I am label!");121Cursor customCursor = null;122123child.setLayout(new FlowLayout());124canvas.setSize(100,100);125canvas.setBackground(Color.red);126127button.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));128label.setCursor(new Cursor(Cursor.TEXT_CURSOR));129choice.setCursor(new Cursor(Cursor.WAIT_CURSOR));130list.setCursor(new Cursor(Cursor.HAND_CURSOR));131checkBox.setCursor(new Cursor(Cursor.MOVE_CURSOR));132radioButton.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));133scrollBar.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));134canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));135textField.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));136137/* create a custom cursor */138Toolkit toolkit = Toolkit.getDefaultToolkit();139Dimension d = toolkit.getBestCursorSize(32,32);140int color = toolkit.getMaximumCursorColors();141142if(!d.equals(new Dimension(0,0)) && color != 0 )143customCursor = toolkit.createCustomCursor(new BufferedImage( 16, 16, BufferedImage.TYPE_INT_RGB ), new Point(10, 10), "custom cursor.");144else145System.err.println("Platform doesn't support to create a custom cursor.");146147textArea.setCursor(customCursor);148child.add(label);149child.add(button);150child.add(choice);151child.add(list);152child.add(checkBox);153child.add(radioButton);154child.add(scrollBar);155child.add(canvas);156child.add(textArea);157child.add(textField);158child.add(button);159child.revalidate();160161child.pack();162child.setVisible(true);163refs.add(new PhantomReference<Dialog>(child, que));164}165}166167168