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