1 30 31 package com.jgoodies.animation.tests; 32 33 import junit.framework.TestCase; 34 35 import com.jgoodies.animation.AnimationFunction; 36 import com.jgoodies.animation.AnimationFunctions; 37 38 39 45 public final class AnimationFunctionsTest extends TestCase { 46 47 private static Float FLOAT_VALUE1 = new Float (5.12f); 48 private static Float FLOAT_VALUE2 = new Float (19.67f); 49 private static Float FLOAT_VALUE3 = new Float (18.5f); 50 private static Float FLOAT_VALUE4 = new Float (19.80f); 51 52 private static float FROM_VALUE = 1.0f; 53 private static float TO_VALUE = 3.0f; 54 55 private static float[] KEY_TIMES = {0.0f, 0.25f, 0.5f, 0.75f}; 56 57 private AnimationFunction constant1; 58 private AnimationFunction constant2; 59 private AnimationFunction discrete1; 60 private AnimationFunction discrete2; 61 private AnimationFunction linear; 62 63 protected void setUp() throws Exception { 64 super.setUp(); 65 constant1 = AnimationFunctions.constant(1000, FLOAT_VALUE1); 66 constant2 = AnimationFunctions.constant(3000, FLOAT_VALUE2); 67 discrete1 = AnimationFunctions.discrete(4000, new Object []{ 68 FLOAT_VALUE1, FLOAT_VALUE2, 69 FLOAT_VALUE3, FLOAT_VALUE4}); 70 discrete2 = AnimationFunctions.discrete(4000, new Object []{ 71 FLOAT_VALUE1, FLOAT_VALUE2, 72 FLOAT_VALUE3, FLOAT_VALUE4}, 73 KEY_TIMES ); 74 linear = AnimationFunctions.fromTo(7000, FROM_VALUE, TO_VALUE); 75 } 76 77 protected void tearDown() throws Exception { 78 super.tearDown(); 79 constant1 = null; 80 constant2 = null; 81 discrete1 = null; 82 discrete2 = null; 83 linear = null; 84 } 85 86 89 public void testIllegalDuration() { 90 try { 91 AnimationFunctions.constant(-100, new Integer (42)); 92 fail("Should prevent negative durations."); 93 } catch (IllegalArgumentException e) { 94 } 96 } 97 98 101 public void testIllegalInterval() { 102 try { 103 constant1.valueAt(-100); 104 fail("#valueAt should forbid an invalid time (-100)."); 105 } catch (IllegalArgumentException e) { 106 } 108 try { 109 constant1.valueAt(-1); 110 fail("#valueAt should forbid an invalid time (-1)."); 111 } catch (IllegalArgumentException e) { 112 } 114 try { 115 constant1.valueAt(constant1.duration()); 116 fail("#valueAt should forbid an invalid time (duration)."); 117 } catch (IllegalArgumentException e) { 118 } 120 } 121 122 126 public void testConstant() { 127 long duration1 = constant1.duration(); 128 long step = duration1 / 10; 129 for (long time = 0; time < duration1; time += step) { 130 assertSame( 131 "A constant function should answer a constant value.", 132 constant1.valueAt(time), 133 FLOAT_VALUE1); 134 } 135 } 136 137 140 public void testConcat() { 141 AnimationFunction concatenated = 142 AnimationFunctions.concat(constant1, constant2); 143 144 long duration1 = constant1.duration(); 145 long duration2 = constant2.duration(); 146 long durationSum = duration1 + duration2; 147 148 assertEquals("Concat does not sum up the durations.", 149 concatenated.duration(), 150 durationSum); 151 152 long t0 = 0; 153 long t1 = duration1; 154 long t2 = durationSum - 1; 155 156 assertSame("concat.valueAt(" + (t0) + ") failed.", 157 concatenated.valueAt(t0), 158 FLOAT_VALUE1); 159 160 assertSame("concat.valueAt(" + (t1 - 1) + ") failed.", 161 concatenated.valueAt(t1 -1), 162 FLOAT_VALUE1); 163 164 assertSame("concat.valueAt(" + (t1) + ") failed.", 165 concatenated.valueAt(t1), 166 FLOAT_VALUE2); 167 168 assertSame("concat.valueAt(" + (t2) + ") failed.", 169 concatenated.valueAt(t2), 170 FLOAT_VALUE2); 171 172 try { 173 concatenated.valueAt(durationSum); 174 fail("concat.valueAt(totalDuration) is illegal."); 175 } catch (IllegalArgumentException e) { 176 } 178 } 179 180 181 185 public void testDiscrete() { 186 long duration = discrete1.duration(); 187 long intervalLength = duration /4; 188 long t0 = 0; 189 long t1 = 1 * intervalLength; 190 long t2 = 2 * intervalLength; 191 long t3 = 3 * intervalLength; 192 long t4 = duration - 1; 193 194 assertSame("discrete(" + t0 + ") failed", 195 discrete1.valueAt(t0), 196 FLOAT_VALUE1); 197 198 assertSame("discrete(" + (t1 - 1) + ") failed", 199 discrete1.valueAt(t1 - 1), 200 FLOAT_VALUE1); 201 202 assertSame("discrete(" + t1 + ") failed", 203 discrete1.valueAt(t1), 204 FLOAT_VALUE2); 205 206 assertSame("discrete(" + (t2 - 1) + ") failed", 207 discrete1.valueAt(t2 - 1), 208 FLOAT_VALUE2); 209 210 assertSame("discrete(" + t2 + ") failed", 211 discrete1.valueAt(t2), 212 FLOAT_VALUE3); 213 214 assertSame("discrete(" + (t3 - 1) + ") failed", 215 discrete1.valueAt(t3 - 1), 216 FLOAT_VALUE3); 217 218 assertSame("discrete(" + t3 + ") failed", 219 discrete1.valueAt(t3), 220 FLOAT_VALUE4); 221 222 assertSame("discrete(" + (t4) + ") failed", 223 discrete1.valueAt(t4), 224 FLOAT_VALUE4); 225 } 226 227 231 public void testDiscreteKeyTimes() { 232 long duration = discrete2.duration(); 233 long t0 = 0; 234 long t1 = (long) (duration * KEY_TIMES[1]); 235 long t2 = (long) (duration * KEY_TIMES[2]); 236 long t3 = (long) (duration * KEY_TIMES[3]); 237 long t4 = duration - 1; 238 239 assertSame("discrete(" + t0 + ") failed", 240 discrete1.valueAt(t0), 241 FLOAT_VALUE1); 242 243 assertSame("discrete(" + (t1 - 1) + ") failed", 244 discrete1.valueAt(t1 - 1), 245 FLOAT_VALUE1); 246 247 assertSame("discrete(" + t1 + ") failed", 248 discrete1.valueAt(t1), 249 FLOAT_VALUE2); 250 251 assertSame("discrete(" + (t2 - 1) + ") failed", 252 discrete1.valueAt(t2 - 1), 253 FLOAT_VALUE2); 254 255 assertSame("discrete(" + t2 + ") failed", 256 discrete1.valueAt(t2), 257 FLOAT_VALUE3); 258 259 assertSame("discrete(" + (t3 - 1) + ") failed", 260 discrete1.valueAt(t3 - 1), 261 FLOAT_VALUE3); 262 263 assertSame("discrete(" + t3 + ") failed", 264 discrete1.valueAt(t3), 265 FLOAT_VALUE4); 266 267 assertSame("discrete(" + (t4) + ") failed", 268 discrete1.valueAt(t4), 269 FLOAT_VALUE4); 270 } 271 272 276 public void testLinear() { 277 if (!linear.valueAt(0).equals(new Float (FROM_VALUE))) 278 fail("The linear function should answer the from value at t0."); 279 280 Object expected = new Float (TO_VALUE); 281 Object answered = linear.valueAt(linear.duration() - 1); 282 if (!answered.equals(expected)) 283 fail("The linear function should answer the to value at t1." + 284 "\nanswered=" + answered + 285 "\nexpected=" + expected); 286 } 287 288 } 289
| Popular Tags
|