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