1 /*
   2  * Copyright (c) 2014, 2016, 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  * @key headful
  27  * @bug     8036022
  28  * @summary Test verifies that drawing shapes with XOR composite
  29  *          does not trigger an InternalError in GDI surface data.
  30  * @run main/othervm -Dsun.java2d.d3d=True DrawXORModeTest
  31  */
  32 import java.awt.BasicStroke;
  33 import java.awt.Color;
  34 import java.awt.Component;
  35 import java.awt.Dimension;
  36 import java.awt.Frame;
  37 import java.awt.Graphics;
  38 import java.awt.Graphics2D;
  39 import java.awt.Stroke;
  40 import java.awt.geom.Line2D;
  41 import java.util.concurrent.CountDownLatch;
  42 
  43 public class DrawXORModeTest extends Component {
  44 
  45     public static void main(String[] args) {
  46         final DrawXORModeTest c = new DrawXORModeTest();
  47 
  48         final Frame f = new Frame("XOR mode test");
  49         f.add(c);
  50         f.pack();
  51 
  52         f.setVisible(true);
  53 
  54         try {
  55             c.checkResult();
  56         } finally {
  57             f.dispose();
  58         }
  59     }
  60 
  61     @Override
  62     public void paint(Graphics g) {
  63         if (g == null || !(g instanceof Graphics2D)) {
  64             return;
  65         }
  66         g.setColor(Color.white);
  67         g.setXORMode(Color.black);
  68         Graphics2D dg = (Graphics2D) g;
  69         Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
  70                 BasicStroke.JOIN_MITER,
  71                 10.0f,
  72                 new float[]{1.0f, 1.0f},
  73                 0.0f);
  74         dg.setStroke(stroke);
  75         try {
  76             dg.draw(new Line2D.Float(10, 10, 20, 20));
  77         } catch (Throwable e) {
  78             synchronized (this) {
  79                 theError = e;
  80             }
  81         } finally {
  82             didDraw.countDown();
  83         }
  84     }
  85 
  86     @Override
  87     public Dimension getPreferredSize() {
  88         return new Dimension(400, 100);
  89     }
  90 
  91     public void checkResult() {
  92         try {
  93             didDraw.await();
  94         } catch (InterruptedException e) {
  95         }
  96 
  97         synchronized (this) {
  98             if (theError != null) {
  99                 System.out.println("Error: " + theError);
 100 
 101                 throw new RuntimeException("Test FAILED.");
 102             }
 103         }
 104         System.out.println("Test PASSED.");
 105 
 106     }
 107 
 108     private Throwable theError = null;
 109 
 110     private final CountDownLatch didDraw = new CountDownLatch(1);
 111 }