1package com.xtremelabs.robolectric.shadows;
2
3import android.R;
4import android.view.animation.Animation;
5import android.view.animation.LinearInterpolator;
6import android.view.animation.Transformation;
7import com.xtremelabs.robolectric.WithTestDefaultsRunner;
8import com.xtremelabs.robolectric.util.TestAnimationListener;
9import org.junit.Before;
10import org.junit.Test;
11import org.junit.runner.RunWith;
12
13import static com.xtremelabs.robolectric.Robolectric.shadowOf;
14import static org.hamcrest.CoreMatchers.*;
15import static org.junit.Assert.assertThat;
16
17@RunWith(WithTestDefaultsRunner.class)
18public class AnimationTest {
19
20	private TestAnimation animation;
21	private ShadowAnimation shadow;
22	private TestAnimationListener listener;
23
24	@Before
25	public void setUp() throws Exception {
26		animation = new TestAnimation();
27		shadow = shadowOf(animation);
28		listener = new TestAnimationListener();
29		animation.setAnimationListener(listener);
30	}
31
32	@Test
33	public void startShouldInvokeStartCallback() throws Exception {
34		assertThat(listener.wasStartCalled, equalTo(false));
35		animation.start();
36		assertThat(listener.wasStartCalled, equalTo(true));
37		assertThat(listener.wasEndCalled, equalTo(false));
38		assertThat(listener.wasRepeatCalled, equalTo(false));
39	}
40
41	@Test
42	public void cancelShouldInvokeEndCallback() throws Exception {
43		assertThat(listener.wasEndCalled, equalTo(false));
44		animation.cancel();
45		assertThat(listener.wasStartCalled, equalTo(false));
46		assertThat(listener.wasEndCalled, equalTo(true));
47		assertThat(listener.wasRepeatCalled, equalTo(false));
48	}
49
50	@Test
51	public void invokeRepeatShouldInvokeRepeatCallback() throws Exception {
52		assertThat(listener.wasRepeatCalled, equalTo(false));
53		shadow.invokeRepeat();
54		assertThat(listener.wasStartCalled, equalTo(false));
55		assertThat(listener.wasEndCalled, equalTo(false));
56		assertThat(listener.wasRepeatCalled, equalTo(true));
57	}
58
59	@Test
60	public void invokeEndShouldInvokeEndCallback() throws Exception {
61		assertThat(listener.wasEndCalled, equalTo(false));
62		shadow.invokeEnd();
63		assertThat(listener.wasStartCalled, equalTo(false));
64		assertThat(listener.wasEndCalled, equalTo(true));
65		assertThat(listener.wasRepeatCalled, equalTo(false));
66	}
67
68    @Test
69	public void simulateAnimationEndShouldInvokeApplyTransformationWith1() throws Exception {
70		assertThat(animation.interpolatedTime, equalTo(0f));
71		shadow.invokeEnd();
72        assertThat(animation.interpolatedTime, equalTo(1f));
73	}
74
75	@Test
76	public void testHasStarted() throws Exception {
77		assertThat(animation.hasStarted(), equalTo(false));
78		animation.start();
79		assertThat(animation.hasStarted(), equalTo(true));
80		animation.cancel();
81		assertThat(animation.hasStarted(), equalTo(false));
82	}
83
84	@Test
85	public void testDuration() throws Exception {
86		assertThat(animation.getDuration(), not(equalTo(1000l)));
87		animation.setDuration(1000);
88		assertThat(animation.getDuration(), equalTo(1000l));
89	}
90
91	@Test
92	public void testInterpolation() throws Exception {
93		assertThat(animation.getInterpolator(), nullValue());
94		LinearInterpolator i = new LinearInterpolator();
95		animation.setInterpolator(i);
96		assertThat((LinearInterpolator)animation.getInterpolator(), sameInstance(i));
97	}
98
99    @Test
100    public void testRepeatCount() throws Exception {
101        assertThat(animation.getRepeatCount(), not(equalTo(5)));
102        animation.setRepeatCount(5);
103        assertThat(animation.getRepeatCount(), equalTo(5));
104    }
105
106    @Test
107    public void testRepeatMode() throws Exception {
108        assertThat(animation.getRepeatMode(), not(equalTo(Animation.REVERSE)));
109        animation.setRepeatMode(Animation.REVERSE);
110        assertThat(animation.getRepeatMode(), equalTo(Animation.REVERSE));
111    }
112
113    @Test
114    public void testStartOffset() throws Exception {
115        assertThat(animation.getStartOffset(), not(equalTo(500l)));
116        animation.setStartOffset(500l);
117        assertThat(animation.getStartOffset(), equalTo(500l));
118    }
119
120    @Test(expected=IllegalStateException.class)
121    public void testNotLoadedFromResourceId() throws Exception {
122        shadow.getLoadedFromResourceId();
123    }
124
125    @Test
126    public void testLoadedFromResourceId() throws Exception {
127        shadow.setLoadedFromResourceId(R.anim.fade_in);
128        assertThat(shadow.getLoadedFromResourceId(), equalTo(R.anim.fade_in));
129    }
130
131	private class TestAnimation extends Animation {
132        float interpolatedTime;
133        Transformation t;
134
135        @Override protected void applyTransformation(float interpolatedTime, Transformation t) {
136            this.interpolatedTime = interpolatedTime;
137            this.t = t;
138        }
139    }
140}
141