1 /* 2 * Copyright (C) 2023 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 android.car.cts.app.watchdog; 18 19 import static com.google.common.truth.Truth.assertAbout; 20 21 import android.car.watchdog.ResourceOveruseConfiguration; 22 23 import com.google.common.collect.ImmutableSet; 24 import com.google.common.collect.Maps; 25 import com.google.common.truth.Correspondence; 26 import com.google.common.truth.FailureMetadata; 27 import com.google.common.truth.SimpleSubjectBuilder; 28 import com.google.common.truth.Subject; 29 import com.google.common.truth.Truth; 30 31 import java.util.Arrays; 32 33 public final class ResourceOveruseConfigurationSubject extends Subject { 34 // Boiler-plate Subject.Factory for ResourceOveruseConfigurationSubject 35 private static final Subject.Factory< 36 ResourceOveruseConfigurationSubject, 37 Iterable<ResourceOveruseConfiguration>> RESOURCE_OVERUSE_CONFIG_SUBJECT_FACTORY = 38 ResourceOveruseConfigurationSubject::new; 39 40 private final Iterable<ResourceOveruseConfiguration> mActual; 41 42 // User-defined entry point assertThat( Iterable<ResourceOveruseConfiguration> stats)43 public static ResourceOveruseConfigurationSubject assertThat( 44 Iterable<ResourceOveruseConfiguration> stats) { 45 return assertAbout(RESOURCE_OVERUSE_CONFIG_SUBJECT_FACTORY).that(stats); 46 } 47 48 public static SimpleSubjectBuilder<ResourceOveruseConfigurationSubject, assertWithMessage(String format, Object... args)49 Iterable<ResourceOveruseConfiguration>> assertWithMessage(String format, 50 Object... args) { 51 return Truth.assertWithMessage(format, args).about(RESOURCE_OVERUSE_CONFIG_SUBJECT_FACTORY); 52 } 53 54 public static Subject.Factory<ResourceOveruseConfigurationSubject, resourceOveruseStats()55 Iterable<ResourceOveruseConfiguration>> resourceOveruseStats() { 56 return RESOURCE_OVERUSE_CONFIG_SUBJECT_FACTORY; 57 } 58 containsExactly(ResourceOveruseConfiguration... stats)59 public void containsExactly(ResourceOveruseConfiguration... stats) { 60 containsExactlyElementsIn(Arrays.asList(stats)); 61 } 62 containsExactlyElementsIn(Iterable<ResourceOveruseConfiguration> expected)63 public void containsExactlyElementsIn(Iterable<ResourceOveruseConfiguration> expected) { 64 Truth.assertThat(mActual) 65 .comparingElementsUsing(Correspondence.from( 66 ResourceOveruseConfigurationSubject::isEquals, "is equal to")) 67 .containsExactlyElementsIn(expected); 68 } 69 isEquals(ResourceOveruseConfiguration actual, ResourceOveruseConfiguration expected)70 public static boolean isEquals(ResourceOveruseConfiguration actual, 71 ResourceOveruseConfiguration expected) { 72 if (actual == null || expected == null) { 73 return (actual == null) && (expected == null); 74 } 75 return actual.getComponentType() == expected.getComponentType() 76 && ImmutableSet.copyOf(actual.getSafeToKillPackages()).equals( 77 ImmutableSet.copyOf(expected.getSafeToKillPackages())) 78 && ImmutableSet.copyOf(actual.getVendorPackagePrefixes()).equals( 79 ImmutableSet.copyOf(expected.getVendorPackagePrefixes())) 80 && Maps.difference(actual.getPackagesToAppCategoryTypes(), 81 expected.getPackagesToAppCategoryTypes()).areEqual() 82 && IoOveruseConfigurationSubject.isEquals(actual.getIoOveruseConfiguration(), 83 expected.getIoOveruseConfiguration()); 84 } 85 ResourceOveruseConfigurationSubject(FailureMetadata failureMetadata, Iterable<ResourceOveruseConfiguration> iterableSubject)86 private ResourceOveruseConfigurationSubject(FailureMetadata failureMetadata, 87 Iterable<ResourceOveruseConfiguration> iterableSubject) { 88 super(failureMetadata, iterableSubject); 89 this.mActual = iterableSubject; 90 } 91 } 92