1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.os.cts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.os.SystemClock;
25 import android.platform.test.annotations.AppModeSdkSandbox;
26 import android.platform.test.annotations.IgnoreUnderRavenwood;
27 import android.platform.test.ravenwood.RavenwoodRule;
28 
29 import org.junit.Rule;
30 import org.junit.Test;
31 
32 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).")
33 public class SystemClockTest {
34     @Rule public RavenwoodRule mRavenwood = new RavenwoodRule();
35 
36     @Test
37     @IgnoreUnderRavenwood(reason = "Requires kernel support")
testCurrentThreadTimeMillis()38     public void testCurrentThreadTimeMillis() throws InterruptedException {
39 
40         long start = SystemClock.currentThreadTimeMillis();
41         Thread.sleep(100);
42         long end = SystemClock.currentThreadTimeMillis();
43         assertFalse(end - 100 >= start);
44 
45     }
46 
47     @Test
testElapsedRealtime()48     public void testElapsedRealtime() throws InterruptedException {
49 
50         long start = SystemClock.elapsedRealtime();
51         Thread.sleep(100);
52         long end = SystemClock.elapsedRealtime();
53         assertTrue(end - 100 >= start);
54 
55     }
56 
57     @Test
58     @IgnoreUnderRavenwood(reason = "Requires kernel support")
testSetCurrentTimeMillis()59     public void testSetCurrentTimeMillis() {
60 
61         long start = SystemClock.currentThreadTimeMillis();
62         boolean actual = SystemClock.setCurrentTimeMillis(start + 10000);
63         assertFalse(actual);
64         // This test need to be done in permission test.
65 
66     }
67 
68     @Test
69     @IgnoreUnderRavenwood(reason = "Requires kernel support")
testSleep_currentThreadTimeMillis()70     public void testSleep_currentThreadTimeMillis() {
71         long start = SystemClock.currentThreadTimeMillis();
72         SystemClock.sleep(100);
73         long end = SystemClock.currentThreadTimeMillis();
74         assertFalse(end - 100 >= start);
75     }
76 
77     @Test
testSleep_elapsedRealtime()78     public void testSleep_elapsedRealtime() {
79         long start = SystemClock.elapsedRealtime();
80         SystemClock.sleep(100);
81         long end = SystemClock.elapsedRealtime();
82         assertTrue(end - 100 >= start);
83     }
84 
85     @Test
testSleep_uptimeMillis()86     public void testSleep_uptimeMillis() {
87         long start = SystemClock.uptimeMillis();
88         SystemClock.sleep(100);
89         long end = SystemClock.uptimeMillis();
90         assertTrue(end - 100 >= start);
91     }
92 
93     @Test
testUptimeMillis()94     public void testUptimeMillis() throws InterruptedException {
95         long start = SystemClock.uptimeMillis();
96         Thread.sleep(100);
97         long end = SystemClock.uptimeMillis();
98         assertTrue(end - 100 >= start);
99     }
100 
101     @Test
testElapsedVsUptime()102     public void testElapsedVsUptime() throws InterruptedException {
103         // Elapsed also counts time in deep sleep, so it should always be more than uptime
104         assertThat(SystemClock.uptimeMillis()).isAtMost(SystemClock.elapsedRealtime());
105     }
106 
107     @Test
testElapsedRealtime_Valid()108     public void testElapsedRealtime_Valid() {
109         assertThat(SystemClock.elapsedRealtime()).isGreaterThan(0L);
110         assertThat(SystemClock.elapsedRealtimeNanos()).isGreaterThan(0L);
111     }
112 
113     @Test
testUptime_Valid()114     public void testUptime_Valid() {
115         assertThat(SystemClock.uptimeMillis()).isGreaterThan(0L);
116     }
117 }
118