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 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 116 } 117 }