1 /*
   2  * Copyright (c) 2010, 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 javafx.scene.effect;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 
  30 import com.sun.javafx.FXUnit;
  31 import javafx.scene.paint.Color;
  32 
  33 import org.junit.Before;
  34 import org.junit.Rule;
  35 import org.junit.Test;
  36 import org.junit.rules.ExpectedException;
  37 
  38 import com.sun.scenario.effect.Color4f;
  39 
  40 public class SpotLightTest extends LightTestBase {
  41 
  42     @Rule
  43     public FXUnit fx = new FXUnit();
  44 
  45     @Rule
  46     public ExpectedException thrown = ExpectedException.none();
  47     private Light.Spot effect;
  48 
  49     @Before
  50     public void setUp() {
  51         effect = new Light.Spot();
  52         setupTest(effect);
  53     }
  54 
  55     @Test
  56     public void testSetPointsAtX() {
  57         // try setting correct value
  58         effect.setPointsAtX(1.0f);
  59         assertEquals(1.0f, effect.getPointsAtX(), 1e-100);
  60         pulse();
  61         assertEquals(1.0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getPointsAtX(), 1e-100);
  62     }
  63 
  64     @Test
  65     public void testDefaultPointsAtX() {
  66         // default value should be 0
  67         assertEquals(0f, effect.getPointsAtX(), 1e-100);
  68         assertEquals(0f, effect.pointsAtXProperty().get(), 1e-100);
  69         pulse();
  70         assertEquals(0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getPointsAtX(), 1e-100);
  71     }
  72 
  73     @Test
  74     public void testSetPointsAtY() {
  75         // trPointsAtY setting correct value
  76         effect.setPointsAtY(1.0f);
  77         assertEquals(1.0f, effect.getPointsAtY(), 1e-100);
  78         pulse();
  79         assertEquals(1.0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getPointsAtY(), 1e-100);
  80     }
  81 
  82     @Test
  83     public void testDefaultPointsAtY() {
  84         // default value should be 0
  85         assertEquals(0f, effect.getPointsAtY(), 1e-100);
  86         assertEquals(0f, effect.pointsAtYProperty().get(), 1e-100);
  87         pulse();
  88         assertEquals(0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getPointsAtY(), 1e-100);
  89     }
  90 
  91     @Test
  92     public void testSetPointsAtZ() {
  93         // try setting correct value
  94         effect.setPointsAtZ(1.0f);
  95         assertEquals(1.0f, effect.getPointsAtZ(), 1e-100);
  96         pulse();
  97         assertEquals(1.0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getPointsAtZ(), 1e-100);
  98     }
  99 
 100     @Test
 101     public void testDefaultPointsAtZ() {
 102         // default value should be 0
 103         assertEquals(0f, effect.getPointsAtZ(), 1e-100);
 104         assertEquals(0f, effect.pointsAtZProperty().get(), 1e-100);
 105         pulse();
 106         assertEquals(0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getPointsAtZ(), 1e-100);
 107     }
 108     
 109     @Test
 110     public void testSetSpecularExponent() {
 111         // try setting correct value
 112         effect.setSpecularExponent(1.1f);
 113         assertEquals(1.1f, effect.getSpecularExponent(), 1e-100);
 114         pulse();
 115         assertEquals(1.1f, ((com.sun.scenario.effect.light.SpotLight)effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 116     }
 117     
 118     @Test
 119     public void testDefaultSpecularExponent() {
 120         // default value should be 1
 121         assertEquals(1f, effect.getSpecularExponent(), 1e-100);
 122         assertEquals(1f, effect.specularExponentProperty().get(), 1e-100);
 123         pulse();
 124         assertEquals(1f, ((com.sun.scenario.effect.light.SpotLight)effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 125     }
 126     
 127     @Test
 128     public void testMinSpecularExponent() {
 129         // 0 should be ok
 130         effect.setSpecularExponent(0);
 131         // try setting value smaller than minimal
 132         effect.setSpecularExponent(-0.1f);
 133         assertEquals(-0.1f, effect.getSpecularExponent(), 1e-100);
 134         pulse();
 135         assertEquals(0f, ((com.sun.scenario.effect.light.SpotLight)effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 136     }
 137 
 138     @Test
 139     public void testMaxSpecularExponent() {
 140         // 4 should be ok
 141         effect.setSpecularExponent(4);
 142         // try setting value greater than maximal
 143         effect.setSpecularExponent(4.1f);
 144         assertEquals(4.1f, effect.getSpecularExponent(), 1e-100);
 145         pulse();
 146         assertEquals(4f, ((com.sun.scenario.effect.light.SpotLight)effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 147     }
 148 
 149     @Test
 150     public void testPointsAtXSynced() throws Exception {
 151         checkDoublePropertySynced(
 152                 effect, effect.impl_getImpl(),
 153                 "javafx.scene.effect.Light$Spot", "pointsAtX",
 154                 "com.sun.scenario.effect.light.SpotLight", "pointsAtX", 0.3);
 155     }
 156 
 157     @Test
 158     public void testPointsAtYSynced() throws Exception {
 159         checkDoublePropertySynced(
 160                 effect, effect.impl_getImpl(),
 161                 "javafx.scene.effect.Light$Spot", "pointsAtY",
 162                 "com.sun.scenario.effect.light.SpotLight", "pointsAtY", 0.3);
 163     }
 164 
 165     @Test
 166     public void testSpecularExponentSynced() throws Exception {
 167         checkDoublePropertySynced(
 168                 effect, effect.impl_getImpl(),
 169                 "javafx.scene.effect.Light$Spot", "specularExponent",
 170                 "com.sun.scenario.effect.light.SpotLight", "specularExponent", 0.3);
 171     }
 172 
 173     @Test
 174     public void testPointsAtZSynced() throws Exception {
 175         checkDoublePropertySynced(
 176                 effect, effect.impl_getImpl(),
 177                 "javafx.scene.effect.Light$Spot", "pointsAtZ",
 178                 "com.sun.scenario.effect.light.SpotLight", "pointsAtZ", 0.3);
 179     }
 180 
 181     @Test
 182     public void testColorSynced() throws Exception {
 183         Color color = Color.RED;
 184         Color4f red = new Color4f((float) color.getRed(), (float) color.getGreen(),
 185                 (float) color.getBlue(), (float) color.getOpacity());
 186         Color4f result = (Color4f) getObjectPropertySynced(
 187                 effect, effect.impl_getImpl(),
 188                 "javafx.scene.effect.Light$Spot", "color",
 189                 "com.sun.scenario.effect.light.SpotLight", "color",
 190                 Color.RED);
 191         assertColor4fEquals(red, result);
 192     }
 193 
 194     @Test
 195     public void testCreateWithParams() {
 196         effect = new Light.Spot(1, 2, 3, 4, Color.RED);
 197         setupTest(effect);
 198         assertEquals(1, effect.getX(), 1e-100);
 199         assertEquals(2, effect.getY(), 1e-100);
 200         assertEquals(3, effect.getZ(), 1e-100);
 201         assertEquals(4, effect.getSpecularExponent(), 1e-100);
 202         assertEquals(Color.RED, effect.getColor());
 203         pulse();        
 204         assertEquals(1.0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getX(), 1e-100);
 205         assertEquals(2.0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getY(), 1e-100);
 206         assertEquals(3.0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getZ(), 1e-100);
 207         assertEquals(4.0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 208         Color4f c = ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getColor();
 209         assertEquals(1f, c.getRed(), 1e-5);
 210         assertEquals(0f, c.getGreen(), 1e-5);
 211         assertEquals(0f, c.getBlue(), 1e-5);
 212         assertEquals(1f, c.getAlpha(), 1e-5);
 213     }
 214 
 215     @Test
 216     public void testCreateWithDefaultParams() {
 217         effect = new Light.Spot(0, 0, 0, 1, Color.WHITE);
 218         setupTest(effect);
 219         assertEquals(0, effect.getX(), 1e-100);
 220         assertEquals(0, effect.getY(), 1e-100);
 221         assertEquals(0, effect.getZ(), 1e-100);
 222         assertEquals(1, effect.getSpecularExponent(), 1e-100);
 223         assertEquals(Color.WHITE, effect.getColor());
 224         pulse();        
 225         assertEquals(0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getX(), 1e-100);
 226         assertEquals(0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getY(), 1e-100);
 227         assertEquals(0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getZ(), 1e-100);
 228         assertEquals(1.0f, ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 229         Color4f c = ((com.sun.scenario.effect.light.SpotLight) effect.impl_getImpl()).getColor();
 230         assertEquals(1f, c.getRed(), 1e-5);
 231         assertEquals(1f, c.getGreen(), 1e-5);
 232         assertEquals(1f, c.getBlue(), 1e-5);
 233         assertEquals(1f, c.getAlpha(), 1e-5);
 234     }
 235 }