1 /* 2 * Copyright (c) 2010, 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.scene; 27 28 import static javafx.scene.image.TestImages.TEST_ERROR_IMAGE; 29 import static org.junit.Assert.assertEquals; 30 import static org.junit.Assert.assertNotSame; 31 import static org.junit.Assert.assertSame; 32 import javafx.scene.image.Image; 33 import javafx.scene.image.TestImages; 34 35 import org.junit.Test; 36 37 import com.sun.javafx.pgstub.StubToolkit; 38 import com.sun.javafx.tk.Toolkit; 39 40 public final class ImageCursor_getCurrentFrame_Test { 41 private final StubToolkit toolkit; 42 43 public ImageCursor_getCurrentFrame_Test() { 44 toolkit = (StubToolkit) Toolkit.getToolkit(); 45 } 46 47 @Test 48 public void specialCasesTest() { 49 final Object defaultCursorFrame = 50 Cursor.DEFAULT.getCurrentFrame(); 51 52 assertEquals(defaultCursorFrame, 53 new ImageCursor(null).getCurrentFrame()); 54 55 assertEquals(defaultCursorFrame, 56 new ImageCursor(TEST_ERROR_IMAGE).getCurrentFrame()); 57 } 58 59 @Test 60 public void animatedCursorTest() { 61 // reset time 62 toolkit.setAnimationTime(0); 63 final Image animatedImage = 64 TestImages.createAnimatedTestImage( 65 300, 400, // width, height 66 0, // loop count 67 2000, 1000, 3000 // frame delays 68 ); 69 final ImageCursor animatedImageCursor = new ImageCursor(animatedImage); 70 71 Object lastCursorFrame; 72 Object currCursorFrame; 73 74 animatedImageCursor.activate(); 75 76 lastCursorFrame = animatedImageCursor.getCurrentFrame(); 77 78 toolkit.setAnimationTime(1000); 79 currCursorFrame = animatedImageCursor.getCurrentFrame(); 80 assertSame(lastCursorFrame, currCursorFrame); 81 82 lastCursorFrame = currCursorFrame; 83 84 toolkit.setAnimationTime(2500); 85 currCursorFrame = animatedImageCursor.getCurrentFrame(); 86 assertNotSame(lastCursorFrame, currCursorFrame); 87 88 lastCursorFrame = currCursorFrame; 89 90 toolkit.setAnimationTime(4500); 91 currCursorFrame = animatedImageCursor.getCurrentFrame(); 92 assertNotSame(lastCursorFrame, currCursorFrame); 93 94 lastCursorFrame = currCursorFrame; 95 96 toolkit.setAnimationTime(7000); 97 currCursorFrame = animatedImageCursor.getCurrentFrame(); 98 assertNotSame(lastCursorFrame, currCursorFrame); 99 100 animatedImageCursor.deactivate(); 101 102 TestImages.disposeAnimatedImage(animatedImage); 103 } 104 105 @Test 106 public void animatedCursorCachingTest() { 107 // reset time 108 toolkit.setAnimationTime(0); 109 final Image animatedImage = 110 TestImages.createAnimatedTestImage( 111 300, 400, // width, height 112 0, // loop count 113 2000, 1000, 3000 // frame delays 114 ); 115 final ImageCursor animatedImageCursor = new ImageCursor(animatedImage); 116 117 animatedImageCursor.activate(); 118 119 toolkit.setAnimationTime(1000); 120 final Object time1000CursorFrame = 121 animatedImageCursor.getCurrentFrame(); 122 123 toolkit.setAnimationTime(2500); 124 final Object time2500CursorFrame = 125 animatedImageCursor.getCurrentFrame(); 126 127 toolkit.setAnimationTime(4500); 128 final Object time4500CursorFrame = 129 animatedImageCursor.getCurrentFrame(); 130 131 toolkit.setAnimationTime(6000 + 1000); 132 assertSame(time1000CursorFrame, 133 animatedImageCursor.getCurrentFrame()); 134 135 toolkit.setAnimationTime(6000 + 2500); 136 assertSame(time2500CursorFrame, 137 animatedImageCursor.getCurrentFrame()); 138 139 toolkit.setAnimationTime(6000 + 4500); 140 assertSame(time4500CursorFrame, 141 animatedImageCursor.getCurrentFrame()); 142 143 animatedImageCursor.deactivate(); 144 145 TestImages.disposeAnimatedImage(animatedImage); 146 } 147 }