blob: 10c110e274580d24a714e4ad0260620bd56cd805 [file] [log] [blame]
Enrico Granata517a1e02017-09-20 16:15:50 -07001/*
2 * Copyright (C) 2017 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
17package com.android.car;
18
Enrico Granata286cd8e2017-09-25 14:46:49 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Enrico Granata517a1e02017-09-20 16:15:50 -070021import android.car.Car;
22import android.car.storagemonitoring.CarStorageMonitoringManager;
Enrico Granataa97fce22017-10-27 14:57:34 -070023import android.car.storagemonitoring.UidIoStats;
Enrico Granata7e0150d2017-11-06 17:20:17 -080024import android.car.storagemonitoring.UidIoStatsDelta;
25import android.car.storagemonitoring.UidIoRecord;
Enrico Granata1172f882017-09-21 14:51:30 -070026import android.car.storagemonitoring.WearEstimate;
Enrico Granata286cd8e2017-09-25 14:46:49 -070027import android.car.storagemonitoring.WearEstimateChange;
Enrico Granata517a1e02017-09-20 16:15:50 -070028import android.test.suitebuilder.annotation.MediumTest;
Enrico Granata286cd8e2017-09-25 14:46:49 -070029import android.util.JsonWriter;
30import android.util.Log;
Enrico Granatab19bc322017-10-12 12:25:06 -070031import android.util.Pair;
Enrico Granataa97fce22017-10-27 14:57:34 -070032import android.util.SparseArray;
33import com.android.car.storagemonitoring.UidIoStatsProvider;
Enrico Granata286cd8e2017-09-25 14:46:49 -070034import com.android.car.storagemonitoring.WearEstimateRecord;
35import com.android.car.storagemonitoring.WearHistory;
Enrico Granata517a1e02017-09-20 16:15:50 -070036import com.android.car.storagemonitoring.WearInformation;
Enrico Granatab19bc322017-10-12 12:25:06 -070037import com.android.car.storagemonitoring.WearInformationProvider;
38import com.android.car.systeminterface.StorageMonitoringInterface;
39import com.android.car.systeminterface.SystemInterface;
40import com.android.car.systeminterface.SystemStateInterface;
41import com.android.car.systeminterface.TimeInterface;
Enrico Granata286cd8e2017-09-25 14:46:49 -070042import java.io.File;
43import java.io.FileWriter;
44import java.io.IOException;
Enrico Granatab19bc322017-10-12 12:25:06 -070045import java.time.Duration;
Enrico Granata286cd8e2017-09-25 14:46:49 -070046import java.time.Instant;
Enrico Granatab19bc322017-10-12 12:25:06 -070047import java.util.ArrayList;
Enrico Granataa97fce22017-10-27 14:57:34 -070048import java.util.Arrays;
Enrico Granatab19bc322017-10-12 12:25:06 -070049import java.util.Comparator;
Enrico Granata286cd8e2017-09-25 14:46:49 -070050import java.util.HashMap;
51import java.util.List;
52import java.util.Map;
Enrico Granata517a1e02017-09-20 16:15:50 -070053
54/** Test the public entry points for the CarStorageMonitoringManager */
55@MediumTest
56public class CarStorageMonitoringTest extends MockedCarTestBase {
57 private static final String TAG = CarStorageMonitoringTest.class.getSimpleName();
58
Enrico Granata1172f882017-09-21 14:51:30 -070059 private static final WearInformation DEFAULT_WEAR_INFORMATION =
Enrico Granata286cd8e2017-09-25 14:46:49 -070060 new WearInformation(30, 0, WearInformation.PRE_EOL_INFO_NORMAL);
61
Enrico Granataa97fce22017-10-27 14:57:34 -070062 private static final class TestData {
63 static final TestData DEFAULT = new TestData(0, DEFAULT_WEAR_INFORMATION, null, null);
Enrico Granata286cd8e2017-09-25 14:46:49 -070064
65 final long uptime;
66 @NonNull
67 final WearInformation wearInformation;
68 @Nullable
69 final WearHistory wearHistory;
Enrico Granataa97fce22017-10-27 14:57:34 -070070 @NonNull
Enrico Granata7e0150d2017-11-06 17:20:17 -080071 final UidIoRecord[] ioStats;
Enrico Granata286cd8e2017-09-25 14:46:49 -070072
Enrico Granataa97fce22017-10-27 14:57:34 -070073 TestData(long uptime,
74 @Nullable WearInformation wearInformation,
75 @Nullable WearHistory wearHistory,
Enrico Granata7e0150d2017-11-06 17:20:17 -080076 @Nullable UidIoRecord[] ioStats) {
Enrico Granata286cd8e2017-09-25 14:46:49 -070077 if (wearInformation == null) wearInformation = DEFAULT_WEAR_INFORMATION;
Enrico Granata7e0150d2017-11-06 17:20:17 -080078 if (ioStats == null) ioStats = new UidIoRecord[0];
Enrico Granata286cd8e2017-09-25 14:46:49 -070079 this.uptime = uptime;
80 this.wearInformation = wearInformation;
81 this.wearHistory = wearHistory;
Enrico Granataa97fce22017-10-27 14:57:34 -070082 this.ioStats = ioStats;
Enrico Granata286cd8e2017-09-25 14:46:49 -070083 }
84 }
85
Enrico Granataa97fce22017-10-27 14:57:34 -070086 private static final Map<String, TestData> PER_TEST_DATA =
Enrico Granata7e0150d2017-11-06 17:20:17 -080087 new HashMap<String, TestData>() {
88 {
89 put("testReadWearHistory",
90 new TestData(6500, DEFAULT_WEAR_INFORMATION,
91 WearHistory.fromRecords(
92 WearEstimateRecord.Builder.newBuilder()
93 .fromWearEstimate(WearEstimate.UNKNOWN_ESTIMATE)
94 .toWearEstimate(new WearEstimate(10, 0))
95 .atUptime(1000)
96 .atTimestamp(Instant.ofEpochMilli(5000)).build(),
97 WearEstimateRecord.Builder.newBuilder()
98 .fromWearEstimate(new WearEstimate(10, 0))
99 .toWearEstimate(new WearEstimate(20, 0))
100 .atUptime(4000)
101 .atTimestamp(Instant.ofEpochMilli(12000)).build(),
102 WearEstimateRecord.Builder.newBuilder()
103 .fromWearEstimate(new WearEstimate(20, 0))
104 .toWearEstimate(new WearEstimate(30, 0))
105 .atUptime(6500)
106 .atTimestamp(Instant.ofEpochMilli(17000)).build()), null));
Enrico Granata286cd8e2017-09-25 14:46:49 -0700107
Enrico Granata7e0150d2017-11-06 17:20:17 -0800108 put("testNotAcceptableWearEvent",
109 new TestData(2520006499L,
110 new WearInformation(40, 0, WearInformation.PRE_EOL_INFO_NORMAL),
111 WearHistory.fromRecords(
112 WearEstimateRecord.Builder.newBuilder()
113 .fromWearEstimate(WearEstimate.UNKNOWN_ESTIMATE)
114 .toWearEstimate(new WearEstimate(10, 0))
115 .atUptime(1000)
116 .atTimestamp(Instant.ofEpochMilli(5000)).build(),
117 WearEstimateRecord.Builder.newBuilder()
118 .fromWearEstimate(new WearEstimate(10, 0))
119 .toWearEstimate(new WearEstimate(20, 0))
120 .atUptime(4000)
121 .atTimestamp(Instant.ofEpochMilli(12000)).build(),
122 WearEstimateRecord.Builder.newBuilder()
123 .fromWearEstimate(new WearEstimate(20, 0))
124 .toWearEstimate(new WearEstimate(30, 0))
125 .atUptime(6500)
126 .atTimestamp(Instant.ofEpochMilli(17000)).build()), null));
Enrico Granata286cd8e2017-09-25 14:46:49 -0700127
Enrico Granata7e0150d2017-11-06 17:20:17 -0800128 put("testAcceptableWearEvent",
129 new TestData(2520006501L,
130 new WearInformation(40, 0, WearInformation.PRE_EOL_INFO_NORMAL),
131 WearHistory.fromRecords(
132 WearEstimateRecord.Builder.newBuilder()
133 .fromWearEstimate(WearEstimate.UNKNOWN_ESTIMATE)
134 .toWearEstimate(new WearEstimate(10, 0))
135 .atUptime(1000)
136 .atTimestamp(Instant.ofEpochMilli(5000)).build(),
137 WearEstimateRecord.Builder.newBuilder()
138 .fromWearEstimate(new WearEstimate(10, 0))
139 .toWearEstimate(new WearEstimate(20, 0))
140 .atUptime(4000)
141 .atTimestamp(Instant.ofEpochMilli(12000)).build(),
142 WearEstimateRecord.Builder.newBuilder()
143 .fromWearEstimate(new WearEstimate(20, 0))
144 .toWearEstimate(new WearEstimate(30, 0))
145 .atUptime(6500)
146 .atTimestamp(Instant.ofEpochMilli(17000)).build()), null));
Enrico Granataa97fce22017-10-27 14:57:34 -0700147
Enrico Granata7e0150d2017-11-06 17:20:17 -0800148 put("testBootIoStats",
149 new TestData(1000L,
150 new WearInformation(0, 0, WearInformation.PRE_EOL_INFO_NORMAL),
151 null,
152 new UidIoRecord[]{
153 new UidIoRecord(0, 5000, 6000, 3000, 1000, 1,
154 0, 0, 0, 0, 0),
155 new UidIoRecord(1000, 200, 5000, 0, 4000, 0,
156 1000, 0, 500, 0, 0)}));
Enrico Granata0f72b742017-11-02 18:26:41 -0700157
Enrico Granata7e0150d2017-11-06 17:20:17 -0800158 put("testAggregateIoStats",
159 new TestData(1000L,
160 new WearInformation(0, 0, WearInformation.PRE_EOL_INFO_NORMAL),
161 null,
162 new UidIoRecord[]{
163 new UidIoRecord(0, 5000, 6000, 3000, 1000, 1,
164 0, 0, 0, 0, 0),
165 new UidIoRecord(1000, 200, 5000, 0, 4000, 0,
166 1000, 0, 500, 0, 0)}));
Enrico Granata0f72b742017-11-02 18:26:41 -0700167
Enrico Granata7e0150d2017-11-06 17:20:17 -0800168 put("testIoStatsDeltas",
169 new TestData(1000L,
170 new WearInformation(0, 0, WearInformation.PRE_EOL_INFO_NORMAL),
171 null,
172 new UidIoRecord[]{
173 new UidIoRecord(0, 5000, 6000, 3000, 1000, 1,
174 0, 0, 0, 0, 0)}));
175 }};
Enrico Granata1172f882017-09-21 14:51:30 -0700176
Enrico Granatab19bc322017-10-12 12:25:06 -0700177 private final MockSystemStateInterface mMockSystemStateInterface =
178 new MockSystemStateInterface();
179 private final MockStorageMonitoringInterface mMockStorageMonitoringInterface =
180 new MockStorageMonitoringInterface();
Enrico Granata0f72b742017-11-02 18:26:41 -0700181 private final MockTimeInterface mMockTimeInterface =
182 new MockTimeInterface();
Enrico Granatab19bc322017-10-12 12:25:06 -0700183
Enrico Granata517a1e02017-09-20 16:15:50 -0700184 private CarStorageMonitoringManager mCarStorageMonitoringManager;
185
186 @Override
Enrico Granatab19bc322017-10-12 12:25:06 -0700187 protected synchronized SystemInterface.Builder getSystemInterfaceBuilder() {
188 SystemInterface.Builder builder = super.getSystemInterfaceBuilder();
189 return builder.withSystemStateInterface(mMockSystemStateInterface)
190 .withStorageMonitoringInterface(mMockStorageMonitoringInterface)
Enrico Granata0f72b742017-11-02 18:26:41 -0700191 .withTimeInterface(mMockTimeInterface);
Enrico Granatab19bc322017-10-12 12:25:06 -0700192 }
193
194 @Override
Enrico Granata517a1e02017-09-20 16:15:50 -0700195 protected synchronized void configureFakeSystemInterface() {
Enrico Granata286cd8e2017-09-25 14:46:49 -0700196 try {
197 final String testName = getName();
Enrico Granataa97fce22017-10-27 14:57:34 -0700198 final TestData wearData = PER_TEST_DATA.getOrDefault(testName, TestData.DEFAULT);
Enrico Granata286cd8e2017-09-25 14:46:49 -0700199 final WearHistory wearHistory = wearData.wearHistory;
200
Enrico Granatab19bc322017-10-12 12:25:06 -0700201 mMockStorageMonitoringInterface.setWearInformation(wearData.wearInformation);
Enrico Granata286cd8e2017-09-25 14:46:49 -0700202
203 if (wearHistory != null) {
204 File wearHistoryFile = new File(getFakeSystemInterface().getFilesDir(),
205 CarStorageMonitoringService.WEAR_INFO_FILENAME);
206 try (JsonWriter jsonWriter = new JsonWriter(new FileWriter(wearHistoryFile))) {
207 wearHistory.writeToJson(jsonWriter);
208 }
209 }
210
211 if (wearData.uptime > 0) {
212 File uptimeFile = new File(getFakeSystemInterface().getFilesDir(),
Enrico Granatab19bc322017-10-12 12:25:06 -0700213 CarStorageMonitoringService.UPTIME_TRACKER_FILENAME);
Enrico Granata286cd8e2017-09-25 14:46:49 -0700214 try (JsonWriter jsonWriter = new JsonWriter(new FileWriter(uptimeFile))) {
215 jsonWriter.beginObject();
216 jsonWriter.name("uptime").value(wearData.uptime);
217 jsonWriter.endObject();
218 }
219 }
Enrico Granataa97fce22017-10-27 14:57:34 -0700220
221 Arrays.stream(wearData.ioStats).forEach(
222 mMockStorageMonitoringInterface::addIoStatsRecord);
223
Enrico Granata286cd8e2017-09-25 14:46:49 -0700224 } catch (IOException e) {
225 Log.e(TAG, "failed to configure fake system interface", e);
226 fail("failed to configure fake system interface instance");
227 }
Enrico Granatab19bc322017-10-12 12:25:06 -0700228
Enrico Granata517a1e02017-09-20 16:15:50 -0700229 }
230
231 @Override
232 protected void setUp() throws Exception {
233 super.setUp();
234
Enrico Granatab19bc322017-10-12 12:25:06 -0700235 mMockSystemStateInterface.executeBootCompletedActions();
Enrico Granatacf53fd72017-09-28 10:45:44 -0700236
Enrico Granata517a1e02017-09-20 16:15:50 -0700237 mCarStorageMonitoringManager =
238 (CarStorageMonitoringManager) getCar().getCarManager(Car.STORAGE_MONITORING_SERVICE);
239 }
240
241 public void testReadPreEolInformation() throws Exception {
Enrico Granata1172f882017-09-21 14:51:30 -0700242 assertEquals(DEFAULT_WEAR_INFORMATION.preEolInfo,
Enrico Granata517a1e02017-09-20 16:15:50 -0700243 mCarStorageMonitoringManager.getPreEolIndicatorStatus());
244 }
Enrico Granata1172f882017-09-21 14:51:30 -0700245
246 public void testReadWearEstimate() throws Exception {
247 final WearEstimate wearEstimate = mCarStorageMonitoringManager.getWearEstimate();
248
249 assertNotNull(wearEstimate);
250 assertEquals(DEFAULT_WEAR_INFORMATION.lifetimeEstimateA, wearEstimate.typeA);
251 assertEquals(DEFAULT_WEAR_INFORMATION.lifetimeEstimateB, wearEstimate.typeB);
252 }
Enrico Granata286cd8e2017-09-25 14:46:49 -0700253
254 public void testReadWearHistory() throws Exception {
255 final List<WearEstimateChange> wearEstimateChanges =
256 mCarStorageMonitoringManager.getWearEstimateHistory();
257
258 assertNotNull(wearEstimateChanges);
259 assertFalse(wearEstimateChanges.isEmpty());
260
Enrico Granataa97fce22017-10-27 14:57:34 -0700261 final WearHistory expectedWearHistory = PER_TEST_DATA.get(getName()).wearHistory;
Enrico Granata286cd8e2017-09-25 14:46:49 -0700262
263 assertEquals(expectedWearHistory.size(), wearEstimateChanges.size());
264 for (int i = 0; i < wearEstimateChanges.size(); ++i) {
265 final WearEstimateRecord expected = expectedWearHistory.get(i);
266 final WearEstimateChange actual = wearEstimateChanges.get(i);
267
268 assertTrue(expected.isSameAs(actual));
269 }
270 }
271
272 private void checkLastWearEvent(boolean isAcceptable) throws Exception {
273 final List<WearEstimateChange> wearEstimateChanges =
274 mCarStorageMonitoringManager.getWearEstimateHistory();
275
276 assertNotNull(wearEstimateChanges);
277 assertFalse(wearEstimateChanges.isEmpty());
278
Enrico Granataa97fce22017-10-27 14:57:34 -0700279 final TestData wearData = PER_TEST_DATA.get(getName());
Enrico Granata286cd8e2017-09-25 14:46:49 -0700280
281 final WearInformation expectedCurrentWear = wearData.wearInformation;
282 final WearEstimate expectedPreviousWear = wearData.wearHistory.getLast().getNewWearEstimate();
283
284 final WearEstimateChange actualCurrentWear =
285 wearEstimateChanges.get(wearEstimateChanges.size() - 1);
286
287 assertEquals(isAcceptable, actualCurrentWear.isAcceptableDegradation);
288 assertEquals(expectedCurrentWear.toWearEstimate(), actualCurrentWear.newEstimate);
289 assertEquals(expectedPreviousWear, actualCurrentWear.oldEstimate);
290 }
291
292 public void testNotAcceptableWearEvent() throws Exception {
293 checkLastWearEvent(false);
294 }
295
296 public void testAcceptableWearEvent() throws Exception {
297 checkLastWearEvent(true);
298 }
Enrico Granatab19bc322017-10-12 12:25:06 -0700299
Enrico Granataa97fce22017-10-27 14:57:34 -0700300 public void testBootIoStats() throws Exception {
301 final List<UidIoStats> bootIoStats =
302 mCarStorageMonitoringManager.getBootIoStats();
303
304 assertNotNull(bootIoStats);
305 assertFalse(bootIoStats.isEmpty());
306
Enrico Granata7e0150d2017-11-06 17:20:17 -0800307 final UidIoRecord[] bootIoRecords = PER_TEST_DATA.get(getName()).ioStats;
Enrico Granataa97fce22017-10-27 14:57:34 -0700308
309 bootIoStats.forEach(uidIoStats -> assertTrue(Arrays.stream(bootIoRecords).anyMatch(
310 ioRecord -> uidIoStats.representsSameMetrics(ioRecord))));
311 }
312
Enrico Granata0f72b742017-11-02 18:26:41 -0700313 public void testAggregateIoStats() throws Exception {
Enrico Granata7e0150d2017-11-06 17:20:17 -0800314 UidIoRecord oldRecord1000 = mMockStorageMonitoringInterface.getIoStatsRecord(1000);
Enrico Granata0f72b742017-11-02 18:26:41 -0700315
Enrico Granata7e0150d2017-11-06 17:20:17 -0800316 UidIoRecord newRecord1000 = new UidIoRecord(1000,
Enrico Granata0f72b742017-11-02 18:26:41 -0700317 oldRecord1000.foreground_rchar,
318 oldRecord1000.foreground_wchar + 50,
319 oldRecord1000.foreground_read_bytes,
320 oldRecord1000.foreground_write_bytes + 100,
321 oldRecord1000.foreground_fsync + 1,
322 oldRecord1000.background_rchar,
323 oldRecord1000.background_wchar,
324 oldRecord1000.background_read_bytes,
325 oldRecord1000.background_write_bytes,
326 oldRecord1000.background_fsync);
327
328 mMockStorageMonitoringInterface.addIoStatsRecord(newRecord1000);
329
Enrico Granata7e0150d2017-11-06 17:20:17 -0800330 UidIoRecord record2000 = new UidIoRecord(2000,
Enrico Granata0f72b742017-11-02 18:26:41 -0700331 1024,
332 2048,
333 0,
334 1024,
335 1,
336 0,
337 0,
338 0,
339 0,
340 0);
341
342 mMockStorageMonitoringInterface.addIoStatsRecord(record2000);
343
344 mMockTimeInterface.tick();
345
346 List<UidIoStats> aggregateIoStats = mCarStorageMonitoringManager.getAggregateIoStats();
347
348 assertNotNull(aggregateIoStats);
349 assertFalse(aggregateIoStats.isEmpty());
350
351 aggregateIoStats.forEach(serviceIoStat -> {
Enrico Granata7e0150d2017-11-06 17:20:17 -0800352 UidIoRecord mockIoStat = mMockStorageMonitoringInterface.getIoStatsRecord(
Enrico Granata0f72b742017-11-02 18:26:41 -0700353 serviceIoStat.uid);
354
355 assertNotNull(mockIoStat);
356
357 assertTrue(serviceIoStat.representsSameMetrics(mockIoStat));
358 });
359 }
360
Enrico Granata7e0150d2017-11-06 17:20:17 -0800361 public void testIoStatsDeltas() throws Exception {
362 UidIoRecord oldRecord0 = mMockStorageMonitoringInterface.getIoStatsRecord(0);
363
364 UidIoRecord newRecord0 = new UidIoRecord(0,
365 oldRecord0.foreground_rchar,
366 oldRecord0.foreground_wchar + 100,
367 oldRecord0.foreground_read_bytes,
368 oldRecord0.foreground_write_bytes + 50,
369 oldRecord0.foreground_fsync,
370 oldRecord0.background_rchar,
371 oldRecord0.background_wchar,
372 oldRecord0.background_read_bytes + 100,
373 oldRecord0.background_write_bytes,
374 oldRecord0.background_fsync);
375
376 mMockStorageMonitoringInterface.addIoStatsRecord(newRecord0);
377 mMockTimeInterface.setUptime(500).tick();
378
379 List<UidIoStatsDelta> deltas = mCarStorageMonitoringManager.getIoStatsDeltas();
380 assertNotNull(deltas);
381 assertEquals(1, deltas.size());
382
383 UidIoStatsDelta delta0 = deltas.get(0);
384 assertNotNull(delta0);
385 assertEquals(500, delta0.getTimestamp());
386
387 List<UidIoStats> delta0Stats = delta0.getStats();
388 assertNotNull(delta0Stats);
389 assertEquals(1, delta0Stats.size());
390
391 UidIoStats deltaRecord0 = delta0Stats.get(0);
392
393 assertTrue(deltaRecord0.representsSameMetrics(newRecord0.delta(oldRecord0)));
394
395 UidIoRecord newerRecord0 = new UidIoRecord(0,
396 newRecord0.foreground_rchar + 200,
397 newRecord0.foreground_wchar + 10,
398 newRecord0.foreground_read_bytes,
399 newRecord0.foreground_write_bytes,
400 newRecord0.foreground_fsync,
401 newRecord0.background_rchar,
402 newRecord0.background_wchar + 100,
403 newRecord0.background_read_bytes,
404 newRecord0.background_write_bytes + 30,
405 newRecord0.background_fsync + 2);
406
407 mMockStorageMonitoringInterface.addIoStatsRecord(newerRecord0);
408 mMockTimeInterface.setUptime(1000).tick();
409
410 deltas = mCarStorageMonitoringManager.getIoStatsDeltas();
411 assertNotNull(deltas);
412 assertEquals(2, deltas.size());
413
414 delta0 = deltas.get(0);
415 assertNotNull(delta0);
416 assertEquals(500, delta0.getTimestamp());
417
418 delta0Stats = delta0.getStats();
419 assertNotNull(delta0Stats);
420 assertEquals(1, delta0Stats.size());
421
422 deltaRecord0 = delta0Stats.get(0);
423
424 assertTrue(deltaRecord0.representsSameMetrics(newRecord0.delta(oldRecord0)));
425
426 UidIoStatsDelta delta1 = deltas.get(1);
427 assertNotNull(delta1);
428 assertEquals(1000, delta1.getTimestamp());
429
430 List<UidIoStats> delta1Stats = delta1.getStats();
431 assertNotNull(delta1Stats);
432 assertEquals(1, delta1Stats.size());
433
434 deltaRecord0 = delta1Stats.get(0);
435
436 assertTrue(deltaRecord0.representsSameMetrics(newerRecord0.delta(newRecord0)));
437 }
438
Enrico Granatab19bc322017-10-12 12:25:06 -0700439 static final class MockStorageMonitoringInterface implements StorageMonitoringInterface,
440 WearInformationProvider {
441 private WearInformation mWearInformation = null;
Enrico Granata7e0150d2017-11-06 17:20:17 -0800442 private SparseArray<UidIoRecord> mIoStats = new SparseArray<>();
Enrico Granataa97fce22017-10-27 14:57:34 -0700443 private UidIoStatsProvider mIoStatsProvider = () -> mIoStats;
Enrico Granatab19bc322017-10-12 12:25:06 -0700444
445 void setWearInformation(WearInformation wearInformation) {
446 mWearInformation = wearInformation;
447 }
448
Enrico Granata7e0150d2017-11-06 17:20:17 -0800449 void addIoStatsRecord(UidIoRecord record) {
Enrico Granataa97fce22017-10-27 14:57:34 -0700450 mIoStats.append(record.uid, record);
451 }
452
Enrico Granata7e0150d2017-11-06 17:20:17 -0800453 UidIoRecord getIoStatsRecord(int uid) {
Enrico Granata0f72b742017-11-02 18:26:41 -0700454 return mIoStats.get(uid);
455 }
456
Enrico Granataa97fce22017-10-27 14:57:34 -0700457 void deleteIoStatsRecord(int uid) {
458 mIoStats.delete(uid);
459 }
460
Enrico Granatab19bc322017-10-12 12:25:06 -0700461 @Override
462 public WearInformation load() {
463 return mWearInformation;
464 }
465
466 @Override
467 public WearInformationProvider[] getFlashWearInformationProviders() {
468 return new WearInformationProvider[] {this};
469 }
Enrico Granataa97fce22017-10-27 14:57:34 -0700470
471 @Override
472 public UidIoStatsProvider getUidIoStatsProvider() {
473 return mIoStatsProvider;
474 }
Enrico Granatab19bc322017-10-12 12:25:06 -0700475 }
476
477 static final class MockTimeInterface implements TimeInterface {
Enrico Granata0f72b742017-11-02 18:26:41 -0700478 private final List<Pair<Runnable, Long>> mActionsList = new ArrayList<>();
Enrico Granata7e0150d2017-11-06 17:20:17 -0800479 private long mUptime = 0;
Enrico Granatab19bc322017-10-12 12:25:06 -0700480
481 @Override
482 public long getUptime(boolean includeDeepSleepTime) {
Enrico Granata7e0150d2017-11-06 17:20:17 -0800483 return mUptime;
Enrico Granatab19bc322017-10-12 12:25:06 -0700484 }
485
486 @Override
Enrico Granata0f72b742017-11-02 18:26:41 -0700487 public void scheduleAction(Runnable r, long delayMs) {
488 mActionsList.add(Pair.create(r, delayMs));
489 mActionsList.sort(Comparator.comparing(d -> d.second));
490 }
Enrico Granatab19bc322017-10-12 12:25:06 -0700491
492 @Override
Enrico Granata0f72b742017-11-02 18:26:41 -0700493 public void cancelAllActions() {
494 mActionsList.clear();
495 }
496
497 void tick() {
498 mActionsList.forEach(pair -> pair.first.run());
499 }
Enrico Granata7e0150d2017-11-06 17:20:17 -0800500
501 MockTimeInterface setUptime(long time) {
502 mUptime = time;
503 return this;
504 }
Enrico Granatab19bc322017-10-12 12:25:06 -0700505 }
506
507 static final class MockSystemStateInterface implements SystemStateInterface {
508 private final List<Pair<Runnable, Duration>> mActionsList = new ArrayList<>();
509
510 @Override
511 public void shutdown() {}
512
513 @Override
514 public void enterDeepSleep(int wakeupTimeSec) {}
515
516 @Override
517 public void scheduleActionForBootCompleted(Runnable action, Duration delay) {
518 mActionsList.add(Pair.create(action, delay));
519 mActionsList.sort(Comparator.comparing(d -> d.second));
520 }
521
522 void executeBootCompletedActions() {
523 for (Pair<Runnable, Duration> action : mActionsList) {
524 action.first.run();
525 }
526 }
527 }
Enrico Granata517a1e02017-09-20 16:15:50 -0700528}