1 /* 2 * Copyright (c) 2011, 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.animation; 27 28 import javafx.animation.AnimationShim; 29 import javafx.animation.FillTransition; 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.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 FillTransitionTest { 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 FillTransition t0 = new FillTransition(); 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 FillTransition(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 FillTransition(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 FillTransition(TWO_SECS, 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 FillTransition(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 FillTransition t0 = new FillTransition(ONE_SEC, shape, fromValue, toValue); 128 129 assertTrue(AnimationShim.startable(t0,false)); 130 AnimationShim.doStart(t0,false); 131 TransitionShim.interpolate(t0,0.0); 132 assertColorEquals(Color.color(0.2, 0.3, 0.7, 0.1), shape.getFill()); 133 TransitionShim.interpolate(t0,0.4); 134 assertColorEquals(Color.color(0.44, 0.34, 0.5, 0.42), shape.getFill()); 135 TransitionShim.interpolate(t0,1.0); 136 assertColorEquals(Color.color(0.8, 0.4, 0.2, 0.9), shape.getFill()); 137 AnimationShim.finished(t0); 138 } 139 140 @Test 141 public void testValueCombinations() { 142 final FillTransition t0 = new FillTransition(ONE_SEC, shape, null, Color.WHITE); 143 final double original = 0.6; 144 final double from = 0.4; 145 final Color originalValue = Color.color(original, original, original); 146 final Color fromValue = Color.color(from, from, from); 147 148 // no from value set 149 shape.setFill(originalValue); 150 t0.setFromValue(null); 151 assertTrue(AnimationShim.startable(t0,false)); 152 AnimationShim.doStart(t0,false); 153 TransitionShim.interpolate(t0,0.0); 154 assertColorEquals(originalValue, shape.getFill()); 155 AnimationShim.finished(t0); 156 157 // from-value set 158 shape.setFill(originalValue); 159 t0.setFromValue(fromValue); 160 assertTrue(AnimationShim.startable(t0,true)); 161 AnimationShim.doStart(t0,true); 162 TransitionShim.interpolate(t0,0.0); 163 assertColorEquals(fromValue, shape.getFill()); 164 AnimationShim.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 FillTransition ft = new FillTransition(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.setFill(Color.WHITE); 178 shape2.setFill(Color.WHITE); 179 180 // node set, parent set 181 assertTrue(AnimationShim.startable(ft,false)); 182 AnimationShim.doStart(ft,false); 183 TransitionShim.interpolate(ft,0.5); 184 assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.7), shape.getFill()); 185 assertColorEquals(Color.WHITE, shape2.getFill()); 186 AnimationShim.finished(ft); 187 188 // node null, parent set 189 ft.setShape(null); 190 assertTrue(AnimationShim.startable(ft,true)); 191 AnimationShim.doStart(ft,true); 192 TransitionShim.interpolate(ft,0.4); 193 assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.7), shape.getFill()); 194 assertColorEquals(Color.color(0.4, 0.56, 0.72, 0.76), shape2.getFill()); 195 AnimationShim.finished(ft); 196 197 // node null, parent not shape set 198 pt.setNode(new Group()); 199 assertFalse(AnimationShim.startable(ft,true)); 200 201 // node null, parent null 202 pt.setNode(null); 203 assertFalse(AnimationShim.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 FillTransition ft = new FillTransition(ONE_SEC, shape, fromValue, toValue); 211 ft.setInterpolator(Interpolator.LINEAR); 212 213 // start 214 assertTrue(AnimationShim.startable(ft,false)); 215 AnimationShim.doStart(ft,false); 216 ft.setFromValue(Color.WHITE); 217 TransitionShim.interpolate(ft,0.5); 218 assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.3), shape.getFill()); 219 AnimationShim.finished(ft); 220 ft.setFromValue(fromValue); 221 222 // end 223 assertTrue(AnimationShim.startable(ft,true)); 224 AnimationShim.doStart(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.getFill()); 228 AnimationShim.finished(ft); 229 ft.setToValue(toValue); 230 231 // shape 232 assertTrue(AnimationShim.startable(ft,true)); 233 AnimationShim.doStart(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.getFill()); 237 AnimationShim.finished(ft); 238 ft.setShape(shape); 239 240 // interpolator 241 assertTrue(AnimationShim.startable(ft,true)); 242 AnimationShim.doStart(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.getFill()); 246 AnimationShim.finished(ft); 247 ft.setInterpolator(Interpolator.LINEAR); 248 } 249 250 @Test 251 public void testStartable_unsynchronized() { 252 final FillTransition t0 = new FillTransition(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.startable(t0,true)); 256 257 // duration is 0 258 t0.setDuration(Duration.ZERO); 259 assertFalse(AnimationShim.startable(t0,true)); 260 t0.setDuration(Duration.ONE); 261 assertTrue(AnimationShim.startable(t0,true)); 262 263 // interpolator is null 264 t0.setInterpolator(null); 265 assertFalse(AnimationShim.startable(t0,true)); 266 t0.setInterpolator(Interpolator.LINEAR); 267 assertTrue(AnimationShim.startable(t0,true)); 268 269 // shape is null 270 t0.setShape(null); 271 assertFalse(AnimationShim.startable(t0,false)); 272 assertFalse(AnimationShim.startable(t0,true)); 273 t0.setShape(shape); 274 assertTrue(AnimationShim.startable(t0,false)); 275 assertTrue(AnimationShim.startable(t0,true)); 276 277 // fromValue 278 t0.setFromValue(null); 279 shape.setFill(paint2); 280 assertFalse(AnimationShim.startable(t0,false)); 281 assertFalse(AnimationShim.startable(t0,true)); 282 shape.setFill(Color.BLACK); 283 assertTrue(AnimationShim.startable(t0,false)); 284 assertTrue(AnimationShim.startable(t0,true)); 285 t0.setFromValue(Color.WHITE); 286 shape.setFill(paint2); 287 assertTrue(AnimationShim.startable(t0,false)); 288 assertTrue(AnimationShim.startable(t0,true)); 289 290 // toValue 291 t0.setToValue(null); 292 assertFalse(AnimationShim.startable(t0,false)); 293 assertFalse(AnimationShim.startable(t0,true)); 294 t0.setToValue(Color.BLACK); 295 assertTrue(AnimationShim.startable(t0,false)); 296 assertTrue(AnimationShim.startable(t0,true)); 297 } 298 299 @Test 300 public void testStartable_synchronized() { 301 final FillTransition t0 = new FillTransition(Duration.ONE, shape, Color.WHITE, Color.BLACK); 302 final Paint paint2 = new LinearGradient(0, 0, 1, 1, false, null, 303 new Stop[] { new Stop(0, Color.RED) }); 304 assertTrue(AnimationShim.startable(t0,true)); 305 AnimationShim.doStart(t0,true); 306 AnimationShim.finished(t0); 307 308 // shape is null 309 t0.setShape(null); 310 assertTrue(AnimationShim.startable(t0,false)); 311 assertFalse(AnimationShim.startable(t0,true)); 312 t0.setShape(shape); 313 assertTrue(AnimationShim.startable(t0,false)); 314 assertTrue(AnimationShim.startable(t0,true)); 315 316 // fromValue 317 t0.setFromValue(null); 318 shape.setFill(paint2); 319 assertTrue(AnimationShim.startable(t0,false)); 320 assertFalse(AnimationShim.startable(t0,true)); 321 shape.setFill(Color.BLACK); 322 assertTrue(AnimationShim.startable(t0,false)); 323 assertTrue(AnimationShim.startable(t0,true)); 324 t0.setFromValue(Color.WHITE); 325 shape.setFill(paint2); 326 assertTrue(AnimationShim.startable(t0,false)); 327 assertTrue(AnimationShim.startable(t0,true)); 328 329 // toValue 330 t0.setToValue(null); 331 assertTrue(AnimationShim.startable(t0,false)); 332 assertFalse(AnimationShim.startable(t0,true)); 333 t0.setToValue(Color.BLACK); 334 assertTrue(AnimationShim.startable(t0,false)); 335 assertTrue(AnimationShim.startable(t0,true)); 336 } 337 338 @Test 339 public void testEvaluateStartValue() { 340 final FillTransition t0 = new FillTransition(Duration.INDEFINITE, shape, null, Color.WHITE); 341 342 // first run 343 shape.setFill(Color.GREY); 344 assertTrue(AnimationShim.startable(t0,false)); 345 AnimationShim.doStart(t0,false); 346 shape.setFill(Color.TRANSPARENT); 347 TransitionShim.interpolate(t0,0.0); 348 assertColorEquals(Color.GREY, shape.getFill()); 349 AnimationShim.finished(t0); 350 351 // second run 352 shape.setFill(Color.BLACK); 353 assertTrue(AnimationShim.startable(t0,true)); 354 AnimationShim.doStart(t0,true); 355 shape.setFill(Color.WHITE); 356 TransitionShim.interpolate(t0,0.0); 357 assertColorEquals(Color.BLACK, shape.getFill()); 358 AnimationShim.finished(t0); 359 } 360 361 }