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