1 /*
   2  * Copyright (c) 2011, 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 test.javafx.scene.bounds;
  27 
  28 import static test.com.sun.javafx.test.TestHelper.box;
  29 import static org.junit.Assert.assertEquals;
  30 import javafx.scene.effect.DropShadow;
  31 import javafx.scene.effect.Glow;
  32 import javafx.scene.effect.Reflection;
  33 import javafx.scene.shape.Rectangle;
  34 
  35 import org.junit.Test;
  36 
  37 
  38 public class EffectBoundsTest {
  39 
  40     // test setting an effect on a Node alters the bounds
  41     public @Test void testBoundsOnRectangleWithShadow() {
  42         Rectangle rect = new Rectangle(100, 100);
  43         DropShadow ds = new DropShadow();
  44         ds.setRadius(2);
  45         rect.setEffect(ds);
  46         
  47         assertEquals(box(-2, -2, 104, 104), rect.getBoundsInLocal());
  48         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  49     }
  50     
  51     // test setting an effect on a Node and removing the effect is right bounds
  52     public @Test void testBoundsOnRectangleWithShadowRemoved() {
  53         Rectangle rect = new Rectangle(100, 100);
  54         DropShadow ds = new DropShadow();
  55         ds.setRadius(2);
  56         rect.setEffect(ds);
  57         
  58         assertEquals(box(-2, -2, 104, 104), rect.getBoundsInLocal());
  59         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  60         
  61         rect.setEffect(null);
  62         assertEquals(box(0, 0, 100, 100), rect.getBoundsInLocal());
  63         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  64     }
  65     
  66     // test setting an effect on a Node and changing the effect params
  67     //   (which causes the effect bounds to change) will update the bounds
  68     public @Test void testBoundsOnRectangleWithShadowChanged() {
  69         Rectangle rect = new Rectangle(100, 100);
  70         DropShadow ds = new DropShadow();
  71         ds.setRadius(2);
  72         rect.setEffect(ds);
  73 
  74         assertEquals(box(-2, -2, 104, 104), rect.getBoundsInLocal());
  75         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  76 
  77         ds.setRadius(4);
  78         assertEquals(box(-3, -3, 106, 106), rect.getBoundsInLocal());
  79         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  80      }
  81 
  82     // test setting chained effects on a Node and changing the effect params
  83     //   (which causes the effect bounds to change) will update the bounds
  84     public @Test void testBoundsOnRectangleWithShadowAndGlowChanged() {
  85         Rectangle rect = new Rectangle(100, 100);
  86         DropShadow ds = new DropShadow();
  87         ds.setRadius(2);
  88         Glow g = new Glow();
  89         g.setInput(ds);
  90 
  91         rect.setEffect(g);
  92 
  93         assertEquals(box(-2, -2, 104, 104), rect.getBoundsInLocal());
  94         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  95         ds.setRadius(4);
  96         assertEquals(box(-3, -3, 106, 106), rect.getBoundsInLocal());
  97         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
  98     }
  99 
 100     // test setting an effect on a Node and a Clip works as expected
 101     public @Test void testBoundsOnRectangleWithShadowAndClip() {
 102         Rectangle rect = new Rectangle(100, 100);
 103         DropShadow ds = new DropShadow();
 104         ds.setRadius(2);
 105         rect.setEffect(ds);
 106         rect.setClip(new Rectangle(-10, -10, 30, 30));
 107         
 108         assertEquals(box(-2, -2, 22, 22), rect.getBoundsInLocal());
 109         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 110     }
 111 
 112     // test setting an effect on a Node alters the bounds
 113     public @Test void testBoundsOnRectangleWithShadowAndReflection() {
 114         Rectangle rect = new Rectangle(100, 100);
 115         DropShadow ds = new DropShadow();
 116         ds.setRadius(2);
 117         Reflection r = new Reflection();
 118         r.setFraction(0.5f);
 119         ds.setInput(r);
 120         rect.setEffect(ds);
 121         assertEquals(box(-2, -2, 104, 154), rect.getBoundsInLocal());
 122         assertEquals(rect.getBoundsInLocal(), rect.getBoundsInParent());
 123     }
 124 
 125     // test setting an effect on two Nodes and changing the effect params
 126     //   (which causes the effect bounds to change) will update the bounds
 127     // of both nodes
 128     public @Test void testBoundsOnRectanglesWithShadowChanged() {
 129         Rectangle rect1 = new Rectangle(100, 100);
 130         Rectangle rect2 = new Rectangle(100, 100);
 131         DropShadow ds = new DropShadow();
 132         ds.setRadius(2);
 133 
 134         rect1.setEffect(ds);
 135         rect2.setEffect(ds);
 136 
 137         assertEquals(box(-2, -2, 104, 104), rect1.getBoundsInLocal());
 138         assertEquals(rect1.getBoundsInLocal(), rect1.getBoundsInParent());
 139         assertEquals(box(-2, -2, 104, 104), rect2.getBoundsInLocal());
 140         assertEquals(rect2.getBoundsInLocal(), rect2.getBoundsInParent());
 141         ds.setRadius(4);
 142         assertEquals(box(-3, -3, 106, 106), rect1.getBoundsInLocal());
 143         assertEquals(rect1.getBoundsInLocal(), rect1.getBoundsInParent());
 144         assertEquals(box(-3, -3, 106, 106), rect2.getBoundsInLocal());
 145         assertEquals(rect2.getBoundsInLocal(), rect2.getBoundsInParent());
 146     }
 147 
 148     // test setting two effects on two Nodes and changing the effect params
 149     //   (which causes the effect bounds to change) will update the bounds
 150     // of both nodes
 151     public @Test void testBoundsOnRectanglesWithShadowAndGlowChanged() {
 152         Rectangle rect1 = new Rectangle(100, 100);
 153         Rectangle rect2 = new Rectangle(100, 100);
 154         DropShadow ds = new DropShadow();
 155         ds.setRadius(2);
 156         Glow g = new Glow();
 157         g.setInput(ds);
 158 
 159         rect1.setEffect(g);
 160         rect2.setEffect(g);
 161 
 162         assertEquals(box(-2, -2, 104, 104), rect1.getBoundsInLocal());
 163         assertEquals(rect1.getBoundsInLocal(), rect1.getBoundsInParent());
 164         assertEquals(box(-2, -2, 104, 104), rect2.getBoundsInLocal());
 165         assertEquals(rect2.getBoundsInLocal(), rect2.getBoundsInParent());
 166 
 167         ds.setRadius(4);
 168         assertEquals(box(-3, -3, 106, 106), rect1.getBoundsInLocal());
 169         assertEquals(rect1.getBoundsInLocal(), rect1.getBoundsInParent());
 170         assertEquals(box(-3, -3, 106, 106), rect2.getBoundsInLocal());
 171         assertEquals(rect2.getBoundsInLocal(), rect2.getBoundsInParent());
 172     }
 173 }