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