001: /*
002: * Copyright (c) 2001-2006 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.animation.tests;
032:
033: import junit.framework.TestCase;
034:
035: import com.jgoodies.animation.AnimationFunction;
036: import com.jgoodies.animation.AnimationFunctions;
037:
038: /**
039: * A test case for class {@link AnimationFunctions}.
040: *
041: * @author Karsten Lentzsch
042: * @version $Revision: 1.6 $
043: */
044: public final class AnimationFunctionsTest extends TestCase {
045:
046: private static Float FLOAT_VALUE1 = new Float(5.12f);
047: private static Float FLOAT_VALUE2 = new Float(19.67f);
048: private static Float FLOAT_VALUE3 = new Float(18.5f);
049: private static Float FLOAT_VALUE4 = new Float(19.80f);
050:
051: private static float FROM_VALUE = 1.0f;
052: private static float TO_VALUE = 3.0f;
053:
054: private static float[] KEY_TIMES = { 0.0f, 0.25f, 0.5f, 0.75f };
055:
056: private AnimationFunction constant1;
057: private AnimationFunction constant2;
058: private AnimationFunction discrete1;
059: private AnimationFunction discrete2;
060: private AnimationFunction linear;
061:
062: protected void setUp() throws Exception {
063: super .setUp();
064: constant1 = AnimationFunctions.constant(1000, FLOAT_VALUE1);
065: constant2 = AnimationFunctions.constant(3000, FLOAT_VALUE2);
066: discrete1 = AnimationFunctions.discrete(4000,
067: new Object[] { FLOAT_VALUE1, FLOAT_VALUE2,
068: FLOAT_VALUE3, FLOAT_VALUE4 });
069: discrete2 = AnimationFunctions.discrete(4000,
070: new Object[] { FLOAT_VALUE1, FLOAT_VALUE2,
071: FLOAT_VALUE3, FLOAT_VALUE4 }, KEY_TIMES);
072: linear = AnimationFunctions.fromTo(7000, FROM_VALUE, TO_VALUE);
073: }
074:
075: protected void tearDown() throws Exception {
076: super .tearDown();
077: constant1 = null;
078: constant2 = null;
079: discrete1 = null;
080: discrete2 = null;
081: linear = null;
082: }
083:
084: /**
085: * Checks if negative durations throw an IllegalArgumentException.
086: */
087: public void testIllegalDuration() {
088: try {
089: AnimationFunctions.constant(-100, new Integer(42));
090: fail("Should prevent negative durations.");
091: } catch (IllegalArgumentException e) {
092: // The expected behavior
093: }
094: }
095:
096: /**
097: * Checks if value request outside the duration throw an IllegalArgumentException.
098: */
099: public void testIllegalInterval() {
100: try {
101: constant1.valueAt(-100);
102: fail("#valueAt should forbid an invalid time (-100).");
103: } catch (IllegalArgumentException e) {
104: // The expected behavior.
105: }
106: try {
107: constant1.valueAt(-1);
108: fail("#valueAt should forbid an invalid time (-1).");
109: } catch (IllegalArgumentException e) {
110: // The expected behavior.
111: }
112: try {
113: constant1.valueAt(constant1.duration());
114: fail("#valueAt should forbid an invalid time (duration).");
115: } catch (IllegalArgumentException e) {
116: // The expected behavior.
117: }
118: }
119:
120: /**
121: * Checks that #constant answers an AnimationFunction that in turn
122: * answers a sole value over the whole time interval.
123: */
124: public void testConstant() {
125: long duration1 = constant1.duration();
126: long step = duration1 / 10;
127: for (long time = 0; time < duration1; time += step) {
128: assertSame(
129: "A constant function should answer a constant value.",
130: constant1.valueAt(time), FLOAT_VALUE1);
131: }
132: }
133:
134: /**
135: * Checks that #concat sums up the durations and answers the related values.
136: */
137: public void testConcat() {
138: AnimationFunction concatenated = AnimationFunctions.concat(
139: constant1, constant2);
140:
141: long duration1 = constant1.duration();
142: long duration2 = constant2.duration();
143: long durationSum = duration1 + duration2;
144:
145: assertEquals("Concat does not sum up the durations.",
146: concatenated.duration(), durationSum);
147:
148: long t0 = 0;
149: long t1 = duration1;
150: long t2 = durationSum - 1;
151:
152: assertSame("concat.valueAt(" + (t0) + ") failed.", concatenated
153: .valueAt(t0), FLOAT_VALUE1);
154:
155: assertSame("concat.valueAt(" + (t1 - 1) + ") failed.",
156: concatenated.valueAt(t1 - 1), FLOAT_VALUE1);
157:
158: assertSame("concat.valueAt(" + (t1) + ") failed.", concatenated
159: .valueAt(t1), FLOAT_VALUE2);
160:
161: assertSame("concat.valueAt(" + (t2) + ") failed.", concatenated
162: .valueAt(t2), FLOAT_VALUE2);
163:
164: try {
165: concatenated.valueAt(durationSum);
166: fail("concat.valueAt(totalDuration) is illegal.");
167: } catch (IllegalArgumentException e) {
168: // The expected behavior.
169: }
170: }
171:
172: /**
173: * Checks that a discrete animation function answers the correct values
174: * over the duration.
175: */
176: public void testDiscrete() {
177: long duration = discrete1.duration();
178: long intervalLength = duration / 4;
179: long t0 = 0;
180: long t1 = 1 * intervalLength;
181: long t2 = 2 * intervalLength;
182: long t3 = 3 * intervalLength;
183: long t4 = duration - 1;
184:
185: assertSame("discrete(" + t0 + ") failed",
186: discrete1.valueAt(t0), FLOAT_VALUE1);
187:
188: assertSame("discrete(" + (t1 - 1) + ") failed", discrete1
189: .valueAt(t1 - 1), FLOAT_VALUE1);
190:
191: assertSame("discrete(" + t1 + ") failed",
192: discrete1.valueAt(t1), FLOAT_VALUE2);
193:
194: assertSame("discrete(" + (t2 - 1) + ") failed", discrete1
195: .valueAt(t2 - 1), FLOAT_VALUE2);
196:
197: assertSame("discrete(" + t2 + ") failed",
198: discrete1.valueAt(t2), FLOAT_VALUE3);
199:
200: assertSame("discrete(" + (t3 - 1) + ") failed", discrete1
201: .valueAt(t3 - 1), FLOAT_VALUE3);
202:
203: assertSame("discrete(" + t3 + ") failed",
204: discrete1.valueAt(t3), FLOAT_VALUE4);
205:
206: assertSame("discrete(" + (t4) + ") failed", discrete1
207: .valueAt(t4), FLOAT_VALUE4);
208: }
209:
210: /**
211: * Checks that a discrete animation function answers the correct values
212: * over the duration.
213: */
214: public void testDiscreteKeyTimes() {
215: long duration = discrete2.duration();
216: long t0 = 0;
217: long t1 = (long) (duration * KEY_TIMES[1]);
218: long t2 = (long) (duration * KEY_TIMES[2]);
219: long t3 = (long) (duration * KEY_TIMES[3]);
220: long t4 = duration - 1;
221:
222: assertSame("discrete(" + t0 + ") failed",
223: discrete1.valueAt(t0), FLOAT_VALUE1);
224:
225: assertSame("discrete(" + (t1 - 1) + ") failed", discrete1
226: .valueAt(t1 - 1), FLOAT_VALUE1);
227:
228: assertSame("discrete(" + t1 + ") failed",
229: discrete1.valueAt(t1), FLOAT_VALUE2);
230:
231: assertSame("discrete(" + (t2 - 1) + ") failed", discrete1
232: .valueAt(t2 - 1), FLOAT_VALUE2);
233:
234: assertSame("discrete(" + t2 + ") failed",
235: discrete1.valueAt(t2), FLOAT_VALUE3);
236:
237: assertSame("discrete(" + (t3 - 1) + ") failed", discrete1
238: .valueAt(t3 - 1), FLOAT_VALUE3);
239:
240: assertSame("discrete(" + t3 + ") failed",
241: discrete1.valueAt(t3), FLOAT_VALUE4);
242:
243: assertSame("discrete(" + (t4) + ") failed", discrete1
244: .valueAt(t4), FLOAT_VALUE4);
245: }
246:
247: /**
248: * Checks that a linear animation function answers the correct values
249: * over the duration.
250: */
251: public void testLinear() {
252: if (!linear.valueAt(0).equals(new Float(FROM_VALUE)))
253: fail("The linear function should answer the from value at t0.");
254:
255: Object expected = new Float(TO_VALUE);
256: Object answered = linear.valueAt(linear.duration() - 1);
257: if (!answered.equals(expected))
258: fail("The linear function should answer the to value at t1."
259: + "\nanswered="
260: + answered
261: + "\nexpected="
262: + expected);
263: }
264:
265: }
|