Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Frame/FramesGC/FramesGC.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 Verify that disposed frames are collected with GC33* @author Dmitriy Ermashov ([email protected])34* @library ../../../../lib/testlibrary35* @build ExtendedRobot36* @run main/othervm -Xmx20m FramesGC37*/383940public class FramesGC {4142ExtendedRobot robot;43ArrayList<PhantomReference<Frame>> refs = new ArrayList<PhantomReference<Frame>>();44ReferenceQueue<Frame> que = new ReferenceQueue<Frame>();4546public static void main(String []args) throws Exception {47new FramesGC().doTest();48}4950FramesGC() throws Exception{51robot = new ExtendedRobot();52}5354void doTest() throws Exception {55for( int i = 1; i <= 3; i++) {56final int j = i;57EventQueue.invokeAndWait(() -> {58createFrame(j);59});60}61robot.waitForIdle();6263for (Frame f : Frame.getFrames())64f.dispose();6566robot.waitForIdle();6768Vector garbage = new Vector();69while (true) {70try {71garbage.add(new byte[1000]);72} catch (OutOfMemoryError er) {73break;74}75}76garbage = null;7778int count = 1;79for(; count <= 3; count++)80if(que.remove(5000) == null)81break;8283System.out.println("Total no of instances eligible for GC = " + count);84if(count < 3)85throw new RuntimeException("Count = "+count+". Test failed!");8687}8889void createFrame(int i){90Frame frame = new Frame("Frame " + i);9192Button button=new Button("Press Me");93TextArea textArea=new TextArea(5,5);94TextField textField=new TextField(10);95Choice choice=new Choice();96choice.add("One");97choice.add("Two");98choice.add("Three");99choice.add("Four");100choice.add("Five");101List list = new List();102list.add("One");103list.add("Two");104list.add("Three");105list.add("Four");106list.add("Five");107Checkbox checkBox= new Checkbox("Hai");108Scrollbar scrollBar=new Scrollbar(Scrollbar.VERTICAL,0,1,0,200);109CheckboxGroup checkboxGroup=new CheckboxGroup();110Checkbox radioButton=new Checkbox("Hello" ,true, checkboxGroup);111Canvas canvas=new Canvas();112canvas.setSize(100, 100);113canvas.setBackground(java.awt.Color.red);114Label label=new Label("I am label.!");115Cursor customCursor=null;116117frame.setLayout(new java.awt.FlowLayout());118119button.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));120label.setCursor(new Cursor(Cursor.TEXT_CURSOR));121choice.setCursor(new Cursor(Cursor.WAIT_CURSOR));122list.setCursor(new Cursor(Cursor.HAND_CURSOR));123checkBox.setCursor(new Cursor(Cursor.MOVE_CURSOR));124radioButton.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));125scrollBar.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));126canvas.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));127textField.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));128129/* create a custom cursor */130Toolkit toolkit = Toolkit.getDefaultToolkit();131Dimension d = toolkit.getBestCursorSize(32,32);132int color = toolkit.getMaximumCursorColors();133134if(!d.equals(new Dimension(0,0)) && color != 0 )135customCursor = toolkit.createCustomCursor(new BufferedImage( 16, 16, BufferedImage.TYPE_INT_RGB ), new Point(10, 10), "custom cursor.");136else137System.err.println("Platform doesn't support to create a custom cursor.");138139textArea.setCursor(customCursor);140frame.add(label);141frame.add(button);142frame.add(choice);143frame.add(list);144frame.add(checkBox);145frame.add(radioButton);146frame.add(scrollBar);147frame.add(canvas);148frame.add(textArea);149frame.add(textField);150frame.add(button);151152frame.setLocation(20, 140 * i);153frame.pack();154frame.setVisible(true);155refs.add(new PhantomReference<Frame>(frame, que));156}157158}159160