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 javafx.animation;
  27 
  28 import static org.junit.Assert.assertEquals;
  29 import static org.junit.Assert.assertTrue;
  30 
  31 import java.util.Arrays;
  32 import java.util.Collections;
  33 
  34 import com.sun.javafx.tk.Toolkit;
  35 import javafx.animation.Animation.Status;
  36 import javafx.util.Duration;
  37 
  38 import org.junit.Before;
  39 import org.junit.Test;
  40 
  41 
  42 /**
  43  *
  44  */
  45 public class TimelineTest {
  46 
  47     private static final double DEFAULT_RATE = 1.0;
  48     private static final int DEFAULT_REPEAT_COUNT = 1;
  49     private static final boolean DEFAULT_AUTO_REVERSE = false;
  50     
  51     private static final double EPSILON = 1e-12;
  52 
  53     private Timeline timeline;
  54 
  55     @Before
  56     public void setUp() {
  57         timeline = new Timeline();
  58     }
  59     
  60     @Test
  61     public void testDefaultValues() {
  62         final Duration oneSec = Duration.millis(1000);
  63         final KeyFrame kf0 = new KeyFrame(Duration.ZERO);
  64         final KeyFrame kf1 = new KeyFrame(oneSec, "oneSec");
  65 
  66         // empty ctor
  67         final Timeline timeline0 = new Timeline();
  68         assertTrue(timeline0.getKeyFrames().isEmpty());
  69         assertEquals(DEFAULT_RATE, timeline0.getRate(), EPSILON);
  70         assertEquals(0.0, timeline0.getCurrentRate(), EPSILON);
  71         assertEquals(Duration.ZERO, timeline0.getCycleDuration());
  72         assertEquals(Duration.ZERO, timeline0.getTotalDuration());
  73         assertEquals(Duration.ZERO, timeline0.getCurrentTime());
  74         assertEquals(DEFAULT_REPEAT_COUNT, timeline0.getCycleCount());
  75         assertEquals(DEFAULT_AUTO_REVERSE, timeline0.isAutoReverse());
  76         assertEquals(Status.STOPPED, timeline0.getStatus());
  77         assertEquals(6000.0 / Toolkit.getToolkit().getMasterTimer().getDefaultResolution(), timeline0.getTargetFramerate(), EPSILON);
  78         assertEquals(null, timeline0.getOnFinished());
  79         assertTrue(timeline0.getCuePoints().isEmpty());
  80 
  81         // only target framerate
  82         final Timeline timeline1 = new Timeline(42.0);
  83         assertTrue(timeline1.getKeyFrames().isEmpty());
  84         assertEquals(DEFAULT_RATE, timeline1.getRate(), EPSILON);
  85         assertEquals(0.0, timeline1.getCurrentRate(), EPSILON);
  86         assertEquals(Duration.ZERO, timeline1.getCycleDuration());
  87         assertEquals(Duration.ZERO, timeline1.getTotalDuration());
  88         assertEquals(Duration.ZERO, timeline1.getCurrentTime());
  89         assertEquals(DEFAULT_REPEAT_COUNT, timeline1.getCycleCount());
  90         assertEquals(DEFAULT_AUTO_REVERSE, timeline1.isAutoReverse());
  91         assertEquals(Status.STOPPED, timeline1.getStatus());
  92         assertEquals(42.0, timeline1.getTargetFramerate(), EPSILON);
  93         assertEquals(null, timeline1.getOnFinished());
  94         assertTrue(timeline1.getCuePoints().isEmpty());
  95         
  96         // only keyframes
  97         final Timeline timeline2 = new Timeline(kf0, kf1);
  98         assertEquals(Arrays.asList(kf0, kf1), timeline2.getKeyFrames());
  99         assertEquals(DEFAULT_RATE, timeline2.getRate(), EPSILON);
 100         assertEquals(0.0, timeline2.getCurrentRate(), EPSILON);
 101         assertEquals(oneSec, timeline2.getCycleDuration());
 102         assertEquals(oneSec, timeline2.getTotalDuration());
 103         assertEquals(Duration.ZERO, timeline2.getCurrentTime());
 104         assertEquals(DEFAULT_REPEAT_COUNT, timeline2.getCycleCount());
 105         assertEquals(DEFAULT_AUTO_REVERSE, timeline2.isAutoReverse());
 106         assertEquals(Status.STOPPED, timeline2.getStatus());
 107         assertEquals(6000.0 / Toolkit.getToolkit().getMasterTimer().getDefaultResolution(), timeline2.getTargetFramerate(), EPSILON);
 108         assertEquals(null, timeline2.getOnFinished());
 109         assertEquals(Collections.singletonMap("oneSec", oneSec), timeline2.getCuePoints());
 110 
 111         // target framerate and keyframes
 112         final Timeline timeline3 = new Timeline(42.0, kf0, kf1);
 113         assertEquals(Arrays.asList(kf0, kf1), timeline3.getKeyFrames());
 114         assertEquals(DEFAULT_RATE, timeline3.getRate(), EPSILON);
 115         assertEquals(0.0, timeline3.getCurrentRate(), EPSILON);
 116         assertEquals(oneSec, timeline3.getCycleDuration());
 117         assertEquals(oneSec, timeline3.getTotalDuration());
 118         assertEquals(Duration.ZERO, timeline3.getCurrentTime());
 119         assertEquals(DEFAULT_REPEAT_COUNT, timeline3.getCycleCount());
 120         assertEquals(DEFAULT_AUTO_REVERSE, timeline3.isAutoReverse());
 121         assertEquals(Status.STOPPED, timeline3.getStatus());
 122         assertEquals(42.0, timeline1.getTargetFramerate(), EPSILON);
 123         assertEquals(null, timeline3.getOnFinished());
 124         assertEquals(Collections.singletonMap("oneSec", oneSec), timeline2.getCuePoints());
 125     }
 126 
 127     @Test
 128     public void testKeyFrames() {
 129         final Duration oneSec = Duration.millis(1000);
 130         final KeyFrame kf0 = new KeyFrame(Duration.ZERO);
 131         final KeyFrame kf1 = new KeyFrame(oneSec, "oneSec");
 132         
 133         // insert key frame
 134         timeline.getKeyFrames().add(kf1);
 135         assertEquals(Collections.singletonList(kf1), timeline.getKeyFrames());
 136         assertEquals(Collections.singletonMap("oneSec", oneSec), timeline.getCuePoints());
 137         assertEquals(oneSec, timeline.getCycleDuration());
 138 
 139         // remove key frame
 140         timeline.getKeyFrames().clear();
 141         assertTrue(timeline.getKeyFrames().isEmpty());
 142         assertEquals(Collections.emptyMap(), timeline.getCuePoints());
 143         assertEquals(Duration.ZERO, timeline.getCycleDuration());
 144 
 145         // insert two key frames
 146         timeline.getKeyFrames().addAll(kf1, kf0);
 147         assertEquals(Arrays.asList(kf1, kf0), timeline.getKeyFrames());
 148         assertEquals(Collections.singletonMap("oneSec", oneSec), timeline.getCuePoints());
 149         assertEquals(oneSec, timeline.getCycleDuration());
 150 
 151         // remove key frame
 152         timeline.getKeyFrames().remove(kf1);
 153         assertEquals(Collections.singletonList(kf0), timeline.getKeyFrames());
 154         assertEquals(Collections.emptyMap(), timeline.getCuePoints());
 155         assertEquals(Duration.ZERO, timeline.getCycleDuration());
 156     }
 157 
 158 }