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 com.sun.scenario.animation.shared; 27 28 import static org.junit.Assert.assertEquals; 29 import static org.junit.Assert.assertFalse; 30 import static org.junit.Assert.assertTrue; 31 import javafx.animation.KeyFrame; 32 import javafx.animation.KeyValue; 33 import javafx.animation.Timeline; 34 import javafx.animation.TimelineHelper; 35 import javafx.beans.property.IntegerProperty; 36 import javafx.beans.property.SimpleIntegerProperty; 37 import javafx.event.ActionEvent; 38 import javafx.event.EventHandler; 39 import javafx.util.Duration; 40 41 import org.junit.Before; 42 import org.junit.Ignore; 43 import org.junit.Test; 44 45 import java.io.IOException; 46 import java.io.OutputStream; 47 import java.io.PrintStream; 48 49 public class TimelineClipCoreTest { 50 private Timeline timeline; 51 52 private KeyFrame start; 53 private KeyFrame middle; 54 private KeyFrame end; 55 private IntegerProperty target; 56 57 private TimelineClipCore core; 58 59 private boolean tmpBool; 60 61 @Before 62 public void setUp() { 63 target = new SimpleIntegerProperty(); 64 65 start = new KeyFrame(Duration.ZERO, new KeyValue(target, 10)); 66 middle = new KeyFrame(new Duration(500)); 67 end = new KeyFrame(new Duration(1000), new KeyValue(target, 20)); 68 69 timeline = new Timeline(); 70 timeline.getKeyFrames().setAll(start, middle, end); 71 timeline.setRate(1.0); 72 timeline.setCycleCount(1); 73 timeline.setAutoReverse(false); 74 core = TimelineHelper.getClipCore(timeline); 75 } 76 77 @Test 78 public void testPlayTo() { 79 //forward 80 timeline.play(); 81 timeline.pause(); 82 core.playTo(6 * 500); 83 assertEquals(15, target.get()); 84 85 //to the end 86 core.playTo(6 * 1000); 87 assertEquals(20, target.get()); 88 89 //backwards 90 core.playTo(6 * 200); 91 assertEquals(12, target.get()); 92 93 //back to start 94 core.playTo(0); 95 assertEquals(10, target.get()); 96 97 //catching up 98 tmpBool = false; 99 final KeyFrame newMiddle = new KeyFrame( 100 Duration.millis(500), 101 event -> { 102 tmpBool = true; 103 } 104 ); 105 timeline.getKeyFrames().set(1, newMiddle); 106 107 core.playTo(6 * 1000); 108 assertEquals(20, target.get()); 109 assertTrue(tmpBool); 110 111 // //visit last 112 // core.start(); 113 // tmpBool = false; 114 // end.setCanSkip(true); 115 // end.setAction(new Runnable() { 116 // 117 // @Override 118 // public void run() { 119 // tmpBool = true; 120 // } 121 // }); 122 // 123 // core.playTo(1000, true, true); 124 // assertTrue(tmpBool); 125 } 126 127 @Test 128 public void testPlayTo_ThrowsException() { 129 final PrintStream defaultErrorStream = System.err; 130 final PrintStream nirvana = new PrintStream(new OutputStream() { 131 @Override 132 public void write(int i) throws IOException { 133 } 134 }); 135 final OnFinishedExceptionListener eventHandler = new OnFinishedExceptionListener() ; 136 start = new KeyFrame(Duration.ZERO, eventHandler); 137 middle = new KeyFrame(new Duration(500), eventHandler); 138 end = new KeyFrame(new Duration(1000), eventHandler); 139 timeline.getKeyFrames().setAll(start, middle, end); 140 141 try { 142 System.setErr(nirvana); 143 } catch (SecurityException ex) { 144 // ignore 145 } 146 timeline.play(); 147 timeline.pause(); 148 core.playTo(6 * 100); 149 try { 150 System.setErr(defaultErrorStream); 151 } catch (SecurityException ex) { 152 // ignore 153 } 154 assertEquals(1, eventHandler.callCount); 155 156 try { 157 System.setErr(nirvana); 158 } catch (SecurityException ex) { 159 // ignore 160 } 161 core.playTo(6 * 600); 162 try { 163 System.setErr(defaultErrorStream); 164 } catch (SecurityException ex) { 165 // ignore 166 } 167 assertEquals(2, eventHandler.callCount); 168 169 try { 170 System.setErr(nirvana); 171 } catch (SecurityException ex) { 172 // ignore 173 } 174 core.playTo(6 * 1000); 175 try { 176 System.setErr(defaultErrorStream); 177 } catch (SecurityException ex) { 178 // ignore 179 } 180 assertEquals(3, eventHandler.callCount); 181 } 182 183 @Ignore 184 @Test 185 public void testJumpTo() { 186 // jumpTo on stopped timeline 187 tmpBool = false; 188 final KeyFrame newMiddle = new KeyFrame( 189 Duration.millis(500), 190 event -> { 191 tmpBool = true; 192 } 193 ); 194 timeline.getKeyFrames().set(1, newMiddle); 195 196 core.jumpTo(6 * 600, false); 197 assertEquals(0, target.get()); 198 assertFalse(tmpBool); 199 200 // jumpTo on paused timeline 201 tmpBool = false; 202 timeline.play(); 203 timeline.pause(); 204 core.jumpTo(6 * 400, false); 205 assertEquals(14, target.get()); 206 assertFalse(tmpBool); 207 } 208 209 private static class OnFinishedExceptionListener implements EventHandler<ActionEvent> { 210 211 private int callCount = 0; 212 213 @Override 214 public void handle(ActionEvent event) { 215 callCount++; 216 throw new RuntimeException("Test Exception"); 217 } 218 219 } 220 221 }