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