1 /*
   2  * Copyright (c) 2010, 2014, 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 com.sun.javafx.test.TestHelper.box;
  29 import com.sun.scenario.effect.Color4f;
  30 import static org.junit.Assert.assertEquals;
  31 import static org.junit.Assert.assertNotNull;
  32 import static org.junit.Assert.assertNull;
  33 import static org.junit.Assert.assertTrue;
  34 import static org.junit.Assert.fail;
  35 import javafx.beans.property.ObjectProperty;
  36 import javafx.beans.property.SimpleObjectProperty;
  37 import javafx.scene.paint.Color;
  38 
  39 import org.junit.Before;
  40 import org.junit.Test;
  41 
  42 public class LightingTest extends EffectsTestBase {
  43     private Lighting effect;
  44 
  45     @Before
  46     public void setUp() {
  47         effect = new Lighting();
  48         setupTest(effect);
  49     }
  50 
  51     @Test
  52     public void testSetDiffuseConstant() {
  53         // try setting correct value
  54         effect.setDiffuseConstant(1.1f);
  55         assertEquals(1.1f, (float) effect.getDiffuseConstant(), 1e-100);
  56         pulse();
  57         assertEquals(1.1f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getDiffuseConstant(), 1e-100);
  58     }
  59 
  60     @Test
  61     public void testDefaultDiffuseConstant() {
  62         // default value should be 1
  63         assertEquals(1f, (float) effect.getDiffuseConstant(), 1e-100);
  64         assertEquals(1f, (float) effect.diffuseConstantProperty().get(), 1e-100);
  65         pulse();
  66         assertEquals(1f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getDiffuseConstant(), 1e-100);
  67     }
  68 
  69     @Test
  70     public void testMinDiffuseConstant() {
  71         // 0 should be ok
  72         effect.setDiffuseConstant(0);
  73         // try setting value smaller than minimal
  74         effect.setDiffuseConstant(-0.1f);
  75         assertEquals(-0.1f, (float) effect.getDiffuseConstant(), 1e-100);
  76         pulse();
  77         assertEquals(0f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getDiffuseConstant(), 1e-100);
  78     }
  79 
  80     @Test
  81     public void testMaxDiffuseConstant() {
  82         // 2 should be ok
  83         effect.setDiffuseConstant(2);
  84         // try setting value greater than maximal
  85         effect.setDiffuseConstant(2.1f);
  86         assertEquals(2.1f, (float) effect.getDiffuseConstant(), 1e-100);
  87         pulse();
  88         assertEquals(2f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getDiffuseConstant(), 1e-100);
  89     }
  90 
  91     @Test
  92     public void testSetSpecularConstant() {
  93         // try setting correct value
  94         effect.setSpecularConstant(1.0f);
  95         assertEquals(1.0f, (float) effect.getSpecularConstant(), 1e-100);
  96         pulse();
  97         assertEquals(1.0f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSpecularConstant(), 1e-100);
  98     }
  99 
 100     @Test
 101     public void testDefaultSpecularConstant() {
 102         // default value should be 0.3
 103         assertEquals(0.3f, (float) effect.getSpecularConstant(), 1e-100);
 104         assertEquals(0.3f, (float) effect.specularConstantProperty().get(), 1e-100);
 105         pulse();
 106         assertEquals(0.3f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSpecularConstant(), 1e-100);
 107     }
 108     
 109     @Test
 110     public void testMinSpecularConstant() {
 111         // 0 should be ok
 112         effect.setSpecularConstant(0);
 113         // try setting value smaller than minimal
 114         effect.setSpecularConstant(-0.1f);
 115         assertEquals(-0.1f, (float) effect.getSpecularConstant(), 1e-100);
 116         pulse();
 117         assertEquals(0f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSpecularConstant(), 1e-100);
 118     }
 119 
 120     @Test
 121     public void testMaxSpecularConstant() {
 122         // 2 should be ok
 123         effect.setSpecularConstant(2);
 124         // try setting value greater than maximal
 125         effect.setSpecularConstant(2.1f);
 126         assertEquals(2.1f, (float) effect.getSpecularConstant(), 1e-100);
 127         pulse();
 128         assertEquals(2f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSpecularConstant(), 1e-100);
 129     }
 130 
 131     @Test
 132     public void testSetSpecularExponent() {
 133         // try setting correct value
 134         effect.setSpecularExponent(1.0f);
 135         assertEquals(1.0f, (float) effect.getSpecularExponent(), 1e-100);
 136         pulse();
 137         assertEquals(1.0f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 138     }
 139 
 140     @Test
 141     public void testDefaultSpecularExponent() {
 142         // default value should be 20
 143         assertEquals(20f, (float) effect.getSpecularExponent(), 1e-100);
 144         assertEquals(20f, (float) effect.specularExponentProperty().get(), 1e-100);
 145         pulse();
 146         assertEquals(20f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 147     }
 148 
 149     @Test
 150     public void testMinSpecularExponent() {
 151         // 0 should be ok
 152         effect.setSpecularExponent(0);
 153         // try setting value smaller than minimal
 154         effect.setSpecularExponent(-0.1f);
 155         assertEquals(-0.1f, (float) effect.getSpecularExponent(), 1e-100);
 156         pulse();
 157         assertEquals(0f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 158     }
 159 
 160     @Test
 161     public void testMaxSpecularExponent() {
 162         // 40 should be ok
 163         effect.setSpecularExponent(40);
 164         // try setting value greater than maximal
 165         effect.setSpecularExponent(40.1f);
 166         assertEquals(40.1f, (float) effect.getSpecularExponent(), 1e-100);
 167         pulse();
 168         assertEquals(40f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSpecularExponent(), 1e-100);
 169     }
 170 
 171     @Test
 172     public void testSetSurfaceScale() {
 173         // try setting correct value
 174         effect.setSurfaceScale(1.0f);
 175         assertEquals(1.0f, (float) effect.getSurfaceScale(), 1e-100);
 176         pulse();
 177         assertEquals(1.0f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSurfaceScale(), 1e-100);
 178     }
 179 
 180     @Test
 181     public void testDefaultSurfaceScale() {
 182         // default value should be 1.5
 183         assertEquals(1.5f, (float) effect.getSurfaceScale(), 1e-100);
 184         assertEquals(1.5f, (float) effect.surfaceScaleProperty().get(), 1e-100);
 185         pulse();
 186         assertEquals(1.5f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSurfaceScale(), 1e-100);
 187     }
 188 
 189     @Test
 190     public void testMinSurfaceScale() {
 191         // 0 should be ok
 192         effect.setSurfaceScale(0);
 193         // try setting value smaller than minimal
 194         effect.setSurfaceScale(-0.1f);
 195         assertEquals(-0.1f, (float) effect.getSurfaceScale(), 1e-100);
 196         pulse();
 197         assertEquals(0f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSurfaceScale(), 1e-100);
 198     }
 199 
 200     @Test
 201     public void testMaxSurfaceScale() {
 202         // 10 should be ok
 203         effect.setSurfaceScale(10);
 204         // try setting value greater than maximal
 205         effect.setSurfaceScale(10.1f);
 206         assertEquals(10.1f, (float) effect.getSurfaceScale(), 1e-100);
 207         pulse();
 208         assertEquals(10f, (float) ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getSurfaceScale(), 1e-100);
 209     }
 210     
 211     @Test
 212     public void testBumpInput() {
 213         // default is not null
 214         assertNotNull(effect.getBumpInput());
 215         // default is shadow effect with radius of 10
 216         Effect e = effect.getBumpInput();
 217         assertTrue(e instanceof Shadow);
 218         assertEquals(10f, (float) ((Shadow)e).getRadius(), 1e-100);
 219                 
 220         // try setting input to some other effect
 221         Effect blur = new BoxBlur();
 222         effect.setBumpInput(blur);
 223         assertEquals(blur, effect.getBumpInput());
 224 
 225         // try setting input to null
 226         effect.setBumpInput(null);
 227         assertNull(effect.getBumpInput());
 228     }
 229 
 230     @Test
 231     public void testContentInput() {
 232         // default is null
 233         assertNull(effect.getContentInput());
 234         // try setting input to some other effect
 235         Effect blur = new BoxBlur();
 236         effect.setContentInput(blur);
 237         assertEquals(blur, effect.getContentInput());
 238 
 239         // try setting input to null
 240         effect.setContentInput(null);
 241         assertNull(effect.getContentInput());
 242     }
 243     
 244     @Test
 245     public void testSetLight() {
 246         // try setting correct value
 247         Light l = new Light.Point();
 248         effect.setLight(l);
 249         assertEquals(l, effect.getLight());
 250         pulse();
 251         assertEquals(l.impl_getImpl(), ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getLight());
 252     }
 253 
 254     @Test
 255     public void testDefaultLight() {
 256         // default value should be distant light
 257         Light l = effect.getLight();
 258         assertNotNull(l);
 259         assertTrue(l instanceof Light.Distant);
 260         assertEquals(l, effect.lightProperty().get());
 261         pulse();
 262         assertEquals(l.impl_getImpl(), ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getLight());
 263     }
 264 
 265     @Test
 266     public void testNullLight() {
 267         // nullvalue should default to Distant light
 268         effect.setLight(null);
 269         Light l = effect.getLight();
 270         assertNull(l);
 271         assertNull(effect.lightProperty().get());
 272         pulse();
 273         assertNotNull(((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getLight());
 274     }
 275 
 276     @Test
 277     public void testDefaultLightNotChangedByOtherLightingEffect() {
 278         // default value should be distant light
 279         Light l = effect.getLight();
 280         assertNotNull(l);
 281         assertTrue(l instanceof Light.Distant);
 282         assertEquals(l, effect.lightProperty().get());
 283 
 284         Lighting lighting = new Lighting();
 285         Light l2 = lighting.getLight();
 286         assertNotNull(l2);
 287         assertTrue(l2 instanceof Light.Distant);
 288         assertEquals(l2, lighting.lightProperty().get());
 289 
 290         l.setColor(Color.AQUA);
 291 
 292         assertEquals(Color.AQUA, l.getColor());
 293         assertEquals(Color.WHITE, l2.getColor());
 294     }
 295 
 296     @Test
 297     public void testDefaultLightNotChangedByThisLightingEffect() {
 298         // default value should be distant light
 299         Light l = effect.getLight();
 300         l.setColor(Color.BEIGE);
 301 
 302         effect.setLight(null);
 303         // null light should default to Distant Light with WHITE color
 304         assertNull(effect.getLight());
 305         pulse();
 306         Color4f c = ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getLight().getColor();
 307         assertEquals(1f, c.getRed(), 1e-5);
 308         assertEquals(1f, c.getGreen(), 1e-5);
 309         assertEquals(1f, c.getBlue(), 1e-5);
 310         assertEquals(1f, c.getAlpha(), 1e-5);
 311     }
 312 
 313     @Test
 314     public void testChangeLight() {
 315         // try setting correct value
 316         Light.Point l = new Light.Point();
 317         effect.setLight(l);
 318         assertEquals(l, effect.getLight());
 319         pulse();
 320         assertEquals(l.impl_getImpl(), ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getLight());
 321         l.setX(100);
 322         pulse();
 323         assertEquals(100f, (float) ((com.sun.scenario.effect.light.PointLight) l.impl_getImpl()).getX(), 1e-100);
 324     }
 325 
 326     @Test
 327     public void testCycles() {
 328         // try setting itself as content input
 329         try {
 330             effect.setContentInput(effect);
 331             fail("IllegalArgument should have been thrown.");
 332         } catch (IllegalArgumentException e) {
 333             assertEquals(null, effect.getContentInput());
 334         }
 335 
 336         // try setting itself as bump input
 337         try {
 338             effect.setBumpInput(effect);
 339             fail("IllegalArgument should have been thrown.");
 340         } catch (IllegalArgumentException e) {
 341             Effect efct = effect.getBumpInput();
 342             assertTrue(efct instanceof Shadow);
 343         }
 344 
 345         // test following cycle
 346         // Lighting <- BoxBlur <- Lighting
 347         BoxBlur blur = new BoxBlur();
 348         effect.setBumpInput(blur);
 349         effect.setContentInput(blur);
 350         try {
 351             blur.setInput(effect);
 352             fail("IllegalArgument should have been thrown.");
 353         } catch (IllegalArgumentException e) {
 354             assertEquals(null, blur.getInput());
 355         }
 356 
 357         // test following cycle
 358         // BoxBlur <- Lighting <- BoxBlur
 359         effect.setBumpInput(null);
 360         effect.setContentInput(null);
 361         blur.setInput(effect);
 362         try {
 363             effect.setBumpInput(blur);
 364             fail("IllegalArgument should have been thrown.");
 365         } catch (IllegalArgumentException e) {}
 366 
 367         try {
 368             effect.setContentInput(blur);
 369             fail("IllegalArgument should have been thrown.");
 370         } catch (IllegalArgumentException e) {}
 371 
 372         assertEquals(null, effect.getContentInput());
 373         assertEquals(null, effect.getBumpInput());
 374     }
 375 
 376     @Test
 377     public void testCyclesForBoundInput() {
 378         ObjectProperty vContentInput = new SimpleObjectProperty();
 379         effect.contentInputProperty().bind(vContentInput);
 380         // try setting itself as content input
 381         vContentInput.set(effect);
 382         assertEquals(null, effect.getContentInput());
 383         vContentInput.set(null);
 384 
 385         effect.contentInputProperty().bind(vContentInput);
 386 
 387         // try setting itself as bump input
 388         ObjectProperty vBumpInput = new SimpleObjectProperty();
 389         effect.bumpInputProperty().bind(vBumpInput);
 390         vBumpInput.set(effect);
 391         Effect efct = effect.getBumpInput();
 392         assertNull(efct);
 393         vBumpInput.set(null);
 394         effect.bumpInputProperty().bind(vBumpInput);
 395 
 396         // test following cycle
 397         // Lighting <- BoxBlur <- Lighting
 398         BoxBlur blur = new BoxBlur();
 399         ObjectProperty vBlur = new SimpleObjectProperty();
 400         blur.inputProperty().bind(vBlur);
 401         vBumpInput.set(blur);
 402         vContentInput.set(blur);
 403         vBlur.set(effect);
 404         assertEquals(null, blur.getInput());
 405         vBlur.set(null);
 406         blur.inputProperty().bind(vBlur);
 407 
 408         // test following cycle
 409         // BoxBlur <- Lighting <- BoxBlur
 410         vBumpInput.set(null);
 411         vContentInput.set(null);
 412         vBlur.set(effect);
 413         vBumpInput.set(blur);
 414         assertEquals(null, effect.getContentInput());
 415         vBumpInput.set(null);
 416         effect.bumpInputProperty().bind(vBumpInput);
 417 
 418         vContentInput.set(blur);
 419         assertEquals(null, effect.getContentInput());
 420         vContentInput.set(null);
 421         effect.contentInputProperty().bind(vContentInput);
 422 
 423         assertEquals(null, effect.getContentInput());
 424         assertEquals(null, effect.getBumpInput());
 425     }
 426 
 427     @Test
 428     public void testDiffuseConstantSynced() throws Exception {
 429         checkDoublePropertySynced(
 430                 "javafx.scene.effect.Lighting", "diffuseConstant",
 431                 "com.sun.scenario.effect.PhongLighting", "diffuseConstant", 1.5);
 432     }
 433 
 434     @Test
 435     public void testSpecularConstantSynced() throws Exception {
 436         checkDoublePropertySynced(
 437                 "javafx.scene.effect.Lighting", "specularConstant",
 438                 "com.sun.scenario.effect.PhongLighting", "specularConstant", 1.5);
 439     }
 440 
 441 
 442     @Test
 443     public void testSpecularExponentSynced() throws Exception {
 444         checkDoublePropertySynced(
 445                 "javafx.scene.effect.Lighting", "specularExponent",
 446                 "com.sun.scenario.effect.PhongLighting", "specularExponent", 10);
 447     }
 448 
 449     @Test
 450     public void testSurfaceScaleSynced() throws Exception {
 451         checkDoublePropertySynced(
 452                 "javafx.scene.effect.Lighting", "surfaceScale",
 453                 "com.sun.scenario.effect.PhongLighting", "surfaceScale", 10);
 454     }
 455 
 456     @Test
 457     public void testLightSynced() throws Exception {
 458         Light l = new Light.Point();
 459         checkObjectPropertySynced(
 460                 "javafx.scene.effect.Lighting", "light",
 461                 "com.sun.scenario.effect.PhongLighting", "light",
 462                 l, l.impl_getImpl(),
 463                 null);
 464     }
 465 
 466     @Test
 467     public void testBumpInputSynced() throws Exception {
 468         BoxBlur blur = new BoxBlur();
 469         checkEffectPropertySynced(
 470                 "javafx.scene.effect.Lighting", "bumpInput",
 471                 "com.sun.scenario.effect.PhongLighting", "bumpInput",
 472                 blur, (com.sun.scenario.effect.BoxBlur)blur.impl_getImpl());
 473     }
 474 
 475     @Test
 476     public void testContentInputSynced() throws Exception {
 477         BoxBlur blur = new BoxBlur();
 478         checkEffectPropertySynced(
 479                 "javafx.scene.effect.Lighting", "contentInput",
 480                 "com.sun.scenario.effect.PhongLighting", "contentInput",
 481                 blur, (com.sun.scenario.effect.BoxBlur)blur.impl_getImpl());
 482     }
 483 
 484     @Test
 485     public void testBounds() {
 486         assertEquals(box(0, 0, 100, 100), n.getBoundsInLocal());
 487     }
 488 
 489     @Test
 490     public void testBoundsWidthContentInput() {
 491         assertEquals(box(0, 0, 100, 100), n.getBoundsInLocal());
 492         BoxBlur blur = new BoxBlur();
 493         effect.setContentInput(blur);
 494         assertEquals(box(-2, -2, 104, 104), n.getBoundsInLocal());
 495     }
 496 
 497     @Test
 498     public void testBoundsWidthBumpInput() {
 499         assertEquals(box(0, 0, 100, 100), n.getBoundsInLocal());
 500         BoxBlur blur = new BoxBlur();
 501         effect.setBumpInput(blur);
 502         assertEquals(box(0, 0, 100, 100), n.getBoundsInLocal());
 503     }
 504 
 505     @Test
 506     public void testBoundsWidthBumpAndContentInput() {
 507         assertEquals(box(0, 0, 100, 100), n.getBoundsInLocal());
 508         BoxBlur blur = new BoxBlur();
 509         effect.setContentInput(blur);
 510         effect.setBumpInput(blur);
 511         assertEquals(box(-2, -2, 104, 104), n.getBoundsInLocal());
 512     }
 513     
 514     @Test
 515     public void testCreateWithParams() {
 516         Light l = new Light.Point();
 517         effect = new Lighting(l);
 518         setupTest(effect);
 519         effect.setLight(l);
 520         assertEquals(l, effect.getLight());
 521         pulse();
 522         assertEquals(l.impl_getImpl(), ((com.sun.scenario.effect.PhongLighting) effect.impl_getImpl()).getLight());
 523     }
 524 }