aboutsummaryrefslogtreecommitdiff
path: root/v1/src/test/java/com/xtremelabs/robolectric/shadows/AnimationTest.java
blob: fc80eca3a592f8f2eff9af01c77d4291b2bca59b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.xtremelabs.robolectric.shadows;

import android.R;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.Transformation;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import com.xtremelabs.robolectric.util.TestAnimationListener;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

@RunWith(WithTestDefaultsRunner.class)
public class AnimationTest {
	
	private TestAnimation animation;
	private ShadowAnimation shadow;
	private TestAnimationListener listener;

	@Before
	public void setUp() throws Exception {
		animation = new TestAnimation();
		shadow = shadowOf(animation);
		listener = new TestAnimationListener();
		animation.setAnimationListener(listener);
	}

	@Test
	public void startShouldInvokeStartCallback() throws Exception {
		assertThat(listener.wasStartCalled, equalTo(false));
		animation.start();
		assertThat(listener.wasStartCalled, equalTo(true));
		assertThat(listener.wasEndCalled, equalTo(false));
		assertThat(listener.wasRepeatCalled, equalTo(false));
	}
	
	@Test
	public void cancelShouldInvokeEndCallback() throws Exception {
		assertThat(listener.wasEndCalled, equalTo(false));
		animation.cancel();
		assertThat(listener.wasStartCalled, equalTo(false));
		assertThat(listener.wasEndCalled, equalTo(true));
		assertThat(listener.wasRepeatCalled, equalTo(false));		
	}
	
	@Test
	public void invokeRepeatShouldInvokeRepeatCallback() throws Exception {
		assertThat(listener.wasRepeatCalled, equalTo(false));
		shadow.invokeRepeat();
		assertThat(listener.wasStartCalled, equalTo(false));
		assertThat(listener.wasEndCalled, equalTo(false));
		assertThat(listener.wasRepeatCalled, equalTo(true));	
	}
	
	@Test
	public void invokeEndShouldInvokeEndCallback() throws Exception {
		assertThat(listener.wasEndCalled, equalTo(false));
		shadow.invokeEnd();
		assertThat(listener.wasStartCalled, equalTo(false));
		assertThat(listener.wasEndCalled, equalTo(true));
		assertThat(listener.wasRepeatCalled, equalTo(false));		
	}

    @Test
	public void simulateAnimationEndShouldInvokeApplyTransformationWith1() throws Exception {
		assertThat(animation.interpolatedTime, equalTo(0f));
		shadow.invokeEnd();
        assertThat(animation.interpolatedTime, equalTo(1f));
	}

	@Test
	public void testHasStarted() throws Exception {
		assertThat(animation.hasStarted(), equalTo(false));
		animation.start();
		assertThat(animation.hasStarted(), equalTo(true));
		animation.cancel();
		assertThat(animation.hasStarted(), equalTo(false));
	}
	
	@Test
	public void testDuration() throws Exception {
		assertThat(animation.getDuration(), not(equalTo(1000l)));
		animation.setDuration(1000);
		assertThat(animation.getDuration(), equalTo(1000l));
	}
	
	@Test
	public void testInterpolation() throws Exception {
		assertThat(animation.getInterpolator(), nullValue());
		LinearInterpolator i = new LinearInterpolator();
		animation.setInterpolator(i);
		assertThat((LinearInterpolator)animation.getInterpolator(), sameInstance(i));
	}

    @Test
    public void testRepeatCount() throws Exception {
        assertThat(animation.getRepeatCount(), not(equalTo(5)));
        animation.setRepeatCount(5);
        assertThat(animation.getRepeatCount(), equalTo(5));
    }

    @Test
    public void testRepeatMode() throws Exception {
        assertThat(animation.getRepeatMode(), not(equalTo(Animation.REVERSE)));
        animation.setRepeatMode(Animation.REVERSE);
        assertThat(animation.getRepeatMode(), equalTo(Animation.REVERSE));
    }

    @Test
    public void testStartOffset() throws Exception {
        assertThat(animation.getStartOffset(), not(equalTo(500l)));
        animation.setStartOffset(500l);
        assertThat(animation.getStartOffset(), equalTo(500l));
    }
    
    @Test(expected=IllegalStateException.class)
    public void testNotLoadedFromResourceId() throws Exception {
        shadow.getLoadedFromResourceId();
    }

    @Test
    public void testLoadedFromResourceId() throws Exception {
        shadow.setLoadedFromResourceId(R.anim.fade_in);
        assertThat(shadow.getLoadedFromResourceId(), equalTo(R.anim.fade_in));
    }
    
	private class TestAnimation extends Animation {
        float interpolatedTime;
        Transformation t;

        @Override protected void applyTransformation(float interpolatedTime, Transformation t) {
            this.interpolatedTime = interpolatedTime;
            this.t = t;
        }
    }
}