13f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesipackage com.xtremelabs.robolectric.shadows;
23f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi
3602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesiimport android.R;
45f52066979dd3a3eb6a0df2cd360611c50fb31ecRyan Richardimport android.view.animation.Animation;
55f52066979dd3a3eb6a0df2cd360611c50fb31ecRyan Richardimport android.view.animation.LinearInterpolator;
6cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultzimport android.view.animation.Transformation;
75f52066979dd3a3eb6a0df2cd360611c50fb31ecRyan Richardimport com.xtremelabs.robolectric.WithTestDefaultsRunner;
85f52066979dd3a3eb6a0df2cd360611c50fb31ecRyan Richardimport com.xtremelabs.robolectric.util.TestAnimationListener;
95f52066979dd3a3eb6a0df2cd360611c50fb31ecRyan Richardimport org.junit.Before;
105f52066979dd3a3eb6a0df2cd360611c50fb31ecRyan Richardimport org.junit.Test;
115f52066979dd3a3eb6a0df2cd360611c50fb31ecRyan Richardimport org.junit.runner.RunWith;
125f52066979dd3a3eb6a0df2cd360611c50fb31ecRyan Richard
13cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultzimport static com.xtremelabs.robolectric.Robolectric.shadowOf;
14531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richardimport static org.hamcrest.CoreMatchers.*;
153f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesiimport static org.junit.Assert.assertThat;
163f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi
173f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi@RunWith(WithTestDefaultsRunner.class)
183f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesipublic class AnimationTest {
193f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi
20cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz	private TestAnimation animation;
213f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	private ShadowAnimation shadow;
223f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	private TestAnimationListener listener;
233f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi
243f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	@Before
253f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	public void setUp() throws Exception {
263f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		animation = new TestAnimation();
27cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz		shadow = shadowOf(animation);
283f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		listener = new TestAnimationListener();
293f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		animation.setAnimationListener(listener);
303f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	}
313f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi
323f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	@Test
333f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	public void startShouldInvokeStartCallback() throws Exception {
343f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasStartCalled, equalTo(false));
353f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		animation.start();
363f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasStartCalled, equalTo(true));
373f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasEndCalled, equalTo(false));
383f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasRepeatCalled, equalTo(false));
393f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	}
403f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi
413f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	@Test
423f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	public void cancelShouldInvokeEndCallback() throws Exception {
433f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasEndCalled, equalTo(false));
443f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		animation.cancel();
453f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasStartCalled, equalTo(false));
463f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasEndCalled, equalTo(true));
473f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasRepeatCalled, equalTo(false));
483f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	}
493f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi
503f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	@Test
513f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	public void invokeRepeatShouldInvokeRepeatCallback() throws Exception {
523f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasRepeatCalled, equalTo(false));
533f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		shadow.invokeRepeat();
543f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasStartCalled, equalTo(false));
553f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasEndCalled, equalTo(false));
563f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasRepeatCalled, equalTo(true));
573f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	}
583f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi
593f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	@Test
603f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	public void invokeEndShouldInvokeEndCallback() throws Exception {
613f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasEndCalled, equalTo(false));
623f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		shadow.invokeEnd();
633f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasStartCalled, equalTo(false));
643f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasEndCalled, equalTo(true));
653f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi		assertThat(listener.wasRepeatCalled, equalTo(false));
663f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi	}
67cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz
68cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz    @Test
69cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz	public void simulateAnimationEndShouldInvokeApplyTransformationWith1() throws Exception {
70cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz		assertThat(animation.interpolatedTime, equalTo(0f));
71cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz		shadow.invokeEnd();
72cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz        assertThat(animation.interpolatedTime, equalTo(1f));
73cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz	}
74cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz
75a8afbfc349156649b7b358130c2a3706c31bf5e5Kathy Lin	@Test
76a8afbfc349156649b7b358130c2a3706c31bf5e5Kathy Lin	public void testHasStarted() throws Exception {
77a8afbfc349156649b7b358130c2a3706c31bf5e5Kathy Lin		assertThat(animation.hasStarted(), equalTo(false));
78a8afbfc349156649b7b358130c2a3706c31bf5e5Kathy Lin		animation.start();
79a8afbfc349156649b7b358130c2a3706c31bf5e5Kathy Lin		assertThat(animation.hasStarted(), equalTo(true));
80a8afbfc349156649b7b358130c2a3706c31bf5e5Kathy Lin		animation.cancel();
81a8afbfc349156649b7b358130c2a3706c31bf5e5Kathy Lin		assertThat(animation.hasStarted(), equalTo(false));
82a8afbfc349156649b7b358130c2a3706c31bf5e5Kathy Lin	}
83a8afbfc349156649b7b358130c2a3706c31bf5e5Kathy Lin
84b22138612daf36116564a003e1a799d6b517fbfcKathy Lin	@Test
85b22138612daf36116564a003e1a799d6b517fbfcKathy Lin	public void testDuration() throws Exception {
86b22138612daf36116564a003e1a799d6b517fbfcKathy Lin		assertThat(animation.getDuration(), not(equalTo(1000l)));
87b22138612daf36116564a003e1a799d6b517fbfcKathy Lin		animation.setDuration(1000);
88b22138612daf36116564a003e1a799d6b517fbfcKathy Lin		assertThat(animation.getDuration(), equalTo(1000l));
89b22138612daf36116564a003e1a799d6b517fbfcKathy Lin	}
90b22138612daf36116564a003e1a799d6b517fbfcKathy Lin
91b22138612daf36116564a003e1a799d6b517fbfcKathy Lin	@Test
92b22138612daf36116564a003e1a799d6b517fbfcKathy Lin	public void testInterpolation() throws Exception {
93b22138612daf36116564a003e1a799d6b517fbfcKathy Lin		assertThat(animation.getInterpolator(), nullValue());
94b22138612daf36116564a003e1a799d6b517fbfcKathy Lin		LinearInterpolator i = new LinearInterpolator();
95b22138612daf36116564a003e1a799d6b517fbfcKathy Lin		animation.setInterpolator(i);
96b22138612daf36116564a003e1a799d6b517fbfcKathy Lin		assertThat((LinearInterpolator)animation.getInterpolator(), sameInstance(i));
97b22138612daf36116564a003e1a799d6b517fbfcKathy Lin	}
987598f0c0b1cce3bf1902772ea09139ef98083864Ryan Richard & Tyler Schultz
99531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard    @Test
100531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard    public void testRepeatCount() throws Exception {
101531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard        assertThat(animation.getRepeatCount(), not(equalTo(5)));
102531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard        animation.setRepeatCount(5);
103531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard        assertThat(animation.getRepeatCount(), equalTo(5));
104531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard    }
105531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard
106531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard    @Test
107531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard    public void testRepeatMode() throws Exception {
108531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard        assertThat(animation.getRepeatMode(), not(equalTo(Animation.REVERSE)));
109531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard        animation.setRepeatMode(Animation.REVERSE);
110531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard        assertThat(animation.getRepeatMode(), equalTo(Animation.REVERSE));
111531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard    }
112531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard
113531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard    @Test
114531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard    public void testStartOffset() throws Exception {
115531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard        assertThat(animation.getStartOffset(), not(equalTo(500l)));
116531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard        animation.setStartOffset(500l);
117531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard        assertThat(animation.getStartOffset(), equalTo(500l));
118531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard    }
119602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi
120602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi    @Test(expected=IllegalStateException.class)
121602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi    public void testNotLoadedFromResourceId() throws Exception {
122602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi        shadow.getLoadedFromResourceId();
123602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi    }
124531a4341d552a7b42fb07223629798e543015f3eAaron VonderHaar & Ryan Richard
125602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi    @Test
126602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi    public void testLoadedFromResourceId() throws Exception {
127602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi        shadow.setLoadedFromResourceId(R.anim.fade_in);
128602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi        assertThat(shadow.getLoadedFromResourceId(), equalTo(R.anim.fade_in));
129602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi    }
130602891a60a47b9721d8d2216f359d570a3753bacMichael Portuesi
131cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz	private class TestAnimation extends Animation {
132cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz        float interpolatedTime;
133cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz        Transformation t;
134cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz
135cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz        @Override protected void applyTransformation(float interpolatedTime, Transformation t) {
136cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz            this.interpolatedTime = interpolatedTime;
137cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz            this.t = t;
138cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz        }
139cbd31a3b96809ef1a1bd82ea92bcaadd916106eaTyler Schultz    }
1403f78da998ce73c1c7c25798a7e7e4f19287eff23Michael Portuesi}
141