modules/graphics/src/test/java/test/com/sun/javafx/sg/prism/NGCircleTest.java

Print this page
rev 9250 : 8134762: Refactor Javafx graphics module tests for clear separation of tests
Reviewed-by:


   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 


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