• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
now()19   static long now() {
20     if (ShadowApplication.getInstance() == null) {
21       return 0;
22     }
23     return ShadowApplication.getInstance().getForegroundThreadScheduler().getCurrentTime();
24   }
25 
26   @Implementation
sleep(long millis)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
setCurrentTimeMillis(long millis)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
uptimeMillis()51   public static long uptimeMillis() {
52     return now() - bootedAt;
53   }
54 
55   @Implementation
elapsedRealtime()56   public static long elapsedRealtime() {
57     return uptimeMillis();
58   }
59 
60   @Implementation
currentThreadTimeMillis()61   public static long currentThreadTimeMillis() {
62     return uptimeMillis();
63   }
64 
65   @HiddenApi
66   @Implementation
currentThreadTimeMicro()67   public static long currentThreadTimeMicro() {
68     return uptimeMillis() * 1000;
69   }
70 
71   @HiddenApi
72   @Implementation
currentTimeMicro()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")
currentTimeMillis()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")
nanoTime()93   public static long nanoTime() {
94     return nanoTime;
95   }
96 
setNanoTime(long nanoTime)97   public static void setNanoTime(long nanoTime) {
98     ShadowSystemClock.nanoTime = nanoTime;
99   }
100 }
101