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