Test processsing latest I/O stats in CarWatchdogService.
Test: atest CarWatchdogServiceUnitTest CarWatchdogDaemonHelperTest
libwatchdog_test
Bug: 170741935
Bug: 184310189
Change-Id: I84ccb9e2463a7bface8083778138cafd6993187a
diff --git a/tests/carservice_unit_test/src/com/android/car/watchdog/ResourceOveruseStatsSubject.java b/tests/carservice_unit_test/src/com/android/car/watchdog/ResourceOveruseStatsSubject.java
new file mode 100644
index 0000000..6b7b7ba
--- /dev/null
+++ b/tests/carservice_unit_test/src/com/android/car/watchdog/ResourceOveruseStatsSubject.java
@@ -0,0 +1,79 @@
+/*
+ * 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.car.watchdog;
+
+import static com.google.common.truth.Truth.assertAbout;
+
+import android.annotation.Nullable;
+import android.car.watchdog.ResourceOveruseStats;
+
+import com.google.common.truth.Correspondence;
+import com.google.common.truth.FailureMetadata;
+import com.google.common.truth.Subject;
+import com.google.common.truth.Truth;
+
+import java.util.Arrays;
+
+public final class ResourceOveruseStatsSubject extends Subject {
+ // Boiler-plate Subject.Factory for ResourceOveruseStatsSubject
+ private static final Subject.Factory<com.android.car.watchdog.ResourceOveruseStatsSubject,
+ Iterable<ResourceOveruseStats>> RESOURCE_OVERUSE_STATS_SUBJECT_FACTORY =
+ com.android.car.watchdog.ResourceOveruseStatsSubject::new;
+
+ private final Iterable<ResourceOveruseStats> mActual;
+
+ // User-defined entry point
+ public static ResourceOveruseStatsSubject assertThat(
+ @Nullable Iterable<ResourceOveruseStats> stats) {
+ return assertAbout(RESOURCE_OVERUSE_STATS_SUBJECT_FACTORY).that(stats);
+ }
+
+ public static Subject.Factory<ResourceOveruseStatsSubject, Iterable<ResourceOveruseStats>>
+ resourceOveruseStats() {
+ return RESOURCE_OVERUSE_STATS_SUBJECT_FACTORY;
+ }
+
+ private ResourceOveruseStatsSubject(FailureMetadata failureMetadata,
+ @Nullable Iterable<ResourceOveruseStats> iterableSubject) {
+ super(failureMetadata, iterableSubject);
+ this.mActual = iterableSubject;
+ }
+
+ public void containsExactly(ResourceOveruseStats... stats) {
+ containsExactlyElementsIn(Arrays.asList(stats));
+ }
+
+ public void containsExactlyElementsIn(Iterable<ResourceOveruseStats> expected) {
+ Truth.assertThat(mActual)
+ .comparingElementsUsing(Correspondence.from(ResourceOveruseStatsSubject::isEquals,
+ "is equal to"))
+ .containsExactlyElementsIn(expected);
+ }
+
+ public static boolean isEquals(ResourceOveruseStats actual, ResourceOveruseStats expected) {
+ if (actual == expected) {
+ return true;
+ }
+ if (actual == null || expected == null) {
+ return false;
+ }
+ return actual.getPackageName() == expected.getPackageName()
+ && actual.getUserHandle().equals(expected.getUserHandle())
+ && IoOveruseStatsSubject.isEquals(actual.getIoOveruseStats(),
+ expected.getIoOveruseStats());
+ }
+}