Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import android.os.SystemClock;
      4 import org.robolectric.annotation.HiddenApi;
      5 import org.robolectric.annotation.Implementation;
      6 import org.robolectric.annotation.Implements;
      7 
      8 /**
      9  * Robolectric's concept of current time is base on the current time of the UI Scheduler for
     10  * consistency with previous implementations. This is not ideal, since both schedulers
     11  * (background and foreground), can see different values for the current time.
     12  */
     13 @Implements(SystemClock.class)
     14 public class ShadowSystemClock {
     15   private static long bootedAt = 0;
     16   private static long nanoTime = 0;
     17   private static final int MILLIS_PER_NANO = 1000000;
     18 
     19   static long now() {
     20     if (ShadowApplication.getInstance() == null) {
     21       return 0;
     22     }
     23     return ShadowApplication.getInstance().getForegroundThreadScheduler().getCurrentTime();
     24   }
     25 
     26   @Implementation
     27   public static void sleep(long millis) {
     28     if (ShadowApplication.getInstance() == null) {
     29       return;
     30     }
     31 
     32     nanoTime = millis * MILLIS_PER_NANO;
     33     ShadowApplication.getInstance().getForegroundThreadScheduler().advanceBy(millis);
     34   }
     35 
     36   @Implementation
     37   public static boolean setCurrentTimeMillis(long millis) {
     38     if (ShadowApplication.getInstance() == null) {
     39       return false;
     40     }
     41 
     42     if (now() > millis) {
     43       return false;
     44     }
     45     nanoTime = millis * MILLIS_PER_NANO;
     46     ShadowApplication.getInstance().getForegroundThreadScheduler().advanceTo(millis);
     47     return true;
     48   }
     49 
     50   @Implementation
     51   public static long uptimeMillis() {
     52     return now() - bootedAt;
     53   }
     54 
     55   @Implementation
     56   public static long elapsedRealtime() {
     57     return uptimeMillis();
     58   }
     59 
     60   @Implementation
     61   public static long currentThreadTimeMillis() {
     62     return uptimeMillis();
     63   }
     64 
     65   @HiddenApi
     66   @Implementation
     67   public static long currentThreadTimeMicro() {
     68     return uptimeMillis() * 1000;
     69   }
     70 
     71   @HiddenApi
     72   @Implementation
     73   public static long currentTimeMicro() {
     74     return now() * 1000;
     75   }
     76 
     77   /**
     78    * Implements {@link System#currentTimeMillis} through ShadowWrangler.
     79    *
     80    * @return Current time in millis.
     81    */
     82   @SuppressWarnings("unused")
     83   public static long currentTimeMillis() {
     84     return nanoTime / MILLIS_PER_NANO;
     85   }
     86 
     87   /**
     88    * Implements {@link System#nanoTime} through ShadowWrangler.
     89    *
     90    * @return Current time with nanos.
     91    */
     92   @SuppressWarnings("unused")
     93   public static long nanoTime() {
     94     return nanoTime;
     95   }
     96 
     97   public static void setNanoTime(long nanoTime) {
     98     ShadowSystemClock.nanoTime = nanoTime;
     99   }
    100 }
    101