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.install.lib;
18 
19 import static com.android.cts.shim.lib.ShimPackage.SHIM_APEX_PACKAGE_NAME;
20 
21 import android.content.pm.VersionedPackage;
22 
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileNotFoundException;
26 import java.io.InputStream;
27 import java.util.function.Function;
28 
29 /**
30  * Collection of placeholder apps used in tests.
31  */
32 public class TestApp {
33 
34     public static final String A = "com.android.cts.install.lib.testapp.A";
35     public static final String B = "com.android.cts.install.lib.testapp.B";
36     public static final String C = "com.android.cts.install.lib.testapp.C";
37     public static final String S = "com.android.cts.install.lib.testapp.S";
38 
39     // Apk collection
40     public static final TestApp A1 = new TestApp("Av1", A, 1, /*isApex*/false,
41             "TestAppAv1.apk");
42     public static final TestApp A2 = new TestApp("Av2", A, 2, /*isApex*/false,
43             "TestAppAv2.apk");
44     public static final TestApp A3 = new TestApp("Av3", A, 3, /*isApex*/false,
45             "TestAppAv3.apk");
46     public static final TestApp ACrashing2 = new TestApp("ACrashingV2", A, 2, /*isApex*/false,
47             "TestAppACrashingV2.apk");
48     public static final TestApp ASplit1 = new TestApp("ASplitV1", A, 1, /*isApex*/false,
49             "TestAppASplitV1.apk", "TestAppASplitV1_anydpi.apk");
50     public static final TestApp ASplit2 = new TestApp("ASplitV2", A, 2, /*isApex*/false,
51             "TestAppASplitV2.apk", "TestAppASplitV2_anydpi.apk");
52     public static final TestApp AIncompleteSplit = new TestApp("AIncompleteSplit", A, 1,
53             /*isApex*/false, "TestAppASplitV1_anydpi.apk");
54     public static final TestApp AOriginal1 = new TestApp("AOriginalV1", A, 1, /*isApex*/false,
55             "TestAppAOriginalV1.apk");
56     public static final TestApp ARotated2 = new TestApp("ARotatedV2", A, 2, /*isApex*/false,
57             "TestAppARotatedV2.apk");
58     public static final TestApp ARollbackWipe2 = new TestApp("ARollbackWipe2", A, 2,
59             /*isApex*/false, "TestAppARollbackWipeV2.apk");
60 
61     public static final TestApp B1 = new TestApp("Bv1", B, 1, /*isApex*/false,
62             "TestAppBv1.apk");
63     public static final TestApp B2 = new TestApp("Bv2", B, 2, /*isApex*/false,
64             "TestAppBv2.apk");
65     public static final TestApp B3 = new TestApp("Bv3", B, 3, /*isApex*/false,
66             "TestAppBv3.apk");
67     public static final TestApp BRollbackRestore2 = new TestApp("BRollbackRestore2", B, 2,
68             /*isApex*/false, "TestAppBRollbackRestoreV2.apk");
69 
70     public static final TestApp C1 = new TestApp("Cv1", C, 1, /*isApex*/false,
71             "TestAppCv1.apk");
72     public static final TestApp C2 = new TestApp("Cv2", C, 2, /*isApex*/false,
73             "TestAppCv2.apk");
74     public static final TestApp CRollbackRetain2 = new TestApp("CRollbackRetain2", C, 2,
75             /*isApex*/false, "TestAppCRollbackRetainV2.apk");
76 
77     public static final TestApp S1 = new TestApp("Sv1", S, 1, /*isApex*/false,
78             "TestAppSv1.apk");
79     public static final TestApp S2 = new TestApp("Sv1", S, 2, /*isApex*/false,
80             "TestAppSv2.apk");
81 
82     // Apex collection
83     public static final TestApp Apex1 = new TestApp("Apex1", SHIM_APEX_PACKAGE_NAME, 1,
84             /*isApex*/true, "com.android.apex.cts.shim.v1.apex");
85     public static final TestApp Apex2 = new TestApp("Apex2", SHIM_APEX_PACKAGE_NAME, 2,
86             /*isApex*/true, "com.android.apex.cts.shim.v2.apex");
87     public static final TestApp Apex3 = new TestApp("Apex3", SHIM_APEX_PACKAGE_NAME, 3,
88             /*isApex*/true, "com.android.apex.cts.shim.v3.apex");
89 
90     private final String mName;
91     private final String mPackageName;
92     private final long mVersionCode;
93     private final String[] mResourceNames;
94     private final boolean mIsApex;
95     private final Function<String, InputStream> mGetResourceStream;
96 
TestApp(String name, String packageName, long versionCode, boolean isApex, String... resourceNames)97     public TestApp(String name, String packageName, long versionCode, boolean isApex,
98             String... resourceNames) {
99         mName = name;
100         mPackageName = packageName;
101         mVersionCode = versionCode;
102         mResourceNames = resourceNames;
103         mIsApex = isApex;
104         mGetResourceStream = (res) -> TestApp.class.getClassLoader().getResourceAsStream(res);
105     }
106 
TestApp(String name, String packageName, long versionCode, boolean isApex, File[] paths)107     public TestApp(String name, String packageName, long versionCode, boolean isApex,
108             File[] paths) {
109         mName = name;
110         mPackageName = packageName;
111         mVersionCode = versionCode;
112         mResourceNames = new String[paths.length];
113         for (int i = 0; i < paths.length; ++i) {
114             mResourceNames[i] = paths[i].getName();
115         }
116         mIsApex = isApex;
117         mGetResourceStream = (res) -> {
118             try {
119                 for (int i = 0; i < paths.length; ++i) {
120                     if (paths[i].getName().equals(res)) {
121                         return new FileInputStream(paths[i]);
122                     }
123                 }
124             } catch (FileNotFoundException ignore) {
125             }
126             return null;
127         };
128     }
129 
TestApp(String name, String packageName, long versionCode, boolean isApex, File path)130     public TestApp(String name, String packageName, long versionCode, boolean isApex, File path) {
131         this(name, packageName, versionCode, isApex, new File[]{ path });
132     }
133 
getPackageName()134     public String getPackageName() {
135         return mPackageName;
136     }
137 
getVersionCode()138     public long getVersionCode() {
139         return mVersionCode;
140     }
141 
getVersionedPackage()142     public VersionedPackage getVersionedPackage() {
143         return new VersionedPackage(mPackageName, mVersionCode);
144     }
145 
146     @Override
toString()147     public String toString() {
148         return mName;
149     }
150 
isApex()151     boolean isApex() {
152         return mIsApex;
153     }
154 
getResourceNames()155     public String[] getResourceNames() {
156         return mResourceNames;
157     }
158 
159     /**
160      * Returns an InputStream for the resource name.
161      */
getResourceStream(String name)162     public InputStream getResourceStream(String name) {
163         return mGetResourceStream.apply(name);
164     }
165 }
166