1 /*
   2  * Copyright (c) 2011, 2013, 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;
  27 
  28 
  29 import javafx.animation.Interpolator;
  30 import static org.junit.Assert.*;
  31 import static javafx.util.Duration.*;
  32 
  33 import org.junit.Before;
  34 import org.junit.Test;
  35 
  36 public class NumberTangentInterpolatorTest {
  37         
  38         private static final double EPSILON_DOUBLE = 1e-12;
  39         private static final double EPSILON_FLOAT = 1e-6;
  40 
  41         private NumberTangentInterpolator interpolator;
  42         
  43         @Before
  44         public void setUp() throws Exception {
  45                 interpolator = new NumberTangentInterpolator(ZERO, 0);
  46         }
  47         
  48         @Test
  49         public void testCreate() {
  50                 final NumberTangentInterpolator interpolator1 = new NumberTangentInterpolator(millis(2000), Math.PI);
  51                 assertEquals(Math.PI, interpolator1.getInValue(), EPSILON_DOUBLE);
  52                 assertEquals(12000.0, interpolator1.getInTicks(), EPSILON_DOUBLE);
  53                 assertEquals(Math.PI, interpolator1.getOutValue(), EPSILON_DOUBLE);
  54                 assertEquals(12000.0, interpolator1.getOutTicks(), EPSILON_DOUBLE);
  55 
  56                 final NumberTangentInterpolator interpolator2 = new NumberTangentInterpolator(millis(500), Math.E, millis(1000), -Math.PI);
  57                 assertEquals(Math.E, interpolator2.getInValue(), EPSILON_DOUBLE);
  58                 assertEquals(3000.0, interpolator2.getInTicks(), EPSILON_DOUBLE);
  59                 assertEquals(-Math.PI, interpolator2.getOutValue(), EPSILON_DOUBLE);
  60                 assertEquals(6000.0, interpolator2.getOutTicks(), EPSILON_DOUBLE);
  61         }
  62         
  63         @Test
  64         public void testInterpolate_boolean() {
  65                 assertEquals(false, interpolator.interpolate(false, true, 0.0));
  66                 assertEquals(false, interpolator.interpolate(false, true, 0.5));
  67                 assertEquals(true, interpolator.interpolate(false, true, 1.0));
  68         }
  69 
  70         @Test
  71         public void testInterpolate_double() {
  72                 assertEquals( 0.0, interpolator.interpolate(0.0, 10.0, 0.0), EPSILON_DOUBLE);
  73                 assertEquals( 5.0, interpolator.interpolate(0.0, 10.0, 0.5), EPSILON_DOUBLE);
  74                 assertEquals(10.0, interpolator.interpolate(0.0, 10.0, 1.0), EPSILON_DOUBLE);
  75         }
  76 
  77         @Test
  78         public void testInterpolate_int() {
  79                 assertEquals( 0, interpolator.interpolate(0, 10, 0.0));
  80                 assertEquals( 5, interpolator.interpolate(0, 10, 0.5));
  81                 assertEquals(10, interpolator.interpolate(0, 10, 1.0));
  82         }
  83 
  84         @Test
  85         public void testInterpolate_long() {
  86                 assertEquals( 0L, interpolator.interpolate(0L, 10L, 0.0));
  87                 assertEquals( 5L, interpolator.interpolate(0L, 10L, 0.5));
  88                 assertEquals(10L, interpolator.interpolate(0L, 10L, 1.0));
  89         }
  90 
  91         @Test
  92         public void testInterpolate_float() {
  93                 assertEquals( 0.0f, interpolator.interpolate(0.0f, 10.0f, 0.0), EPSILON_FLOAT);
  94                 assertEquals( 5.0f, interpolator.interpolate(0.0f, 10.0f, 0.5), EPSILON_FLOAT);
  95                 assertEquals(10.0f, interpolator.interpolate(0.0f, 10.0f, 1.0), EPSILON_FLOAT);
  96         }
  97 
  98         @Test
  99         public void testInterpolate_Object() {
 100                 assertEquals("Hello World", interpolator.interpolate("Hello World", "Goodbye World", 0.0));
 101                 assertEquals("Hello World", interpolator.interpolate("Hello World", "Goodbye World", 0.5));
 102                 assertEquals("Goodbye World", interpolator.interpolate("Hello World", "Goodbye World", 1.0));
 103         }
 104 
 105         @Test
 106         public void testInterpolate_Number() {
 107                 assertEquals(Integer.valueOf( 0), interpolator.interpolate(Integer.valueOf(0), Integer.valueOf(10), 0.0));
 108                 assertEquals(Integer.valueOf( 5), interpolator.interpolate(Integer.valueOf(0), Integer.valueOf(10), 0.5));
 109                 assertEquals(Integer.valueOf(10), interpolator.interpolate(Integer.valueOf(0), Integer.valueOf(10), 1.0));
 110         }
 111 
 112         private static void testEqualsAndHashCode(Interpolator one, Interpolator another) {
 113                 assertTrue(one.equals(another));
 114                 assertTrue(another.equals(one));
 115                 assertEquals(one.hashCode(), another.hashCode());
 116         }
 117 
 118         private static void testNotEqualsAndHashCode(Interpolator one, Interpolator another) {
 119                 assertFalse(one.equals(another));
 120                 assertFalse(another.equals(one));
 121                 assertFalse(one.hashCode() == another.hashCode());
 122         }
 123 
 124         @Test public void testEqualsAndHashCodeShort() {
 125                 testEqualsAndHashCode(interpolator, new NumberTangentInterpolator(ZERO, 0));
 126         }
 127 
 128         @Test public void testNotEqualsAndHashCodeShort() {
 129                 testNotEqualsAndHashCode(interpolator, new NumberTangentInterpolator(millis(500), 0));
 130                 testNotEqualsAndHashCode(interpolator, new NumberTangentInterpolator(ZERO, 1));
 131         }
 132 
 133         @Test public void testEqualsAndHashCode() {
 134                 Interpolator one = new NumberTangentInterpolator(millis(500), Math.E, millis(1000), -Math.PI);
 135                 Interpolator another = new NumberTangentInterpolator(millis(500), Math.E, millis(1000), -Math.PI);
 136                 testEqualsAndHashCode(one, another);
 137         }
 138 
 139         @Test public void testNotEqualsAndHashCode() {
 140                 Interpolator one = new NumberTangentInterpolator(millis(500), Math.E, millis(1000), -Math.PI);
 141                 Interpolator another = new NumberTangentInterpolator(millis(500), Math.E, millis(1000), Math.PI);
 142                 testNotEqualsAndHashCode(one, another);
 143 
 144                 another = new NumberTangentInterpolator(millis(500), Math.E, millis(800), -Math.PI);
 145                 testNotEqualsAndHashCode(one, another);
 146 
 147                 another = new NumberTangentInterpolator(millis(500), Math.PI, millis(1000), -Math.PI);
 148                 testNotEqualsAndHashCode(one, another);
 149 
 150                 another = new NumberTangentInterpolator(millis(100), Math.E, millis(1000), -Math.PI);
 151                 testNotEqualsAndHashCode(one, another);
 152         }
 153 
 154         @Test public void testEqualsAndHashCodeFullToShort() {
 155                 NumberTangentInterpolator sh = new NumberTangentInterpolator(millis(200), 105);
 156                 NumberTangentInterpolator full = new NumberTangentInterpolator(millis(200), 105, millis(200), 105);
 157                 testEqualsAndHashCode(sh, full);
 158         }
 159         
 160         @Test public void testNotEqualsAndHashCodeFullToShort() {
 161                 NumberTangentInterpolator sh = new NumberTangentInterpolator(millis(200), 105);
 162                 Interpolator full = new NumberTangentInterpolator(millis(500), 105, millis(500), 105);
 163                 testNotEqualsAndHashCode(sh, full);
 164 
 165                 full = new NumberTangentInterpolator(millis(200), 115, millis(200), 115);
 166                 testNotEqualsAndHashCode(sh, full);
 167 
 168                 full = new NumberTangentInterpolator(millis(200), 105, millis(200), 100);
 169                 testNotEqualsAndHashCode(sh, full);
 170 
 171                 full = new NumberTangentInterpolator(millis(200), 105, millis(205), 105);
 172                 testNotEqualsAndHashCode(sh, full);
 173 
 174                 full = new NumberTangentInterpolator(millis(200), 106, millis(200), 105);
 175                 testNotEqualsAndHashCode(sh, full);
 176 
 177                 full = new NumberTangentInterpolator(millis(210), 105, millis(200), 105);
 178                 testNotEqualsAndHashCode(sh, full);
 179         }
 180 }