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 NGCircleTest extends NGTestBase {
  40 
  41     NGCircle circle;
  42 
  43     @Before public void setup() {
  44         circle = new NGCircle();
  45         circle.setFillPaint(Color.RED);
  46         circle.updateCircle(10, 10, 5);
  47     }
  48 
  49     @Test
  50     public void testSupportsOpaqueRegion() {
  51         assertTrue(circle.supportsOpaqueRegions());
  52     }
  53 
  54     @Test
  55     public void hasOpaqueRegionIfRadiusIsGreaterThanZero() {
  56         assertTrue(circle.hasOpaqueRegion());
  57         circle.updateCircle(10, 10, 0);
  58         assertFalse(circle.hasOpaqueRegion());
  59         circle.updateCircle(10, 10, .0001f);
  60         assertTrue(circle.hasOpaqueRegion());
  61     }
  62 
  63     @Test
  64     public void opaqueRegionLiesWithinCircle() {
  65         RectBounds or = new RectBounds();
  66         // Just sort of try a range of values. They should all be good.
  67         final float[] radiusValues = new float[] {
  68                 .001f,
  69                 1f/3f,
  70                 (float) Math.E,
  71                 (float) Math.PI,
  72                 10f,
  73                 13.321f // some random number
  74         };
  75         for (float r : radiusValues) {
  76             circle.updateCircle(10, 10, r);
  77             or = circle.computeOpaqueRegion(or);
  78             assertNotNull(or);
  79             assertTrue(circle.getShape().contains(or.getMinX(), or.getMinY(), or.getWidth(), or.getHeight()));
  80         }
  81     }
  82 
  83     /**
  84      * Perform a simple test to be sure that the opaque region is just
  85      * about as large as it could possibly be without going outside the
  86      * bounds of the ellipse. This is basically using the same math as
  87      * the implementation, except that we use the more precise math here
  88      * than what the implementation uses. So I will compute the wider box
  89      * and a narrower box and make sure the implementation is between the two.
  90      */
  91     @Test
  92     public void testComputeOpaqueRegion() {
  93         RectBounds or = circle.computeOpaqueRegion(new RectBounds());
  94 
  95         // First we will compute with the highest precision we can.
  96         float r = 5; // same as the ellipse
  97         float side = 2*r / (float) Math.sqrt(2);
  98         float halfSide = side / 2f;
  99         float x1 = 10 - halfSide; // centerX = 10
 100         float y1 = 10 - halfSide; // centerY = 10
 101         float x2 = 10 + halfSide;
 102         float y2 = 10 + halfSide;
 103         // Less than really accurate
 104         assertTrue(x1 < or.getMinX());
 105         assertTrue(y1 < or.getMinY());
 106         assertTrue(x2 > or.getMaxX());
 107         assertTrue(y2 > or.getMaxY());
 108         // But not too far off
 109         assertEquals(x1, or.getMinX(), .1f);
 110         assertEquals(y1, or.getMinY(), .1f);
 111         assertEquals(x2, or.getMaxX(), .1f);
 112         assertEquals(y2, or.getMaxY(), .1f);
 113 
 114     }
 115 }