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 test.javafx.scene.effect;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 
  30 import java.lang.reflect.Method;
  31 
  32 import javafx.beans.property.BooleanProperty;
  33 import javafx.beans.property.DoubleProperty;
  34 import javafx.beans.property.IntegerProperty;
  35 import javafx.beans.property.ObjectProperty;
  36 import javafx.beans.property.SimpleBooleanProperty;
  37 import javafx.beans.property.SimpleDoubleProperty;
  38 import javafx.beans.property.SimpleIntegerProperty;
  39 import javafx.beans.property.SimpleObjectProperty;
  40 import javafx.scene.Group;
  41 import javafx.scene.Node;
  42 import javafx.scene.Scene;
  43 import javafx.scene.paint.Color;
  44 import javafx.scene.shape.Rectangle;
  45 import javafx.stage.Stage;
  46 
  47 import test.com.sun.javafx.pgstub.StubToolkit;
  48 import com.sun.javafx.tk.Toolkit;
  49 import com.sun.scenario.effect.Color4f;
  50 import javafx.scene.effect.BoxBlur;
  51 import javafx.scene.effect.Effect;
  52 
  53 public class EffectsTestBase {
  54     private Scene scene;
  55     private StubToolkit toolkit;
  56     private Stage stage;
  57     protected Node n;
  58     private Effect e;
  59     
  60     protected void setupTest(Effect effect) {
  61         e = effect;
  62         Group root = new Group();
  63         scene = new Scene(root);
  64         stage = new Stage();
  65         stage.setScene(scene);
  66         stage.show();
  67         toolkit = (StubToolkit) Toolkit.getToolkit(); 
  68         n = new Rectangle(100, 100);
  69         n.setEffect(effect);
  70         root.getChildren().add(n);
  71     }
  72 
  73     protected void setEffect(Effect effect) {
  74         n.setEffect(effect);
  75     }
  76     
  77     protected void pulse() {
  78         toolkit.fireTestPulse();
  79     }
  80 
  81     protected void checkDoublePropertySynced(
  82             String effectName,
  83             String propertyName,
  84             String pgEffectName,
  85             String pgPropertyName,
  86             double expected)
  87             throws Exception {
  88        checkDoublePropertySynced(e, e.impl_getImpl(), effectName, propertyName, pgEffectName, pgPropertyName, expected);
  89     }
  90 
  91     protected void checkDoublePropertySynced(
  92             Object inputObject,
  93             Object pgObject,
  94             String effectName,
  95             String propertyName,
  96             String pgEffectName,
  97             String pgPropertyName,
  98             double expected)
  99             throws Exception {
 100         final Class effectClass = Class.forName(effectName);
 101         final Class pgEffectClass = Class.forName(pgEffectName);
 102 
 103         final StringBuilder pgPropertyNameBuilder = new StringBuilder(pgPropertyName);
 104         pgPropertyNameBuilder.setCharAt(0, Character.toUpperCase(pgPropertyName.charAt(0)));
 105         final String pgGetterName = new StringBuilder("get").append(pgPropertyNameBuilder).toString();
 106         final Method pgGetter = pgEffectClass.getMethod(pgGetterName);
 107 
 108         DoubleProperty v = new SimpleDoubleProperty();
 109         Method m = effectClass.getMethod(propertyName + "Property", new Class[] {});
 110         ((DoubleProperty)m.invoke(inputObject)).bind(v);
 111 
 112         pulse(); // make sure that the dirty flag is cleaned before testing of binding
 113         v.set(expected);
 114         pulse();
 115         assertEquals((float)expected, ((Number)pgGetter.invoke(pgObject)).floatValue(), 1e-100);
 116     }
 117 
 118     protected void checkIntPropertySynced(
 119             String effectName,
 120             String propertyName,
 121             String pgEffectName,
 122             String pgPropertyName,
 123             int expected)
 124             throws Exception {
 125         checkIntPropertySynced(e, e.impl_getImpl(), effectName, propertyName, pgEffectName, pgPropertyName, expected);
 126     }
 127 
 128     protected void checkIntPropertySynced(
 129             Object inputObject,
 130             Object pgObject,
 131             String effectName,
 132             String propertyName,
 133             String pgEffectName,
 134             String pgPropertyName,
 135             int expected)
 136             throws Exception {
 137         final Class effectClass = Class.forName(effectName);
 138         final Class pgEffectClass = Class.forName(pgEffectName);
 139 
 140         final StringBuilder pgPropertyNameBuilder = new StringBuilder(pgPropertyName);
 141         pgPropertyNameBuilder.setCharAt(0, Character.toUpperCase(pgPropertyName.charAt(0)));
 142         final String pgGetterName = new StringBuilder("get").append(pgPropertyNameBuilder).toString();
 143         final Method pgGetter = pgEffectClass.getMethod(pgGetterName);
 144 
 145         IntegerProperty v = new SimpleIntegerProperty();
 146         Method m = effectClass.getMethod(propertyName + "Property", new Class[] {});
 147         ((IntegerProperty)m.invoke(inputObject)).bind(v);
 148 
 149         pulse(); // make sure that the dirty flag is cleaned before testing of binding
 150         v.set(expected);
 151         pulse();
 152         assertEquals(expected, ((Number)pgGetter.invoke(pgObject)).intValue());
 153     }
 154 
 155     protected void checkBooleanPropertySynced(
 156             String effectName,
 157             String propertyName,
 158             String pgEffectName,
 159             String pgPropertyName,
 160             boolean expected)
 161             throws Exception {
 162         checkBooleanPropertySynced(e, e.impl_getImpl(), effectName, propertyName, pgEffectName, pgPropertyName, expected);
 163     }
 164 
 165     protected void checkBooleanPropertySynced(
 166             Object inputObject,
 167             Object pgObject,
 168             String effectName,
 169             String propertyName,
 170             String pgEffectName,
 171             String pgPropertyName,
 172             boolean expected)
 173             throws Exception {
 174         final Class effectClass = Class.forName(effectName);
 175         final Class pgEffectClass = Class.forName(pgEffectName);
 176 
 177         final StringBuilder pgPropertyNameBuilder = new StringBuilder(pgPropertyName);
 178         pgPropertyNameBuilder.setCharAt(0, Character.toUpperCase(pgPropertyName.charAt(0)));
 179         final String pgGetterName = new StringBuilder("get").append(pgPropertyNameBuilder).toString();
 180         final Method pgGetter = pgEffectClass.getMethod(pgGetterName);
 181 
 182         BooleanProperty v = new SimpleBooleanProperty();
 183         Method m = effectClass.getMethod(propertyName + "Property", new Class[] {});
 184         ((BooleanProperty)m.invoke(inputObject)).bind(v);
 185 
 186         pulse(); // make sure that the dirty flag is cleaned before testing of binding
 187         v.set(expected);
 188         pulse();
 189         assertEquals(expected, pgGetter.invoke(pgObject));
 190     }
 191 
 192     protected void checkObjectPropertySynced(
 193             String effectName,
 194             String propertyName,
 195             String pgEffectName,
 196             String pgPropertyName,
 197             Object expected,
 198             Object pgExpected,
 199             Object defaultVal)
 200             throws Exception {
 201        checkObjectPropertySynced(e, e.impl_getImpl(), effectName, propertyName, pgEffectName, pgPropertyName, expected, pgExpected, defaultVal);
 202     }
 203 
 204     protected void checkObjectPropertySynced(
 205             Object inputObject,
 206             Object pgObject,
 207             String effectName,
 208             String propertyName,
 209             String pgEffectName,
 210             String pgPropertyName,
 211             Object expected,
 212             Object pgExpected,
 213             Object defaultVal)
 214             throws Exception {
 215         final Class effectClass = Class.forName(effectName);
 216         final Class pgEffectClass = Class.forName(pgEffectName);
 217 
 218         final StringBuilder pgPropertyNameBuilder = new StringBuilder(pgPropertyName);
 219         pgPropertyNameBuilder.setCharAt(0, Character.toUpperCase(pgPropertyName.charAt(0)));
 220         final String pgGetterName = new StringBuilder("get").append(pgPropertyNameBuilder).toString();
 221         final Method pgGetter = pgEffectClass.getMethod(pgGetterName);
 222 
 223         ObjectProperty v = new SimpleObjectProperty(defaultVal);
 224         Method m = effectClass.getMethod(propertyName + "Property", new Class[] {});
 225 
 226         ((ObjectProperty)m.invoke(inputObject)).bind(v);
 227 
 228         pulse(); // make sure that the dirty flag is cleaned before testing of binding
 229         v.set(expected);
 230         pulse();
 231         assertEquals(pgExpected, pgGetter.invoke(pgObject));
 232     }
 233 
 234     protected static void assertColor4fEquals(Color4f expected, Color4f actual) {
 235         assertEquals(expected.getRed(), actual.getRed(), 1e-100);
 236         assertEquals(expected.getGreen(), actual.getGreen(), 1e-100);
 237         assertEquals(expected.getBlue(), actual.getBlue(), 1e-100);
 238         assertEquals(expected.getAlpha(), actual.getAlpha(), 1e-100);
 239     }
 240 
 241     protected void checkEffectPropertySynced(
 242             String effectName,
 243             String propertyName,
 244             String pgEffectName,
 245             String pgPropertyName,
 246             BoxBlur expected,
 247             com.sun.scenario.effect.BoxBlur pgExpected)
 248             throws Exception {
 249         final Class effectClass = Class.forName(effectName);
 250         final Class pgEffectClass = Class.forName(pgEffectName);
 251 
 252         final StringBuilder pgPropertyNameBuilder = new StringBuilder(pgPropertyName);
 253         pgPropertyNameBuilder.setCharAt(0, Character.toUpperCase(pgPropertyName.charAt(0)));
 254         final String pgGetterName = new StringBuilder("get").append(pgPropertyNameBuilder).toString();
 255         final Method pgGetter = pgEffectClass.getMethod(pgGetterName);
 256 
 257         ObjectProperty v = new SimpleObjectProperty();
 258         Method m = effectClass.getMethod(propertyName + "Property", new Class[] {});
 259         ((ObjectProperty)m.invoke(e)).bind(v);
 260 
 261         pulse(); // make sure that the dirty flag is cleaned before testing of binding
 262         v.set(expected);
 263         pulse();
 264         assertEquals(pgExpected, pgGetter.invoke(e.impl_getImpl()));
 265 
 266         // test wheter input listeners were correctly registered
 267         expected.setWidth(150);
 268         assertEquals(150, expected.getWidth(), 1e-100);
 269         pulse();
 270         assertEquals(150, pgExpected.getHorizontalSize());
 271     }
 272 
 273     protected Object getObjectPropertySynced(
 274             String effectName,
 275             String propertyName,
 276             String pgEffectName,
 277             String pgPropertyName,
 278             Object expected)
 279             throws Exception {
 280         return getObjectPropertySynced(e, e.impl_getImpl(), effectName, propertyName, pgEffectName, pgPropertyName, expected);
 281     }
 282 
 283     protected Object getObjectPropertySynced(
 284             Object inputObject,
 285             Object pgObject,
 286             String effectName,
 287             String propertyName,
 288             String pgEffectName,
 289             String pgPropertyName,
 290             Object expected)
 291             throws Exception {
 292         final Class effectClass = Class.forName(effectName);
 293         final Class pgEffectClass = Class.forName(pgEffectName);
 294 
 295         final StringBuilder pgPropertyNameBuilder = new StringBuilder(pgPropertyName);
 296         pgPropertyNameBuilder.setCharAt(0, Character.toUpperCase(pgPropertyName.charAt(0)));
 297         final String pgGetterName = new StringBuilder("get").append(pgPropertyNameBuilder).toString();
 298         final Method pgGetter = pgEffectClass.getMethod(pgGetterName);
 299 
 300         ObjectProperty v = new SimpleObjectProperty(Color.BLACK);
 301         Method m = effectClass.getMethod(propertyName + "Property", new Class[] {});
 302 
 303         ((ObjectProperty)m.invoke(inputObject)).bind(v);
 304         pulse(); // make sure that the dirty flag is cleaned before testing of binding
 305         v.set(expected);
 306         pulse();
 307         return pgGetter.invoke(pgObject);
 308     }
 309     
 310     protected Object getDoublePropertySynced(
 311             String effectName,
 312             String propertyName,
 313             String pgEffectName,
 314             String pgPropertyName,
 315             double expected)
 316             throws Exception {
 317         final Class effectClass = Class.forName(effectName);
 318         final Class pgEffectClass = Class.forName(pgEffectName);
 319 
 320         final StringBuilder pgPropertyNameBuilder = new StringBuilder(pgPropertyName);
 321         pgPropertyNameBuilder.setCharAt(0, Character.toUpperCase(pgPropertyName.charAt(0)));
 322         final String pgGetterName = new StringBuilder("get").append(pgPropertyNameBuilder).toString();
 323         final Method pgGetter = pgEffectClass.getMethod(pgGetterName);
 324 
 325         DoubleProperty v = new SimpleDoubleProperty();
 326         Method m = effectClass.getMethod(propertyName + "Property", new Class[] {});
 327 
 328         ((DoubleProperty)m.invoke(e)).bind(v);
 329         pulse(); // make sure that the dirty flag is cleaned before testing of binding
 330         v.set(expected);
 331         pulse();
 332         return pgGetter.invoke(e.impl_getImpl());
 333     }
 334 }