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