blob: 319b89586d592abaeef250cc7b3a4ec26909f30a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
package org.robolectric.shadows;
import static android.os.Build.VERSION_CODES.P;
import android.os.SystemClock;
import java.time.DateTimeException;
import java.util.concurrent.TimeUnit;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.HiddenApi;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.Resetter;
/**
* A shadow SystemClock for {@link LooperMode.Mode.LEGACY}
*
* <p>In LEGACY LooperMode, Robolectric's concept of current time is base on the current time of the
* UI Scheduler for consistency with previous implementations. This is not ideal, since both
* schedulers (background and foreground), can see different values for the current time.
*/
@Implements(
value = SystemClock.class,
shadowPicker = ShadowSystemClock.Picker.class,
// turn off shadowOf generation
isInAndroidSdk = false)
public class ShadowLegacySystemClock extends ShadowSystemClock {
private static long bootedAt = 0;
private static long nanoTime = 0;
private static final int MILLIS_PER_NANO = 1000000;
static long now() {
return RuntimeEnvironment.getMasterScheduler().getCurrentTime();
}
@Implementation
protected static void sleep(long millis) {
nanoTime = millis * MILLIS_PER_NANO;
RuntimeEnvironment.getMasterScheduler().advanceBy(millis, TimeUnit.MILLISECONDS);
}
@Implementation
protected static boolean setCurrentTimeMillis(long millis) {
if (now() > millis) {
return false;
}
nanoTime = millis * MILLIS_PER_NANO;
RuntimeEnvironment.getMasterScheduler().advanceTo(millis);
return true;
}
@Implementation
protected static long uptimeMillis() {
return now() - bootedAt;
}
@Implementation
protected static long elapsedRealtime() {
return uptimeMillis();
}
@Implementation
protected static long elapsedRealtimeNanos() {
return elapsedRealtime() * MILLIS_PER_NANO;
}
@Implementation
protected static long currentThreadTimeMillis() {
return uptimeMillis();
}
@HiddenApi
@Implementation
public static long currentThreadTimeMicro() {
return uptimeMillis() * 1000;
}
@HiddenApi
@Implementation
public static long currentTimeMicro() {
return now() * 1000;
}
/**
* Implements {@link System#currentTimeMillis} through ShadowWrangler.
*
* @return Current time in millis.
*/
@SuppressWarnings("unused")
public static long currentTimeMillis() {
return nanoTime / MILLIS_PER_NANO;
}
/**
* Implements {@link System#nanoTime} through ShadowWrangler.
*
* @return Current time with nanos.
*/
@SuppressWarnings("unused")
public static long nanoTime() {
return nanoTime;
}
public static void setNanoTime(long nanoTime) {
ShadowLegacySystemClock.nanoTime = nanoTime;
}
@Implementation(minSdk = P)
@HiddenApi
protected static long currentNetworkTimeMillis() {
if (networkTimeAvailable) {
return currentTimeMillis();
} else {
throw new DateTimeException("Network time not available");
}
}
@Resetter
public static void reset() {
ShadowSystemClock.reset();
}
}
|