Path: blob/master/test/jdk/java/awt/Frame/FramesGC/FramesGC.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 Verify that disposed frames are collected with GC34* @author Dmitriy Ermashov ([email protected])35* @library /lib/client36* @build ExtendedRobot37* @run main/othervm -Xmx20m FramesGC38*/394041public class FramesGC {4243ExtendedRobot robot;44ArrayList<PhantomReference<Frame>> refs = new ArrayList<PhantomReference<Frame>>();45ReferenceQueue<Frame> que = new ReferenceQueue<Frame>();4647public static void main(String []args) throws Exception {48new FramesGC().doTest();49}5051FramesGC() throws Exception{52robot = new ExtendedRobot();53}5455void doTest() throws Exception {56for( int i = 1; i <= 3; i++) {57final int j = i;58EventQueue.invokeAndWait(() -> {59createFrame(j);60});61}62robot.waitForIdle();6364for (Frame f : Frame.getFrames())65f.dispose();6667robot.waitForIdle();6869Vector garbage = new Vector();70while (true) {71try {72garbage.add(new byte[1000]);73} catch (OutOfMemoryError er) {74break;75}76}77garbage = null;7879int count = 1;80for(; count <= 3; count++)81if(que.remove(5000) == null)82break;8384System.out.println("Total no of instances eligible for GC = " + count);85if(count < 3)86throw new RuntimeException("Count = "+count+". Test failed!");8788}8990void createFrame(int i){91Frame frame = new Frame("Frame " + i);9293Button button=new Button("Press Me");94TextArea textArea=new TextArea(5,5);95TextField textField=new TextField(10);96Choice choice=new Choice();97choice.add("One");98choice.add("Two");99choice.add("Three");100choice.add("Four");101choice.add("Five");102List list = new List();103list.add("One");104list.add("Two");105list.add("Three");106list.add("Four");107list.add("Five");108Checkbox checkBox= new Checkbox("Hai");109Scrollbar scrollBar=new Scrollbar(Scrollbar.VERTICAL,0,1,0,200);110CheckboxGroup checkboxGroup=new CheckboxGroup();111Checkbox radioButton=new Checkbox("Hello" ,true, checkboxGroup);112Canvas canvas=new Canvas();113canvas.setSize(100, 100);114canvas.setBackground(java.awt.Color.red);115Label label=new Label("I am label.!");116Cursor customCursor=null;117118frame.setLayout(new java.awt.FlowLayout());119120button.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));121label.setCursor(new Cursor(Cursor.TEXT_CURSOR));122choice.setCursor(new Cursor(Cursor.WAIT_CURSOR));123list.setCursor(new Cursor(Cursor.HAND_CURSOR));124checkBox.setCursor(new Cursor(Cursor.MOVE_CURSOR));125radioButton.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));126scrollBar.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));127canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));128textField.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));129130/* create a custom cursor */131Toolkit toolkit = Toolkit.getDefaultToolkit();132Dimension d = toolkit.getBestCursorSize(32,32);133int color = toolkit.getMaximumCursorColors();134135if(!d.equals(new Dimension(0,0)) && color != 0 )136customCursor = toolkit.createCustomCursor(new BufferedImage( 16, 16, BufferedImage.TYPE_INT_RGB ), new Point(10, 10), "custom cursor.");137else138System.err.println("Platform doesn't support to create a custom cursor.");139140textArea.setCursor(customCursor);141frame.add(label);142frame.add(button);143frame.add(choice);144frame.add(list);145frame.add(checkBox);146frame.add(radioButton);147frame.add(scrollBar);148frame.add(canvas);149frame.add(textArea);150frame.add(textField);151frame.add(button);152153frame.setLocation(20, 140 * i);154frame.pack();155frame.setVisible(true);156refs.add(new PhantomReference<Frame>(frame, que));157}158159}160161162