1 /*
   2  * Copyright (c) 2011, 2013, 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  * @summary <rdar://problem/4904332> GradientPaint causes Java hang/crash
  27  * @summary com.apple.junit.java.graphics.GradientPaint
  28  * @library ../../regtesthelpers
  29  * @build VisibilityValidator Waypoint
  30  * @run junit R4904332GradientHang
  31  */
  32 
  33 import test.java.awt.regtesthelpers.VisibilityValidator;
  34 import test.java.awt.regtesthelpers.Waypoint;
  35 import junit.framework.*;
  36 import javax.swing.*;
  37 import java.awt.*;
  38 import java.util.Vector;
  39 
  40 public class R4904332GradientHang extends TestCase {
  41 
  42     private JFrame frame = null;
  43 
  44     @Override
  45     protected void setUp() throws Exception {
  46         frame = new JFrame(System.getProperty("vm.version"));
  47     }
  48 
  49     @Override
  50     protected void tearDown() throws Exception {
  51         assertNotNull(frame);
  52         frame.dispose();
  53     }
  54 
  55     public void testR4904332() throws Exception {
  56         assertNotNull(frame);
  57         Container contentPane = frame.getContentPane();
  58         assertNotNull(contentPane);
  59         contentPane.setLayout(new BorderLayout());
  60         GradientJPanel gradientPanel = new GradientJPanel(5);
  61         assertNotNull(gradientPanel);
  62         contentPane.add(gradientPanel, BorderLayout.CENTER);
  63         frame.pack();
  64         VisibilityValidator.setVisibleAndConfirm(frame);
  65         gradientPanel.requirePaint("Panel with gradient paint failed to paint");
  66         frame.repaint();
  67         gradientPanel.requirePaint("Panel with gradient paint did not get 2nd paint");
  68     }
  69 
  70     public void testVarients() throws Exception {
  71         assertNotNull(frame);
  72         Container contentPane = frame.getContentPane();
  73         assertNotNull(contentPane);
  74         contentPane.setLayout(new FlowLayout());
  75 
  76         Vector<GradientJPanel> panels = new Vector<GradientJPanel>();
  77 
  78         // Add a bunch of different panels with different sized gradients
  79         for (int i = 1; i < 100; i+=10) {
  80             GradientJPanel panel =new GradientJPanel(i);
  81             panels.add(panel);
  82             contentPane.add(panel);
  83         }
  84 
  85         // Show the whole thing
  86         frame.pack();
  87         VisibilityValidator.setVisibleAndConfirm(frame);
  88 
  89         // check that everything paints once
  90         for ( GradientJPanel panel : panels) {
  91             panel.requirePaint("Panel with gradient paint failed to paint");
  92         }
  93 
  94         frame.repaint();
  95 
  96         // check that everything paints again
  97         for ( GradientJPanel panel : panels) {
  98             panel.requirePaint("Panel with gradient paint failed to paint");
  99         }
 100     }
 101 
 102     // A panel painted with a gradient
 103     class GradientJPanel extends JPanel {
 104         private int size = 5;
 105         private GradientPaint gp = null;
 106         public final Waypoint painted = new Waypoint();
 107 
 108         public GradientJPanel(int size) {
 109             super();
 110             this.size = size;
 111             gp =new GradientPaint(5, 5, Color.white, size, 5+size, Color.black);
 112         }
 113 
 114         @Override
 115         public void paint(Graphics gx) {
 116             Graphics2D g = (Graphics2D)gx;
 117             g.setStroke(new BasicStroke( (1.0f * size) - 1 ));
 118             g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
 119             g.setPaint(gp);
 120             g.drawLine(5, 5, size, 5+size);
 121             painted.clear();
 122         }
 123 
 124         @Override
 125         public Dimension getPreferredSize() {
 126             return new Dimension(size+10, size+10);
 127         }
 128 
 129         public void requirePaint() throws RuntimeException {
 130             requirePaint("Timed out waiting for paint to compelete");
 131         }
 132 
 133         public void requirePaint(String msg) throws RuntimeException {
 134             painted.requireClear(msg);
 135             painted.reset();
 136         }
 137     }
 138 
 139 
 140 // Boilerplate below
 141 
 142 public static Test suite() {
 143     return new TestSuite(R4904332GradientHang.class);
 144 }
 145 
 146     public static void main (String[] args) throws RuntimeException {
 147         TestResult tr = junit.textui.TestRunner.run(suite());
 148         if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) {
 149             throw new RuntimeException("### FAILED: unexpected JUnit errors or failures.");
 150         }
 151     }
 152 
 153 
 154 }