1 /*
   2  * Copyright (c) 2002, 2008, 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 /*
  25  * @test
  26  * @bug 4177735
  27  * @summary Tests that JColorChooser leaves no threads when disposed
  28  * @author Shannon Hickey
  29  */
  30 
  31 import java.awt.Point;
  32 import javax.swing.JColorChooser;
  33 import javax.swing.JDialog;
  34 import javax.swing.SwingUtilities;
  35 import javax.swing.colorchooser.AbstractColorChooserPanel;
  36 
  37 public class Test4177735 implements Runnable {
  38     private static final long DELAY = 1000L;
  39 
  40     public static void main(String[] args) throws Exception {
  41         int hsvIndex = 0;
  42         int panelsLength;
  43         int finalIndex;
  44         JColorChooser chooser = new JColorChooser();
  45         AbstractColorChooserPanel[] panels = chooser.getChooserPanels();
  46         panelsLength = panels.length;
  47 
  48         for(int i = 0; i < panelsLength; i++) {
  49             if(panels[i].getDisplayName().equals("HSV")) {
  50                 hsvIndex = i;
  51             }
  52         }
  53         finalIndex = Math.min(hsvIndex, panelsLength - 1);
  54         chooser.setChooserPanels(new AbstractColorChooserPanel[] { panels[finalIndex] });
  55 
  56         JDialog dialog = show(chooser);
  57         pause(DELAY);
  58 
  59         dialog.dispose();
  60         pause(DELAY);
  61 
  62         Test4177735 test = new Test4177735();
  63         SwingUtilities.invokeAndWait(test);
  64         if (test.count != 0) {
  65             throw new Error("JColorChooser leaves " + test.count + " threads running");
  66         }
  67     }
  68 
  69     static JDialog show(JColorChooser chooser) {
  70         JDialog dialog = JColorChooser.createDialog(null, null, false, chooser, null, null);
  71         dialog.setVisible(true);
  72         // block till displayed
  73         Point point = null;
  74         while (point == null) {
  75             try {
  76                 point = dialog.getLocationOnScreen();
  77             }
  78             catch (IllegalStateException exception) {
  79                 pause(DELAY);
  80             }
  81         }
  82         return dialog;
  83     }
  84 
  85     private static void pause(long delay) {
  86         try {
  87             Thread.sleep(delay);
  88         }
  89         catch (InterruptedException exception) {
  90         }
  91     }
  92 
  93     private int count;
  94 
  95     public void run() {
  96         ThreadGroup group = Thread.currentThread().getThreadGroup();
  97         Thread[] threads = new Thread[group.activeCount()];
  98         int count = group.enumerate(threads, false);
  99         for (int i = 0; i < count; i++) {
 100             String name = threads[i].getName();
 101             if ("SyntheticImageGenerator".equals(name)) { // NON-NLS: thread name
 102                 this.count++;
 103             }
 104         }
 105     }
 106 }