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