1 /*
   2  * Copyright (c) 2011, 2015, 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.animation;
  27 
  28 import javafx.animation.AnimationShim;
  29 import javafx.animation.Interpolator;
  30 import javafx.animation.ParallelTransition;
  31 import javafx.animation.StrokeTransition;
  32 import javafx.animation.TransitionShim;
  33 import static org.junit.Assert.assertEquals;
  34 import static org.junit.Assert.assertFalse;
  35 import static org.junit.Assert.assertNull;
  36 import static org.junit.Assert.assertTrue;
  37 import javafx.scene.Group;
  38 import javafx.scene.paint.Color;
  39 import javafx.scene.paint.LinearGradient;
  40 import javafx.scene.paint.Paint;
  41 import javafx.scene.paint.Stop;
  42 import javafx.scene.shape.Rectangle;
  43 import javafx.scene.shape.Shape;
  44 import javafx.util.Duration;
  45 
  46 import org.junit.Before;
  47 import org.junit.Test;
  48 
  49 public class StrokeTransitionTest {
  50 
  51     private static Duration DEFAULT_DURATION = Duration.millis(400);
  52     private static Interpolator DEFAULT_INTERPOLATOR = Interpolator.EASE_BOTH;
  53 
  54     private static float EPSILON = 1e-6f;
  55     private static Duration ONE_SEC = Duration.millis(1000);
  56     private static Duration TWO_SECS = Duration.millis(2000);
  57 
  58     private Shape shape;
  59 
  60     @Before
  61     public void setUp() {
  62         shape = new Rectangle();
  63     }
  64 
  65     private void assertColorEquals(Color expected, Paint actualPaint) {
  66         assertTrue(actualPaint instanceof Color);
  67         final Color actual = (Color)actualPaint;
  68         assertEquals(expected.getRed(), actual.getRed(), EPSILON);
  69         assertEquals(expected.getGreen(), actual.getGreen(), EPSILON);
  70         assertEquals(expected.getBlue(), actual.getBlue(), EPSILON);
  71         assertEquals(expected.getOpacity(), actual.getOpacity(), EPSILON);
  72     }
  73 
  74     @Test
  75     public void testDefaultValues() {
  76         // empty ctor
  77         StrokeTransition t0 = new StrokeTransition();
  78         assertEquals(DEFAULT_DURATION, t0.getDuration());
  79         assertEquals(DEFAULT_DURATION, t0.getCycleDuration());
  80         assertNull(t0.getFromValue());
  81         assertNull(t0.getToValue());
  82         assertNull(t0.getShape());
  83         assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
  84         assertNull(t0.getOnFinished());
  85 
  86         // duration only
  87         t0 = new StrokeTransition(ONE_SEC);
  88         assertEquals(ONE_SEC, t0.getDuration());
  89         assertNull(t0.getFromValue());
  90         assertNull(t0.getToValue());
  91         assertNull(t0.getShape());
  92         assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
  93         assertNull(t0.getOnFinished());
  94 
  95         // duration and shape
  96         t0 = new StrokeTransition(TWO_SECS, shape);
  97         assertEquals(TWO_SECS, t0.getDuration());
  98         assertNull(t0.getFromValue());
  99         assertNull(t0.getToValue());
 100         assertEquals(shape, t0.getShape());
 101         assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
 102         assertNull(t0.getOnFinished());
 103 
 104         // duration and values
 105         t0 = new StrokeTransition(TWO_SECS, null, Color.BLACK, Color.WHITE);
 106         assertEquals(TWO_SECS, t0.getDuration());
 107         assertColorEquals(Color.BLACK, t0.getFromValue());
 108         assertColorEquals(Color.WHITE, t0.getToValue());
 109         assertNull(t0.getShape());
 110         assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
 111         assertNull(t0.getOnFinished());
 112 
 113         // duration, shape, and values
 114         t0 = new StrokeTransition(TWO_SECS, shape, Color.BLACK, Color.WHITE);
 115         assertEquals(TWO_SECS, t0.getDuration());
 116         assertColorEquals(Color.BLACK, t0.getFromValue());
 117         assertColorEquals(Color.WHITE, t0.getToValue());
 118         assertEquals(shape, t0.getShape());
 119         assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
 120         assertNull(t0.getOnFinished());
 121     }
 122 
 123     @Test
 124     public void testInterpolate() {
 125         final Color fromValue = Color.color(0.2, 0.3, 0.7, 0.1);
 126         final Color toValue = Color.color(0.8, 0.4, 0.2, 0.9);
 127         final StrokeTransition t0 = new StrokeTransition(ONE_SEC, shape, fromValue, toValue);
 128 
 129         assertTrue(AnimationShim.impl_startable(t0,true));
 130         AnimationShim.impl_start(t0,true);
 131         TransitionShim.interpolate(t0,0.0);
 132         assertColorEquals(Color.color(0.2, 0.3, 0.7, 0.1), shape.getStroke());
 133         TransitionShim.interpolate(t0,0.4);
 134         assertColorEquals(Color.color(0.44, 0.34, 0.5, 0.42), shape.getStroke());
 135         TransitionShim.interpolate(t0,1.0);
 136         assertColorEquals(Color.color(0.8, 0.4, 0.2, 0.9), shape.getStroke());
 137         AnimationShim.impl_finished(t0);
 138     }
 139 
 140     @Test
 141     public void testRedValueCombinations() {
 142         final StrokeTransition t0 = new StrokeTransition(ONE_SEC, shape, null, Color.WHITE);
 143         final double originalRed = 0.6;
 144         final double fromRed = 0.4;
 145         final Color originalValue = Color.color(originalRed, 1.0, 1.0);
 146         final Color fromValue = Color.color(fromRed, 1.0, 1.0);
 147 
 148         // no from value set
 149         shape.setStroke(originalValue);
 150         t0.setFromValue(null);
 151         assertTrue(AnimationShim.impl_startable(t0,true));
 152         AnimationShim.impl_start(t0,true);
 153         TransitionShim.interpolate(t0,0.0);
 154         assertColorEquals(originalValue, shape.getStroke());
 155         AnimationShim.impl_finished(t0);
 156 
 157         // from-value set
 158         shape.setStroke(originalValue);
 159         t0.setFromValue(fromValue);
 160         assertTrue(AnimationShim.impl_startable(t0,true));
 161         AnimationShim.impl_start(t0,true);
 162         TransitionShim.interpolate(t0,0.0);
 163         assertColorEquals(fromValue, shape.getStroke());
 164         AnimationShim.impl_finished(t0);
 165     }
 166 
 167     @Test
 168     public void testGetTargetNode() {
 169         final Color fromValue = Color.color(0.0, 0.4, 0.8, 1.0);
 170         final Color toValue = Color.color(1.0, 0.8, 0.6, 0.4);
 171         final StrokeTransition ft = new StrokeTransition(ONE_SEC, shape, fromValue, toValue);
 172         ft.setInterpolator(Interpolator.LINEAR);
 173         final Shape shape2 = new Rectangle();
 174         final ParallelTransition pt = new ParallelTransition();
 175         pt.getChildren().add(ft);
 176         pt.setNode(shape2);
 177         shape.setStroke(Color.WHITE);
 178         shape2.setStroke(Color.WHITE);
 179 
 180         // node set, parent set
 181         assertTrue(AnimationShim.impl_startable(ft,true));
 182         AnimationShim.impl_start(ft,true);
 183         TransitionShim.interpolate(ft,0.5);
 184         assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.7), shape.getStroke());
 185         assertColorEquals(Color.WHITE, shape2.getStroke());
 186         AnimationShim.impl_finished(ft);
 187 
 188         // node null, parent set
 189         ft.setShape(null);
 190         assertTrue(AnimationShim.impl_startable(ft,true));
 191         AnimationShim.impl_start(ft,true);
 192         TransitionShim.interpolate(ft,0.4);
 193         assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.7), shape.getStroke());
 194         assertColorEquals(Color.color(0.4, 0.56, 0.72, 0.76), shape2.getStroke());
 195         AnimationShim.impl_finished(ft);
 196 
 197         // node null, parent not shape set
 198         pt.setNode(new Group());
 199         assertFalse(AnimationShim.impl_startable(ft,true));
 200 
 201         // node null, parent null
 202         pt.setNode(null);
 203         assertFalse(AnimationShim.impl_startable(ft,true));
 204     }
 205 
 206     @Test
 207     public void testCachedValues() {
 208         final Color fromValue = Color.color(0.0, 0.4, 0.8, 0.2);
 209         final Color toValue = Color.color(1.0, 0.8, 0.6, 0.4);
 210         final StrokeTransition ft = new StrokeTransition(ONE_SEC, shape, fromValue, toValue);
 211         ft.setInterpolator(Interpolator.LINEAR);
 212 
 213         // start
 214         assertTrue(AnimationShim.impl_startable(ft,true));
 215         AnimationShim.impl_start(ft,true);
 216         ft.setFromValue(Color.WHITE);
 217         TransitionShim.interpolate(ft,0.5);
 218         assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.3), shape.getStroke());
 219         AnimationShim.impl_finished(ft);
 220         ft.setFromValue(fromValue);
 221 
 222         // end
 223         assertTrue(AnimationShim.impl_startable(ft,true));
 224         AnimationShim.impl_start(ft,true);
 225         ft.setToValue(Color.BLACK);
 226         TransitionShim.interpolate(ft,0.2);
 227         assertColorEquals(Color.color(0.2, 0.48, 0.76, 0.24), shape.getStroke());
 228         AnimationShim.impl_finished(ft);
 229         ft.setToValue(toValue);
 230 
 231         // shape
 232         assertTrue(AnimationShim.impl_startable(ft,true));
 233         AnimationShim.impl_start(ft,true);
 234         ft.setShape(null);
 235         TransitionShim.interpolate(ft,0.7);
 236         assertColorEquals(Color.color(0.7, 0.68, 0.66, 0.34), shape.getStroke());
 237         AnimationShim.impl_finished(ft);
 238         ft.setShape(shape);
 239 
 240         // interpolator
 241         assertTrue(AnimationShim.impl_startable(ft,true));
 242         AnimationShim.impl_start(ft,true);
 243         ft.setInterpolator(null);
 244         TransitionShim.interpolate(ft,0.1);
 245         assertColorEquals(Color.color(0.1, 0.44, 0.78, 0.22), shape.getStroke());
 246         AnimationShim.impl_finished(ft);
 247         ft.setInterpolator(Interpolator.LINEAR);
 248     }
 249 
 250     @Test
 251     public void testStartable() {
 252         final StrokeTransition t0 = new StrokeTransition(Duration.ONE, shape, Color.WHITE, Color.BLACK);
 253         final Paint paint2 = new LinearGradient(0, 0, 1, 1, false, null,
 254                 new Stop[] { new Stop(0, Color.RED) });
 255         assertTrue(AnimationShim.impl_startable(t0,true));
 256 
 257         // duration is 0
 258         t0.setDuration(Duration.ZERO);
 259         assertFalse(AnimationShim.impl_startable(t0,true));
 260         t0.setDuration(Duration.ONE);
 261         assertTrue(AnimationShim.impl_startable(t0,true));
 262 
 263         // shape is null
 264         t0.setShape(null);
 265         assertFalse(AnimationShim.impl_startable(t0,true));
 266         t0.setShape(shape);
 267         assertTrue(AnimationShim.impl_startable(t0,true));
 268 
 269         // interpolator is null
 270         t0.setInterpolator(null);
 271         assertFalse(AnimationShim.impl_startable(t0,true));
 272         t0.setInterpolator(Interpolator.LINEAR);
 273         assertTrue(AnimationShim.impl_startable(t0,true));
 274 
 275         // fromValue
 276         t0.setFromValue(null);
 277         shape.setStroke(paint2);
 278         assertFalse(AnimationShim.impl_startable(t0,true));
 279         shape.setStroke(Color.BLACK);
 280         assertTrue(AnimationShim.impl_startable(t0,true));
 281         t0.setFromValue(Color.WHITE);
 282         shape.setStroke(paint2);
 283         assertTrue(AnimationShim.impl_startable(t0,true));
 284 
 285         // toValue
 286         t0.setToValue(null);
 287         assertFalse(AnimationShim.impl_startable(t0,true));
 288         t0.setToValue(Color.BLACK);
 289         assertTrue(AnimationShim.impl_startable(t0,true));
 290     }
 291 
 292     @Test
 293     public void testEvaluateStartValue() {
 294         final StrokeTransition t0 = new StrokeTransition(Duration.INDEFINITE, shape, null, Color.WHITE);
 295 
 296         // first run
 297         shape.setStroke(Color.GREY);
 298         assertTrue(AnimationShim.impl_startable(t0,true));
 299         AnimationShim.impl_start(t0,true);
 300         shape.setStroke(Color.TRANSPARENT);
 301         TransitionShim.interpolate(t0,0.0);
 302         assertColorEquals(Color.GREY, shape.getStroke());
 303         AnimationShim.impl_finished(t0);
 304 
 305         // second run
 306         shape.setStroke(Color.BLACK);
 307         assertTrue(AnimationShim.impl_startable(t0,true));
 308         AnimationShim.impl_start(t0,true);
 309         shape.setStroke(Color.WHITE);
 310         TransitionShim.interpolate(t0,0.0);
 311         assertColorEquals(Color.BLACK, shape.getStroke());
 312         AnimationShim.impl_finished(t0);
 313     }
 314 
 315 }