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