1package com.xtremelabs.robolectric.shadows;
2
3import org.junit.Before;
4import org.junit.Test;
5import org.junit.runner.RunWith;
6
7import android.os.CountDownTimer;
8
9import com.xtremelabs.robolectric.Robolectric;
10import com.xtremelabs.robolectric.WithTestDefaultsRunner;
11
12import static org.hamcrest.CoreMatchers.equalTo;
13import static org.hamcrest.CoreMatchers.not;
14import static org.hamcrest.CoreMatchers.notNullValue;
15
16import static org.junit.Assert.assertThat;
17
18@RunWith(WithTestDefaultsRunner.class)
19public class CountDownTimerTest {
20
21	private ShadowCountDownTimer shadowCountDownTimer;
22	private CountDownTimer countDownTimer;
23	private long millisInFuture = 2000;
24	private long countDownInterval = 1000;
25	private String msg = null;
26
27    @Before
28    public void setUp() throws Exception {
29
30    	countDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {
31
32			@Override
33			public void onFinish() {
34				msg = "onFinish() is called";
35			}
36
37			@Override
38			public void onTick(long millisUnitilFinished) {
39				msg = "onTick() is called";
40			}
41    	};
42    	shadowCountDownTimer = Robolectric.shadowOf(countDownTimer);
43    }
44
45
46	@Test
47	public void testInvokeOnTick() {
48		assertThat(msg, not(equalTo("onTick() is called")));
49		shadowCountDownTimer.invokeTick(countDownInterval);
50		assertThat(msg, equalTo("onTick() is called"));
51	}
52
53	@Test
54	public void testInvokeOnFinish() {
55		assertThat(msg, not(equalTo("onFinish() is called")));
56		shadowCountDownTimer.invokeFinish();
57		assertThat(msg, equalTo("onFinish() is called"));
58	}
59
60	@Test
61	public void testStart() {
62		assertThat(shadowCountDownTimer.hasStarted(), equalTo(false));
63		CountDownTimer timer = shadowCountDownTimer.start();
64		assertThat(timer, notNullValue());
65		assertThat(shadowCountDownTimer.hasStarted(), equalTo(true));
66	}
67
68	@Test
69	public void testCancel() {
70		CountDownTimer timer = shadowCountDownTimer.start();
71		assertThat(timer, notNullValue());
72		assertThat(shadowCountDownTimer.hasStarted(), equalTo(true));
73		shadowCountDownTimer.cancel();
74		assertThat(shadowCountDownTimer.hasStarted(), equalTo(false));
75	}
76
77	@Test
78	public void testAccessors() {
79		assertThat(shadowCountDownTimer.getCountDownInterval(), equalTo(countDownInterval));
80		assertThat(shadowCountDownTimer.getMillisInFuture(), equalTo(millisInFuture));
81	}
82}
83