1 /*
   2  * Copyright (c) 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.javafx.sg.prism;
  27 
  28 import com.sun.javafx.geom.RectBounds;
  29 import com.sun.prism.paint.Color;
  30 import org.junit.Before;
  31 import org.junit.Test;
  32 import static org.junit.Assert.assertEquals;
  33 import static org.junit.Assert.assertFalse;
  34 import static org.junit.Assert.assertNotNull;
  35 import static org.junit.Assert.assertTrue;
  36 
  37 /**
  38  */
  39 public class NGEllipseTest extends NGTestBase {
  40 
  41     NGEllipse ellipse;
  42 
  43     @Before
  44     public void setup() {
  45         ellipse = new NGEllipse();
  46         ellipse.setFillPaint(Color.RED);
  47         ellipse.updateEllipse(10, 10, 3, 4);
  48     }
  49 
  50     @Test
  51     public void testSupportsOpaqueRegion() {
  52         assertTrue(ellipse.supportsOpaqueRegions());
  53     }
  54 
  55     @Test
  56     public void hasOpaqueRegionIfRadiusIsGreaterThanZero() {
  57         assertTrue(ellipse.hasOpaqueRegion());
  58         ellipse.updateEllipse(10, 10, 0, 10);
  59         assertFalse(ellipse.hasOpaqueRegion());
  60         ellipse.updateEllipse(10, 10, 10, 0);
  61         assertFalse(ellipse.hasOpaqueRegion());
  62         ellipse.updateEllipse(10, 10, .0001f, .0001f);
  63         assertTrue(ellipse.hasOpaqueRegion());
  64     }
  65 
  66     @Test
  67     public void opaqueRegionLiesWithinCircle() {
  68         RectBounds or = new RectBounds();
  69         // Just sort of try a range of values. They should all be good.
  70         final float[] radiusValues = new float[] {
  71                 .001f,
  72                 1f/3f,
  73                 (float) Math.E,
  74                 (float) Math.PI,
  75                 10f,
  76                 13.321f // some random number
  77         };
  78         for (int i=0; i<radiusValues.length; i++) {
  79             ellipse.updateEllipse(10, 10, radiusValues[i], radiusValues[radiusValues.length-i-1]);
  80             or = ellipse.computeOpaqueRegion(or);
  81             assertNotNull(or);
  82             assertTrue(ellipse.getShape().contains(or.getMinX(), or.getMinY(), or.getWidth(), or.getHeight()));
  83         }
  84     }
  85 
  86     /**
  87      * Perform a simple test to be sure that the opaque region is just
  88      * about as large as it could possibly be without going outside the
  89      * bounds of the ellipse. This is basically using the same math as
  90      * the implementation, except that we use the more precise math here
  91      * than what the implementation uses. So I will compute the wider box
  92      * and a narrower box and make sure the implementation is between the two.
  93      */
  94     @Test
  95     public void testComputeOpaqueRegion() {
  96         RectBounds or = ellipse.computeOpaqueRegion(new RectBounds());
  97 
  98         // First we will compute with the highest precision we can.
  99         float rx = 3; // same as the ellipse
 100         float ry = 4; // same as the ellipse
 101         float width = 2*rx / (float) Math.sqrt(2);
 102         float halfWidth = width / 2f;
 103         float height = 2*ry / (float) Math.sqrt(2);
 104         float halfHeight = height / 2f;
 105         float x1 = 10 - halfWidth; // centerX = 10
 106         float y1 = 10 - halfHeight; // centerY = 10
 107         float x2 = 10 + halfWidth;
 108         float y2 = 10 + halfHeight;
 109         // Less than really accurate
 110         assertTrue(x1 < or.getMinX());
 111         assertTrue(y1 < or.getMinY());
 112         assertTrue(x2 > or.getMaxX());
 113         assertTrue(y2 > or.getMaxY());
 114         // But not too far off
 115         assertEquals(x1, or.getMinX(), .1f);
 116         assertEquals(y1, or.getMinY(), .1f);
 117         assertEquals(x2, or.getMaxX(), .1f);
 118         assertEquals(y2, or.getMaxY(), .1f);
 119 
 120     }
 121 }
 122