1 /*
2  * Copyright (C) 2015 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 com.android.tv.testing.fakes;
18 
19 import com.android.tv.common.util.Clock;
20 import java.util.concurrent.TimeUnit;
21 
22 /**
23  * Fake implementation of Clock suitable for testing.
24  *
25  * <p>The current time only changes if {@link #setCurrentTimeMillis(long)}, {@link #increment} or
26  * {@link #sleep(long)} is called.
27  */
28 public class FakeClock implements Clock {
29     /** Creates a fake clock with the time set to now and the boot time set to now - 100,000. */
createWithCurrentTime()30     public static FakeClock createWithCurrentTime() {
31         long now = System.currentTimeMillis();
32         return new FakeClock(now, now - 100_000);
33     }
34 
35     /** Creates a fake clock with the time set to zero. */
createWithTimeOne()36     public static FakeClock createWithTimeOne() {
37         return new FakeClock(1L, 0L);
38     }
39     /** Creates a fake clock with the time set to {@code time}. */
createWithTime(long time)40     public static FakeClock createWithTime(long time) {
41         return new FakeClock(time, 0L);
42     }
43 
44     private long mCurrentTimeMillis;
45 
46     private long mBootTimeMillis;
47 
FakeClock(long currentTimeMillis, long bootTimeMillis)48     private FakeClock(long currentTimeMillis, long bootTimeMillis) {
49         mCurrentTimeMillis = currentTimeMillis;
50         mBootTimeMillis = bootTimeMillis;
51     }
52 
setCurrentTimeMillis(long ms)53     public void setCurrentTimeMillis(long ms) {
54         if (ms < mBootTimeMillis) {
55             throw new IllegalStateException("current time can not be before boot time");
56         }
57         mCurrentTimeMillis = ms;
58     }
59 
setBootTimeMillis(long ms)60     public void setBootTimeMillis(long ms) {
61         if (ms > mCurrentTimeMillis) {
62             throw new IllegalStateException("boot time can not be after current time");
63         }
64         mBootTimeMillis = ms;
65     }
66 
67     /**
68      * Increment the current time by one unit of time.
69      *
70      * @param unit The time unit to increment by.
71      */
increment(TimeUnit unit)72     public void increment(TimeUnit unit) {
73         increment(unit, 1);
74     }
75 
76     /**
77      * Increment the current time by {@code amount} unit of time.
78      *
79      * @param unit The time unit to increment by.
80      * @param amount The amount of time units to increment by.
81      */
increment(TimeUnit unit, long amount)82     public void increment(TimeUnit unit, long amount) {
83         mCurrentTimeMillis += unit.toMillis(amount);
84     }
85 
86     @Override
currentTimeMillis()87     public long currentTimeMillis() {
88         return mCurrentTimeMillis;
89     }
90 
91     @Override
elapsedRealtime()92     public long elapsedRealtime() {
93         return mCurrentTimeMillis - mBootTimeMillis;
94     }
95 
96     @Override
uptimeMillis()97     public long uptimeMillis() {
98         return elapsedRealtime();
99     }
100 
101     /** Sleep does not block it just updates the current time. */
102     @Override
sleep(long ms)103     public void sleep(long ms) {
104         // TODO: implement blocking if needed.
105         mCurrentTimeMillis += ms;
106     }
107 }
108