1 /*
   2  * Copyright (c) 2011, 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 test.javafx.animation;
  27 
  28 import javafx.animation.AnimationShim;
  29 import javafx.animation.FadeTransition;
  30 import javafx.animation.Interpolator;
  31 import javafx.animation.ParallelTransition;
  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.Node;
  38 import javafx.scene.shape.Rectangle;
  39 import javafx.util.Duration;
  40 
  41 import org.junit.Before;
  42 import org.junit.Test;
  43 
  44 public class FadeTransitionTest {
  45         
  46     private static Duration DEFAULT_DURATION = Duration.millis(400);
  47     private static Interpolator DEFAULT_INTERPOLATOR = Interpolator.EASE_BOTH;
  48 
  49     private static double EPSILON = 1e-12;
  50     private static Duration ONE_SEC = Duration.millis(1000);
  51     private static Duration TWO_SECS = Duration.millis(2000);
  52 
  53     private Node node;
  54 
  55     @Before
  56     public void setUp() {
  57         node = new Rectangle();
  58     }
  59 
  60     @Test
  61     public void testDefaultValues() {
  62         // empty ctor
  63         final FadeTransition t0 = new FadeTransition();
  64         assertEquals(DEFAULT_DURATION, t0.getDuration());
  65         assertEquals(DEFAULT_DURATION, t0.getCycleDuration());
  66         assertTrue(Double.isNaN(t0.getFromValue()));
  67         assertTrue(Double.isNaN(t0.getToValue()));
  68         assertEquals(0.0, t0.getByValue(), EPSILON);
  69         assertNull(t0.getNode());
  70         assertEquals(DEFAULT_INTERPOLATOR, t0.getInterpolator());
  71         assertNull(t0.getOnFinished());
  72 
  73         // duration only
  74         final FadeTransition t1 = new FadeTransition(ONE_SEC);
  75         assertEquals(ONE_SEC, t1.getDuration());
  76         assertTrue(Double.isNaN(t1.getFromValue()));
  77         assertTrue(Double.isNaN(t1.getToValue()));
  78         assertEquals(0.0, t1.getByValue(), EPSILON);
  79         assertNull(t1.getNode());
  80         assertEquals(DEFAULT_INTERPOLATOR, t1.getInterpolator());
  81         assertNull(t1.getOnFinished());
  82 
  83         // duration and node
  84         final FadeTransition t2 = new FadeTransition(TWO_SECS, node);
  85         assertEquals(TWO_SECS, t2.getDuration());
  86         assertTrue(Double.isNaN(t2.getFromValue()));
  87         assertTrue(Double.isNaN(t2.getToValue()));
  88         assertEquals(0.0, t2.getByValue(), EPSILON);
  89         assertEquals(node, t2.getNode());
  90         assertEquals(DEFAULT_INTERPOLATOR, t2.getInterpolator());
  91         assertNull(t2.getOnFinished());
  92     }
  93 
  94     @Test
  95     public void testDefaultValuesFromProperties() {
  96         // empty ctor
  97         final FadeTransition t0 = new FadeTransition();
  98         assertEquals(DEFAULT_DURATION, t0.durationProperty().get());
  99         assertTrue(Double.isNaN(t0.fromValueProperty().get()));
 100         assertTrue(Double.isNaN(t0.toValueProperty().get()));
 101         assertEquals(0.0, t0.byValueProperty().get(), EPSILON);
 102         assertNull(t0.nodeProperty().get());
 103         assertEquals(DEFAULT_INTERPOLATOR, t0.interpolatorProperty().get());
 104         assertNull(t0.onFinishedProperty().get());
 105 
 106         // duration only
 107         final FadeTransition t1 = new FadeTransition(ONE_SEC);
 108         assertEquals(ONE_SEC, t1.durationProperty().get());
 109         assertTrue(Double.isNaN(t1.fromValueProperty().get()));
 110         assertTrue(Double.isNaN(t1.toValueProperty().get()));
 111         assertEquals(0.0, t1.byValueProperty().get(), EPSILON);
 112         assertNull(t1.nodeProperty().get());
 113         assertEquals(DEFAULT_INTERPOLATOR, t1.interpolatorProperty().get());
 114         assertNull(t1.onFinishedProperty().get());
 115 
 116         // duration and node
 117         final FadeTransition t2 = new FadeTransition(TWO_SECS, node);
 118         assertEquals(TWO_SECS, t2.durationProperty().get());
 119         assertTrue(Double.isNaN(t2.fromValueProperty().get()));
 120         assertTrue(Double.isNaN(t2.toValueProperty().get()));
 121         assertEquals(0.0, t2.byValueProperty().get(), EPSILON);
 122         assertEquals(node, t2.nodeProperty().get());
 123         assertEquals(DEFAULT_INTERPOLATOR, t2.interpolatorProperty().get());
 124         assertNull(t2.onFinishedProperty().get());
 125     }
 126 
 127         @Test
 128         public void testInterpolate() {
 129                 final FadeTransition t0 = new FadeTransition(ONE_SEC, node);
 130                 t0.setFromValue(0.5);
 131                 t0.setToValue(1.0);
 132                 
 133                 assertTrue(AnimationShim.impl_startable(t0,true));
 134                 AnimationShim.impl_start(t0,true);
 135                 TransitionShim.interpolate(t0,0.0);
 136                 assertEquals(0.5, node.getOpacity(), EPSILON);
 137                 TransitionShim.interpolate(t0,0.4);
 138                 assertEquals(0.7, node.getOpacity(), EPSILON);
 139                 TransitionShim.interpolate(t0,1.0);
 140                 assertEquals(1.0, node.getOpacity(), EPSILON);
 141                 AnimationShim.impl_finished(t0);
 142         }
 143         
 144         @Test
 145         public void testValueCombinations() {
 146                 final FadeTransition t0 = new FadeTransition(ONE_SEC, node);
 147                 final double originalValue = 0.6;
 148                 final double fromValue = 0.4;
 149                 final double toValue = 0.9;
 150                 final double byValue = -0.2;
 151                 
 152                 // no value set
 153                 node.setOpacity(originalValue);
 154                 t0.setFromValue(Double.NaN);
 155                 t0.setToValue(Double.NaN);
 156                 t0.setByValue(0.0);
 157                 assertTrue(AnimationShim.impl_startable(t0,true));
 158                 AnimationShim.impl_start(t0,true);
 159                 TransitionShim.interpolate(t0,0.0);
 160                 assertEquals(originalValue, node.getOpacity(), EPSILON);
 161                 TransitionShim.interpolate(t0,1.0);
 162                 assertEquals(originalValue, node.getOpacity(), EPSILON);
 163                 AnimationShim.impl_finished(t0);
 164                 
 165                 // only from-value set
 166                 node.setOpacity(originalValue);
 167                 t0.setFromValue(fromValue);
 168                 t0.setToValue(Double.NaN);
 169                 t0.setByValue(0.0);
 170                 assertTrue(AnimationShim.impl_startable(t0,true));
 171                 AnimationShim.impl_start(t0,true);
 172                 TransitionShim.interpolate(t0,0.0);
 173                 assertEquals(fromValue, node.getOpacity(), EPSILON);
 174                 TransitionShim.interpolate(t0,1.0);
 175                 assertEquals(fromValue, node.getOpacity(), EPSILON);
 176                 AnimationShim.impl_finished(t0);
 177                 
 178                 // only to-value set
 179                 node.setOpacity(originalValue);
 180                 t0.setFromValue(Double.NaN);
 181                 t0.setToValue(toValue);
 182                 t0.setByValue(0.0);
 183                 assertTrue(AnimationShim.impl_startable(t0,true));
 184                 AnimationShim.impl_start(t0,true);
 185                 TransitionShim.interpolate(t0,0.0);
 186                 assertEquals(originalValue, node.getOpacity(), EPSILON);
 187                 TransitionShim.interpolate(t0,1.0);
 188                 assertEquals(toValue, node.getOpacity(), EPSILON);
 189                 AnimationShim.impl_finished(t0);
 190                 
 191                 // only by-value set
 192                 node.setOpacity(originalValue);
 193                 t0.setFromValue(Double.NaN);
 194                 t0.setToValue(Double.NaN);
 195                 t0.setByValue(byValue);
 196                 assertTrue(AnimationShim.impl_startable(t0,true));
 197                 AnimationShim.impl_start(t0,true);
 198                 TransitionShim.interpolate(t0,0.0);
 199                 assertEquals(originalValue, node.getOpacity(), EPSILON);
 200                 TransitionShim.interpolate(t0,1.0);
 201                 assertEquals(originalValue + byValue, node.getOpacity(), EPSILON);
 202                 AnimationShim.impl_finished(t0);
 203                 
 204                 // from- and to-values set
 205                 node.setOpacity(originalValue);
 206                 t0.setFromValue(fromValue);
 207                 t0.setToValue(toValue);
 208                 t0.setByValue(0.0);
 209                 assertTrue(AnimationShim.impl_startable(t0,true));
 210                 AnimationShim.impl_start(t0,true);
 211                 TransitionShim.interpolate(t0,0.0);
 212                 assertEquals(fromValue, node.getOpacity(), EPSILON);
 213                 TransitionShim.interpolate(t0,1.0);
 214                 assertEquals(toValue, node.getOpacity(), EPSILON);
 215                 AnimationShim.impl_finished(t0);
 216                 
 217                 // from- and by-values set
 218                 node.setOpacity(originalValue);
 219                 t0.setFromValue(fromValue);
 220                 t0.setToValue(Double.NaN);
 221                 t0.setByValue(byValue);
 222                 assertTrue(AnimationShim.impl_startable(t0,true));
 223                 AnimationShim.impl_start(t0,true);
 224                 TransitionShim.interpolate(t0,0.0);
 225                 assertEquals(fromValue, node.getOpacity(), EPSILON);
 226                 TransitionShim.interpolate(t0,1.0);
 227                 assertEquals(fromValue + byValue, node.getOpacity(), EPSILON);
 228                 AnimationShim.impl_finished(t0);
 229                 
 230                 // to- and by-values set
 231                 node.setOpacity(originalValue);
 232                 t0.setFromValue(Double.NaN);
 233                 t0.setToValue(toValue);
 234                 t0.setByValue(byValue);
 235                 assertTrue(AnimationShim.impl_startable(t0,true));
 236                 AnimationShim.impl_start(t0,true);
 237                 TransitionShim.interpolate(t0,0.0);
 238                 assertEquals(originalValue, node.getOpacity(), EPSILON);
 239                 TransitionShim.interpolate(t0,1.0);
 240                 assertEquals(toValue, node.getOpacity(), EPSILON);
 241                 AnimationShim.impl_finished(t0);
 242                 
 243                 // all values set
 244                 node.setOpacity(originalValue);
 245                 t0.setFromValue(fromValue);
 246                 t0.setToValue(toValue);
 247                 t0.setByValue(byValue);
 248                 assertTrue(AnimationShim.impl_startable(t0,true));
 249                 AnimationShim.impl_start(t0,true);
 250                 TransitionShim.interpolate(t0,0.0);
 251                 assertEquals(fromValue, node.getOpacity(), EPSILON);
 252                 TransitionShim.interpolate(t0,1.0);
 253                 assertEquals(toValue, node.getOpacity(), EPSILON);
 254                 AnimationShim.impl_finished(t0);
 255         }
 256         
 257         @Test
 258         public void testOutOfBoundValues() {
 259                 final FadeTransition t0 = new FadeTransition(ONE_SEC, node);
 260                 t0.setInterpolator(Interpolator.LINEAR);
 261                 
 262                 // start < 0.0
 263                 t0.setFromValue(-0.4);
 264                 t0.setToValue(0.6);
 265                 assertTrue(AnimationShim.impl_startable(t0,true));
 266                 AnimationShim.impl_start(t0,true);
 267                 TransitionShim.interpolate(t0,0.0);
 268                 assertEquals(0.0, node.getOpacity(), EPSILON);
 269                 TransitionShim.interpolate(t0,0.5);
 270                 assertEquals(0.3, node.getOpacity(), EPSILON);
 271                 TransitionShim.interpolate(t0,1.0);
 272                 assertEquals(0.6, node.getOpacity(), EPSILON);
 273                 AnimationShim.impl_finished(t0);
 274                 
 275                 // start > 1.0
 276                 t0.setFromValue(1.3);
 277                 t0.setToValue(0.3);
 278                 assertTrue(AnimationShim.impl_startable(t0,true));
 279                 AnimationShim.impl_start(t0,true);
 280                 TransitionShim.interpolate(t0,0.0);
 281                 assertEquals(1.0, node.getOpacity(), EPSILON);
 282                 TransitionShim.interpolate(t0,0.5);
 283                 assertEquals(0.65, node.getOpacity(), EPSILON);
 284                 TransitionShim.interpolate(t0,1.0);
 285                 assertEquals(0.3, node.getOpacity(), EPSILON);
 286                 AnimationShim.impl_finished(t0);
 287                 
 288                 // end < 0.0
 289                 t0.setFromValue(0.2);
 290                 t0.setToValue(-1.2);
 291                 assertTrue(AnimationShim.impl_startable(t0,true));
 292                 AnimationShim.impl_start(t0,true);
 293                 TransitionShim.interpolate(t0,0.0);
 294                 assertEquals(0.2, node.getOpacity(), EPSILON);
 295                 TransitionShim.interpolate(t0,0.5);
 296                 assertEquals(0.1, node.getOpacity(), EPSILON);
 297                 TransitionShim.interpolate(t0,1.0);
 298                 assertEquals(0.0, node.getOpacity(), EPSILON);
 299                 AnimationShim.impl_finished(t0);
 300                 
 301                 // end > 1.0
 302                 t0.setFromValue(0.9);
 303                 t0.setToValue(1.9);
 304                 assertTrue(AnimationShim.impl_startable(t0,true));
 305                 AnimationShim.impl_start(t0,true);
 306                 TransitionShim.interpolate(t0,0.0);
 307                 assertEquals(0.9, node.getOpacity(), EPSILON);
 308                 TransitionShim.interpolate(t0,0.5);
 309                 assertEquals(0.95, node.getOpacity(), EPSILON);
 310                 TransitionShim.interpolate(t0,1.0);
 311                 assertEquals(1.0, node.getOpacity(), EPSILON);
 312                 AnimationShim.impl_finished(t0);
 313         }
 314 
 315     @Test
 316     public void testGetTargetNode() {
 317         final FadeTransition ft = new FadeTransition(ONE_SEC, node);
 318         ft.setInterpolator(Interpolator.LINEAR);
 319                 ft.setFromValue(0.5);
 320                 ft.setToValue(1.0);
 321         final Rectangle node2 = new Rectangle();
 322         final ParallelTransition pt = new ParallelTransition();
 323         pt.getChildren().add(ft);
 324         pt.setNode(node2);
 325 
 326         // node set, parent set
 327         assertTrue(AnimationShim.impl_startable(ft,true));
 328         AnimationShim.impl_start(ft,true);
 329         TransitionShim.interpolate(ft,0.5);
 330         assertEquals(0.75, node.getOpacity(), EPSILON);
 331         assertEquals(1.0, node2.getOpacity(), EPSILON);
 332         AnimationShim.impl_finished(ft);
 333 
 334         // node null, parent set
 335         ft.setNode(null);
 336         assertTrue(AnimationShim.impl_startable(ft,true));
 337         AnimationShim.impl_start(ft,true);
 338         TransitionShim.interpolate(ft,0.4);
 339         assertEquals(0.75, node.getOpacity(), EPSILON);
 340         assertEquals(0.7, node2.getOpacity(), EPSILON);
 341         AnimationShim.impl_finished(ft);
 342 
 343         // node null, parent null
 344         pt.setNode(null);
 345         assertFalse(AnimationShim.impl_startable(ft,true));
 346     }
 347 
 348     @Test
 349     public void testCachedValues() {
 350         final FadeTransition ft = new FadeTransition(ONE_SEC, node);
 351         ft.setInterpolator(Interpolator.LINEAR);
 352                 ft.setFromValue(0.5);
 353                 ft.setToValue(1.0);
 354 
 355         // start
 356         assertTrue(AnimationShim.impl_startable(ft,true));
 357         AnimationShim.impl_start(ft,true);
 358         ft.setFromValue(0.0);
 359         TransitionShim.interpolate(ft,0.5);
 360         assertEquals(0.75, node.getOpacity(), EPSILON);
 361         AnimationShim.impl_finished(ft);
 362         ft.setFromValue(0.5);
 363 
 364         // end
 365         assertTrue(AnimationShim.impl_startable(ft,true));
 366         AnimationShim.impl_start(ft,true);
 367         ft.setToValue(0.0);
 368         TransitionShim.interpolate(ft,0.2);
 369         assertEquals(0.6, node.getOpacity(), EPSILON);
 370         AnimationShim.impl_finished(ft);
 371         ft.setToValue(1.0);
 372 
 373         // node
 374         assertTrue(AnimationShim.impl_startable(ft,true));
 375         AnimationShim.impl_start(ft,true);
 376         ft.setNode(null);
 377         TransitionShim.interpolate(ft,0.7);
 378         assertEquals(0.85, node.getOpacity(), EPSILON);
 379         AnimationShim.impl_finished(ft);
 380         ft.setNode(node);
 381 
 382         // interpolator
 383         assertTrue(AnimationShim.impl_startable(ft,true));
 384         AnimationShim.impl_start(ft,true);
 385         ft.setInterpolator(null);
 386         TransitionShim.interpolate(ft,0.1);
 387         assertEquals(0.55, node.getOpacity(), EPSILON);
 388         AnimationShim.impl_finished(ft);
 389         ft.setInterpolator(Interpolator.LINEAR);
 390     }
 391 
 392         @Test
 393         public void testStartable() {
 394                 final FadeTransition t0 = new FadeTransition(Duration.ONE, node);
 395                 assertTrue(AnimationShim.impl_startable(t0,true));
 396                 
 397                 // duration is 0
 398                 t0.setDuration(Duration.ZERO);
 399                 assertFalse(AnimationShim.impl_startable(t0,true));
 400                 t0.setDuration(Duration.ONE);
 401                 assertTrue(AnimationShim.impl_startable(t0,true));
 402                 
 403                 // node is null
 404                 t0.setNode(null);
 405                 assertFalse(AnimationShim.impl_startable(t0,true));
 406                 t0.setNode(node);
 407                 assertTrue(AnimationShim.impl_startable(t0,true));
 408                 
 409                 // interpolator is null
 410                 t0.setInterpolator(null);
 411                 assertFalse(AnimationShim.impl_startable(t0,true));
 412                 t0.setInterpolator(Interpolator.LINEAR);
 413                 assertTrue(AnimationShim.impl_startable(t0,true));
 414         }
 415 
 416         @Test
 417         public void testEvaluateStartValue() {
 418                 final FadeTransition t0 = new FadeTransition(Duration.INDEFINITE, node);
 419                 
 420                 // first run
 421                 node.setOpacity(0.6);
 422                 assertTrue(AnimationShim.impl_startable(t0,true));
 423                 AnimationShim.impl_start(t0,true);
 424                 node.setOpacity(0.8);
 425                 TransitionShim.interpolate(t0,0.0);
 426                 assertEquals(0.6, node.getOpacity(), EPSILON);
 427                 AnimationShim.impl_finished(t0);
 428                 
 429                 // second run
 430                 node.setOpacity(0.2);
 431                 assertTrue(AnimationShim.impl_startable(t0,true));
 432                 AnimationShim.impl_start(t0,true);
 433                 node.setOpacity(0.8);
 434                 TransitionShim.interpolate(t0,0.0);
 435                 assertEquals(0.2, node.getOpacity(), EPSILON);
 436                 AnimationShim.impl_finished(t0);
 437         }
 438 
 439 }