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