Lakshman Annadorai | 93e4912 | 2021-04-02 15:26:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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.car.watchdog; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertAbout; |
| 20 | |
| 21 | import android.annotation.Nullable; |
| 22 | import android.car.watchdog.ResourceOveruseStats; |
| 23 | |
| 24 | import com.google.common.truth.Correspondence; |
| 25 | import com.google.common.truth.FailureMetadata; |
| 26 | import com.google.common.truth.Subject; |
| 27 | import com.google.common.truth.Truth; |
| 28 | |
| 29 | import java.util.Arrays; |
| 30 | |
| 31 | public final class ResourceOveruseStatsSubject extends Subject { |
| 32 | // Boiler-plate Subject.Factory for ResourceOveruseStatsSubject |
| 33 | private static final Subject.Factory<com.android.car.watchdog.ResourceOveruseStatsSubject, |
| 34 | Iterable<ResourceOveruseStats>> RESOURCE_OVERUSE_STATS_SUBJECT_FACTORY = |
| 35 | com.android.car.watchdog.ResourceOveruseStatsSubject::new; |
| 36 | |
| 37 | private final Iterable<ResourceOveruseStats> mActual; |
| 38 | |
| 39 | // User-defined entry point |
| 40 | public static ResourceOveruseStatsSubject assertThat( |
| 41 | @Nullable Iterable<ResourceOveruseStats> stats) { |
| 42 | return assertAbout(RESOURCE_OVERUSE_STATS_SUBJECT_FACTORY).that(stats); |
| 43 | } |
| 44 | |
| 45 | public static Subject.Factory<ResourceOveruseStatsSubject, Iterable<ResourceOveruseStats>> |
| 46 | resourceOveruseStats() { |
| 47 | return RESOURCE_OVERUSE_STATS_SUBJECT_FACTORY; |
| 48 | } |
| 49 | |
| 50 | private ResourceOveruseStatsSubject(FailureMetadata failureMetadata, |
| 51 | @Nullable Iterable<ResourceOveruseStats> iterableSubject) { |
| 52 | super(failureMetadata, iterableSubject); |
| 53 | this.mActual = iterableSubject; |
| 54 | } |
| 55 | |
| 56 | public void containsExactly(ResourceOveruseStats... stats) { |
| 57 | containsExactlyElementsIn(Arrays.asList(stats)); |
| 58 | } |
| 59 | |
| 60 | public void containsExactlyElementsIn(Iterable<ResourceOveruseStats> expected) { |
| 61 | Truth.assertThat(mActual) |
| 62 | .comparingElementsUsing(Correspondence.from(ResourceOveruseStatsSubject::isEquals, |
| 63 | "is equal to")) |
| 64 | .containsExactlyElementsIn(expected); |
| 65 | } |
| 66 | |
| 67 | public static boolean isEquals(ResourceOveruseStats actual, ResourceOveruseStats expected) { |
| 68 | if (actual == expected) { |
| 69 | return true; |
| 70 | } |
| 71 | if (actual == null || expected == null) { |
| 72 | return false; |
| 73 | } |
| 74 | return actual.getPackageName() == expected.getPackageName() |
| 75 | && actual.getUserHandle().equals(expected.getUserHandle()) |
| 76 | && IoOveruseStatsSubject.isEquals(actual.getIoOveruseStats(), |
| 77 | expected.getIoOveruseStats()); |
| 78 | } |
| 79 | } |