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