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