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 
  29 import static org.junit.Assert.assertEquals;
  30 import static org.junit.Assert.assertNull;
  31 import static org.junit.Assert.assertTrue;
  32 
  33 import java.util.Arrays;
  34 import java.util.Collection;
  35 import java.util.List;
  36 import java.util.Set;
  37 
  38 import javafx.beans.property.IntegerProperty;
  39 import javafx.beans.property.SimpleIntegerProperty;
  40 import javafx.event.ActionEvent;
  41 import javafx.event.EventHandler;
  42 import javafx.util.Duration;
  43 
  44 import org.junit.Test;
  45 
  46 public class KeyFrameTest {
  47     private static final Duration TIME = Duration.ONE;
  48     private static final KeyValue[] NO_KEYVALUES = new KeyValue[0];
  49     private static final List<KeyValue> NO_KEYVALUES_COL = Arrays.asList(NO_KEYVALUES);
  50     private static final String NAME = "KeyFrameName";
  51     private static final EventHandler<ActionEvent> ACTION = event -> {};
  52 
  53     private final IntegerProperty TARGET = new SimpleIntegerProperty();
  54     private final KeyValue[] ONE_KEYVALUE = new KeyValue[] {
  55                 new KeyValue(TARGET, 1)
  56     };
  57     private final KeyValue[] TWO_KEYVALUES = new KeyValue[] {
  58                 new KeyValue(TARGET, 0), 
  59                 new KeyValue(TARGET, 1)
  60     };
  61     private final List<KeyValue> ONE_KEYVALUE_COL = Arrays.asList(ONE_KEYVALUE);
  62     private final List<KeyValue> TWO_KEYVALUES_COL = Arrays.asList(TWO_KEYVALUES);
  63     
  64     private <T> void assertSetEquals(T[] expected, Set<T> result) {
  65         assertEquals(expected.length, result.size());
  66         for (final T element : expected) {
  67                 assertTrue(result.contains(element));
  68         }
  69     }
  70 
  71     @Test public void testConstructor_ObservableList() {
  72         // test no values
  73         final KeyFrame kf0 = new KeyFrame(TIME, NAME, ACTION, NO_KEYVALUES_COL);
  74         assertEquals(TIME, kf0.getTime());
  75         assertEquals(NAME, kf0.getName());
  76         assertEquals(ACTION, kf0.getOnFinished());
  77         assertSetEquals(NO_KEYVALUES, kf0.getValues());
  78         
  79         // test two values
  80         final KeyFrame kf1 = new KeyFrame(TIME, NAME, ACTION, ONE_KEYVALUE_COL);
  81         assertEquals(TIME, kf1.getTime());
  82         assertEquals(NAME, kf1.getName());
  83         assertEquals(ACTION, kf1.getOnFinished());
  84         assertSetEquals(ONE_KEYVALUE, kf1.getValues());
  85         
  86         // test two values
  87         final KeyFrame kf2 = new KeyFrame(TIME, NAME, ACTION, TWO_KEYVALUES_COL);
  88         assertEquals(TIME, kf2.getTime());
  89         assertEquals(NAME, kf2.getName());
  90         assertEquals(ACTION, kf2.getOnFinished());
  91         assertSetEquals(TWO_KEYVALUES, kf2.getValues());
  92     }
  93         
  94     @Test public void testConstructor_ObservableList_SpecialCases() {
  95         // name is null
  96         final KeyFrame kf1 = new KeyFrame(TIME, null, ACTION, TWO_KEYVALUES_COL);
  97         assertEquals(TIME, kf1.getTime());
  98         assertNull(kf1.getName());
  99         assertEquals(ACTION, kf1.getOnFinished());
 100         assertSetEquals(TWO_KEYVALUES, kf1.getValues());
 101         
 102         // action is null
 103         final KeyFrame kf2 = new KeyFrame(TIME, NAME, null, TWO_KEYVALUES_COL);
 104         assertEquals(TIME, kf2.getTime());
 105         assertEquals(NAME, kf2.getName());
 106         assertNull(kf2.getOnFinished());
 107         assertSetEquals(TWO_KEYVALUES, kf2.getValues());
 108         
 109         // observableArrayList is null
 110         final KeyFrame kf3 = new KeyFrame(TIME, NAME, ACTION, (Collection<KeyValue>)null);
 111         assertEquals(TIME, kf3.getTime());
 112         assertEquals(NAME, kf3.getName());
 113         assertEquals(ACTION, kf3.getOnFinished());
 114         assertSetEquals(NO_KEYVALUES, kf3.getValues());
 115         
 116         // empty observableArrayList
 117         final KeyFrame kf4 = new KeyFrame(TIME, NAME, ACTION, (Collection<KeyValue>)null);
 118         assertEquals(TIME, kf4.getTime());
 119         assertEquals(NAME, kf4.getName());
 120         assertEquals(ACTION, kf4.getOnFinished());
 121         assertSetEquals(NO_KEYVALUES, kf4.getValues());
 122         
 123         // a value is null
 124         final KeyFrame kf5 = new KeyFrame(TIME, NAME, ACTION, Arrays.asList(TWO_KEYVALUES[0], null, TWO_KEYVALUES[1]));
 125         assertEquals(TIME, kf5.getTime());
 126         assertEquals(NAME, kf5.getName());
 127         assertEquals(ACTION, kf5.getOnFinished());
 128         assertSetEquals(TWO_KEYVALUES, kf2.getValues());
 129     };
 130     
 131     @Test(expected=NullPointerException.class)
 132     public void testConstructor_ObservableList_DurationIsNull(){
 133         new KeyFrame(null, NAME, ACTION, TWO_KEYVALUES_COL);
 134     };
 135     
 136     @Test(expected=IllegalArgumentException.class)
 137     public void testConstructor_ObservableList_DurationIsNegative(){
 138         new KeyFrame(Duration.millis(-1), NAME, ACTION, TWO_KEYVALUES_COL);
 139     };
 140     
 141     @Test(expected=IllegalArgumentException.class)
 142     public void testConstructor_ObservableList_DuratinIsUnknown(){
 143         new KeyFrame(Duration.UNKNOWN, NAME, ACTION, TWO_KEYVALUES_COL);
 144     };
 145     
 146     @Test public void testConstructor_Time_Name_Action_Valus() {
 147         // no values
 148         final KeyFrame kf0 = new KeyFrame(TIME, NAME, ACTION, NO_KEYVALUES);
 149         assertEquals(TIME, kf0.getTime());
 150         assertEquals(NAME, kf0.getName());
 151         assertEquals(ACTION, kf0.getOnFinished());
 152         assertSetEquals(NO_KEYVALUES, kf0.getValues());
 153         
 154         // one value
 155         final KeyFrame kf1 = new KeyFrame(TIME, NAME, ACTION, ONE_KEYVALUE);
 156         assertEquals(TIME, kf1.getTime());
 157         assertEquals(NAME, kf1.getName());
 158         assertEquals(ACTION, kf1.getOnFinished());
 159         assertSetEquals(ONE_KEYVALUE, kf1.getValues());
 160         
 161         // two values
 162         final KeyFrame kf2 = new KeyFrame(TIME, NAME, ACTION, TWO_KEYVALUES);
 163         assertEquals(TIME, kf2.getTime());
 164         assertEquals(NAME, kf2.getName());
 165         assertEquals(ACTION, kf2.getOnFinished());
 166         assertSetEquals(TWO_KEYVALUES, kf2.getValues());
 167     }
 168         
 169     @Test public void testConstructor_Time_Name_Action_Valus_SpecialCases() {
 170         // name is null
 171         final KeyFrame kf1 = new KeyFrame(TIME, null, ACTION, TWO_KEYVALUES);
 172         assertEquals(TIME, kf1.getTime());
 173         assertNull(kf1.getName());
 174         assertEquals(ACTION, kf1.getOnFinished());
 175         assertSetEquals(TWO_KEYVALUES, kf1.getValues());
 176         
 177         // action is null
 178         final KeyFrame kf2 = new KeyFrame(TIME, NAME, null, TWO_KEYVALUES);
 179         assertEquals(TIME, kf2.getTime());
 180         assertEquals(NAME, kf2.getName());
 181         assertNull(kf2.getOnFinished());
 182         assertSetEquals(TWO_KEYVALUES, kf2.getValues());
 183         
 184         // values are empty
 185         final KeyFrame kf3 = new KeyFrame(TIME, NAME, ACTION, new KeyValue[0]);
 186         assertEquals(TIME, kf3.getTime());
 187         assertEquals(NAME, kf3.getName());
 188         assertEquals(ACTION, kf3.getOnFinished());
 189         assertSetEquals(NO_KEYVALUES, kf3.getValues());
 190         
 191         // values are null
 192         final KeyFrame kf4 = new KeyFrame(TIME, NAME, ACTION, (KeyValue[])null);
 193         assertEquals(TIME, kf4.getTime());
 194         assertEquals(NAME, kf4.getName());
 195         assertEquals(ACTION, kf4.getOnFinished());
 196         assertSetEquals(NO_KEYVALUES, kf4.getValues());
 197         
 198         // a value is null
 199         final KeyFrame kf5 = new KeyFrame(TIME, NAME, ACTION, TWO_KEYVALUES[0], null, TWO_KEYVALUES[1]);
 200         assertEquals(TIME, kf5.getTime());
 201         assertEquals(NAME, kf5.getName());
 202         assertEquals(ACTION, kf5.getOnFinished());
 203         assertSetEquals(TWO_KEYVALUES, kf5.getValues());
 204         
 205         // only value is null
 206         final KeyFrame kf6 = new KeyFrame(TIME, NAME, ACTION, (KeyValue)null);
 207         assertEquals(TIME, kf6.getTime());
 208         assertEquals(NAME, kf6.getName());
 209         assertEquals(ACTION, kf6.getOnFinished());
 210         assertSetEquals(NO_KEYVALUES, kf6.getValues());
 211     }
 212 
 213     @Test(expected=NullPointerException.class)
 214     public void testConstructor_KeyValues_DurationIsNull(){
 215         new KeyFrame(null, NAME, ACTION, TWO_KEYVALUES);
 216     };
 217     
 218     @Test(expected=IllegalArgumentException.class)
 219     public void testConstructor_KeyValues_DurationIsNegative(){
 220         new KeyFrame(Duration.millis(-1), NAME, ACTION, TWO_KEYVALUES);
 221     };
 222     
 223     @Test(expected=IllegalArgumentException.class)
 224     public void testConstructor_KeyValues_DurationIsUnknown(){
 225         new KeyFrame(Duration.UNKNOWN, NAME, ACTION, TWO_KEYVALUES);
 226     };
 227     
 228     @Test public void testConstructor_Time_Action_Valus() {
 229         final KeyFrame kf0 = new KeyFrame(TIME, ACTION, TWO_KEYVALUES);
 230         assertEquals(TIME, kf0.getTime());
 231         assertNull(kf0.getName());
 232         assertEquals(ACTION, kf0.getOnFinished());
 233         assertSetEquals(TWO_KEYVALUES, kf0.getValues());
 234     }
 235 
 236     @Test public void testConstructor_Time_Name_Valus() {
 237         final KeyFrame kf0 = new KeyFrame(TIME, NAME, TWO_KEYVALUES);
 238         assertEquals(TIME, kf0.getTime());
 239         assertEquals(NAME, kf0.getName());
 240         assertNull(kf0.getOnFinished());
 241         assertSetEquals(TWO_KEYVALUES, kf0.getValues());
 242     }
 243 
 244     @Test public void testConstructor_Time_Valus() {
 245         final KeyFrame kf0 = new KeyFrame(TIME, TWO_KEYVALUES);
 246         assertEquals(TIME, kf0.getTime());
 247         assertNull(kf0.getName());
 248         assertNull(kf0.getOnFinished());
 249         assertSetEquals(TWO_KEYVALUES, kf0.getValues());
 250     }
 251 }