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