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 29 import javafx.animation.Animation.Status; 30 import javafx.util.Duration; 31 import com.sun.scenario.animation.AbstractMasterTimerMock; 32 import com.sun.scenario.animation.shared.ClipEnvelopeMock; 33 import org.junit.Before; 34 import org.junit.Test; 35 36 import static org.junit.Assert.*; 37 38 public class AnimationSetRateTest { 39 40 private static final double EPSILON = 1e-12; 41 42 private AbstractMasterTimerMock timer; 43 private Animation animation; 44 private ClipEnvelopeMock clipEnvelope; 45 46 @Before 47 public void setUp() throws Exception { 48 timer = new AbstractMasterTimerMock(); 49 clipEnvelope = new ClipEnvelopeMock(); 50 animation = new AnimationImpl(timer, clipEnvelope, 1); 51 animation.setCycleDuration(Duration.millis(1000)); 52 clipEnvelope.setAnimation(animation); 53 } 54 55 private void assertAnimation(double rate, double currentRate, Status status, boolean addedToMasterTimer) { 56 assertEquals(rate, animation.getRate(), EPSILON); 57 assertEquals(currentRate, animation.getCurrentRate(), EPSILON); 58 assertEquals(status, animation.getStatus()); 59 assertEquals(addedToMasterTimer, timer.containsPulseReceiver(animation.pulseReceiver)); 60 } 61 62 @Test 63 public void testSetRate() { 64 // changing the rate of a playing animation 65 animation.play(); 66 animation.setRate(3.0); 67 assertAnimation(3.0, 3.0, Status.RUNNING, true); 68 69 // toggling a playing animation 70 animation.setRate(-2.0); 71 assertAnimation(-2.0, -2.0, Status.RUNNING, true); 72 73 // changing the rate 74 animation.setRate(-2.5); 75 assertAnimation(-2.5, -2.5, Status.RUNNING, true); 76 77 // toggling back 78 animation.setRate(1.5); 79 assertAnimation(1.5, 1.5, Status.RUNNING, true); 80 81 // changing the rate of a animation playing in reverse 82 animation.impl_setCurrentRate(-1.5); 83 animation.setRate(2.2); 84 assertAnimation(2.2, -2.2, Status.RUNNING, true); 85 86 // toggling a animation playing in reverse 87 animation.setRate(-1.8); 88 assertAnimation(-1.8, 1.8, Status.RUNNING, true); 89 90 // changing the rate 91 animation.setRate(-1.3); 92 assertAnimation(-1.3, 1.3, Status.RUNNING, true); 93 94 // toggling back 95 animation.setRate(0.5); 96 assertAnimation(0.5, -0.5, Status.RUNNING, true); 97 } 98 99 @Test 100 public void testSetRateOfStoppedAnimation() { 101 // changing the rate 102 animation.setRate(2.0); 103 assertAnimation(2.0, 0.0, Status.STOPPED, false); 104 animation.play(); 105 assertAnimation(2.0, 2.0, Status.RUNNING, true); 106 107 // toggling the rate of a stopped animation 108 animation.stop(); 109 animation.setRate(-1.0); 110 assertAnimation(-1.0, 0.0, Status.STOPPED, false); 111 animation.play(); 112 assertAnimation(-1.0, -1.0, Status.RUNNING, true); 113 114 // toggling back 115 animation.stop(); 116 animation.setRate(3.0); 117 assertAnimation(3.0, 0.0, Status.STOPPED, false); 118 animation.play(); 119 assertAnimation(3.0, 3.0, Status.RUNNING, true); 120 121 // setting rate of stopped animation to zero 122 animation.stop(); 123 animation.setRate(0); 124 assertAnimation(0.0, 0.0, Status.STOPPED, false); 125 animation.play(); 126 assertAnimation(0.0, 0.0, Status.RUNNING, false); 127 128 // setting rate of stopped animation to non-zero 129 animation.stop(); 130 animation.setRate(1.5); 131 assertAnimation(1.5, 0.0, Status.STOPPED, false); 132 animation.play(); 133 assertAnimation(1.5, 1.5, Status.RUNNING, true); 134 135 // setting rate of stopped animation to zero 136 animation.stop(); 137 animation.setRate(0); 138 assertAnimation(0.0, 0.0, Status.STOPPED, false); 139 animation.play(); 140 assertAnimation(0.0, 0.0, Status.RUNNING, false); 141 142 // toggling rate of stopped animation to non-zero 143 animation.stop(); 144 animation.setRate(-0.5); 145 assertAnimation(-0.5, 0.0, Status.STOPPED, false); 146 animation.play(); 147 assertAnimation(-0.5, -0.5, Status.RUNNING, true); 148 149 // setting rate of stopped animation to zero 150 animation.stop(); 151 animation.setRate(0); 152 assertAnimation(0.0, 0.0, Status.STOPPED, false); 153 animation.play(); 154 assertAnimation(0.0, 0.0, Status.RUNNING, false); 155 156 // setting rate of stopped animation to non-zero 157 animation.stop(); 158 animation.setRate(-2.3); 159 assertAnimation(-2.3, 0.0, Status.STOPPED, false); 160 animation.play(); 161 assertAnimation(-2.3, -2.3, Status.RUNNING, true); 162 163 // setting rate of stopped animation to zero 164 animation.stop(); 165 animation.setRate(0); 166 assertAnimation(0.0, 0.0, Status.STOPPED, false); 167 animation.play(); 168 assertAnimation(0.0, 0.0, Status.RUNNING, false); 169 170 // toggling rate of stopped animation to non-zero 171 animation.stop(); 172 animation.setRate(1.7); 173 assertAnimation(1.7, 0.0, Status.STOPPED, false); 174 animation.play(); 175 assertAnimation(1.7, 1.7, Status.RUNNING, true); 176 } 177 178 @Test 179 public void testSetRateToZeroForRunningAnimation() { 180 // changing the rate of a playing animation 181 animation.play(); 182 animation.setRate(0.0); 183 assertAnimation(0.0, 0.0, Status.RUNNING, false); 184 animation.setRate(3.0); 185 assertAnimation(3.0, 3.0, Status.RUNNING, true); 186 187 // toggling a playing animation 188 animation.setRate(0.0); 189 assertAnimation(0.0, 0.0, Status.RUNNING, false); 190 animation.setRate(-2.0); 191 assertAnimation(-2.0, -2.0, Status.RUNNING, true); 192 193 // changing the rate 194 animation.setRate(0.0); 195 assertAnimation(0.0, 0.0, Status.RUNNING, false); 196 animation.setRate(-2.5); 197 assertAnimation(-2.5, -2.5, Status.RUNNING, true); 198 199 // toggling back 200 animation.setRate(0.0); 201 assertAnimation(0.0, 0.0, Status.RUNNING, false); 202 animation.setRate(1.5); 203 assertAnimation(1.5, 1.5, Status.RUNNING, true); 204 205 // changing the rate of a animation playing in reverse 206 animation.impl_setCurrentRate(-1.5); 207 animation.setRate(0.0); 208 assertAnimation(0.0, 0.0, Status.RUNNING, false); 209 animation.setRate(2.2); 210 assertAnimation(2.2, -2.2, Status.RUNNING, true); 211 212 // toggling a animation playing in reverse 213 animation.setRate(0.0); 214 assertAnimation(0.0, 0.0, Status.RUNNING, false); 215 animation.setRate(-1.8); 216 assertAnimation(-1.8, 1.8, Status.RUNNING, true); 217 218 // changing the rate 219 animation.setRate(0.0); 220 assertAnimation(0.0, 0.0, Status.RUNNING, false); 221 animation.setRate(-1.3); 222 assertAnimation(-1.3, 1.3, Status.RUNNING, true); 223 224 // toggling back 225 animation.setRate(0.0); 226 assertAnimation(0.0, 0.0, Status.RUNNING, false); 227 animation.setRate(0.5); 228 assertAnimation(0.5, -0.5, Status.RUNNING, true); 229 } 230 231 @Test 232 public void testSetRateOfPausedAnimation() { 233 // changing the rate of a paused animation 234 animation.play(); 235 animation.pause(); 236 animation.setRate(3.0); 237 assertAnimation(3.0, 0.0, Status.PAUSED, false); 238 animation.play(); 239 assertAnimation(3.0, 3.0, Status.RUNNING, true); 240 241 // toggling a pausing animation 242 animation.pause(); 243 animation.setRate(-2.0); 244 assertAnimation(-2.0, 0.0, Status.PAUSED, false); 245 animation.play(); 246 assertAnimation(-2.0, -2.0, Status.RUNNING, true); 247 248 // changing the rate 249 animation.pause(); 250 animation.setRate(-2.5); 251 assertAnimation(-2.5, 0.0, Status.PAUSED, false); 252 animation.play(); 253 assertAnimation(-2.5, -2.5, Status.RUNNING, true); 254 255 // toggling back 256 animation.pause(); 257 animation.setRate(1.5); 258 assertAnimation(1.5, 0.0, Status.PAUSED, false); 259 animation.play(); 260 assertAnimation(1.5, 1.5, Status.RUNNING, true); 261 262 // changing the rate of a paused animation pointing in reverse 263 animation.impl_setCurrentRate(-1.5); 264 animation.pause(); 265 animation.setRate(2.2); 266 assertAnimation(2.2, 0.0, Status.PAUSED, false); 267 animation.play(); 268 assertAnimation(2.2, -2.2, Status.RUNNING, true); 269 270 // toggling a paused playing pointing in reverse 271 animation.pause(); 272 animation.setRate(-1.8); 273 assertAnimation(-1.8, 0.0, Status.PAUSED, false); 274 animation.play(); 275 assertAnimation(-1.8, 1.8, Status.RUNNING, true); 276 277 // changing the rate 278 animation.pause(); 279 animation.setRate(-1.3); 280 assertAnimation(-1.3, 0.0, Status.PAUSED, false); 281 animation.play(); 282 assertAnimation(-1.3, 1.3, Status.RUNNING, true); 283 284 // toggling back 285 animation.pause(); 286 animation.setRate(0.5); 287 assertAnimation(0.5, 0.0, Status.PAUSED, false); 288 animation.play(); 289 assertAnimation(0.5, -0.5, Status.RUNNING, true); 290 } 291 292 @Test 293 public void testSetRateToZeroForPausedAnimation() { 294 // starting a paused animation with rate 0 295 animation.play(); 296 animation.pause(); 297 animation.setRate(0.0); 298 assertAnimation(0.0, 0.0, Status.PAUSED, false); 299 animation.play(); 300 assertAnimation(0.0, 0.0, Status.RUNNING, false); 301 302 // changing the rate of a paused animation 303 animation.pause(); 304 animation.setRate(0.0); 305 assertAnimation(0.0, 0.0, Status.PAUSED, false); 306 animation.setRate(3.0); 307 assertAnimation(3.0, 0.0, Status.PAUSED, false); 308 animation.play(); 309 assertAnimation(3.0, 3.0, Status.RUNNING, true); 310 311 // toggling a paused animation 312 animation.pause(); 313 animation.setRate(0.0); 314 assertAnimation(0.0, 0.0, Status.PAUSED, false); 315 animation.setRate(-2.0); 316 assertAnimation(-2.0, 0.0, Status.PAUSED, false); 317 animation.play(); 318 assertAnimation(-2.0, -2.0, Status.RUNNING, true); 319 320 // changing the rate 321 animation.pause(); 322 animation.setRate(0.0); 323 assertAnimation(0.0, 0.0, Status.PAUSED, false); 324 animation.setRate(-2.5); 325 assertAnimation(-2.5, 0.0, Status.PAUSED, false); 326 animation.play(); 327 assertAnimation(-2.5, -2.5, Status.RUNNING, true); 328 329 // toggling back 330 animation.pause(); 331 animation.setRate(0.0); 332 assertAnimation(0.0, 0.0, Status.PAUSED, false); 333 animation.setRate(1.5); 334 assertAnimation(1.5, 0.0, Status.PAUSED, false); 335 animation.play(); 336 assertAnimation(1.5, 1.5, Status.RUNNING, true); 337 338 // changing the rate of a paused animation pointing in reverse 339 animation.impl_setCurrentRate(-1.5); 340 animation.pause(); 341 animation.setRate(0.0); 342 assertAnimation(0.0, 0.0, Status.PAUSED, false); 343 animation.setRate(2.2); 344 assertAnimation(2.2, 0.0, Status.PAUSED, false); 345 animation.play(); 346 assertAnimation(2.2, -2.2, Status.RUNNING, true); 347 348 // toggling a paused animation pointing in reverse 349 animation.pause(); 350 animation.setRate(0.0); 351 assertAnimation(0.0, 0.0, Status.PAUSED, false); 352 animation.setRate(-1.8); 353 assertAnimation(-1.8, 0.0, Status.PAUSED, false); 354 animation.play(); 355 assertAnimation(-1.8, 1.8, Status.RUNNING, true); 356 357 // changing the rate 358 animation.pause(); 359 animation.setRate(0.0); 360 assertAnimation(0.0, 0.0, Status.PAUSED, false); 361 animation.setRate(-1.3); 362 assertAnimation(-1.3, 0.0, Status.PAUSED, false); 363 animation.play(); 364 assertAnimation(-1.3, 1.3, Status.RUNNING, true); 365 366 // toggling back 367 animation.pause(); 368 animation.setRate(0.0); 369 assertAnimation(0.0, 0.0, Status.PAUSED, false); 370 animation.setRate(0.5); 371 assertAnimation(0.5, 0.0, Status.PAUSED, false); 372 animation.play(); 373 assertAnimation(0.5, -0.5, Status.RUNNING, true); 374 } 375 }