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 StrokeTransitionTest { 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 StrokeTransition t0 = new StrokeTransition(); 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 StrokeTransition(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 StrokeTransition(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 StrokeTransition(TWO_SECS, null, 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 StrokeTransition(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 StrokeTransition t0 = new StrokeTransition(ONE_SEC, shape, fromValue, toValue); 123 124 assertTrue(t0.impl_startable(true)); 125 t0.impl_start(true); 126 t0.interpolate(0.0); 127 assertColorEquals(Color.color(0.2, 0.3, 0.7, 0.1), shape.getStroke()); 128 t0.interpolate(0.4); 129 assertColorEquals(Color.color(0.44, 0.34, 0.5, 0.42), shape.getStroke()); 130 t0.interpolate(1.0); 131 assertColorEquals(Color.color(0.8, 0.4, 0.2, 0.9), shape.getStroke()); 132 t0.impl_finished(); 133 } 134 135 @Test 136 public void testRedValueCombinations() { 137 final StrokeTransition t0 = new StrokeTransition(ONE_SEC, shape, null, Color.WHITE); 138 final double originalRed = 0.6; 139 final double fromRed = 0.4; 140 final Color originalValue = Color.color(originalRed, 1.0, 1.0); 141 final Color fromValue = Color.color(fromRed, 1.0, 1.0); 142 143 // no from value set 144 shape.setStroke(originalValue); 145 t0.setFromValue(null); 146 assertTrue(t0.impl_startable(true)); 147 t0.impl_start(true); 148 t0.interpolate(0.0); 149 assertColorEquals(originalValue, shape.getStroke()); 150 t0.impl_finished(); 151 152 // from-value set 153 shape.setStroke(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.getStroke()); 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 StrokeTransition ft = new StrokeTransition(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.setStroke(Color.WHITE); 173 shape2.setStroke(Color.WHITE); 174 175 // node set, parent set 176 assertTrue(ft.impl_startable(true)); 177 ft.impl_start(true); 178 ft.interpolate(0.5); 179 assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.7), shape.getStroke()); 180 assertColorEquals(Color.WHITE, shape2.getStroke()); 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.getStroke()); 189 assertColorEquals(Color.color(0.4, 0.56, 0.72, 0.76), shape2.getStroke()); 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 StrokeTransition ft = new StrokeTransition(ONE_SEC, shape, fromValue, toValue); 206 ft.setInterpolator(Interpolator.LINEAR); 207 208 // start 209 assertTrue(ft.impl_startable(true)); 210 ft.impl_start(true); 211 ft.setFromValue(Color.WHITE); 212 ft.interpolate(0.5); 213 assertColorEquals(Color.color(0.5, 0.6, 0.7, 0.3), shape.getStroke()); 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.getStroke()); 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.getStroke()); 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.getStroke()); 241 ft.impl_finished(); 242 ft.setInterpolator(Interpolator.LINEAR); 243 } 244 245 @Test 246 public void testStartable() { 247 final StrokeTransition t0 = new StrokeTransition(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 // shape is null 259 t0.setShape(null); 260 assertFalse(t0.impl_startable(true)); 261 t0.setShape(shape); 262 assertTrue(t0.impl_startable(true)); 263 264 // interpolator is null 265 t0.setInterpolator(null); 266 assertFalse(t0.impl_startable(true)); 267 t0.setInterpolator(Interpolator.LINEAR); 268 assertTrue(t0.impl_startable(true)); 269 270 // fromValue 271 t0.setFromValue(null); 272 shape.setStroke(paint2); 273 assertFalse(t0.impl_startable(true)); 274 shape.setStroke(Color.BLACK); 275 assertTrue(t0.impl_startable(true)); 276 t0.setFromValue(Color.WHITE); 277 shape.setStroke(paint2); 278 assertTrue(t0.impl_startable(true)); 279 280 // toValue 281 t0.setToValue(null); 282 assertFalse(t0.impl_startable(true)); 283 t0.setToValue(Color.BLACK); 284 assertTrue(t0.impl_startable(true)); 285 } 286 287 @Test 288 public void testEvaluateStartValue() { 289 final StrokeTransition t0 = new StrokeTransition(Duration.INDEFINITE, shape, null, Color.WHITE); 290 291 // first run 292 shape.setStroke(Color.GREY); 293 assertTrue(t0.impl_startable(true)); 294 t0.impl_start(true); 295 shape.setStroke(Color.TRANSPARENT); 296 t0.interpolate(0.0); 297 assertColorEquals(Color.GREY, shape.getStroke()); 298 t0.impl_finished(); 299 300 // second run 301 shape.setStroke(Color.BLACK); 302 assertTrue(t0.impl_startable(true)); 303 t0.impl_start(true); 304 shape.setStroke(Color.WHITE); 305 t0.interpolate(0.0); 306 assertColorEquals(Color.BLACK, shape.getStroke()); 307 t0.impl_finished(); 308 } 309 310 }