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 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertFalse;
  30 import static org.junit.Assert.assertTrue;
  31 
  32 import com.sun.javafx.tk.Toolkit;
  33 import javafx.animation.Animation;
  34 
  35 import org.junit.After;
  36 import org.junit.Before;
  37 import org.junit.Test;
  38 
  39 import com.sun.scenario.animation.AbstractMasterTimerMock;
  40 import javafx.animation.AnimationMock;
  41 import javafx.util.Duration;
  42 
  43 public class AnimationPulseReceiverTest {
  44 
  45     private static final int DEFAULT_RESOLUTION = Toolkit.getToolkit().getMasterTimer().getDefaultResolution();
  46     private static final double TICKS_2_NANOS = 1.0 / 6e-6;
  47     private AbstractMasterTimerMock timer;
  48     private AnimationMock animation;
  49 
  50     @Before
  51     public void setUp() {
  52         timer = new AbstractMasterTimerMock();
  53         animation = new AnimationMock(timer, Duration.INDEFINITE, 1.0, 1, false);
  54     }
  55 
  56     @After
  57     public void tearDown() {
  58         animation.impl_stop();
  59     }
  60 
  61     @Test
  62     public void testPlay_DefaultResolution() {
  63         // start animatiom
  64         timer.setNanos(Math.round(3 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
  65         animation.startReceiver(0);
  66         assertTrue(timer.containsPulseReceiver(animation.pulseReceiver));
  67 
  68         // send pulse
  69         animation.pulseReceiver.timePulse(7 * DEFAULT_RESOLUTION);
  70         assertEquals(4 * DEFAULT_RESOLUTION, animation.getLastTimePulse());
  71 
  72         // another pulse
  73         animation.pulseReceiver.timePulse(16 * DEFAULT_RESOLUTION);
  74         assertEquals(13 * DEFAULT_RESOLUTION, animation.getLastTimePulse());
  75 
  76         // stop animation
  77         animation.impl_stop();
  78         assertFalse(timer.containsPulseReceiver(animation.pulseReceiver));
  79 
  80         // stop again
  81         animation.impl_stop();
  82         assertFalse(timer.containsPulseReceiver(animation.pulseReceiver));
  83 
  84         // start again
  85         timer.setNanos(Math.round(30 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
  86         animation.startReceiver(0);
  87         assertTrue(timer.containsPulseReceiver(animation.pulseReceiver));
  88 
  89         // send pulse
  90         animation.pulseReceiver.timePulse(43 * DEFAULT_RESOLUTION);
  91         assertEquals(13 * DEFAULT_RESOLUTION, animation.getLastTimePulse());
  92     }
  93 
  94     @Test
  95     public void testPause_DefaultResolution() {
  96         // start animation
  97         timer.setNanos(Math.round(3 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
  98         animation.startReceiver(0);
  99         assertTrue(timer.containsPulseReceiver(animation.pulseReceiver));
 100 
 101         // pause animation
 102         timer.setNanos(Math.round(18 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
 103         animation.pauseReceiver();
 104         assertFalse(timer.containsPulseReceiver(animation.pulseReceiver));
 105 
 106         // pause again
 107         timer.setNanos(Math.round(27 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
 108         animation.pauseReceiver();
 109         assertFalse(timer.containsPulseReceiver(animation.pulseReceiver));
 110 
 111         // resume
 112         timer.setNanos(Math.round(36 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
 113         animation.resumeReceiver();
 114         assertTrue(timer.containsPulseReceiver(animation.pulseReceiver));
 115 
 116         // resume again
 117         timer.setNanos(Math.round(42 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
 118         animation.resumeReceiver();
 119         assertTrue(timer.containsPulseReceiver(animation.pulseReceiver));
 120 
 121         // send pulse
 122         animation.pulseReceiver.timePulse(51 * DEFAULT_RESOLUTION);
 123         assertEquals(30 * DEFAULT_RESOLUTION, animation.getLastTimePulse());
 124     }
 125 
 126     @Test
 127     public void testDelay() {
 128         // start animatiom
 129         timer.setNanos(Math.round(3 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
 130         animation.startReceiver(17 * DEFAULT_RESOLUTION);
 131         assertTrue(timer.containsPulseReceiver(animation.pulseReceiver));
 132 
 133         // send pulse during delay
 134         animation.pulseReceiver.timePulse(5 * DEFAULT_RESOLUTION);
 135         assertEquals(0, animation.getLastTimePulse());
 136 
 137         // pause & resume
 138         timer.setNanos(Math.round(10 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
 139         animation.pauseReceiver();
 140         timer.setNanos(Math.round(37 * DEFAULT_RESOLUTION * TICKS_2_NANOS));
 141         animation.resumeReceiver();
 142 
 143         // send pulse during delay
 144         animation.pulseReceiver.timePulse(41 * DEFAULT_RESOLUTION);
 145         assertEquals(0, animation.getLastTimePulse());
 146 
 147         // send pulse after delay
 148         animation.pulseReceiver.timePulse(48 * DEFAULT_RESOLUTION);
 149         assertEquals(1 * DEFAULT_RESOLUTION, animation.getLastTimePulse());
 150     }
 151 }