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