1 package org.robolectric.shadows; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.os.CountDownTimer; 6 import androidx.test.ext.junit.runners.AndroidJUnit4; 7 import org.junit.Before; 8 import org.junit.Test; 9 import org.junit.runner.RunWith; 10 import org.robolectric.Shadows; 11 12 @RunWith(AndroidJUnit4.class) 13 public class ShadowCountDownTimerTest { 14 15 private ShadowCountDownTimer shadowCountDownTimer; 16 private final long millisInFuture = 2000; 17 private final long countDownInterval = 1000; 18 private String msg = null; 19 20 @Before setUp()21 public void setUp() throws Exception { 22 23 CountDownTimer countDownTimer = 24 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 testInvokeOnTick()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 testInvokeOnFinish()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 testStart()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 testCancel()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 testAccessors()72 public void testAccessors() { 73 assertThat(shadowCountDownTimer.getCountDownInterval()).isEqualTo(countDownInterval); 74 assertThat(shadowCountDownTimer.getMillisInFuture()).isEqualTo(millisInFuture); 75 } 76 } 77