1 /*
   2  * Copyright (c) 2010, 2014, 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.image;
  27 
  28 import com.sun.javafx.pgstub.StubImageLoaderFactory;
  29 import com.sun.javafx.pgstub.StubPlatformImageInfo;
  30 import com.sun.javafx.pgstub.StubToolkit;
  31 import com.sun.javafx.sg.prism.NGImageView;
  32 import com.sun.javafx.sg.prism.NGNode;
  33 import com.sun.javafx.tk.Toolkit;
  34 import javafx.geometry.Rectangle2D;
  35 import javafx.scene.NodeTest;
  36 import org.junit.After;
  37 import org.junit.Before;
  38 import org.junit.Test;
  39 
  40 import java.util.Comparator;
  41 import javafx.beans.value.ChangeListener;
  42 import javafx.beans.value.ObservableValue;
  43 import javafx.geometry.Bounds;
  44 
  45 import static org.junit.Assert.*;
  46 
  47 public final class ImageViewTest {
  48     private ImageView imageView;
  49 
  50     @Before
  51     public void setUp() {
  52         imageView = new StubImageView();
  53         imageView.setImage(TestImages.TEST_IMAGE_100x200);
  54     }
  55 
  56     @After
  57     public void tearDown() {
  58         imageView = null;
  59     }
  60 
  61     @Test
  62     public void testPropertyPropagation_x() throws Exception {
  63         NodeTest.testDoublePropertyPropagation(imageView, "X", 100, 200);
  64     }
  65 
  66     @Test
  67     public void testPropertyPropagation_y() throws Exception {
  68         NodeTest.testDoublePropertyPropagation(imageView, "Y", 100, 200);
  69     }
  70 
  71     @Test
  72     public void testPropertyPropagation_smooth() throws Exception {
  73         NodeTest.testBooleanPropertyPropagation(
  74                 imageView, "smooth", false, true);
  75     }
  76 
  77     @Test
  78     public void testPropertyPropagation_viewport() throws Exception {
  79         NodeTest.testObjectPropertyPropagation(
  80                 imageView, "viewport",
  81                 new Rectangle2D(10, 20, 200, 100),
  82                 new Rectangle2D(20, 10, 100, 200));
  83     }
  84 
  85     @Test
  86     public void testPropertyPropagation_image() throws Exception {
  87         NodeTest.testObjectPropertyPropagation(
  88                 imageView, "image", "image",
  89                 null,
  90                 TestImages.TEST_IMAGE_200x100,
  91                 (sgValue, pgValue) -> {
  92                     if (sgValue == null) {
  93                         assertNull(pgValue);
  94                     } else {
  95                         assertSame(
  96                                 ((Image) sgValue).impl_getPlatformImage(),
  97                                 pgValue);
  98                     }
  99 
 100                     return 0;
 101                 }
 102         );
 103     }
 104 
 105     @Test
 106     public void testUrlConstructor() {
 107         final StubImageLoaderFactory imageLoaderFactory =
 108                 ((StubToolkit) Toolkit.getToolkit()).getImageLoaderFactory();
 109 
 110         final String url = "file:img_view_image.png";
 111         imageLoaderFactory.registerImage(
 112                 url, new StubPlatformImageInfo(50, 40));
 113 
 114         final ImageView newImageView = new ImageView(url);
 115 
 116         assertEquals(url, newImageView.getImage().impl_getUrl());
 117     }
 118 
 119     @Test
 120     public void testNullImage() {
 121         imageView.setImage(null);
 122         assertNull(imageView.getImage());
 123     }
 124 
 125     @Test
 126     public void testNullViewport() {
 127         imageView.setViewport(null);
 128         assertNull(imageView.getViewport());
 129     }
 130 
 131     private static class BoundsChangedListener implements ChangeListener<Bounds> {
 132         private boolean wasCalled = false;
 133 
 134         public void changed(ObservableValue<? extends Bounds> ov, Bounds oldValue, Bounds newValue) {
 135                 assertEquals(oldValue.getWidth(), 32, 1e-10);
 136                 assertEquals(oldValue.getHeight(), 32, 1e-10);
 137                 assertEquals(newValue.getWidth(), 200, 1e-10);
 138                 assertEquals(newValue.getHeight(), 100, 1e-10);
 139                 wasCalled = true;
 140         }
 141     }
 142 
 143     @Test
 144     public void testImageChangesBoundsWithListener() {
 145         BoundsChangedListener listener = new BoundsChangedListener();
 146         imageView.setImage(TestImages.TEST_IMAGE_32x32);
 147         imageView.boundsInParentProperty().addListener(listener);
 148         imageView.setImage(TestImages.TEST_IMAGE_200x100);
 149         assertTrue(listener.wasCalled);
 150     }
 151 
 152     /*
 153     @Test
 154     public void testPlatformImageChangeForAsyncLoadedImage() {
 155         final StubImageLoaderFactory imageLoaderFactory =
 156                 ((StubToolkit) Toolkit.getToolkit()).getImageLoaderFactory();
 157 
 158         final String url = "file:async.png";
 159         imageLoaderFactory.registerImage(
 160                 url, new StubPlatformImageInfo(100, 200));
 161 
 162         final Image placeholderImage =
 163                 TestImages.TEST_IMAGE_200x100;
 164         final Image asyncImage =
 165                 new Image(url, 200, 100, true, true, true);
 166 
 167         final StubAsyncImageLoader lastAsyncImageLoader =
 168                 imageLoaderFactory.getLastAsyncImageLoader();
 169 
 170         final StubImageView pgImageView =
 171                 (StubImageView) imageView.impl_getPGNode();
 172 
 173         imageView.setImage(asyncImage);
 174 
 175         NodeTest.callSyncPGNode(imageView);
 176         assertSame(placeholderImage.impl_getPlatformImage(),
 177                    pgImageView.getImage());
 178         assertBoundsEqual(box(0, 0, 200, 100), imageView.getBoundsInLocal());
 179 
 180         lastAsyncImageLoader.finish();
 181 
 182         NodeTest.callSyncPGNode(imageView);
 183         assertSame(asyncImage.impl_getPlatformImage(),
 184                    pgImageView.getImage());
 185         assertBoundsEqual(box(0, 0, 100, 200), imageView.getBoundsInLocal());
 186     }
 187     */
 188 
 189     public class StubImageView extends ImageView {
 190         public StubImageView() {
 191             super();
 192         }
 193 
 194         @Override
 195         protected NGNode impl_createPeer() {
 196             return new StubNGImageView();
 197         }
 198     }
 199 
 200     public class StubNGImageView extends NGImageView {
 201         // for tests
 202         private Object image;
 203         private float x;
 204         private float y;
 205         private boolean smooth;
 206 
 207         private float cw;
 208         private float ch;
 209         private Rectangle2D viewport;
 210 
 211         @Override public void setImage(Object image) { this.image = image; }
 212         public Object getImage() { return image; }
 213         @Override public void setX(float x) { this.x = x; }
 214         public float getX() { return x; }
 215         @Override public void setY(float y) { this.y = y; }
 216         public float getY() { return y; }
 217 
 218         @Override public void setViewport(float vx, float vy, float vw, float vh,
 219                                 float cw, float ch) {
 220             this.viewport = new Rectangle2D(vx, vy, vw, vh);
 221             this.cw = cw;
 222             this.ch = ch;
 223         }
 224 
 225         @Override public void setSmooth(boolean smooth) { this.smooth = smooth; }
 226         public boolean isSmooth() { return this.smooth; }
 227         public Rectangle2D getViewport() { return viewport; }
 228         public float getContentWidth() { return cw; }
 229         public float getContentHeight() { return ch; }
 230     }
 231 }