KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > animation > tests > AnimationFunctionsTest


1 /*
2  * Copyright (c) 2001-2004 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

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 /**
40  * A test case for class {@link AnimationFunctions}.
41  *
42  * @author Karsten Lentzsch
43  * @version $Revision: 1.4 $
44  */

45 public final class AnimationFunctionsTest extends TestCase {
46     
47     private static Float JavaDoc FLOAT_VALUE1 = new Float JavaDoc(5.12f);
48     private static Float JavaDoc FLOAT_VALUE2 = new Float JavaDoc(19.67f);
49     private static Float JavaDoc FLOAT_VALUE3 = new Float JavaDoc(18.5f);
50     private static Float JavaDoc FLOAT_VALUE4 = new Float JavaDoc(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 JavaDoc {
64         super.setUp();
65         constant1 = AnimationFunctions.constant(1000, FLOAT_VALUE1);
66         constant2 = AnimationFunctions.constant(3000, FLOAT_VALUE2);
67         discrete1 = AnimationFunctions.discrete(4000, new Object JavaDoc[]{
68                         FLOAT_VALUE1, FLOAT_VALUE2,
69                         FLOAT_VALUE3, FLOAT_VALUE4});
70         discrete2 = AnimationFunctions.discrete(4000, new Object JavaDoc[]{
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 JavaDoc {
78         super.tearDown();
79         constant1 = null;
80         constant2 = null;
81         discrete1 = null;
82         discrete2 = null;
83         linear = null;
84     }
85     
86     /**
87      * Checks if negative durations throw an IllegalArgumentException.
88      */

89     public void testIllegalDuration() {
90         try {
91             AnimationFunctions.constant(-100, new Integer JavaDoc(42));
92             fail("Should prevent negative durations.");
93         } catch (IllegalArgumentException JavaDoc e) {
94             // The expected behavior
95
}
96     }
97
98     /**
99      * Checks if value request outside the duration throw an IllegalArgumentException.
100      */

101     public void testIllegalInterval() {
102         try {
103             constant1.valueAt(-100);
104             fail("#valueAt should forbid an invalid time (-100).");
105         } catch (IllegalArgumentException JavaDoc e) {
106             // The expected behavior.
107
}
108         try {
109             constant1.valueAt(-1);
110             fail("#valueAt should forbid an invalid time (-1).");
111         } catch (IllegalArgumentException JavaDoc e) {
112             // The expected behavior.
113
}
114         try {
115             constant1.valueAt(constant1.duration());
116             fail("#valueAt should forbid an invalid time (duration).");
117         } catch (IllegalArgumentException JavaDoc e) {
118             // The expected behavior.
119
}
120     }
121
122     /**
123      * Checks that #constant answers an AnimationFunction that in turn
124      * answers a sole value over the whole time interval.
125      */

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     /**
138      * Checks that #concat sums up the durations and answers the related values.
139      */

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 JavaDoc e) {
176             // The expected behavior.
177
}
178     }
179
180
181     /**
182      * Checks that a discrete animation function answers the correct values
183      * over the duration.
184      */

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     /**
228      * Checks that a discrete animation function answers the correct values
229      * over the duration.
230      */

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     /**
273      * Checks that a linear animation function answers the correct values
274      * over the duration.
275      */

276     public void testLinear() {
277         if (!linear.valueAt(0).equals(new Float JavaDoc(FROM_VALUE)))
278             fail("The linear function should answer the from value at t0.");
279
280         Object JavaDoc expected = new Float JavaDoc(TO_VALUE);
281         Object JavaDoc 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