1 /* 2 * Copyright (C) 2019 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.cts.rollback.lib; 18 19 import android.content.pm.VersionedPackage; 20 import android.content.rollback.PackageRollbackInfo; 21 import android.content.rollback.RollbackInfo; 22 23 import com.google.common.truth.FailureStrategy; 24 import com.google.common.truth.Subject; 25 import com.google.common.truth.SubjectFactory; 26 import com.google.common.truth.Truth; 27 28 import java.util.ArrayList; 29 import java.util.List; 30 31 /** 32 * Subject for asserting things about RollbackInfo instances. 33 */ 34 public final class RollbackInfoSubject extends Subject<RollbackInfoSubject, RollbackInfo> { 35 /** 36 * Asserts something about RollbackInfo. 37 */ assertThat(RollbackInfo rollback)38 public static RollbackInfoSubject assertThat(RollbackInfo rollback) { 39 return Truth.assert_().about(rollbacks()).that(rollback); 40 } 41 42 /** 43 * Gets the subject factory for RollbackInfo. 44 */ rollbacks()45 public static SubjectFactory<RollbackInfoSubject, RollbackInfo> rollbacks() { 46 return SUBJECT_FACTORY; 47 } 48 49 private static final SubjectFactory<RollbackInfoSubject, RollbackInfo> SUBJECT_FACTORY = 50 new SubjectFactory<RollbackInfoSubject, RollbackInfo>() { 51 @Override 52 public RollbackInfoSubject getSubject(FailureStrategy fs, RollbackInfo that) { 53 return new RollbackInfoSubject(fs, that); 54 } 55 }; 56 RollbackInfoSubject(FailureStrategy failureStrategy, RollbackInfo subject)57 private RollbackInfoSubject(FailureStrategy failureStrategy, RollbackInfo subject) { 58 super(failureStrategy, subject); 59 } 60 61 /** 62 * Asserts that the RollbackInfo has given rollbackId. 63 */ hasRollbackId(int rollbackId)64 public void hasRollbackId(int rollbackId) { 65 check().that(getSubject().getRollbackId()).isEqualTo(rollbackId); 66 } 67 68 /** 69 * Asserts that the RollbackInfo is for a staged rollback. 70 */ isStaged()71 public void isStaged() { 72 check().that(getSubject().isStaged()).isTrue(); 73 } 74 75 /** 76 * Asserts that the RollbackInfo is not for a staged rollback. 77 */ isNotStaged()78 public void isNotStaged() { 79 check().that(getSubject().isStaged()).isFalse(); 80 } 81 82 /** 83 * Asserts that the RollbackInfo contains exactly the list of provided 84 * package rollbacks. Though they may be in any order. 85 */ packagesContainsExactly(Rollback... expected)86 public void packagesContainsExactly(Rollback... expected) { 87 List<Rollback> actualPackages = new ArrayList<>(); 88 for (PackageRollbackInfo info : getSubject().getPackages()) { 89 actualPackages.add(new Rollback(info)); 90 } 91 check().that(actualPackages).containsExactly((Object[]) expected); 92 } 93 94 private static class VersionedPackageWithEquals { 95 private final VersionedPackage mVp; 96 VersionedPackageWithEquals(VersionedPackage versionedPackage)97 VersionedPackageWithEquals(VersionedPackage versionedPackage) { 98 mVp = versionedPackage; 99 } 100 101 @Override toString()102 public String toString() { 103 return mVp.toString(); 104 } 105 106 @Override equals(Object other)107 public boolean equals(Object other) { 108 if (!(other instanceof VersionedPackageWithEquals)) { 109 return false; 110 } 111 112 VersionedPackageWithEquals r = (VersionedPackageWithEquals) other; 113 return mVp.getPackageName().equals(r.mVp.getPackageName()) 114 && mVp.getLongVersionCode() == r.mVp.getLongVersionCode(); 115 } 116 } 117 118 /** 119 * Asserts that the RollbackInfo contains exactly the list of provided 120 * cause packages. Though they may be in any order. 121 */ causePackagesContainsExactly(TestApp... causes)122 public void causePackagesContainsExactly(TestApp... causes) { 123 List<VersionedPackageWithEquals> expectedVps = new ArrayList<>(); 124 for (TestApp cause : causes) { 125 expectedVps.add(new VersionedPackageWithEquals(cause.getVersionedPackage())); 126 } 127 128 List<VersionedPackageWithEquals> actualVps = new ArrayList<>(); 129 for (VersionedPackage vp : getSubject().getCausePackages()) { 130 actualVps.add(new VersionedPackageWithEquals(vp)); 131 } 132 133 check().that(actualVps).containsExactlyElementsIn(expectedVps); 134 } 135 } 136