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 com.sun.scenario.animation.shared;
  27 
  28 import com.sun.javafx.tk.Toolkit;
  29 import javafx.animation.Animation.Status;
  30 import javafx.animation.AnimationMock;
  31 import javafx.animation.AnimationMock.Command;
  32 import javafx.util.Duration;
  33 import org.junit.Before;
  34 import org.junit.Test;
  35 
  36 import static org.junit.Assert.*;
  37 
  38 public class SingleLoopClipEnvelopeTest {
  39     
  40     
  41     private final long CYCLE_TICKS = Math.round(6.0 * AnimationMock.DEFAULT_DURATION.toMillis());
  42 
  43     private ClipEnvelope clip;
  44     private AnimationMock animation;
  45     
  46     @Before
  47     public void setUp() {
  48         animation = new AnimationMock(Toolkit.getToolkit().getMasterTimer(), AnimationMock.DEFAULT_DURATION, AnimationMock.DEFAULT_RATE, 1, AnimationMock.DEFAULT_AUTOREVERSE);
  49         clip = new SingleLoopClipEnvelope(animation);
  50     }
  51     
  52     @Test
  53     public void testSetValues() {
  54         ClipEnvelope c;
  55         
  56         // Setting cycleCount to 2
  57         animation.setCycleCount(2);
  58         animation.mockCycleDuration(AnimationMock.DEFAULT_DURATION);
  59         c = clip.setCycleCount(2);
  60         assertNotSame(clip, c);
  61         assertTrue(c instanceof FiniteClipEnvelope);
  62         
  63         // Setting cycleDuration to INDEFINITE
  64         animation.setCycleCount(1);
  65         animation.mockCycleDuration(Duration.INDEFINITE);
  66         c = clip.setCycleDuration(Duration.INDEFINITE);
  67         assertSame(clip, c);
  68 
  69         // Setting cycleCount to 2
  70         animation.setCycleCount(2);
  71         animation.mockCycleDuration(Duration.INDEFINITE);
  72         c = clip.setCycleCount(2);
  73         assertSame(clip, c);
  74         
  75         // Setting cycleDuration to < INDEFINITE
  76         animation.setCycleCount(2);
  77         animation.mockCycleDuration(AnimationMock.DEFAULT_DURATION);
  78         c = clip.setCycleDuration(AnimationMock.DEFAULT_DURATION);
  79         assertNotSame(clip, c);
  80         assertTrue(c instanceof FiniteClipEnvelope);
  81 
  82         // Setting cycleCount to 1
  83         animation.setCycleCount(1);
  84         animation.mockCycleDuration(AnimationMock.DEFAULT_DURATION);
  85         c = clip.setCycleCount(1);
  86         assertSame(clip, c);
  87     }
  88 
  89     @Test
  90     public void testJump() {
  91         clip.jumpTo(0);
  92         animation.check(Command.JUMP, 0, CYCLE_TICKS);
  93         
  94         clip.jumpTo(6 * 300);
  95         animation.check(Command.JUMP, 6 * 300, CYCLE_TICKS);
  96         
  97         clip.jumpTo(6 * 300);
  98         animation.check(Command.JUMP, 6 * 300, CYCLE_TICKS);
  99         
 100         clip.jumpTo(0);
 101         animation.check(Command.JUMP, 0, CYCLE_TICKS);
 102         
 103         clip.jumpTo(6 * 1000);
 104         animation.check(Command.JUMP, 6 * 1000, CYCLE_TICKS);
 105         
 106         clip.jumpTo(-1);
 107         animation.check(Command.JUMP, 0, CYCLE_TICKS);
 108         
 109         clip.jumpTo(6 * 1000 + 1);
 110         animation.check(Command.JUMP, 6 * 1000, CYCLE_TICKS);
 111     }
 112         
 113     @Test
 114     public void testTimePulseForward() {
 115         animation.mockStatus(Status.RUNNING);
 116         clip.start();
 117        
 118         clip.timePulse(1);
 119         animation.check(Command.PLAY, 1, CYCLE_TICKS);
 120         assertFalse(animation.finishCalled());
 121         
 122         clip.timePulse(6 * 1000 - 1);
 123         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 124         assertFalse(animation.finishCalled());
 125 
 126         clip.timePulse(6 * 1000);
 127         animation.check(Command.PLAY, 6 * 1000, CYCLE_TICKS);
 128         assertTrue(animation.finishCalled());
 129     }
 130     
 131     @Test
 132     public void testTimePulseBackward() {
 133         clip.jumpTo(6 * 1000);
 134         clip.setRate(-1.0);
 135         animation.mockStatus(Status.RUNNING);
 136         clip.start();
 137         
 138         clip.timePulse(1);
 139         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 140         assertFalse(animation.finishCalled());
 141 
 142         clip.timePulse(6 * 1000 - 1);
 143         animation.check(Command.PLAY, 1, CYCLE_TICKS);
 144         assertFalse(animation.finishCalled());
 145 
 146         clip.timePulse(6 * 1000);
 147         animation.check(Command.PLAY, 0, CYCLE_TICKS);
 148         assertTrue(animation.finishCalled());
 149     }
 150     
 151     @Test
 152     public void testJumpAndPulseForward() {
 153         animation.mockStatus(Status.RUNNING);
 154         clip.start();
 155         
 156         clip.jumpTo(6 * 300);
 157         animation.check(Command.JUMP, 6 * 300, CYCLE_TICKS);
 158 
 159         clip.timePulse(6 * 700 - 1);
 160         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 161         assertFalse(animation.finishCalled());
 162 
 163         clip.jumpTo(6 * 500);
 164         animation.check(Command.JUMP, 6 * 500, CYCLE_TICKS);
 165 
 166         clip.timePulse(6 * 700 - 1 + 6 * 500 - 1);
 167         animation.check(Command.PLAY, 6 * 1000 - 1, CYCLE_TICKS);
 168         assertFalse(animation.finishCalled());
 169         
 170         clip.timePulse(6 * 700 - 1 + 6 * 500 - 1 + 1);
 171         animation.check(Command.PLAY, 6 * 1000, CYCLE_TICKS);
 172         assertTrue(animation.finishCalled());
 173     }
 174     
 175     @Test
 176     public void testJumpAndPulseBackward() {
 177         clip.jumpTo(6 * 1000);
 178         clip.setRate(-1.0);
 179         animation.mockStatus(Status.RUNNING);
 180         clip.start();
 181 
 182         clip.jumpTo(6 * 300);
 183         animation.check(Command.JUMP, 6 * 300, CYCLE_TICKS);
 184 
 185         clip.timePulse(6 * 300 - 1);
 186         animation.check(Command.PLAY, 1, CYCLE_TICKS);
 187         assertFalse(animation.finishCalled());
 188 
 189         clip.jumpTo(6 * 500);
 190         animation.check(Command.JUMP, 6 * 500, CYCLE_TICKS);
 191 
 192         clip.timePulse(6 * 300 - 1 + 6 * 500 - 1);
 193         animation.check(Command.PLAY, 1, CYCLE_TICKS);
 194         assertFalse(animation.finishCalled());
 195         
 196         clip.timePulse(6 * 300 - 1 + 6 * 500 - 1 + 1);
 197         animation.check(Command.PLAY, 0, CYCLE_TICKS);
 198         assertTrue(animation.finishCalled());
 199     }
 200     
 201     @Test
 202     public void testRate() {
 203         clip.setRate(0.5);
 204         animation.mockStatus(Status.RUNNING);
 205         clip.start();
 206 
 207         clip.timePulse(6 * 200);
 208         animation.check(Command.PLAY, 6 * 100, CYCLE_TICKS);
 209         
 210         clip.setRate(3.0);
 211         clip.timePulse(6 * 300);
 212         animation.check(Command.PLAY, 6 * 400, CYCLE_TICKS);
 213         
 214         clip.setRate(2.0);
 215         clip.timePulse(6 * 500);
 216         animation.check(Command.PLAY, 6 * 800, CYCLE_TICKS);
 217         
 218         clip.setRate(-0.5);
 219         clip.timePulse(6 * 1100);
 220         animation.check(Command.PLAY, 6 * 500, CYCLE_TICKS);
 221         
 222         clip.setRate(-3.0);
 223         clip.timePulse(6 * 1200);
 224         animation.check(Command.PLAY, 6 * 200, CYCLE_TICKS);
 225         
 226         clip.setRate(0.5);
 227         clip.timePulse(6 * 2100);
 228         animation.check(Command.PLAY, 6 * 650, CYCLE_TICKS);
 229     }
 230     
 231 }