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