diff options
4 files changed, 218 insertions, 0 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/dagger/SystemUIModule.java b/packages/SystemUI/src/com/android/systemui/dagger/SystemUIModule.java index 6ae21b39f331..bcaad2225724 100644 --- a/packages/SystemUI/src/com/android/systemui/dagger/SystemUIModule.java +++ b/packages/SystemUI/src/com/android/systemui/dagger/SystemUIModule.java @@ -32,6 +32,8 @@ import com.android.systemui.statusbar.notification.people.PeopleHubModule; import com.android.systemui.statusbar.phone.KeyguardLiftController; import com.android.systemui.statusbar.phone.StatusBar; import com.android.systemui.util.sensors.AsyncSensorManager; +import com.android.systemui.util.time.SystemClock; +import com.android.systemui.util.time.SystemClockImpl; import javax.inject.Singleton; @@ -84,4 +86,8 @@ public abstract class SystemUIModule { @BindsOptionalOf abstract StatusBar optionalStatusBar(); + + @Singleton + @Binds + abstract SystemClock bindSystemClock(SystemClockImpl systemClock); } diff --git a/packages/SystemUI/src/com/android/systemui/util/time/SystemClock.java b/packages/SystemUI/src/com/android/systemui/util/time/SystemClock.java new file mode 100644 index 000000000000..4316df1ced04 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/util/time/SystemClock.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.util.time; + +/** + * Testable wrapper around {@link android.os.SystemClock}. + * + * Dagger can inject this wrapper into your classes. The implementation just proxies calls to the + * real SystemClock. + * + * In tests, pass an instance of FakeSystemClock, which allows you to control the values returned by + * the various getters below. + */ +public interface SystemClock { + /** @see android.os.SystemClock#uptimeMillis() */ + long uptimeMillis(); + + /** @see android.os.SystemClock#elapsedRealtime() */ + long elapsedRealtime(); + + /** @see android.os.SystemClock#elapsedRealtimeNanos() */ + long elapsedRealtimeNanos(); + + /** @see android.os.SystemClock#currentThreadTimeMillis() */ + long currentThreadTimeMillis(); + + /** @see android.os.SystemClock#currentThreadTimeMicro() */ + long currentThreadTimeMicro(); + + /** @see android.os.SystemClock#currentTimeMicro() */ + long currentTimeMicro(); +} diff --git a/packages/SystemUI/src/com/android/systemui/util/time/SystemClockImpl.java b/packages/SystemUI/src/com/android/systemui/util/time/SystemClockImpl.java new file mode 100644 index 000000000000..532ea050bfdd --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/util/time/SystemClockImpl.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.util.time; + +import javax.inject.Inject; + +/** Default implementation of {@link SystemClock}. */ +public class SystemClockImpl implements SystemClock { + @Inject + public SystemClockImpl() {} + + @Override + public long uptimeMillis() { + return android.os.SystemClock.uptimeMillis(); + } + + @Override + public long elapsedRealtime() { + return android.os.SystemClock.elapsedRealtime(); + } + + @Override + public long elapsedRealtimeNanos() { + return android.os.SystemClock.elapsedRealtimeNanos(); + } + + @Override + public long currentThreadTimeMillis() { + return android.os.SystemClock.currentThreadTimeMillis(); + } + + @Override + public long currentThreadTimeMicro() { + return android.os.SystemClock.currentThreadTimeMicro(); + } + + @Override + public long currentTimeMicro() { + return android.os.SystemClock.currentTimeMicro(); + } +} diff --git a/packages/SystemUI/tests/src/com/android/systemui/util/time/FakeSystemClock.java b/packages/SystemUI/tests/src/com/android/systemui/util/time/FakeSystemClock.java new file mode 100644 index 000000000000..7b5417cd5c36 --- /dev/null +++ b/packages/SystemUI/tests/src/com/android/systemui/util/time/FakeSystemClock.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.util.time; + +public class FakeSystemClock implements SystemClock { + private boolean mAutoIncrement = true; + + private long mUptimeMillis; + private long mElapsedRealtime; + private long mElapsedRealtimeNanos; + private long mCurrentThreadTimeMillis; + private long mCurrentThreadTimeMicro; + private long mCurrentTimeMicro; + + @Override + public long uptimeMillis() { + long value = mUptimeMillis; + if (mAutoIncrement) { + mUptimeMillis++; + } + return value; + } + + @Override + public long elapsedRealtime() { + long value = mElapsedRealtime; + if (mAutoIncrement) { + mElapsedRealtime++; + } + return value; + } + + @Override + public long elapsedRealtimeNanos() { + long value = mElapsedRealtimeNanos; + if (mAutoIncrement) { + mElapsedRealtimeNanos++; + } + return value; + } + + @Override + public long currentThreadTimeMillis() { + long value = mCurrentThreadTimeMillis; + if (mAutoIncrement) { + mCurrentThreadTimeMillis++; + } + return value; + } + + @Override + public long currentThreadTimeMicro() { + long value = mCurrentThreadTimeMicro; + if (mAutoIncrement) { + mCurrentThreadTimeMicro++; + } + return value; + } + + @Override + public long currentTimeMicro() { + long value = mCurrentTimeMicro; + if (mAutoIncrement) { + mCurrentTimeMicro++; + } + return value; + } + + public void setUptimeMillis(long uptimeMillis) { + mUptimeMillis = uptimeMillis; + } + + public void setElapsedRealtime(long elapsedRealtime) { + mElapsedRealtime = elapsedRealtime; + } + + public void setElapsedRealtimeNanos(long elapsedRealtimeNanos) { + mElapsedRealtimeNanos = elapsedRealtimeNanos; + } + + public void setCurrentThreadTimeMillis(long currentThreadTimeMillis) { + mCurrentThreadTimeMillis = currentThreadTimeMillis; + } + + public void setCurrentThreadTimeMicro(long currentThreadTimeMicro) { + mCurrentThreadTimeMicro = currentThreadTimeMicro; + } + + public void setCurrentTimeMicro(long currentTimeMicro) { + mCurrentTimeMicro = currentTimeMicro; + } + + /** If true, each call to get____ will be one higher than the previous call to that method. */ + public void setAutoIncrement(boolean autoIncrement) { + mAutoIncrement = autoIncrement; + } +} |