1 /*
   2  * Copyright (c) 2011, 2016, 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 
  29 import com.sun.javafx.animation.KeyValueHelper;
  30 import com.sun.javafx.animation.KeyValueType;
  31 import javafx.animation.Interpolator;
  32 import javafx.animation.KeyValue;
  33 import static org.junit.Assert.assertEquals;
  34 import javafx.beans.property.BooleanProperty;
  35 import javafx.beans.property.DoubleProperty;
  36 import javafx.beans.property.FloatProperty;
  37 import javafx.beans.property.IntegerProperty;
  38 import javafx.beans.property.LongProperty;
  39 import javafx.beans.property.SimpleBooleanProperty;
  40 import javafx.beans.property.SimpleDoubleProperty;
  41 import javafx.beans.property.SimpleFloatProperty;
  42 import javafx.beans.property.SimpleIntegerProperty;
  43 import javafx.beans.property.SimpleLongProperty;
  44 import javafx.beans.property.SimpleStringProperty;
  45 import javafx.beans.property.StringProperty;
  46 import javafx.beans.value.WritableFloatValue;
  47 import javafx.beans.value.WritableIntegerValue;
  48 import javafx.beans.value.WritableLongValue;
  49 import javafx.beans.value.WritableValue;
  50 
  51 import org.junit.Test;
  52 
  53 public class KeyValueTest {
  54 
  55     private static final double EPSILON_DOUBLE = 1e-12;
  56     private static final float EPSILON_FLOAT = 1e-6f;
  57 
  58     private void assertKeyValue(KeyValueType type, WritableValue<?> target, Object endValue, Interpolator interpolator, KeyValue kv) {
  59         assertEquals(type, KeyValueHelper.getType(kv));
  60         assertEquals(target, kv.getTarget());
  61         assertEquals(endValue, kv.getEndValue());
  62         assertEquals(interpolator, kv.getInterpolator());
  63     }
  64 
  65     private void assertKeyValue(KeyValueType type, WritableValue<?> target, Interpolator interpolator, KeyValue kv) {
  66         assertEquals(type, KeyValueHelper.getType(kv));
  67         assertEquals(target, kv.getTarget());
  68         assertEquals(interpolator, kv.getInterpolator());
  69     }
  70 
  71 
  72 
  73     @Test
  74     public void testBooleanFactory_Interpolator() {
  75         final BooleanProperty v = new SimpleBooleanProperty();
  76         final KeyValue kv = new KeyValue(v, true, Interpolator.EASE_BOTH);
  77         assertKeyValue(KeyValueType.BOOLEAN, v, Boolean.TRUE, Interpolator.EASE_BOTH, kv);
  78     }
  79 
  80     @Test(expected=NullPointerException.class)
  81     public void testBooleanFactory_Interpolator_NullTarget() {
  82         new KeyValue(null, true, Interpolator.EASE_BOTH);
  83     }
  84 
  85     @Test(expected=NullPointerException.class)
  86     public void testBooleanFactory_Interpolator_NullInterpolator() {
  87         final BooleanProperty v = new SimpleBooleanProperty();
  88         new KeyValue(v, true, null);
  89     }
  90 
  91 
  92 
  93     @Test
  94     public void testBooleanFactory() {
  95         final BooleanProperty v = new SimpleBooleanProperty();
  96         final KeyValue kv = new KeyValue(v, true);
  97         assertKeyValue(KeyValueType.BOOLEAN, v, Boolean.TRUE, Interpolator.LINEAR, kv);
  98     }
  99 
 100     @Test(expected=NullPointerException.class)
 101     public void testBooleanFactory_NullTarget() {
 102         new KeyValue(null, true);
 103     }
 104 
 105 
 106 
 107     @Test
 108     public void testDoubleFactory_Interpolator() {
 109         final DoubleProperty v = new SimpleDoubleProperty();
 110         final KeyValue kv = new KeyValue(v, Math.PI, Interpolator.EASE_BOTH);
 111         assertKeyValue(KeyValueType.DOUBLE, v, Interpolator.EASE_BOTH, kv);
 112         assertEquals(Math.PI, ((Number)kv.getEndValue()).doubleValue(), EPSILON_DOUBLE);
 113     }
 114 
 115     @Test(expected=NullPointerException.class)
 116     public void testDoubleFactory_Interpolator_NullTarget() {
 117         new KeyValue(null, Math.PI, Interpolator.EASE_BOTH);
 118     }
 119 
 120     @Test(expected=NullPointerException.class)
 121     public void testDoubleFactory_Interpolator_NullInterpolator() {
 122         final DoubleProperty v = new SimpleDoubleProperty();
 123         new KeyValue(v, Math.PI, null);
 124     }
 125 
 126 
 127 
 128     @Test
 129     public void testDoubleFactory() {
 130         final DoubleProperty v = new SimpleDoubleProperty();
 131         final KeyValue kv = new KeyValue(v, Math.E);
 132         assertKeyValue(KeyValueType.DOUBLE, v, Interpolator.LINEAR, kv);
 133         assertEquals(Math.E, ((Number)kv.getEndValue()).doubleValue(), EPSILON_DOUBLE);
 134     }
 135 
 136     @Test(expected=NullPointerException.class)
 137     public void testDoubleFactory_NullTarget() {
 138         new KeyValue(null, Math.E);
 139     }
 140 
 141 
 142 
 143     @Test
 144     public void testFloatFactory_Interpolator() {
 145         final FloatProperty v = new SimpleFloatProperty();
 146         final KeyValue kv = new KeyValue(v, (float)Math.E, Interpolator.EASE_BOTH);
 147         assertKeyValue(KeyValueType.FLOAT, v, Interpolator.EASE_BOTH, kv);
 148         assertEquals((float)Math.E, ((Number)kv.getEndValue()).floatValue(), EPSILON_FLOAT);
 149     }
 150 
 151     @Test(expected=NullPointerException.class)
 152     public void testFloatFactory_Interpolator_NullTarget() {
 153         new KeyValue((WritableFloatValue)null, (float)Math.E, Interpolator.EASE_BOTH);
 154     }
 155 
 156     @Test(expected=NullPointerException.class)
 157     public void testFloatFactory_Interpolator_NullInterpolator() {
 158         final FloatProperty v = new SimpleFloatProperty();
 159         new KeyValue(v, (float)Math.E, null);
 160     }
 161 
 162 
 163 
 164     @Test
 165     public void testFloatFactory() {
 166         final FloatProperty v = new SimpleFloatProperty();
 167         final KeyValue kv = new KeyValue(v, (float)Math.PI);
 168         assertKeyValue(KeyValueType.FLOAT, v, Interpolator.LINEAR, kv);
 169         assertEquals((float)Math.PI, ((Number)kv.getEndValue()).floatValue(), EPSILON_FLOAT);
 170     }
 171 
 172     @Test(expected=NullPointerException.class)
 173     public void testFloatFactory_NullTarget() {
 174         new KeyValue((WritableFloatValue)null, (float)Math.PI);
 175     }
 176 
 177 
 178 
 179     @Test
 180     public void testIntegerFactory_Interpolator() {
 181         final IntegerProperty v = new SimpleIntegerProperty();
 182         final KeyValue kv = new KeyValue(v, Integer.MAX_VALUE, Interpolator.EASE_BOTH);
 183         assertKeyValue(KeyValueType.INTEGER, v, Integer.MAX_VALUE, Interpolator.EASE_BOTH, kv);
 184     }
 185 
 186     @Test(expected=NullPointerException.class)
 187     public void testIntegerFactory_Interpolator_NullTarget() {
 188         new KeyValue((WritableIntegerValue)null, 1, Interpolator.EASE_BOTH);
 189     }
 190 
 191     @Test(expected=NullPointerException.class)
 192     public void testIntegerFactory_Interpolator_NullInterpolator() {
 193         final IntegerProperty v = new SimpleIntegerProperty();
 194         new KeyValue(v, 1, null);
 195     }
 196 
 197 
 198 
 199     @Test
 200     public void testIntegerFactory() {
 201         final IntegerProperty v = new SimpleIntegerProperty();
 202         final KeyValue kv = new KeyValue(v, Integer.MIN_VALUE);
 203         assertKeyValue(KeyValueType.INTEGER, v, Integer.MIN_VALUE, Interpolator.LINEAR, kv);
 204     }
 205 
 206     @Test(expected=NullPointerException.class)
 207     public void testIntegerFactory_NullTarget() {
 208         new KeyValue((WritableIntegerValue)null, Integer.MIN_VALUE);
 209     }
 210 
 211 
 212 
 213     @Test
 214     public void testLongFactory_Interpolator() {
 215         final LongProperty v = new SimpleLongProperty();
 216         final KeyValue kv = new KeyValue(v, Long.MAX_VALUE, Interpolator.EASE_BOTH);
 217         assertKeyValue(KeyValueType.LONG, v, Long.MAX_VALUE, Interpolator.EASE_BOTH, kv);
 218     }
 219 
 220     @Test(expected=NullPointerException.class)
 221     public void testLongFactory_Interpolator_NullTarget() {
 222         new KeyValue((WritableLongValue)null, 1L, Interpolator.EASE_BOTH);
 223     }
 224 
 225     @Test(expected=NullPointerException.class)
 226     public void testLongFactory_Interpolator_NullInterpolator() {
 227         final LongProperty v = new SimpleLongProperty();
 228         new KeyValue(v, 1L, null);
 229     }
 230 
 231 
 232 
 233     @Test
 234     public void testLongFactory() {
 235         final LongProperty v = new SimpleLongProperty();
 236         final KeyValue kv = new KeyValue(v, Long.MIN_VALUE);
 237         assertKeyValue(KeyValueType.LONG, v, Long.MIN_VALUE, Interpolator.LINEAR, kv);
 238     }
 239 
 240     @Test(expected=NullPointerException.class)
 241     public void testLongFactory_NullTarget() {
 242         new KeyValue((WritableLongValue)null, Long.MIN_VALUE);
 243     }
 244 
 245 
 246 
 247     @Test
 248     public void testObjectFactory_Interpolator() {
 249         final StringProperty v = new SimpleStringProperty();
 250         final KeyValue kv = new KeyValue(v, "Hello World", Interpolator.EASE_BOTH);
 251         assertKeyValue(KeyValueType.OBJECT, v, "Hello World", Interpolator.EASE_BOTH, kv);
 252     }
 253 
 254     @Test(expected=NullPointerException.class)
 255     public void testObjectFactory_Interpolator_NullTarget() {
 256         new KeyValue(null, "Hello World", Interpolator.EASE_BOTH);
 257     }
 258 
 259     @Test(expected=NullPointerException.class)
 260     public void testObjectFactory_Interpolator_NullInterpolator() {
 261         final StringProperty v = new SimpleStringProperty();
 262         new KeyValue(v, "Hello World", null);
 263     }
 264 
 265 
 266 
 267     @Test
 268     public void testObjectFactory() {
 269         final StringProperty v = new SimpleStringProperty();
 270         final KeyValue kv = new KeyValue(v, "Goodbye World");
 271         assertKeyValue(KeyValueType.OBJECT, v, "Goodbye World", Interpolator.LINEAR, kv);
 272     }
 273 
 274     @Test(expected=NullPointerException.class)
 275     public void testObjectFactory_NullTarget() {
 276         new KeyValue(null, "Goodbye World");
 277     }
 278 
 279 }