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