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 com.sun.javafx.test.TestHelper.box;
  29 import static org.junit.Assert.assertEquals;
  30 import static org.junit.Assert.assertFalse;
  31 import static org.junit.Assert.assertNotNull;
  32 import static org.junit.Assert.assertNull;
  33 import static org.junit.Assert.assertTrue;
  34 
  35 import org.junit.Before;
  36 import org.junit.Test;
  37 
  38 public class DisplacementMapTest extends EffectsTestBase {
  39     private DisplacementMap effect;
  40 
  41     @Before
  42     public void setUp() {
  43         effect = new DisplacementMap();
  44         setupTest(effect);
  45     }
  46 
  47     @Test
  48     public void testSetOffsetX() {
  49         // try setting correct value
  50         effect.setOffsetX(1.0f);
  51         assertEquals(1.0f, effect.getOffsetX(), 1e-100);
  52         pulse();
  53         assertEquals(1.0f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getOffsetX(), 1e-100);
  54     }
  55 
  56     @Test
  57     public void testDefaultOffsetX() {
  58         // default value should be 0
  59         assertEquals(0f, effect.getOffsetX(), 1e-100);
  60         assertEquals(0f, effect.offsetXProperty().get(), 1e-100);
  61         pulse();
  62         assertEquals(0f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getOffsetX(), 1e-100);
  63     }
  64 
  65     @Test
  66     public void testSetOffsetY() {
  67         // try setting correct value
  68         effect.setOffsetY(1.0f);
  69         assertEquals(1.0f, effect.getOffsetY(), 1e-100);
  70         pulse();
  71         assertEquals(1.0f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getOffsetY(), 1e-100);
  72     }
  73 
  74     @Test
  75     public void testDefaultOffsetY() {
  76         // default value should be 0
  77         assertEquals(0f, effect.getOffsetY(), 1e-100);
  78         assertEquals(0f, effect.offsetYProperty().get(), 1e-100);
  79         pulse();
  80         assertEquals(0f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getOffsetY(), 1e-100);
  81     }
  82 
  83     @Test
  84     public void testSetScaleX() {
  85         // try setting correct value
  86         effect.setScaleX(1.1f);
  87         assertEquals(1.1f, effect.getScaleX(), 1e-100);
  88         pulse();
  89         assertEquals(1.1f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getScaleX(), 1e-100);
  90     }
  91 
  92     @Test
  93     public void testDefaultScaleX() {
  94         // default value should be 1
  95         assertEquals(1f, effect.getScaleX(), 1e-100);
  96         assertEquals(1f, effect.scaleXProperty().get(), 1e-100);
  97         pulse();
  98         assertEquals(1f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getScaleX(), 1e-100);
  99     }
 100 
 101     @Test
 102     public void testSetScaleY() {
 103         // try setting correct value
 104         effect.setScaleY(1.1f);
 105         assertEquals(1.1f, effect.getScaleY(), 1e-100);
 106         pulse();
 107         assertEquals(1.1f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getScaleY(), 1e-100);
 108     }
 109 
 110     @Test
 111     public void testDefaultScaleY() {
 112         // default value should be 1
 113         assertEquals(1f, effect.getScaleY(), 1e-100);
 114         assertEquals(1f, effect.scaleYProperty().get(), 1e-100);
 115         pulse();
 116         assertEquals(1f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getScaleY(), 1e-100);
 117     }
 118 
 119     @Test
 120     public void testSetWrap() {
 121         // try setting correct value
 122         effect.setWrap(true);
 123         assertTrue(effect.isWrap());
 124         pulse();
 125         assertTrue(((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getWrap());
 126     }
 127 
 128     @Test
 129     public void testDefaultWrap() {
 130         // default value should be false
 131         assertFalse(effect.isWrap());
 132         assertFalse(effect.wrapProperty().get());
 133         pulse();
 134         assertFalse(((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getWrap());
 135     }
 136 
 137     private FloatMap createFloatMap(int width, int height) {
 138         FloatMap map = new FloatMap();
 139         map.setWidth(width);
 140         map.setHeight(height);
 141 
 142         for (int i = 0; i < width; i++) {
 143             double v = 1.0;
 144             for (int j = 0; j < height; j++) {
 145                 map.setSamples(i, j, 0.0f, (float) v);
 146             }
 147         }
 148 
 149         return map;
 150     }
 151     
 152     @Test
 153     public void testSetMap() {
 154         int w = 100;
 155         int h = 50;
 156 
 157         FloatMap map = createFloatMap(w, h);
 158         map.setWidth(w);
 159         map.setHeight(h);
 160 
 161         effect.setMapData(map);
 162         
 163         assertEquals(w, map.getWidth());
 164         assertEquals(h, map.getHeight());
 165         pulse();
 166         com.sun.scenario.effect.FloatMap mapImpl =
 167                 ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getMapData();
 168         assertNotNull(mapImpl);
 169         assertEquals(w, mapImpl.getWidth());
 170         assertEquals(h, mapImpl.getHeight());      
 171     }
 172 
 173     @Test
 174     public void testDefaultMap() {
 175         // Default is empty map
 176         FloatMap map = effect.getMapData();
 177         assertNotNull(map);
 178         assertEquals(1, map.getWidth());
 179         assertEquals(1, map.getHeight());
 180         pulse();
 181         com.sun.scenario.effect.FloatMap mapImpl =
 182                 ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getMapData();
 183         assertNotNull(mapImpl);
 184         assertEquals(1, mapImpl.getWidth());
 185         assertEquals(1, mapImpl.getHeight());
 186 
 187         assertEquals(map, effect.mapDataProperty().get());
 188     }
 189 
 190     @Test
 191     public void testDefaultMapNotChangedByThisEffect() {
 192         // Default is empty map
 193         FloatMap map = effect.getMapData();
 194         map.setHeight(100);
 195         map.setWidth(200);
 196 
 197         effect.setMapData(null);
 198 
 199         // null map data should default to empty map
 200         assertNull(effect.getMapData());
 201         pulse();
 202         com.sun.scenario.effect.FloatMap mapImpl =
 203                 ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getMapData();
 204         assertNotNull(mapImpl);
 205         assertEquals(1, mapImpl.getWidth());
 206         assertEquals(1, mapImpl.getHeight());
 207     }
 208 
 209     @Test
 210     public void testOffsetXSynced() throws Exception {
 211         checkDoublePropertySynced(
 212                 "javafx.scene.effect.DisplacementMap", "offsetX",
 213                 "com.sun.scenario.effect.DisplacementMap", "offsetX", 10);
 214     }
 215 
 216     @Test
 217     public void testOffsetYSynced() throws Exception {
 218         checkDoublePropertySynced(
 219                 "javafx.scene.effect.DisplacementMap", "offsetY",
 220                 "com.sun.scenario.effect.DisplacementMap", "offsetY", 10);
 221     }
 222 
 223     @Test
 224     public void testScaleXSynced() throws Exception {
 225         checkDoublePropertySynced(
 226                 "javafx.scene.effect.DisplacementMap", "scaleX",
 227                 "com.sun.scenario.effect.DisplacementMap", "scaleX", 10);
 228     }
 229 
 230     @Test
 231     public void testScaleYSynced() throws Exception {
 232         checkDoublePropertySynced(
 233                 "javafx.scene.effect.DisplacementMap", "scaleY",
 234                 "com.sun.scenario.effect.DisplacementMap", "scaleY", 10);
 235     }
 236 
 237     @Test
 238     public void testWrapSynced() throws Exception {
 239         checkBooleanPropertySynced(
 240                 "javafx.scene.effect.DisplacementMap", "wrap",
 241                 "com.sun.scenario.effect.DisplacementMap", "wrap", true);
 242     }
 243 
 244     @Test
 245     public void testInputSynced() throws Exception {
 246         BoxBlur blur = new BoxBlur();
 247         checkEffectPropertySynced(
 248                 "javafx.scene.effect.DisplacementMap", "input",
 249                 "com.sun.scenario.effect.DisplacementMap", "contentInput",
 250                 blur, (com.sun.scenario.effect.BoxBlur)blur.impl_getImpl());
 251     }
 252 
 253     @Test
 254     public void testBounds() {
 255         assertEquals(box(0, 0, 100, 100), n.getBoundsInLocal());
 256     }
 257 
 258     @Test
 259     public void testBoundsWidthInput() {
 260         assertEquals(box(0, 0, 100, 100), n.getBoundsInLocal());
 261         BoxBlur blur = new BoxBlur();
 262         effect.setInput(blur);
 263         assertEquals(box(-2, -2, 104, 104), n.getBoundsInLocal());
 264     }
 265     
 266     @Test
 267     public void testCreateWithParams() {
 268         int w = 100;
 269         int h = 50;
 270 
 271         FloatMap map = createFloatMap(w, h);
 272         effect = new DisplacementMap(map);
 273         setupTest(effect);
 274         assertEquals(map, effect.getMapData());
 275         pulse();
 276         com.sun.scenario.effect.FloatMap mapImpl =
 277                 ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getMapData();
 278         assertNotNull(mapImpl);
 279         assertEquals(w, mapImpl.getWidth());
 280         assertEquals(h, mapImpl.getHeight());
 281     }
 282 
 283     @Test
 284     public void testCreateWithParams5() {
 285         int w = 100;
 286         int h = 50;
 287 
 288         FloatMap map = createFloatMap(w, h);
 289         effect = new DisplacementMap(map, 1, 2, 3, 4);
 290         setupTest(effect);
 291         assertEquals(1, effect.getOffsetX(), 1e-100);
 292         assertEquals(2, effect.getOffsetY(), 1e-100);
 293         assertEquals(3, effect.getScaleX(), 1e-100);
 294         assertEquals(4, effect.getScaleY(), 1e-100);
 295         assertEquals(map, effect.getMapData());
 296         pulse();
 297         assertEquals(1f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getOffsetX(), 1e-100);
 298         assertEquals(2f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getOffsetY(), 1e-100);
 299         assertEquals(3f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getScaleX(), 1e-100);
 300         assertEquals(4f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getScaleY(), 1e-100);
 301         com.sun.scenario.effect.FloatMap mapImpl =
 302                 ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getMapData();
 303         assertNotNull(mapImpl);
 304         assertEquals(w, mapImpl.getWidth());
 305         assertEquals(h, mapImpl.getHeight());
 306     }
 307 
 308     @Test
 309     public void testCreateWithDefaultParams() {
 310         int w = 1;
 311         int h = 1;
 312 
 313         FloatMap map = createFloatMap(w, h);
 314         effect = new DisplacementMap(map);
 315         setupTest(effect);
 316         assertEquals(map, effect.getMapData());
 317         pulse();
 318         com.sun.scenario.effect.FloatMap mapImpl =
 319                 ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getMapData();
 320         assertNotNull(mapImpl);
 321         assertEquals(w, mapImpl.getWidth());
 322         assertEquals(h, mapImpl.getHeight());
 323     }
 324 
 325     @Test
 326     public void testCreateWithDefaultParams5() {
 327         int w = 1;
 328         int h = 1;
 329 
 330         FloatMap map = createFloatMap(w, h);
 331         effect = new DisplacementMap(map, 0, 0, 1, 1);
 332         setupTest(effect);
 333         assertEquals(0, effect.getOffsetX(), 1e-100);
 334         assertEquals(0, effect.getOffsetY(), 1e-100);
 335         assertEquals(1, effect.getScaleX(), 1e-100);
 336         assertEquals(1, effect.getScaleY(), 1e-100);
 337         assertEquals(map, effect.getMapData());
 338         pulse();
 339         assertEquals(0f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getOffsetX(), 1e-100);
 340         assertEquals(0f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getOffsetY(), 1e-100);
 341         assertEquals(1f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getScaleX(), 1e-100);
 342         assertEquals(1f, ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getScaleY(), 1e-100);
 343         com.sun.scenario.effect.FloatMap mapImpl =
 344                 ((com.sun.scenario.effect.DisplacementMap) effect.impl_getImpl()).getMapData();
 345         assertNotNull(mapImpl);
 346         assertEquals(w, mapImpl.getWidth());
 347         assertEquals(h, mapImpl.getHeight());
 348     }
 349 }