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