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