Merge "Fix KernelCpuUidUserSysTimeReaderTest#testThrottler by injecting a mock clock"
This commit is contained in:
committed by
Android (Google) Code Review
commit
bf55fd64ff
File diff suppressed because it is too large
Load Diff
@@ -256,7 +256,7 @@ public class BatteryUsageStatsProvider {
|
||||
|
||||
private long elapsedRealtime() {
|
||||
if (mStats instanceof BatteryStatsImpl) {
|
||||
return ((BatteryStatsImpl) mStats).mClocks.elapsedRealtime();
|
||||
return ((BatteryStatsImpl) mStats).mClock.elapsedRealtime();
|
||||
} else {
|
||||
return SystemClock.elapsedRealtime();
|
||||
}
|
||||
@@ -264,7 +264,7 @@ public class BatteryUsageStatsProvider {
|
||||
|
||||
private long uptimeMillis() {
|
||||
if (mStats instanceof BatteryStatsImpl) {
|
||||
return ((BatteryStatsImpl) mStats).mClocks.uptimeMillis();
|
||||
return ((BatteryStatsImpl) mStats).mClock.uptimeMillis();
|
||||
} else {
|
||||
return SystemClock.uptimeMillis();
|
||||
}
|
||||
@@ -272,7 +272,7 @@ public class BatteryUsageStatsProvider {
|
||||
|
||||
private long currentTimeMillis() {
|
||||
if (mStats instanceof BatteryStatsImpl) {
|
||||
return ((BatteryStatsImpl) mStats).mClocks.currentTimeMillis();
|
||||
return ((BatteryStatsImpl) mStats).mClock.currentTimeMillis();
|
||||
} else {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
57
core/java/com/android/internal/os/Clock.java
Normal file
57
core/java/com/android/internal/os/Clock.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) 2021 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.internal.os;
|
||||
|
||||
import android.os.SystemClock;
|
||||
|
||||
/**
|
||||
* A wrapper for SystemClock, intended for mocking in unit tests.
|
||||
*/
|
||||
public abstract class Clock {
|
||||
/** Elapsed Realtime, see SystemClock.elapsedRealtime() */
|
||||
public long elapsedRealtime() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/** Uptime, see SystemClock.uptimeMillis() */
|
||||
public long uptimeMillis() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/** Wall-clock time as per System.currentTimeMillis() */
|
||||
public long currentTimeMillis() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static final Clock SYSTEM_CLOCK = new Clock() {
|
||||
|
||||
@Override
|
||||
public long elapsedRealtime() {
|
||||
return SystemClock.elapsedRealtime();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long uptimeMillis() {
|
||||
return SystemClock.uptimeMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long currentTimeMillis() {
|
||||
return System.currentTimeMillis();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -17,7 +17,6 @@
|
||||
package com.android.internal.os;
|
||||
|
||||
import android.os.StrictMode;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Slog;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@@ -89,6 +88,7 @@ public class KernelCpuProcStringReader {
|
||||
|
||||
private int mErrors = 0;
|
||||
private final Path mFile;
|
||||
private final Clock mClock;
|
||||
private char[] mBuf;
|
||||
private int mSize;
|
||||
private long mLastReadTime = 0;
|
||||
@@ -97,7 +97,12 @@ public class KernelCpuProcStringReader {
|
||||
private final ReentrantReadWriteLock.WriteLock mWriteLock = mLock.writeLock();
|
||||
|
||||
public KernelCpuProcStringReader(String file) {
|
||||
this(file, Clock.SYSTEM_CLOCK);
|
||||
}
|
||||
|
||||
public KernelCpuProcStringReader(String file, Clock clock) {
|
||||
mFile = Paths.get(file);
|
||||
mClock = clock;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -168,7 +173,7 @@ public class KernelCpuProcStringReader {
|
||||
}
|
||||
}
|
||||
mSize = total;
|
||||
mLastReadTime = SystemClock.elapsedRealtime();
|
||||
mLastReadTime = mClock.elapsedRealtime();
|
||||
// ReentrantReadWriteLock allows lock downgrading.
|
||||
mReadLock.lock();
|
||||
return new ProcFileIterator(total);
|
||||
@@ -186,7 +191,7 @@ public class KernelCpuProcStringReader {
|
||||
}
|
||||
|
||||
private boolean dataValid() {
|
||||
return mSize > 0 && (SystemClock.elapsedRealtime() - mLastReadTime < FRESHNESS);
|
||||
return mSize > 0 && (mClock.elapsedRealtime() - mLastReadTime < FRESHNESS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,7 +21,6 @@ import static com.android.internal.util.Preconditions.checkNotNull;
|
||||
import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.os.StrictMode;
|
||||
import android.os.SystemClock;
|
||||
import android.util.IntArray;
|
||||
import android.util.Slog;
|
||||
import android.util.SparseArray;
|
||||
@@ -60,6 +59,7 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
final boolean mThrottle;
|
||||
protected boolean mBpfTimesAvailable;
|
||||
final KernelCpuUidBpfMapReader mBpfReader;
|
||||
private final Clock mClock;
|
||||
private long mMinTimeBetweenRead = DEFAULT_MIN_TIME_BETWEEN_READ;
|
||||
private long mLastReadTimeMs = 0;
|
||||
|
||||
@@ -76,15 +76,17 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
void onUidCpuTime(int uid, T time);
|
||||
}
|
||||
|
||||
KernelCpuUidTimeReader(KernelCpuProcStringReader reader, @Nullable KernelCpuUidBpfMapReader bpfReader, boolean throttle) {
|
||||
KernelCpuUidTimeReader(KernelCpuProcStringReader reader,
|
||||
@Nullable KernelCpuUidBpfMapReader bpfReader, boolean throttle, Clock clock) {
|
||||
mReader = reader;
|
||||
mThrottle = throttle;
|
||||
mBpfReader = bpfReader;
|
||||
mClock = clock;
|
||||
mBpfTimesAvailable = (mBpfReader != null);
|
||||
}
|
||||
|
||||
KernelCpuUidTimeReader(KernelCpuProcStringReader reader, boolean throttle) {
|
||||
this(reader, null, throttle);
|
||||
KernelCpuUidTimeReader(KernelCpuProcStringReader reader, boolean throttle, Clock clock) {
|
||||
this(reader, null, throttle, clock);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,17 +106,17 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
*/
|
||||
public void readDelta(boolean force, @Nullable Callback<T> cb) {
|
||||
if (!mThrottle) {
|
||||
readDeltaImpl(cb);
|
||||
readDeltaImpl(cb, force);
|
||||
return;
|
||||
}
|
||||
final long currTimeMs = SystemClock.elapsedRealtime();
|
||||
final long currTimeMs = mClock.elapsedRealtime();
|
||||
if (!force && currTimeMs < mLastReadTimeMs + mMinTimeBetweenRead) {
|
||||
if (DEBUG) {
|
||||
Slog.d(mTag, "Throttle readDelta");
|
||||
}
|
||||
return;
|
||||
}
|
||||
readDeltaImpl(cb);
|
||||
readDeltaImpl(cb, force);
|
||||
mLastReadTimeMs = currTimeMs;
|
||||
}
|
||||
|
||||
@@ -128,7 +130,7 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
readAbsoluteImpl(cb);
|
||||
return;
|
||||
}
|
||||
final long currTimeMs = SystemClock.elapsedRealtime();
|
||||
final long currTimeMs = mClock.elapsedRealtime();
|
||||
if (currTimeMs < mLastReadTimeMs + mMinTimeBetweenRead) {
|
||||
if (DEBUG) {
|
||||
Slog.d(mTag, "Throttle readAbsolute");
|
||||
@@ -139,7 +141,7 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
mLastReadTimeMs = currTimeMs;
|
||||
}
|
||||
|
||||
abstract void readDeltaImpl(@Nullable Callback<T> cb);
|
||||
abstract void readDeltaImpl(@Nullable Callback<T> cb, boolean forceRead);
|
||||
|
||||
abstract void readAbsoluteImpl(Callback<T> callback);
|
||||
|
||||
@@ -216,17 +218,22 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
private final long[] mUsrSysTime = new long[2];
|
||||
|
||||
public KernelCpuUidUserSysTimeReader(boolean throttle) {
|
||||
super(KernelCpuProcStringReader.getUserSysTimeReaderInstance(), throttle);
|
||||
this(throttle, Clock.SYSTEM_CLOCK);
|
||||
}
|
||||
|
||||
public KernelCpuUidUserSysTimeReader(boolean throttle, Clock clock) {
|
||||
super(KernelCpuProcStringReader.getUserSysTimeReaderInstance(), throttle, clock);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public KernelCpuUidUserSysTimeReader(KernelCpuProcStringReader reader, boolean throttle) {
|
||||
super(reader, throttle);
|
||||
public KernelCpuUidUserSysTimeReader(KernelCpuProcStringReader reader, boolean throttle,
|
||||
Clock clock) {
|
||||
super(reader, throttle, clock);
|
||||
}
|
||||
|
||||
@Override
|
||||
void readDeltaImpl(@Nullable Callback<long[]> cb) {
|
||||
try (ProcFileIterator iter = mReader.open(!mThrottle)) {
|
||||
void readDeltaImpl(@Nullable Callback<long[]> cb, boolean forceRead) {
|
||||
try (ProcFileIterator iter = mReader.open(!mThrottle || forceRead)) {
|
||||
if (iter == null) {
|
||||
return;
|
||||
}
|
||||
@@ -348,14 +355,23 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
private boolean mAllUidTimesAvailable = true;
|
||||
|
||||
public KernelCpuUidFreqTimeReader(boolean throttle) {
|
||||
this(throttle, Clock.SYSTEM_CLOCK);
|
||||
}
|
||||
|
||||
public KernelCpuUidFreqTimeReader(boolean throttle, Clock clock) {
|
||||
this(UID_TIMES_PROC_FILE, KernelCpuProcStringReader.getFreqTimeReaderInstance(),
|
||||
KernelCpuUidBpfMapReader.getFreqTimeReaderInstance(), throttle);
|
||||
KernelCpuUidBpfMapReader.getFreqTimeReaderInstance(), throttle, clock);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public KernelCpuUidFreqTimeReader(String procFile, KernelCpuProcStringReader reader,
|
||||
KernelCpuUidBpfMapReader bpfReader, boolean throttle) {
|
||||
super(reader, bpfReader, throttle);
|
||||
this(procFile, reader, bpfReader, throttle, Clock.SYSTEM_CLOCK);
|
||||
}
|
||||
|
||||
private KernelCpuUidFreqTimeReader(String procFile, KernelCpuProcStringReader reader,
|
||||
KernelCpuUidBpfMapReader bpfReader, boolean throttle, Clock clock) {
|
||||
super(reader, bpfReader, throttle, clock);
|
||||
mProcFilePath = Paths.get(procFile);
|
||||
}
|
||||
|
||||
@@ -496,7 +512,7 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
void readDeltaImpl(@Nullable Callback<long[]> cb) {
|
||||
void readDeltaImpl(@Nullable Callback<long[]> cb, boolean forceRead) {
|
||||
if (mBpfTimesAvailable) {
|
||||
try (BpfMapIterator iter = mBpfReader.open(!mThrottle)) {
|
||||
if (checkPrecondition(iter)) {
|
||||
@@ -628,13 +644,18 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
private long[] mBuffer;
|
||||
|
||||
public KernelCpuUidActiveTimeReader(boolean throttle) {
|
||||
this(throttle, Clock.SYSTEM_CLOCK);
|
||||
}
|
||||
|
||||
public KernelCpuUidActiveTimeReader(boolean throttle, Clock clock) {
|
||||
super(KernelCpuProcStringReader.getActiveTimeReaderInstance(),
|
||||
KernelCpuUidBpfMapReader.getActiveTimeReaderInstance(), throttle);
|
||||
KernelCpuUidBpfMapReader.getActiveTimeReaderInstance(), throttle, clock);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public KernelCpuUidActiveTimeReader(KernelCpuProcStringReader reader, KernelCpuUidBpfMapReader bpfReader, boolean throttle) {
|
||||
super(reader, bpfReader, throttle);
|
||||
public KernelCpuUidActiveTimeReader(KernelCpuProcStringReader reader,
|
||||
KernelCpuUidBpfMapReader bpfReader, boolean throttle) {
|
||||
super(reader, bpfReader, throttle, Clock.SYSTEM_CLOCK);
|
||||
}
|
||||
|
||||
private void processUidDelta(@Nullable Callback<Long> cb) {
|
||||
@@ -655,7 +676,7 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
void readDeltaImpl(@Nullable Callback<Long> cb) {
|
||||
void readDeltaImpl(@Nullable Callback<Long> cb, boolean forceRead) {
|
||||
if (mBpfTimesAvailable) {
|
||||
try (BpfMapIterator iter = mBpfReader.open(!mThrottle)) {
|
||||
if (checkPrecondition(iter)) {
|
||||
@@ -800,14 +821,18 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
private long[] mDeltaTime;
|
||||
|
||||
public KernelCpuUidClusterTimeReader(boolean throttle) {
|
||||
this(throttle, Clock.SYSTEM_CLOCK);
|
||||
}
|
||||
|
||||
public KernelCpuUidClusterTimeReader(boolean throttle, Clock clock) {
|
||||
super(KernelCpuProcStringReader.getClusterTimeReaderInstance(),
|
||||
KernelCpuUidBpfMapReader.getClusterTimeReaderInstance(), throttle);
|
||||
KernelCpuUidBpfMapReader.getClusterTimeReaderInstance(), throttle, clock);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public KernelCpuUidClusterTimeReader(KernelCpuProcStringReader reader,
|
||||
KernelCpuUidBpfMapReader bpfReader, boolean throttle) {
|
||||
super(reader, bpfReader, throttle);
|
||||
KernelCpuUidBpfMapReader bpfReader, boolean throttle) {
|
||||
super(reader, bpfReader, throttle, Clock.SYSTEM_CLOCK);
|
||||
}
|
||||
|
||||
void processUidDelta(@Nullable Callback<long[]> cb) {
|
||||
@@ -838,7 +863,7 @@ public abstract class KernelCpuUidTimeReader<T> {
|
||||
}
|
||||
|
||||
@Override
|
||||
void readDeltaImpl(@Nullable Callback<long[]> cb) {
|
||||
void readDeltaImpl(@Nullable Callback<long[]> cb, boolean forceRead) {
|
||||
if (mBpfTimesAvailable) {
|
||||
try (BpfMapIterator iter = mBpfReader.open(!mThrottle)) {
|
||||
if (checkPrecondition(iter)) {
|
||||
|
||||
@@ -38,7 +38,7 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
|
||||
/** Test that BatteryStatsImpl.Uid.mOnBatteryBackgroundTimeBase works correctly. */
|
||||
@SmallTest
|
||||
public void testBgTimeBase() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
long cur = 0; // realtime in us
|
||||
|
||||
@@ -106,7 +106,7 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
|
||||
/** Test that BatteryStatsImpl.Uid.mOnBatteryScreenOffBackgroundTimeBase works correctly. */
|
||||
@SmallTest
|
||||
public void testScreenOffBgTimeBase() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
long cur = 0; // realtime in us
|
||||
|
||||
@@ -154,7 +154,7 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testWifiScan() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
long curr = 0; // realtime in us
|
||||
|
||||
@@ -207,7 +207,7 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
|
||||
}
|
||||
|
||||
private void doTestAppBluetoothScanInternal(WorkSource ws) throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
long curr = 0; // realtime in us
|
||||
|
||||
@@ -276,7 +276,7 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testJob() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
final String jobName = "job_name";
|
||||
long curr = 0; // realtime in us
|
||||
@@ -337,7 +337,7 @@ public class BatteryStatsBackgroundStatsTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testSyncs() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
final String syncName = "sync_name";
|
||||
long curr = 0; // realtime in us
|
||||
|
||||
@@ -46,7 +46,7 @@ public class BatteryStatsBinderCallStatsTest extends TestCase {
|
||||
*/
|
||||
@Test
|
||||
public void testNoteBinderCallStats() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
|
||||
int callingUid = Process.FIRST_APPLICATION_UID + 1;
|
||||
@@ -89,7 +89,7 @@ public class BatteryStatsBinderCallStatsTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testProportionalSystemServiceUsage_noStatsForSomeMethods() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
|
||||
int callingUid = Process.FIRST_APPLICATION_UID + 1;
|
||||
|
||||
@@ -30,7 +30,7 @@ public class BatteryStatsCounterTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testCounter() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
|
||||
timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
|
||||
|
||||
@@ -70,7 +70,7 @@ public class BatteryStatsCounterTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testParceling() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
|
||||
timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ public class BatteryStatsCpuTimesTest {
|
||||
@Mock
|
||||
PowerProfile mPowerProfile;
|
||||
|
||||
private MockClocks mClocks;
|
||||
private MockClock mClocks;
|
||||
private MockBatteryStatsImpl mBatteryStatsImpl;
|
||||
private KernelCpuSpeedReader[] mKernelCpuSpeedReaders;
|
||||
|
||||
@@ -104,7 +104,7 @@ public class BatteryStatsCpuTimesTest {
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mClocks = new MockClocks();
|
||||
mClocks = new MockClock();
|
||||
mBatteryStatsImpl = new MockBatteryStatsImpl(mClocks)
|
||||
.setKernelCpuUidUserSysTimeReader(mCpuUidUserSysTimeReader)
|
||||
.setKernelCpuUidFreqTimeReader(mCpuUidFreqTimeReader)
|
||||
|
||||
@@ -29,7 +29,7 @@ public class BatteryStatsDualTimerTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testResetDetach() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
clocks.realtime = clocks.uptime = 100;
|
||||
|
||||
final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
|
||||
|
||||
@@ -33,7 +33,7 @@ public class BatteryStatsDurationTimerTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testStartStop() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
|
||||
final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
|
||||
timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
|
||||
@@ -105,7 +105,7 @@ public class BatteryStatsDurationTimerTest extends TestCase {
|
||||
*/
|
||||
@SmallTest
|
||||
public void testReset() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
|
||||
final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
|
||||
timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
|
||||
@@ -141,7 +141,7 @@ public class BatteryStatsDurationTimerTest extends TestCase {
|
||||
*/
|
||||
@SmallTest
|
||||
public void testResetAndDetach() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
|
||||
final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
|
||||
timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
|
||||
@@ -168,7 +168,7 @@ public class BatteryStatsDurationTimerTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testParceling() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
|
||||
final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
|
||||
timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
|
||||
|
||||
@@ -60,7 +60,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
*/
|
||||
@SmallTest
|
||||
public void testNoteBluetoothScanResultLocked() throws Exception {
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(new MockClocks());
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(new MockClock());
|
||||
bi.updateTimeBasesLocked(true, Display.STATE_OFF, 0, 0);
|
||||
bi.noteUidProcessStateLocked(UID, ActivityManager.PROCESS_STATE_TOP);
|
||||
|
||||
@@ -89,7 +89,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
*/
|
||||
@SmallTest
|
||||
public void testNoteStartWakeLocked() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
|
||||
int pid = 10;
|
||||
@@ -120,7 +120,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
*/
|
||||
@SmallTest
|
||||
public void testNoteUidProcessStateLocked() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
|
||||
// map of ActivityManager process states and how long to simulate run time in each state
|
||||
@@ -205,7 +205,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
*/
|
||||
@SmallTest
|
||||
public void testUpdateTimeBasesLocked() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
|
||||
bi.updateTimeBasesLocked(false, Display.STATE_OFF, 0, 0);
|
||||
@@ -229,7 +229,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
*/
|
||||
@SmallTest
|
||||
public void testNoteScreenStateLocked() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
|
||||
bi.updateTimeBasesLocked(true, Display.STATE_ON, 0, 0);
|
||||
@@ -258,7 +258,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
*/
|
||||
@SmallTest
|
||||
public void testNoteScreenStateTimersLocked() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
|
||||
clocks.realtime = clocks.uptime = 100;
|
||||
@@ -297,7 +297,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
@SmallTest
|
||||
@SkipPresubmit("b/180015146")
|
||||
public void testAlarmStartAndFinishLocked() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.setRecordAllHistoryLocked(true);
|
||||
bi.forceRecordAllHistory();
|
||||
@@ -335,7 +335,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
@SmallTest
|
||||
@SkipPresubmit("b/180015146")
|
||||
public void testAlarmStartAndFinishLocked_workSource() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.setRecordAllHistoryLocked(true);
|
||||
bi.forceRecordAllHistory();
|
||||
@@ -379,7 +379,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testNoteWakupAlarmLocked() {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.setRecordAllHistoryLocked(true);
|
||||
bi.forceRecordAllHistory();
|
||||
@@ -397,7 +397,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testNoteWakupAlarmLocked_workSource_uid() {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.setRecordAllHistoryLocked(true);
|
||||
bi.forceRecordAllHistory();
|
||||
@@ -430,7 +430,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testNoteWakupAlarmLocked_workSource_workChain() {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.setRecordAllHistoryLocked(true);
|
||||
bi.forceRecordAllHistory();
|
||||
@@ -456,7 +456,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testNoteGpsChanged() {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.setRecordAllHistoryLocked(true);
|
||||
bi.forceRecordAllHistory();
|
||||
@@ -480,7 +480,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testNoteGpsChanged_workSource() {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.setRecordAllHistoryLocked(true);
|
||||
bi.forceRecordAllHistory();
|
||||
@@ -504,7 +504,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testUpdateDisplayMeasuredEnergyStatsLocked() {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
final MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.initMeasuredEnergyStats();
|
||||
|
||||
@@ -589,7 +589,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testUpdateCustomMeasuredEnergyStatsLocked_neverCalled() {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
final MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.initMeasuredEnergyStats();
|
||||
bi.setOnBatteryInternal(true);
|
||||
@@ -603,7 +603,7 @@ public class BatteryStatsNoteTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testUpdateCustomMeasuredEnergyStatsLocked() {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
final MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.initMeasuredEnergyStats();
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ public class BatteryStatsSamplingTimerTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testSettingStalePreservesData() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
final BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks,
|
||||
Mockito.mock(BatteryStatsImpl.TimeBase.class));
|
||||
|
||||
@@ -57,7 +57,7 @@ public class BatteryStatsSamplingTimerTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testEndSampleAndContinueWhenTimeOrCountDecreases() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
final BatteryStatsImpl.TimeBase timeBase = Mockito.mock(BatteryStatsImpl.TimeBase.class);
|
||||
final BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks,
|
||||
timeBase);
|
||||
@@ -107,7 +107,7 @@ public class BatteryStatsSamplingTimerTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testFirstUpdateIsAbsorbed() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
final BatteryStatsImpl.TimeBase timeBase = Mockito.mock(BatteryStatsImpl.TimeBase.class);
|
||||
|
||||
BatteryStatsImpl.SamplingTimer timer = new BatteryStatsImpl.SamplingTimer(clocks, timeBase);
|
||||
@@ -146,7 +146,7 @@ public class BatteryStatsSamplingTimerTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testSampleTimerSummaryParceling() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
clocks.realtime = 0;
|
||||
clocks.uptime = 0;
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ public class BatteryStatsSensorTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testSensorStartStop() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.mForceOnBattery = true;
|
||||
clocks.realtime = 100;
|
||||
@@ -71,7 +71,7 @@ public class BatteryStatsSensorTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testCountingWhileOffBattery() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
long curr = 0; // realtime in us
|
||||
|
||||
@@ -107,7 +107,7 @@ public class BatteryStatsSensorTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testCountingWhileOnBattery() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
long curr = 0; // realtime in us
|
||||
|
||||
@@ -142,7 +142,7 @@ public class BatteryStatsSensorTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testBatteryStatusOnToOff() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
long curr = 0; // realtime in us
|
||||
|
||||
@@ -188,7 +188,7 @@ public class BatteryStatsSensorTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testBatteryStatusOffToOn() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
long curr = 0; // realtime in us
|
||||
|
||||
@@ -240,7 +240,7 @@ public class BatteryStatsSensorTest extends TestCase {
|
||||
@SmallTest
|
||||
public void testPooledBackgroundUsage() throws Exception {
|
||||
final int UID_2 = 20000; // second uid for testing pool usage
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.mForceOnBattery = true;
|
||||
long curr = 0; // realtime in us
|
||||
@@ -377,7 +377,7 @@ public class BatteryStatsSensorTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testSensorReset() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
bi.mForceOnBattery = true;
|
||||
clocks.realtime = 100;
|
||||
@@ -421,7 +421,7 @@ public class BatteryStatsSensorTest extends TestCase {
|
||||
|
||||
@SmallTest
|
||||
public void testSensorResetTimes() throws Exception {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
MockBatteryStatsImpl bi = new MockBatteryStatsImpl(clocks);
|
||||
final int which = BatteryStats.STATS_SINCE_CHARGED;
|
||||
bi.mForceOnBattery = true;
|
||||
|
||||
@@ -31,7 +31,7 @@ public class BatteryStatsStopwatchTimerTest extends TestCase {
|
||||
// negative values of count.
|
||||
@SmallTest
|
||||
public void testCount() throws Exception {
|
||||
final MockClocks clocks = new MockClocks(); // holds realtime and uptime in ms
|
||||
final MockClock clocks = new MockClock(); // holds realtime and uptime in ms
|
||||
final BatteryStatsImpl.TimeBase timeBase = new BatteryStatsImpl.TimeBase();
|
||||
timeBase.init(clocks.uptimeMillis(), clocks.elapsedRealtime());
|
||||
final BatteryStatsImpl.StopwatchTimer timer = new BatteryStatsImpl.StopwatchTimer(clocks,
|
||||
@@ -142,7 +142,7 @@ public class BatteryStatsStopwatchTimerTest extends TestCase {
|
||||
assertEquals(expectedCount, timer.getCountLocked(BatteryStats.STATS_SINCE_CHARGED));
|
||||
}
|
||||
|
||||
private static long updateTime(MockClocks clocks, long time) {
|
||||
private static long updateTime(MockClock clocks, long time) {
|
||||
return clocks.realtime = clocks.uptime = time;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import android.util.StringBuilderPrinter;
|
||||
|
||||
import androidx.test.filters.SmallTest;
|
||||
|
||||
import com.android.internal.os.BatteryStatsImpl.Clocks;
|
||||
import com.android.internal.os.BatteryStatsImpl.TimeBase;
|
||||
import com.android.internal.os.BatteryStatsImpl.Timer;
|
||||
|
||||
@@ -41,12 +40,12 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
|
||||
int nextComputeCurrentCount;
|
||||
|
||||
TestTimer(Clocks clocks, int type, TimeBase timeBase, Parcel in) {
|
||||
super(clocks, type, timeBase, in);
|
||||
TestTimer(Clock clock, int type, TimeBase timeBase, Parcel in) {
|
||||
super(clock, type, timeBase, in);
|
||||
}
|
||||
|
||||
TestTimer(Clocks clocks, int type, TimeBase timeBase) {
|
||||
super(clocks, type, timeBase);
|
||||
TestTimer(Clock clock, int type, TimeBase timeBase) {
|
||||
super(clock, type, timeBase);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -91,7 +90,7 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
@SmallTest
|
||||
public void testRunning() throws Exception {
|
||||
TimeBase timeBase = new TimeBase();
|
||||
MockClocks clocks = new MockClocks();
|
||||
MockClock clocks = new MockClock();
|
||||
|
||||
TestTimer timer = new TestTimer(clocks, 0, timeBase);
|
||||
timer.nextComputeCurrentCount = 3000;
|
||||
@@ -112,7 +111,7 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
@SmallTest
|
||||
public void testParceling() throws Exception {
|
||||
TimeBase timeBase = new TimeBase();
|
||||
MockClocks clocks = new MockClocks();
|
||||
MockClock clocks = new MockClock();
|
||||
|
||||
// Test write then read
|
||||
TestTimer timer1 = new TestTimer(clocks, 0, timeBase);
|
||||
@@ -157,7 +156,7 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
@SmallTest
|
||||
public void testResetNoDetach() throws Exception {
|
||||
TimeBase timeBase = new TimeBase();
|
||||
MockClocks clocks = new MockClocks();
|
||||
MockClock clocks = new MockClock();
|
||||
|
||||
TestTimer timer = new TestTimer(clocks, 0, timeBase);
|
||||
timer.setCount(1);
|
||||
@@ -180,7 +179,7 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
@SmallTest
|
||||
public void testResetDetach() throws Exception {
|
||||
TimeBase timeBase = new TimeBase();
|
||||
MockClocks clocks = new MockClocks();
|
||||
MockClock clocks = new MockClock();
|
||||
|
||||
TestTimer timer = new TestTimer(clocks, 0, timeBase);
|
||||
timer.setCount(1);
|
||||
@@ -208,7 +207,7 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
Assert.assertEquals(40, timeBase.getRealtime(200));
|
||||
// the past uptime is 35 and the past runtime is 40
|
||||
|
||||
MockClocks clocks = new MockClocks();
|
||||
MockClock clocks = new MockClock();
|
||||
|
||||
TestTimer timer1 = new TestTimer(clocks, 0, timeBase);
|
||||
timer1.setCount(1);
|
||||
@@ -250,7 +249,7 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
timeBase.setRunning(false, 45, 60);
|
||||
Assert.assertEquals(40, timeBase.getRealtime(200));
|
||||
|
||||
MockClocks clocks = new MockClocks();
|
||||
MockClock clocks = new MockClock();
|
||||
|
||||
TestTimer timer = new TestTimer(clocks, 0, timeBase);
|
||||
timer.setCount(1);
|
||||
@@ -275,7 +274,7 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
timeBase.setRunning(false, 45, 60);
|
||||
Assert.assertEquals(40, timeBase.getRealtime(200));
|
||||
|
||||
MockClocks clocks = new MockClocks();
|
||||
MockClock clocks = new MockClock();
|
||||
|
||||
TestTimer timer = new TestTimer(clocks, 0, timeBase);
|
||||
timer.setCount(1);
|
||||
@@ -296,7 +295,7 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
timeBase.setRunning(false, 45, 60);
|
||||
Assert.assertEquals(40, timeBase.getRealtime(200));
|
||||
|
||||
MockClocks clocks = new MockClocks();
|
||||
MockClock clocks = new MockClock();
|
||||
|
||||
TestTimer timer = new TestTimer(clocks, 0, timeBase);
|
||||
timer.setCount(1);
|
||||
@@ -313,7 +312,7 @@ public class BatteryStatsTimerTest extends TestCase {
|
||||
@SmallTest
|
||||
public void testLogState() throws Exception {
|
||||
TimeBase timeBase = new TimeBase();
|
||||
MockClocks clocks = new MockClocks();
|
||||
MockClock clocks = new MockClock();
|
||||
|
||||
TestTimer timer = new TestTimer(clocks, 0, timeBase);
|
||||
timer.setTotalTime(100);
|
||||
|
||||
@@ -27,7 +27,7 @@ public class BatteryStatsUidTest extends TestCase {
|
||||
private static final String TAG = "BatteryStatsTimeBaseTest";
|
||||
|
||||
static class TestBsi extends BatteryStatsImpl {
|
||||
TestBsi(MockClocks clocks) {
|
||||
TestBsi(MockClock clocks) {
|
||||
super(clocks);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ public class BatteryUsageStatsRule implements TestRule {
|
||||
.build();
|
||||
|
||||
private final PowerProfile mPowerProfile;
|
||||
private final MockClocks mMockClocks = new MockClocks();
|
||||
private final MockClock mMockClock = new MockClock();
|
||||
private final MockBatteryStatsImpl mBatteryStats;
|
||||
|
||||
private BatteryUsageStats mBatteryUsageStats;
|
||||
@@ -63,8 +63,8 @@ public class BatteryUsageStatsRule implements TestRule {
|
||||
public BatteryUsageStatsRule(long currentTime) {
|
||||
Context context = InstrumentationRegistry.getContext();
|
||||
mPowerProfile = spy(new PowerProfile(context, true /* forTest */));
|
||||
mMockClocks.currentTime = currentTime;
|
||||
mBatteryStats = new MockBatteryStatsImpl(mMockClocks);
|
||||
mMockClock.currentTime = currentTime;
|
||||
mBatteryStats = new MockBatteryStatsImpl(mMockClock);
|
||||
mBatteryStats.setPowerProfile(mPowerProfile);
|
||||
mBatteryStats.onSystemReady();
|
||||
}
|
||||
@@ -166,12 +166,12 @@ public class BatteryUsageStatsRule implements TestRule {
|
||||
}
|
||||
|
||||
public void setTime(long realtimeMs, long uptimeMs) {
|
||||
mMockClocks.realtime = realtimeMs;
|
||||
mMockClocks.uptime = uptimeMs;
|
||||
mMockClock.realtime = realtimeMs;
|
||||
mMockClock.uptime = uptimeMs;
|
||||
}
|
||||
|
||||
public void setCurrentTime(long currentTimeMs) {
|
||||
mMockClocks.currentTime = currentTimeMs;
|
||||
mMockClock.currentTime = currentTimeMs;
|
||||
}
|
||||
|
||||
BatteryUsageStats apply(PowerCalculator... calculators) {
|
||||
@@ -191,7 +191,7 @@ public class BatteryUsageStatsRule implements TestRule {
|
||||
}
|
||||
|
||||
for (PowerCalculator calculator : calculators) {
|
||||
calculator.calculate(builder, mBatteryStats, mMockClocks.realtime, mMockClocks.uptime,
|
||||
calculator.calculate(builder, mBatteryStats, mMockClock.realtime, mMockClock.uptime,
|
||||
query);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ import java.util.Arrays;
|
||||
public class BatteryUsageStatsStoreTest {
|
||||
private static final long MAX_BATTERY_STATS_SNAPSHOT_STORAGE_BYTES = 2 * 1024;
|
||||
|
||||
private final MockClocks mMockClocks = new MockClocks();
|
||||
private final MockClock mMockClock = new MockClock();
|
||||
private MockBatteryStatsImpl mBatteryStats;
|
||||
private BatteryUsageStatsStore mBatteryUsageStatsStore;
|
||||
private BatteryUsageStatsProvider mBatteryUsageStatsProvider;
|
||||
@@ -55,8 +55,8 @@ public class BatteryUsageStatsStoreTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mMockClocks.currentTime = 123;
|
||||
mBatteryStats = new MockBatteryStatsImpl(mMockClocks);
|
||||
mMockClock.currentTime = 123;
|
||||
mBatteryStats = new MockBatteryStatsImpl(mMockClock);
|
||||
mBatteryStats.setNoAutoReset(true);
|
||||
mBatteryStats.setPowerProfile(mock(PowerProfile.class));
|
||||
mBatteryStats.onSystemReady();
|
||||
@@ -75,7 +75,7 @@ public class BatteryUsageStatsStoreTest {
|
||||
|
||||
@Test
|
||||
public void testStoreSnapshot() {
|
||||
mMockClocks.currentTime = 1_600_000;
|
||||
mMockClock.currentTime = 1_600_000;
|
||||
|
||||
prepareBatteryStats();
|
||||
mBatteryStats.resetAllStatsCmdLocked();
|
||||
@@ -99,9 +99,9 @@ public class BatteryUsageStatsStoreTest {
|
||||
public void testGarbageCollectOldSnapshots() throws Exception {
|
||||
prepareBatteryStats();
|
||||
|
||||
mMockClocks.realtime = 10_000_000;
|
||||
mMockClocks.uptime = 10_000_000;
|
||||
mMockClocks.currentTime = 10_000_000;
|
||||
mMockClock.realtime = 10_000_000;
|
||||
mMockClock.uptime = 10_000_000;
|
||||
mMockClock.currentTime = 10_000_000;
|
||||
|
||||
final int snapshotFileSize = getSnapshotFileSize();
|
||||
final int numberOfSnapshots =
|
||||
@@ -109,9 +109,9 @@ public class BatteryUsageStatsStoreTest {
|
||||
for (int i = 0; i < numberOfSnapshots + 2; i++) {
|
||||
mBatteryStats.resetAllStatsCmdLocked();
|
||||
|
||||
mMockClocks.realtime += 10_000_000;
|
||||
mMockClocks.uptime += 10_000_000;
|
||||
mMockClocks.currentTime += 10_000_000;
|
||||
mMockClock.realtime += 10_000_000;
|
||||
mMockClock.uptime += 10_000_000;
|
||||
mMockClock.currentTime += 10_000_000;
|
||||
prepareBatteryStats();
|
||||
}
|
||||
|
||||
@@ -137,11 +137,11 @@ public class BatteryUsageStatsStoreTest {
|
||||
private void prepareBatteryStats() {
|
||||
mBatteryStats.setBatteryStateLocked(BatteryManager.BATTERY_STATUS_DISCHARGING, 100,
|
||||
/* plugType */ 0, 90, 72, 3700, 3_600_000, 4_000_000, 0,
|
||||
mMockClocks.realtime, mMockClocks.uptime, mMockClocks.currentTime);
|
||||
mMockClock.realtime, mMockClock.uptime, mMockClock.currentTime);
|
||||
mBatteryStats.setBatteryStateLocked(BatteryManager.BATTERY_STATUS_DISCHARGING, 100,
|
||||
/* plugType */ 0, 85, 72, 3700, 3_000_000, 4_000_000, 0,
|
||||
mMockClocks.realtime + 500_000, mMockClocks.uptime + 500_000,
|
||||
mMockClocks.currentTime + 500_000);
|
||||
mMockClock.realtime + 500_000, mMockClock.uptime + 500_000,
|
||||
mMockClock.currentTime + 500_000);
|
||||
}
|
||||
|
||||
private void clearDirectory(File dir) {
|
||||
|
||||
@@ -228,7 +228,7 @@ public class BatteryUsageStatsTest {
|
||||
}
|
||||
|
||||
private BatteryUsageStats.Builder buildBatteryUsageStats1(boolean includeUserBatteryConsumer) {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
final MockBatteryStatsImpl batteryStats = new MockBatteryStatsImpl(clocks);
|
||||
|
||||
final BatteryUsageStats.Builder builder =
|
||||
@@ -267,7 +267,7 @@ public class BatteryUsageStatsTest {
|
||||
}
|
||||
|
||||
private BatteryUsageStats.Builder buildBatteryUsageStats2(String[] customPowerComponentNames) {
|
||||
final MockClocks clocks = new MockClocks();
|
||||
final MockClock clocks = new MockClock();
|
||||
final MockBatteryStatsImpl batteryStats = new MockBatteryStatsImpl(clocks);
|
||||
|
||||
final BatteryUsageStats.Builder builder =
|
||||
|
||||
@@ -23,7 +23,6 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.FileUtils;
|
||||
import android.os.SystemClock;
|
||||
import android.util.SparseArray;
|
||||
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
@@ -56,7 +55,7 @@ public class KernelCpuUidUserSysTimeReaderTest {
|
||||
private KernelCpuUidTimeReader.KernelCpuUidUserSysTimeReader mReader;
|
||||
private VerifiableCallback mCallback;
|
||||
|
||||
private Random mRand = new Random(12345);
|
||||
private final Random mRand = new Random(12345);
|
||||
private final int[] mUids = {0, 1, 22, 333, 4444, 55555};
|
||||
private final long[][] mInitialTimes = new long[][]{
|
||||
{15334000, 310964000},
|
||||
@@ -67,6 +66,8 @@ public class KernelCpuUidUserSysTimeReaderTest {
|
||||
{47000, 17000}
|
||||
};
|
||||
|
||||
private final MockClock mMockClock = new MockClock();
|
||||
|
||||
private Context getContext() {
|
||||
return InstrumentationRegistry.getContext();
|
||||
}
|
||||
@@ -76,7 +77,8 @@ public class KernelCpuUidUserSysTimeReaderTest {
|
||||
mTestDir = getContext().getDir("test", Context.MODE_PRIVATE);
|
||||
mTestFile = new File(mTestDir, "test.file");
|
||||
mReader = new KernelCpuUidUserSysTimeReader(
|
||||
new KernelCpuProcStringReader(mTestFile.getAbsolutePath()), false);
|
||||
new KernelCpuProcStringReader(mTestFile.getAbsolutePath(), mMockClock),
|
||||
false, mMockClock);
|
||||
mCallback = new VerifiableCallback();
|
||||
}
|
||||
|
||||
@@ -87,10 +89,13 @@ public class KernelCpuUidUserSysTimeReaderTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@SkipPresubmit("b/180015146")
|
||||
public void testThrottler() throws Exception {
|
||||
mMockClock.realtime = 1000;
|
||||
|
||||
mReader = new KernelCpuUidUserSysTimeReader(
|
||||
new KernelCpuProcStringReader(mTestFile.getAbsolutePath()), true);
|
||||
new KernelCpuProcStringReader(mTestFile.getAbsolutePath(), mMockClock),
|
||||
true, mMockClock);
|
||||
|
||||
mReader.setThrottle(500);
|
||||
|
||||
writeToFile(uidLines(mUids, mInitialTimes));
|
||||
@@ -103,8 +108,7 @@ public class KernelCpuUidUserSysTimeReaderTest {
|
||||
mReader.readDelta(false, mCallback);
|
||||
assertEquals(0, mCallback.mData.size());
|
||||
|
||||
// TODO(b/180473895): Replace sleeps with injected simulated time.
|
||||
SystemClock.sleep(600);
|
||||
mMockClock.realtime += 600;
|
||||
|
||||
long[][] times2 = increaseTime(times1);
|
||||
writeToFile(uidLines(mUids, times2));
|
||||
@@ -123,7 +127,7 @@ public class KernelCpuUidUserSysTimeReaderTest {
|
||||
mReader.readDelta(true, mCallback);
|
||||
assertEquals(6, mCallback.mData.size());
|
||||
|
||||
SystemClock.sleep(600);
|
||||
mMockClock.realtime += 600;
|
||||
|
||||
long[][] times4 = increaseTime(times3);
|
||||
writeToFile(uidLines(mUids, times4));
|
||||
@@ -138,7 +142,7 @@ public class KernelCpuUidUserSysTimeReaderTest {
|
||||
mReader.readDelta(false, mCallback);
|
||||
assertEquals(0, mCallback.mData.size());
|
||||
|
||||
SystemClock.sleep(600);
|
||||
mMockClock.realtime += 600;
|
||||
|
||||
mCallback.clear();
|
||||
mReader.readDelta(false, mCallback);
|
||||
|
||||
@@ -36,13 +36,13 @@ import java.util.concurrent.Future;
|
||||
* Mocks a BatteryStatsImpl object.
|
||||
*/
|
||||
public class MockBatteryStatsImpl extends BatteryStatsImpl {
|
||||
public BatteryStatsImpl.Clocks clocks;
|
||||
public Clock mClock;
|
||||
public boolean mForceOnBattery;
|
||||
private NetworkStats mNetworkStats;
|
||||
|
||||
MockBatteryStatsImpl(Clocks clocks) {
|
||||
super(clocks);
|
||||
this.clocks = mClocks;
|
||||
MockBatteryStatsImpl(Clock clock) {
|
||||
super(clock);
|
||||
this.mClock = mClock;
|
||||
initTimersAndCounters();
|
||||
|
||||
setExternalStatsSyncLocked(new DummyExternalStatsSync());
|
||||
@@ -54,7 +54,7 @@ public class MockBatteryStatsImpl extends BatteryStatsImpl {
|
||||
}
|
||||
|
||||
MockBatteryStatsImpl() {
|
||||
this(new MockClocks());
|
||||
this(new MockClock());
|
||||
}
|
||||
|
||||
public void initMeasuredEnergyStats() {
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package com.android.internal.os;
|
||||
|
||||
public class MockClocks implements BatteryStatsImpl.Clocks {
|
||||
public class MockClock extends Clock {
|
||||
/** ElapsedRealtime in ms */
|
||||
public long realtime;
|
||||
/** Uptime in ms */
|
||||
Reference in New Issue
Block a user