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 {
36     private final RollbackInfo mActual;
37 
38     /**
39      * Asserts something about RollbackInfo.
40      */
assertThat(RollbackInfo rollback)41     public static RollbackInfoSubject assertThat(RollbackInfo rollback) {
42         return Truth.assert_().about(rollbacks()).that(rollback);
43     }
44 
45     /**
46      * Gets the subject factory for RollbackInfo.
47      */
rollbacks()48     public static Subject.Factory<RollbackInfoSubject, RollbackInfo> rollbacks() {
49         return SUBJECT_FACTORY;
50     }
51 
52     private static final Subject.Factory<RollbackInfoSubject, RollbackInfo> SUBJECT_FACTORY =
53             new Subject.Factory<RollbackInfoSubject, RollbackInfo>() {
54                 @Override
55                 public RollbackInfoSubject createSubject(FailureMetadata fs, RollbackInfo that) {
56                     return new RollbackInfoSubject(fs, that);
57                 }
58             };
59 
RollbackInfoSubject(FailureMetadata failureMetadata, RollbackInfo subject)60     private RollbackInfoSubject(FailureMetadata failureMetadata, RollbackInfo subject) {
61         super(failureMetadata, subject);
62         mActual = subject;
63     }
64 
65     /**
66      * Asserts that the RollbackInfo has given rollbackId.
67      */
hasRollbackId(int rollbackId)68     public void hasRollbackId(int rollbackId) {
69         check("getRollbackId()").that(mActual.getRollbackId()).isEqualTo(rollbackId);
70     }
71 
72     /**
73      * Asserts that the RollbackInfo is for a staged rollback.
74      */
isStaged()75     public void isStaged() {
76         check("isStaged()").that(mActual.isStaged()).isTrue();
77     }
78 
79     /**
80      * Asserts that the RollbackInfo is not for a staged rollback.
81      */
isNotStaged()82     public void isNotStaged() {
83         check("isStaged()").that(mActual.isStaged()).isFalse();
84     }
85 
86     /**
87      * Asserts that the RollbackInfo contains exactly the list of provided
88      * package rollbacks. Though they may be in any order.
89      */
packagesContainsExactly(Rollback... expected)90     public void packagesContainsExactly(Rollback... expected) {
91         List<Rollback> actualPackages = new ArrayList<>();
92         for (PackageRollbackInfo info : mActual.getPackages()) {
93             actualPackages.add(new Rollback(info));
94         }
95         check("actualPackages").that(actualPackages).containsExactly((Object[]) expected);
96     }
97 
98     /**
99      * Asserts that the RollbackInfo contains exactly the list of provided
100      * cause packages. Though they may be in any order.
101      */
causePackagesContainsExactly(TestApp... causes)102     public void causePackagesContainsExactly(TestApp... causes) {
103         List<VersionedPackage> expectedVps = new ArrayList<>();
104         for (TestApp cause : causes) {
105             expectedVps.add(cause.getVersionedPackage());
106         }
107 
108         check("getCausePackages()").that(mActual.getCausePackages())
109                 .containsExactlyElementsIn(expectedVps);
110     }
111 }
112