Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static org.assertj.core.api.Assertions.assertThat;
      4 import static org.junit.Assert.assertFalse;
      5 import static org.junit.Assert.assertTrue;
      6 
      7 import android.os.SystemClock;
      8 import org.junit.Test;
      9 import org.junit.runner.RunWith;
     10 import org.robolectric.Robolectric;
     11 import org.robolectric.RobolectricTestRunner;
     12 import org.robolectric.internal.bytecode.RobolectricInternals;
     13 
     14 @RunWith(RobolectricTestRunner.class)
     15 public class ShadowSystemClockTest {
     16   @Test
     17   public void shouldAllowForFakingOfTime() throws Exception {
     18     assertThat(SystemClock.uptimeMillis()).isNotEqualTo(1000);
     19     Robolectric.getForegroundThreadScheduler().advanceTo(1000);
     20     assertThat(SystemClock.uptimeMillis()).isEqualTo(1000);
     21   }
     22 
     23   @Test
     24   public void sleep() {
     25     Robolectric.getForegroundThreadScheduler().advanceTo(1000);
     26     SystemClock.sleep(34);
     27     assertThat(SystemClock.uptimeMillis()).isEqualTo(1034);
     28   }
     29 
     30   @Test
     31   public void testSetCurrentTime() {
     32     Robolectric.getForegroundThreadScheduler().advanceTo(1000);
     33     assertThat(ShadowSystemClock.now()).isEqualTo(1000);
     34     assertTrue(SystemClock.setCurrentTimeMillis(1034));
     35     assertThat(ShadowSystemClock.now()).isEqualTo(1034);
     36     assertFalse(SystemClock.setCurrentTimeMillis(1000));
     37     assertThat(ShadowSystemClock.now()).isEqualTo(1034);
     38   }
     39 
     40   @Test
     41   public void shouldInterceptSystemTimeCalls() throws Throwable {
     42     ShadowSystemClock.setNanoTime(3141592L);
     43     long systemNanoTime = (Long) RobolectricInternals.intercept(
     44         "java/lang/System/nanoTime()J", null, null, getClass());
     45     assertThat(systemNanoTime).isEqualTo(3141592L);
     46     long systemMilliTime = (Long) RobolectricInternals.intercept(
     47         "java/lang/System/currentTimeMillis()J", null, null, getClass());
     48     assertThat(systemMilliTime).isEqualTo(3L);
     49   }
     50 }
     51