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 android.content.pm.VersionedPackage;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.FileNotFoundException;
24 import java.io.InputStream;
25 import java.util.function.Function;
26 
27 /**
28  * Collection of dummy apps used in tests.
29  */
30 public class TestApp {
31     public static final String A = "com.android.cts.install.lib.testapp.A";
32     public static final String B = "com.android.cts.install.lib.testapp.B";
33     public static final String C = "com.android.cts.install.lib.testapp.C";
34     public static final String Apex = "com.android.apex.cts.shim";
35     public static final String NotPreInstalledApex = "com.android.apex.cts.shim_not_pre_installed";
36 
37     // Apk collection
38     public static final TestApp A1 = new TestApp("Av1", A, 1, /*isApex*/false,
39             "TestAppAv1.apk");
40     public static final TestApp A2 = new TestApp("Av2", A, 2, /*isApex*/false,
41             "TestAppAv2.apk");
42     public static final TestApp A3 = new TestApp("Av3", A, 3, /*isApex*/false,
43             "TestAppAv3.apk");
44     public static final TestApp ACrashing2 = new TestApp("ACrashingV2", A, 2, /*isApex*/false,
45             "TestAppACrashingV2.apk");
46     public static final TestApp ASplit1 = new TestApp("ASplitV1", A, 1, /*isApex*/false,
47             "TestAppASplitV1.apk", "TestAppASplitV1_anydpi.apk");
48     public static final TestApp ASplit2 = new TestApp("ASplitV2", A, 2, /*isApex*/false,
49             "TestAppASplitV2.apk", "TestAppASplitV2_anydpi.apk");
50 
51     public static final TestApp B1 = new TestApp("Bv1", B, 1, /*isApex*/false,
52             "TestAppBv1.apk");
53     public static final TestApp B2 = new TestApp("Bv2", B, 2, /*isApex*/false,
54             "TestAppBv2.apk");
55 
56     public static final TestApp C1 = new TestApp("Cv1", C, 1, /*isApex*/false,
57             "TestAppCv1.apk");
58 
59     // Apex collection
60     public static final TestApp Apex1 =
61             new TestApp("Apex1", Apex, 1, /*isApex*/true,
62                     "com.android.apex.cts.shim.v1.apex");
63     public static final TestApp Apex2 =
64             new TestApp("Apex2", Apex, 2, /*isApex*/true,
65             "com.android.apex.cts.shim.v2.apex");
66     public static final TestApp ApexNoHashtree2 =
67             new TestApp("Apex2", Apex, 2, /*isApex*/true,
68                     "com.android.apex.cts.shim.v2_no_hashtree.apex");
69     public static final TestApp ApexWrongSha2 = new TestApp(
70             "ApexWrongSha2", Apex, 2, /*isApex*/true,
71             "com.android.apex.cts.shim.v2_wrong_sha.apex");
72     public static final TestApp Apex3 =
73             new TestApp("Apex3", Apex, 3, /*isApex*/true,
74             "com.android.apex.cts.shim.v3.apex");
75     public static final TestApp ApexNotPreInstalled =
76             new TestApp("ApexNotPreInstalled", NotPreInstalledApex, 3, /*isApex*/true,
77             "com.android.apex.cts.shim_not_pre_installed.apex");
78 
79     private final String mName;
80     private final String mPackageName;
81     private final long mVersionCode;
82     private final String[] mResourceNames;
83     private final boolean mIsApex;
84     private final Function<String, InputStream> mGetResourceStream;
85 
TestApp(String name, String packageName, long versionCode, boolean isApex, String... resourceNames)86     public TestApp(String name, String packageName, long versionCode, boolean isApex,
87             String... resourceNames) {
88         mName = name;
89         mPackageName = packageName;
90         mVersionCode = versionCode;
91         mResourceNames = resourceNames;
92         mIsApex = isApex;
93         mGetResourceStream = (res) -> TestApp.class.getClassLoader().getResourceAsStream(res);
94     }
95 
TestApp(String name, String packageName, long versionCode, boolean isApex, File path)96     public TestApp(String name, String packageName, long versionCode, boolean isApex, File path) {
97         mName = name;
98         mPackageName = packageName;
99         mVersionCode = versionCode;
100         mResourceNames = new String[] { path.getName() };
101         mIsApex = isApex;
102         mGetResourceStream = (res) -> {
103             try {
104                 return new FileInputStream(path);
105             } catch (FileNotFoundException e) {
106                 return null;
107             }
108         };
109     }
110 
getPackageName()111     public String getPackageName() {
112         return mPackageName;
113     }
114 
getVersionCode()115     public long getVersionCode() {
116         return mVersionCode;
117     }
118 
getVersionedPackage()119     public VersionedPackage getVersionedPackage() {
120         return new VersionedPackage(mPackageName, mVersionCode);
121     }
122 
123     @Override
toString()124     public String toString() {
125         return mName;
126     }
127 
isApex()128     boolean isApex() {
129         return mIsApex;
130     }
131 
getResourceNames()132     public String[] getResourceNames() {
133         return mResourceNames;
134     }
135 
136     /**
137      * Returns an InputStream for the resource name.
138      */
getResourceStream(String name)139     public InputStream getResourceStream(String name) {
140         return mGetResourceStream.apply(name);
141     }
142 }
143