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 
  30 import java.lang.reflect.Method;
  31 
  32 import com.sun.javafx.FXUnit;
  33 import javafx.beans.property.IntegerProperty;
  34 import javafx.beans.property.SimpleIntegerProperty;
  35 
  36 import org.junit.Before;
  37 import org.junit.Rule;
  38 import org.junit.Test;
  39 
  40 public class FloatMapTest extends EffectsTestBase {
  41 
  42     @Rule
  43     public FXUnit fx = new FXUnit();
  44 
  45     private FloatMap floatMap;
  46     private DisplacementMap displacementMap;
  47 
  48     @Before
  49     public void setUp() {
  50         floatMap = new FloatMap();
  51         displacementMap = new DisplacementMap();
  52         displacementMap.setMapData(floatMap);
  53         setupTest(displacementMap);
  54     }
  55 
  56     @Test
  57     public void testSetWidth() {
  58         // try setting correct value
  59         floatMap.setWidth(2);
  60         assertEquals(2, floatMap.getWidth());
  61         pulse();
  62         assertEquals(2, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getWidth());
  63     }
  64 
  65     @Test
  66     public void testDefaultWidth() {
  67         // default value should be 1
  68         assertEquals(1, floatMap.getWidth());
  69         assertEquals(1, floatMap.widthProperty().get());
  70         pulse();
  71         assertEquals(1, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getWidth());
  72     }
  73 
  74     @Test
  75     public void testMinWidth() {
  76         // 1 should be ok
  77         floatMap.setWidth(1);
  78         // try setting value smaller than minimal
  79         floatMap.setWidth(0);
  80         assertEquals(0, floatMap.getWidth());
  81         pulse();
  82         assertEquals(1, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getWidth());      
  83     }
  84 
  85     @Test
  86     public void testMaxWidth() {
  87         // 4096 should be ok
  88         floatMap.setWidth(4096);
  89         // try setting value greater than maximal
  90         floatMap.setWidth(4097);
  91         assertEquals(4097, floatMap.getWidth());
  92         pulse();
  93         assertEquals(4096, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getWidth());              
  94     }
  95 
  96     @Test
  97     public void testSetHeight() {
  98         // try setting correct value
  99         floatMap.setHeight(5);
 100         assertEquals(5, floatMap.getHeight());
 101         pulse();
 102         assertEquals(5, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getHeight());
 103     }
 104 
 105     @Test
 106     public void testDefaultHeight() {
 107         // default value should be 1
 108         assertEquals(1, floatMap.getHeight());
 109         assertEquals(1, floatMap.heightProperty().get());
 110         pulse();
 111         assertEquals(1, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getHeight());
 112     }
 113 
 114     @Test
 115     public void testMinHeight() {
 116         // 1 should be ok
 117         floatMap.setHeight(1);
 118         // try setting value smaller than minimal
 119         floatMap.setHeight(0);
 120         assertEquals(0, floatMap.getHeight());
 121         pulse();
 122         assertEquals(1, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getHeight());
 123     }
 124 
 125     @Test
 126     public void testMaxHeight() {
 127         // 4096 should be ok
 128         floatMap.setHeight(4096);
 129         // try setting value greater than maximal
 130         floatMap.setHeight(4097);
 131         assertEquals(4097, floatMap.getHeight());
 132         pulse();
 133         assertEquals(4096, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getHeight());
 134     }
 135 
 136     @Test
 137     public void testSetSamples3() {
 138         floatMap.setSamples(0, 0, 0.5f);
 139         pulse();
 140         com.sun.scenario.effect.FloatMap fm = floatMap.getImpl();
 141         float[] data = fm.getData();
 142         assertEquals(0.5f, data[0], 1e-100);
 143         assertEquals(0.0f, data[1], 1e-100);
 144         assertEquals(0.0f, data[2], 1e-100);
 145         assertEquals(0.0f, data[3], 1e-100);
 146     }
 147 
 148     @Test
 149     public void testSetSamples4() {
 150         floatMap.setSamples(0, 0, 0.5f, 0.4f);
 151         pulse();
 152         com.sun.scenario.effect.FloatMap fm = floatMap.getImpl();
 153         float[] data = fm.getData();
 154         assertEquals(0.5f, data[0], 1e-100);
 155         assertEquals(0.4f, data[1], 1e-100);
 156         assertEquals(0.0f, data[2], 1e-100);
 157         assertEquals(0.0f, data[3], 1e-100);        
 158     }
 159 
 160     @Test
 161     public void testSetSamples5() {
 162         floatMap.setSamples(0, 0, 0.5f, 0.4f, 0.3f);
 163         pulse();
 164         com.sun.scenario.effect.FloatMap fm = floatMap.getImpl();
 165         float[] data = fm.getData();
 166         assertEquals(0.5f, data[0], 1e-100);
 167         assertEquals(0.4f, data[1], 1e-100);
 168         assertEquals(0.3f, data[2], 1e-100);
 169         assertEquals(0.0f, data[3], 1e-100);        
 170     }
 171     
 172     @Test
 173     public void testSetSamples6() {
 174         floatMap.setSamples(0, 0, 0.5f, 0.4f, 0.3f, 0.2f);
 175         pulse();
 176         com.sun.scenario.effect.FloatMap fm = floatMap.getImpl();
 177         float[] data = fm.getData();
 178         assertEquals(0.5f, data[0], 1e-100);
 179         assertEquals(0.4f, data[1], 1e-100);
 180         assertEquals(0.3f, data[2], 1e-100);
 181         assertEquals(0.2f, data[3], 1e-100);      
 182     }
 183     
 184     @Test
 185     public void testSetSample() {
 186         floatMap.setSample(0, 0, 0, 0.5f);
 187         floatMap.setSample(0, 0, 1, 0.4f);
 188         floatMap.setSample(0, 0, 2, 0.3f);
 189         floatMap.setSample(0, 0, 3, 0.2f);
 190         pulse();
 191         com.sun.scenario.effect.FloatMap fm = floatMap.getImpl();
 192         float[] data = fm.getData();
 193         assertEquals(0.5f, data[0], 1e-100);
 194         assertEquals(0.4f, data[1], 1e-100);
 195         assertEquals(0.3f, data[2], 1e-100);
 196         assertEquals(0.2f, data[3], 1e-100);      
 197     }
 198 
 199     @Test
 200     public void testHeightSynced() throws Exception {
 201         checkIntPropertySynced(
 202                 "javafx.scene.effect.FloatMap", "height",
 203                 "com.sun.scenario.effect.FloatMap", "height", 10);
 204     }
 205 
 206     @Test
 207     public void testWidthSynced() throws Exception {
 208         checkIntPropertySynced(
 209                 "javafx.scene.effect.FloatMap", "width",
 210                 "com.sun.scenario.effect.FloatMap", "width", 10);
 211     }
 212 
 213     // special version of check method, because floatMap itself is not
 214     // an effect and its peer isn't created until after pulse happens
 215     @Override
 216     protected void checkIntPropertySynced(
 217             String effectName,
 218             String propertyName,
 219             String pgEffectName,
 220             String pgPropertyName,
 221             int expected)
 222             throws Exception {
 223         final Class effectClass = Class.forName(effectName);
 224         final Class pgEffectClass = Class.forName(pgEffectName);
 225 
 226         final StringBuilder pgPropertyNameBuilder = new StringBuilder(pgPropertyName);
 227         pgPropertyNameBuilder.setCharAt(0, Character.toUpperCase(pgPropertyName.charAt(0)));
 228         final String pgGetterName = new StringBuilder("get").append(pgPropertyNameBuilder).toString();
 229         final Method pgGetter = pgEffectClass.getMethod(pgGetterName);
 230 
 231         IntegerProperty v = new SimpleIntegerProperty();
 232         Method m = effectClass.getMethod(propertyName + "Property", new Class[] {});
 233         ((IntegerProperty)m.invoke(floatMap)).bind(v);
 234 
 235         pulse(); // make sure that the dirty flag is cleaned before testing of binding
 236         v.set(expected);
 237         pulse();
 238         assertEquals(expected, ((Number)pgGetter.invoke(floatMap.getImpl())).intValue());
 239     }
 240 
 241     @Test
 242     public void testCreateWithParams() {
 243         floatMap = new FloatMap(2, 3);
 244         displacementMap = new DisplacementMap();
 245         displacementMap.setMapData(floatMap);
 246         setupTest(displacementMap);
 247         assertEquals(2, floatMap.getWidth(), 1e-100);
 248         assertEquals(3, floatMap.getHeight(), 1e-100);
 249         pulse();
 250         assertEquals(2, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getWidth());
 251         assertEquals(3, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getHeight());
 252     }
 253     
 254     @Test
 255     public void testCreateWithDefaultParams() {
 256         floatMap = new FloatMap(1, 1);
 257         displacementMap = new DisplacementMap();
 258         displacementMap.setMapData(floatMap);
 259         setupTest(displacementMap);
 260         assertEquals(1, floatMap.getWidth(), 1e-100);
 261         assertEquals(1, floatMap.getHeight(), 1e-100);
 262         pulse();
 263         assertEquals(1, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getWidth());
 264         assertEquals(1, ((com.sun.scenario.effect.FloatMap) floatMap.getImpl()).getHeight());
 265     }
 266 
 267 }