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 import static org.junit.Assert.assertFalse;
  30 import static org.junit.Assert.assertNull;
  31 import static org.junit.Assert.assertTrue;
  32 import static org.junit.Assert.fail;
  33 import javafx.beans.property.ObjectProperty;
  34 import javafx.beans.property.SimpleObjectProperty;
  35 import javafx.scene.Group;
  36 import javafx.scene.Scene;
  37 import javafx.scene.shape.Rectangle;
  38 import javafx.stage.Stage;
  39 
  40 import org.junit.Test;
  41 
  42 import com.sun.javafx.pgstub.StubToolkit;
  43 import com.sun.javafx.tk.Toolkit;
  44 
  45 public class EffectTest {
  46     
  47     @Test
  48     public void testAdding() {
  49         Bloom b = new Bloom();
  50         Group g = new Group();
  51         g.setEffect(b);
  52         assertEquals(b, g.getEffect());
  53     }
  54     
  55     @Test
  56     public void testRemoving() {
  57         Bloom b = new Bloom();
  58         Group g = new Group();
  59         g.setEffect(b);
  60         g.setEffect(null);
  61         assertNull(g.getEffect());
  62     }
  63 
  64     /*
  65      * Test for testing value propagation in complicated effect hierarchy
  66      */
  67     @Test
  68     public void testPropertyPropagationWithChaining() {
  69         Group root = new Group();
  70         Scene scene = new Scene(root);
  71         Stage stage = new Stage();
  72         stage.setScene(scene);
  73         stage.show();
  74         StubToolkit toolkit = (StubToolkit) Toolkit.getToolkit();
  75         Rectangle n1 = new Rectangle();
  76         Rectangle n2 = new Rectangle();
  77         Rectangle n3 = new Rectangle();
  78         root.getChildren().addAll(n1, n2);
  79 
  80         // Try setting effects to a node which is already in scene
  81         Bloom bloom = new Bloom();
  82         n1.setEffect(bloom);
  83         Glow glow = new Glow();
  84         bloom.setInput(glow);
  85         
  86         glow.setLevel(1.1);
  87         assertEquals(1.1, glow.getLevel(), 1e-100);
  88         toolkit.fireTestPulse();
  89         assertEquals(1.0f, (float) ((com.sun.scenario.effect.Glow)glow.impl_getImpl()).getLevel(), 1e-100);
  90         n2.setEffect(glow);
  91         glow.setLevel(0.5);
  92         assertEquals(0.5, glow.getLevel(), 1e-100);
  93         toolkit.fireTestPulse();
  94         assertEquals(0.5f, (float)((com.sun.scenario.effect.Glow)glow.impl_getImpl()).getLevel(), 1e-100);
  95         Bloom bloom2 = new Bloom();
  96         glow.setInput(bloom2);
  97         bloom2.setThreshold(0.1);
  98         assertEquals(0.1, bloom2.getThreshold(), 1e-100);
  99         toolkit.fireTestPulse();
 100         assertEquals(0.1f, (float) ((com.sun.scenario.effect.Bloom)bloom2.impl_getImpl()).getThreshold(), 1e-100);
 101         // Now try setting first the effect and then adding node to scene
 102         Bloom bloom3 = new Bloom();
 103         n3.setEffect(bloom3);
 104         bloom3.setThreshold(0.1);
 105         root.getChildren().add(n3);
 106         assertEquals(0.1, bloom3.getThreshold(), 1e-100);
 107         toolkit.fireTestPulse();
 108         assertEquals(0.1f, (float) ((com.sun.scenario.effect.Bloom)bloom3.impl_getImpl()).getThreshold(), 1e-100);
 109     }
 110 
 111     /*
 112      * Test for testing value propagation in complicated effect hierarchy
 113      * with binding
 114      */
 115     @Test
 116     public void testPropertyPropagationWithChainingAndBinding() {
 117         Group root = new Group();
 118         Scene scene = new Scene(root);
 119         Stage stage = new Stage();
 120         stage.setScene(scene);
 121         stage.show();
 122         StubToolkit toolkit = (StubToolkit) Toolkit.getToolkit();
 123         Rectangle n1 = new Rectangle();
 124         Rectangle n2 = new Rectangle();
 125         Rectangle n3 = new Rectangle();
 126         root.getChildren().addAll(n1, n2);
 127 
 128         // Try setting effects to a node which is already in scene
 129         Bloom bloom = new Bloom();
 130         n1.setEffect(bloom);
 131 
 132         ObjectProperty ov = new SimpleObjectProperty();
 133         bloom.inputProperty().bind(ov);
 134 
 135         Glow glow = new Glow();
 136         ov.set(glow);
 137 
 138         glow.setLevel(1.1);
 139         assertEquals(1.1, glow.getLevel(), 1e-100);
 140         toolkit.fireTestPulse();
 141         assertEquals(1.0f, (float) ((com.sun.scenario.effect.Glow)glow.impl_getImpl()).getLevel(), 1e-100);
 142         n2.setEffect(glow);
 143         glow.setLevel(0.5);
 144         assertEquals(0.5, glow.getLevel(), 1e-100);
 145         toolkit.fireTestPulse();
 146         assertEquals(0.5f, (float)((com.sun.scenario.effect.Glow)glow.impl_getImpl()).getLevel(), 1e-100);
 147 
 148         ObjectProperty ov2 = new SimpleObjectProperty();
 149         glow.inputProperty().bind(ov2);
 150 
 151         Bloom bloom2 = new Bloom();
 152         ov2.set(bloom2);
 153         bloom2.setThreshold(0.1);
 154         assertEquals(0.1, bloom2.getThreshold(), 1e-100);
 155         toolkit.fireTestPulse();
 156         assertEquals(0.1f, (float) ((com.sun.scenario.effect.Bloom)bloom2.impl_getImpl()).getThreshold(), 1e-100);
 157 
 158         // now change the bound value
 159         // previous input should be deregistred
 160         Bloom bloom3 = new Bloom();
 161         ov2.set(bloom3);
 162         bloom3.setThreshold(0.1);
 163        
 164         assertEquals(0.1, bloom3.getThreshold(), 1e-100);
 165         assertTrue(glow.impl_isEffectDirty());
 166         assertTrue(bloom.impl_isEffectDirty());
 167         toolkit.fireTestPulse();
 168         assertEquals(0.1f, (float) ((com.sun.scenario.effect.Bloom)bloom3.impl_getImpl()).getThreshold(), 1e-100);
 169 
 170         bloom2.setThreshold(0.2);
 171         // test that previously bound effect is correctly deregistred and doesn't propagate its changes via listeners
 172         assertFalse(glow.impl_isEffectDirty());
 173         assertEquals(0.2, bloom2.getThreshold(), 1e-100);
 174         toolkit.fireTestPulse();
 175         assertEquals(0.1f, (float) ((com.sun.scenario.effect.Bloom)bloom2.impl_getImpl()).getThreshold(), 1e-100);
 176     }
 177 
 178     @Test
 179     public void testLongCycle() {
 180         // Testing extremely long cycle of effects
 181         Blend blend = new Blend();
 182         Bloom bloom = new Bloom();
 183         BoxBlur boxBlur = new BoxBlur();
 184         ColorAdjust colorAdjust = new ColorAdjust();
 185         DisplacementMap displacementMap = new DisplacementMap();
 186         DropShadow dropShadow = new DropShadow();
 187         GaussianBlur gaussianBlur = new GaussianBlur();
 188         Glow glow = new Glow();
 189         InnerShadow innerShadow = new InnerShadow();
 190         MotionBlur motionBlur = new MotionBlur();
 191         PerspectiveTransform perspectiveTransform = new PerspectiveTransform();
 192         Reflection reflection = new Reflection();
 193         SepiaTone sepiaTone = new SepiaTone();
 194         Shadow shadow = new Shadow();
 195 
 196         blend.setTopInput(bloom);
 197         bloom.setInput(boxBlur);
 198         boxBlur.setInput(colorAdjust);
 199         colorAdjust.setInput(displacementMap);
 200         displacementMap.setInput(dropShadow);
 201         dropShadow.setInput(gaussianBlur);
 202         gaussianBlur.setInput(glow);
 203         glow.setInput(innerShadow);
 204         innerShadow.setInput(motionBlur);
 205         motionBlur.setInput(perspectiveTransform);
 206         perspectiveTransform.setInput(reflection);
 207         reflection.setInput(sepiaTone);
 208         sepiaTone.setInput(shadow);
 209         try {
 210             shadow.setInput(blend);
 211             fail("IllegalArgument should have been thrown.");
 212         } catch (IllegalArgumentException e) {
 213             assertEquals(null, shadow.getInput());
 214         }
 215     }
 216 }