1 /*
   2  * Copyright (c) 2011, 2014, 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 javafx.event.ActionEvent;
  29 import javafx.event.EventHandler;
  30 import javafx.util.Duration;
  31 import com.sun.scenario.animation.AbstractMasterTimer;
  32 
  33 import static org.junit.Assert.*;
  34 
  35 public class AnimationMock extends Animation {
  36     
  37     public static final Duration DEFAULT_DURATION = Duration.seconds(1);
  38     public static final double DEFAULT_RATE = 1.0;
  39     public static final int DEFAULT_CYCLE_COUNT = 1;
  40     public static final boolean DEFAULT_AUTOREVERSE = false;
  41     private long lastTimePulse;
  42     
  43     public enum Command {PLAY, JUMP, NONE};
  44     
  45     private Command lastCommand = Command.NONE;
  46     private long lastCurrentTicks = -1;
  47     private long lastCycleTicks = -1;
  48     private boolean finishFlag;
  49     
  50     public void mockStatus(Status status) {
  51         this.setStatus(status);
  52     }
  53     
  54     public void mockCycleDuration(Duration duration) {
  55         this.setCycleDuration(duration);
  56     }
  57     
  58     public AnimationMock(AbstractMasterTimer timer, Duration cycleDuration, double rate, int cycleCount, boolean autoReverse) {
  59         super(timer);
  60         setCycleDuration(cycleDuration);
  61         setRate(rate);
  62         setCycleCount(cycleCount);
  63         setAutoReverse(autoReverse);
  64         super.setOnFinished(event -> {
  65             finishFlag = true;
  66         });
  67     }
  68     
  69     public void check(Command lastCommand, long lastCurrentTicks, long lastCycleTicks) {
  70         assertEquals(lastCommand, this.lastCommand);
  71         if (lastCommand != Command.NONE) {
  72             assertEquals(lastCurrentTicks, this.lastCurrentTicks);
  73             assertEquals(lastCycleTicks, this.lastCycleTicks);
  74         }
  75         this.lastCommand = Command.NONE;
  76         this.lastCurrentTicks = -1;
  77         this.lastCycleTicks = -1;
  78     }
  79         
  80     public boolean finishCalled() {
  81         final boolean result = finishFlag;
  82         finishFlag = false;
  83         return result;
  84     }
  85     
  86     public long getLastTimePulse() {
  87             final long p = lastTimePulse;
  88             lastTimePulse = 0L;
  89             return p;
  90         }
  91 
  92     
  93     @Override
  94     public void impl_playTo(long currentTicks, long cycleTicks) {
  95         lastCommand = Command.PLAY;
  96         lastCurrentTicks = currentTicks;
  97         lastCycleTicks = cycleTicks;
  98     }
  99 
 100     @Override
 101     public void impl_jumpTo(long currentTicks, long cycleTicks, boolean forceJump) {
 102         lastCommand = Command.JUMP;
 103         lastCurrentTicks = currentTicks;
 104         lastCycleTicks = cycleTicks;
 105     }
 106 
 107     @Override
 108     void impl_timePulse(long elapsedTime) {
 109         super.impl_timePulse(elapsedTime);
 110         lastTimePulse = elapsedTime;
 111     }
 112 
 113 }