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 test.com.sun.scenario.animation.shared;
  27 
  28 
  29 import com.sun.javafx.tk.Toolkit;
  30 import com.sun.scenario.animation.shared.ClipEnvelope;
  31 import com.sun.scenario.animation.shared.FiniteClipEnvelope;
  32 import com.sun.scenario.animation.shared.InfiniteClipEnvelopeShim;
  33 import com.sun.scenario.animation.shared.SingleLoopClipEnvelope;
  34 import javafx.animation.Animation;
  35 import javafx.animation.Animation.Status;
  36 import test.javafx.animation.AnimationMock;
  37 import test.javafx.animation.AnimationMock.Command;
  38 import javafx.util.Duration;
  39 import org.junit.Before;
  40 import org.junit.Test;
  41 
  42 import static org.junit.Assert.*;
  43 
  44 public class InfiniteClipEnvelopeTest {
  45 
  46     private final long CYCLE_TICKS = Math.round(6.0 * AnimationMock.DEFAULT_DURATION.toMillis());
  47 
  48     private ClipEnvelope clip;
  49     private AnimationMock animation;
  50     
  51     @Before
  52     public void setUp() {
  53         animation = new AnimationMock(Toolkit.getToolkit().getMasterTimer(), AnimationMock.DEFAULT_DURATION, AnimationMock.DEFAULT_RATE, Animation.INDEFINITE, AnimationMock.DEFAULT_AUTOREVERSE);
  54         clip = new InfiniteClipEnvelopeShim(animation);
  55     }
  56     
  57     @Test
  58     public void testSetValues() {
  59         ClipEnvelope c;
  60         
  61         // Setting cycleCount to 2
  62         animation.setCycleCount(2);
  63         animation.mockCycleDuration(AnimationMock.DEFAULT_DURATION);
  64         c = clip.setCycleCount(2);
  65         assertNotSame(clip, c);
  66         assertTrue(c instanceof FiniteClipEnvelope);
  67         
  68         // Setting cycleDuration to INDEFINITE
  69         animation.setCycleCount(Animation.INDEFINITE);
  70         animation.mockCycleDuration(Duration.INDEFINITE);
  71         c = clip.setCycleDuration(Duration.INDEFINITE);
  72         assertNotSame(clip, c);
  73         assertTrue(c instanceof SingleLoopClipEnvelope);
  74 
  75     }
  76 
  77     @Test
  78     public void testJump() {
  79         clip.jumpTo(6 * 300);
  80         animation.check(Command.JUMP, 6 * 300, CYCLE_TICKS);
  81         
  82         clip.jumpTo(0);
  83         animation.check(Command.JUMP, 0, CYCLE_TICKS);
  84         
  85         clip.jumpTo(6 * 1000);
  86         animation.check(Command.JUMP, 6 * 1000, CYCLE_TICKS);
  87         
  88         clip.jumpTo(-1);
  89         animation.check(Command.JUMP, 0, CYCLE_TICKS);
  90         
  91         clip.jumpTo(6 * 1000 + 1);
  92         animation.check(Command.JUMP, 1, CYCLE_TICKS);
  93     }
  94         
  95     @Test
  96     public void testTimePulseForward() {
  97         animation.mockStatus(Status.RUNNING);
  98         clip.start();
  99         
 100         clip.timePulse(1);
 101         animation.check(Command.PLAY, 1, CYCLE_TICKS);
 102         assertFalse(animation.finishCalled());
 103         
 104         clip.timePulse(6 * 500);
 105         animation.check(Command.PLAY, 6 * 500, CYCLE_TICKS);
 106         assertFalse(animation.finishCalled());
 107 
 108         clip.timePulse(6 * 700);
 109         animation.check(Command.PLAY, 6 * 700, CYCLE_TICKS);
 110         assertFalse(animation.finishCalled());
 111 
 112         clip.timePulse(6 * 1000 - 1);
 113         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 114         assertFalse(animation.finishCalled());
 115 
 116         clip.timePulse(6 * 1000 + 1);
 117         animation.check(Command.PLAY, 1, CYCLE_TICKS);
 118         assertFalse(animation.finishCalled());
 119 
 120         clip.timePulse(6 * 2200);
 121         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 122         assertFalse(animation.finishCalled());
 123 
 124         clip.timePulse(6 * 4300);
 125         animation.check(Command.PLAY, 6 * 300, CYCLE_TICKS);
 126         assertFalse(animation.finishCalled());
 127     }
 128     
 129     @Test
 130     public void testTimePulseBackward() {
 131         clip.setRate(-1.0);
 132         animation.mockStatus(Status.RUNNING);
 133         clip.start();
 134         
 135         clip.timePulse(1);
 136         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 137         assertFalse(animation.finishCalled());
 138         
 139         clip.timePulse(6 * 500);
 140         animation.check(Command.PLAY, 6 * 500, CYCLE_TICKS);
 141         assertFalse(animation.finishCalled());
 142 
 143         clip.timePulse(6 * 700);
 144         animation.check(Command.PLAY, 6 * 300, CYCLE_TICKS);
 145         assertFalse(animation.finishCalled());
 146 
 147         clip.timePulse(6 * 1000 - 1);
 148         animation.check(Command.PLAY, 1, CYCLE_TICKS);
 149         assertFalse(animation.finishCalled());
 150 
 151         clip.timePulse(6 * 1000 + 1);
 152         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 153         assertFalse(animation.finishCalled());
 154 
 155         clip.timePulse(6 * 2200);
 156         animation.check(Command.PLAY, 6 * 800, CYCLE_TICKS);
 157         assertFalse(animation.finishCalled());
 158 
 159         clip.timePulse(6 * 4300);
 160         animation.check(Command.PLAY, 6 * 700, CYCLE_TICKS);
 161         assertFalse(animation.finishCalled());
 162     }
 163     
 164     @Test
 165     public void testTimePulseForwardAutoReverse() {
 166         animation.mockStatus(Status.RUNNING);
 167         clip.setAutoReverse(true);
 168         clip.start();
 169 
 170         clip.timePulse(1);
 171         animation.check(Command.PLAY, 1, CYCLE_TICKS);
 172         assertFalse(animation.finishCalled());
 173         
 174         clip.timePulse(6 * 200);
 175         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 176         assertFalse(animation.finishCalled());
 177 
 178         clip.timePulse(6 * 1000 - 1);
 179         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 180         assertFalse(animation.finishCalled());
 181 
 182         clip.timePulse(6 * 1000 + 1);
 183         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 184         assertFalse(animation.finishCalled());
 185 
 186         // pulse one cycle ahead
 187         clip.timePulse(6 * 2200);
 188         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 189         assertFalse(animation.finishCalled());
 190 
 191         // pulse two cycles ahead
 192         clip.timePulse(6 * 4300);
 193         animation.check(Command.PLAY, 6 * 300, CYCLE_TICKS);
 194         assertFalse(animation.finishCalled());
 195 
 196         // another pulse one cycle ahead
 197         clip.timePulse(6 * 5400);
 198         animation.check(Command.PLAY, 6 * 600, CYCLE_TICKS);
 199         assertFalse(animation.finishCalled());
 200 
 201         // another pulse two cycles ahead
 202         clip.timePulse(6 * 7100);
 203         animation.check(Command.PLAY, 6 * 900, CYCLE_TICKS);
 204         assertFalse(animation.finishCalled());
 205     }
 206     
 207     @Test
 208     public void testTimePulseBackwardAutoReverse() {
 209         animation.mockStatus(Status.RUNNING);
 210         clip.setAutoReverse(true);
 211         clip.setRate(-1);
 212         clip.start();
 213 
 214         clip.timePulse(1);
 215         animation.check(Command.PLAY, 1, CYCLE_TICKS);
 216         assertFalse(animation.finishCalled());
 217         
 218         clip.timePulse(6 * 200);
 219         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 220         assertFalse(animation.finishCalled());
 221 
 222         clip.timePulse(6 * 1000 - 1);
 223         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 224         assertFalse(animation.finishCalled());
 225 
 226         clip.timePulse(6 * 1000 + 1);
 227         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 228         assertFalse(animation.finishCalled());
 229 
 230         // pulse one cycle ahead
 231         clip.timePulse(6 * 2200);
 232         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 233         assertFalse(animation.finishCalled());
 234 
 235         // pulse two cycles ahead
 236         clip.timePulse(6 * 4300);
 237         animation.check(Command.PLAY, 6 * 300, CYCLE_TICKS);
 238         assertFalse(animation.finishCalled());
 239 
 240         // another pulse one cycle ahead
 241         clip.timePulse(6 * 5400);
 242         animation.check(Command.PLAY, 6 * 600, CYCLE_TICKS);
 243         assertFalse(animation.finishCalled());
 244 
 245         // another pulse two cycles ahead
 246         clip.timePulse(6 * 7100);
 247         animation.check(Command.PLAY, 6 * 900, CYCLE_TICKS);
 248         assertFalse(animation.finishCalled());
 249     }
 250     
 251     @Test
 252     public void testJumpAndPulseForward() {
 253         animation.mockStatus(Status.RUNNING);
 254         clip.start();
 255         
 256         clip.jumpTo(6 * 300);
 257         animation.check(Command.JUMP, 6 * 300, CYCLE_TICKS);
 258 
 259         clip.timePulse(6 * 800);
 260         animation.check(Command.PLAY, 6 * 100, CYCLE_TICKS);
 261         assertFalse(animation.finishCalled());
 262 
 263         clip.jumpTo(6 * 50);
 264         animation.check(Command.JUMP, 6 * 50, CYCLE_TICKS);
 265 
 266         clip.timePulse(6 * 1850);
 267         animation.check(Command.PLAY, 6 * 100, CYCLE_TICKS);
 268         assertFalse(animation.finishCalled());
 269         
 270         clip.jumpTo(6 * 0);
 271         animation.check(Command.JUMP, 6 * 0, CYCLE_TICKS);
 272 
 273         clip.timePulse(6 * 2000);
 274         animation.check(Command.PLAY, 6 * 150, CYCLE_TICKS);
 275         assertFalse(animation.finishCalled());
 276         
 277         clip.jumpTo(6 * 1000);
 278         animation.check(Command.JUMP, 6 * 1000, CYCLE_TICKS);
 279 
 280         clip.timePulse(6 * 2200);
 281         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 282         assertFalse(animation.finishCalled());
 283     }
 284 
 285     @Test
 286     public void testJumpAndPulseBackward() {
 287         animation.mockStatus(Status.RUNNING);
 288         clip.setRate(-1);
 289         clip.start();
 290         
 291         // jump forward
 292         clip.jumpTo(6 * 300);
 293         animation.check(Command.JUMP, 6 * 300, CYCLE_TICKS);
 294         assertFalse(animation.finishCalled());
 295 
 296         // pulse in next cycle
 297         clip.timePulse(6 * 700);
 298         animation.check(Command.PLAY, 6 * 600, CYCLE_TICKS);
 299         assertFalse(animation.finishCalled());
 300 
 301         // jump backward
 302         clip.jumpTo(6 * 900);
 303         animation.check(Command.JUMP, 6 * 900, CYCLE_TICKS);
 304         assertFalse(animation.finishCalled());
 305 
 306         // pulse one cycle ahead
 307         clip.timePulse(6 * 1750);
 308         animation.check(Command.PLAY, 6 * 850, CYCLE_TICKS);
 309         assertFalse(animation.finishCalled());
 310 
 311         // jump to same position at end
 312         clip.jumpTo(6 * 1000);
 313         animation.check(Command.JUMP, 6 * 1000, CYCLE_TICKS);
 314         assertFalse(animation.finishCalled());
 315 
 316         // normal pulse
 317         clip.timePulse(6 * 2000);
 318         animation.check(Command.PLAY, 6 * 750, CYCLE_TICKS);
 319         assertFalse(animation.finishCalled());
 320 
 321         // jump to start
 322         clip.jumpTo(0);
 323         animation.check(Command.JUMP, 0, CYCLE_TICKS);
 324         assertFalse(animation.finishCalled());
 325 
 326         // normal pulse
 327         clip.timePulse(6 * 2200);
 328         animation.check(Command.PLAY, 6 * 800, CYCLE_TICKS);
 329         assertFalse(animation.finishCalled());
 330     }
 331 
 332     @Test
 333     public void testJumpAndPulseForwardAutoReverse() {
 334         animation.mockStatus(Status.RUNNING);
 335         clip.setAutoReverse(true);
 336         clip.start();
 337         
 338         // jump forward
 339         clip.jumpTo(6 * 300);
 340         animation.check(Command.JUMP, 6 * 300, CYCLE_TICKS);
 341 
 342         // pulse in next cycle
 343         clip.timePulse(6 * 900);
 344         animation.check(Command.PLAY, 6 * 800, CYCLE_TICKS);
 345         assertFalse(animation.finishCalled());
 346 
 347         // jump backward
 348         clip.jumpTo(6 * 900);
 349         animation.check(Command.JUMP, 6 * 900, CYCLE_TICKS);
 350 
 351         // pulse one cycle ahead
 352         clip.timePulse(6 * 1850);
 353         animation.check(Command.PLAY, 6 * 150, CYCLE_TICKS);
 354         assertFalse(animation.finishCalled());
 355         
 356         // jump to start
 357         clip.jumpTo(6 * 0);
 358         animation.check(Command.JUMP, 0, CYCLE_TICKS);
 359 
 360         // normal pulse
 361         clip.timePulse(6 * 2000);
 362         animation.check(Command.PLAY, 6 * 150, CYCLE_TICKS);
 363         assertFalse(animation.finishCalled());
 364         
 365         // jump to end
 366         clip.jumpTo(6 * 1000);
 367         animation.check(Command.JUMP, 6 * 1000, CYCLE_TICKS);
 368 
 369         // normal pulse
 370         clip.timePulse(6 * 2200);
 371         animation.check(Command.PLAY, 6 * 800, CYCLE_TICKS);
 372         assertFalse(animation.finishCalled());
 373     }
 374 
 375     @Test
 376     public void testJumpAndPulseBackwardAutoReverse() {
 377         animation.mockStatus(Status.RUNNING);
 378         clip.setAutoReverse(true);
 379         clip.setRate(-1);
 380         clip.start();
 381         
 382         // jump forward
 383         clip.jumpTo(6 * 300);
 384         animation.check(Command.JUMP, 6 * 300, CYCLE_TICKS);
 385 
 386         // pulse in next cycle
 387         clip.timePulse(6 * 700);
 388         animation.check(Command.PLAY, 6 * 400, CYCLE_TICKS);
 389         assertFalse(animation.finishCalled());
 390 
 391         // jump backward
 392         clip.jumpTo(6 * 100);
 393         animation.check(Command.JUMP, 6 * 100, CYCLE_TICKS);
 394 
 395         // pulse one cycle ahead
 396         clip.timePulse(6 * 1800 - 1);
 397         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 398         assertFalse(animation.finishCalled());
 399         clip.timePulse(6 * 1800 + 1);
 400         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 401         assertFalse(animation.finishCalled());
 402         
 403         // jump to end
 404         clip.jumpTo(6 * 1000);
 405         animation.check(Command.JUMP, 6 * 1000, CYCLE_TICKS);
 406 
 407         // normal pulse
 408         clip.timePulse(6 * 2000);
 409         animation.check(Command.PLAY, 6 * 800 + 1, CYCLE_TICKS);
 410         assertFalse(animation.finishCalled());
 411         
 412         // jump to start
 413         clip.jumpTo(0);
 414         animation.check(Command.JUMP, 0, CYCLE_TICKS);
 415 
 416         // normal pulse
 417         clip.timePulse(6 * 2200);
 418         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 419         assertFalse(animation.finishCalled());
 420     }
 421     
 422     @Test
 423     public void testRate() {
 424         animation.mockStatus(Status.RUNNING);
 425         clip.setRate(0.5);
 426         clip.start();
 427         
 428         clip.timePulse(6 * 200);
 429         animation.check(Command.PLAY, 6 * 100, CYCLE_TICKS);
 430         
 431         clip.setRate(3.0);
 432         clip.timePulse(6 * 300);
 433         animation.check(Command.PLAY, 6 * 400, CYCLE_TICKS);
 434         
 435         clip.setRate(2.0);
 436         clip.timePulse(6 * 500);
 437         animation.check(Command.PLAY, 6 * 800, CYCLE_TICKS);
 438         
 439         clip.setRate(-0.5);
 440         clip.timePulse(6 * 1100);
 441         animation.check(Command.PLAY, 6 * 500, CYCLE_TICKS);
 442         
 443         clip.setRate(-3.0);
 444         clip.timePulse(6 * 1200);
 445         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 446         
 447         clip.setRate(0.5);
 448         clip.timePulse(6 * 2100);
 449         animation.check(Command.PLAY, 6 * 650, CYCLE_TICKS);
 450         
 451         clip.setRate(1.5);
 452         clip.timePulse(6 * 2600);
 453         animation.check(Command.PLAY, 6 * 400, CYCLE_TICKS);
 454         
 455         clip.setRate(-2.0);
 456         clip.timePulse(6 * 3000);
 457         animation.check(Command.PLAY, 6 * 600, CYCLE_TICKS);
 458     }
 459     
 460     @Test
 461     public void testRateAutoReverse() {
 462         animation.mockStatus(Status.RUNNING);
 463         clip.setAutoReverse(true);
 464         clip.setRate(0.5);
 465         clip.start();
 466         
 467         clip.timePulse(6 * 200);
 468         animation.check(Command.PLAY, 6 * 100, CYCLE_TICKS);
 469         
 470         clip.setRate(3.0);
 471         clip.timePulse(6 * 300);
 472         animation.check(Command.PLAY, 6 * 400, CYCLE_TICKS);
 473         
 474         clip.setRate(2.0);
 475         clip.timePulse(6 * 500);
 476         animation.check(Command.PLAY, 6 * 800, CYCLE_TICKS);
 477         
 478         clip.setRate(-0.5);
 479         clip.timePulse(6 * 1100);
 480         animation.check(Command.PLAY, 6 * 500, CYCLE_TICKS);
 481         
 482         clip.setRate(-3.0);
 483         clip.timePulse(6 * 1200);
 484         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 485         
 486         clip.setRate(0.5);
 487         clip.timePulse(6 * 2100);
 488         animation.check(Command.PLAY, 6 * 650, CYCLE_TICKS);
 489         
 490         clip.setRate(1.5);
 491         clip.timePulse(6 * 2600);
 492         animation.check(Command.PLAY, 6 * 600, CYCLE_TICKS);
 493         
 494         clip.setRate(-2.0);
 495         clip.timePulse(6 * 3000);
 496         animation.check(Command.PLAY, 6 * 600, CYCLE_TICKS);
 497     }
 498 }