• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 android.content.pm.cts;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assert.assertArrayEquals;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertSame;
26 import static org.junit.Assert.assertTrue;
27 
28 import android.content.Context;
29 import android.content.pm.ApplicationInfo;
30 import android.content.pm.Attribution;
31 import android.content.pm.ComponentInfo;
32 import android.content.pm.ConfigurationInfo;
33 import android.content.pm.Flags;
34 import android.content.pm.PackageInfo;
35 import android.content.pm.PackageItemInfo;
36 import android.content.pm.PackageManager;
37 import android.content.pm.Signature;
38 import android.os.Parcel;
39 import android.platform.test.annotations.AppModeFull;
40 import android.platform.test.annotations.DisabledOnRavenwood;
41 import android.platform.test.ravenwood.RavenwoodRule;
42 
43 import androidx.test.platform.app.InstrumentationRegistry;
44 import androidx.test.runner.AndroidJUnit4;
45 
46 import org.junit.Rule;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 
50 import java.util.Arrays;
51 
52 @RunWith(AndroidJUnit4.class)
53 @AppModeFull // TODO(Instant) Figure out which APIs should work.
54 public class PackageInfoTest {
55     @Rule
56     public final RavenwoodRule mRavenwood = new RavenwoodRule();
57 
58     private static final String PACKAGE_NAME = "android.content.cts";
59 
getContext()60     private Context getContext() {
61         return InstrumentationRegistry.getInstrumentation().getTargetContext();
62     }
63 
getPackageInfo()64     private PackageInfo getPackageInfo() throws Exception {
65         return getContext().getPackageManager().getPackageInfo(PACKAGE_NAME,
66                 PackageManager.PackageInfoFlags.of(
67                         PackageManager.GET_ACTIVITIES | PackageManager.GET_GIDS
68                                 | PackageManager.GET_CONFIGURATIONS
69                                 | PackageManager.GET_INSTRUMENTATION
70                                 | PackageManager.GET_PERMISSIONS
71                                 | PackageManager.GET_PROVIDERS | PackageManager.GET_RECEIVERS
72                                 | PackageManager.GET_SERVICES | PackageManager.GET_ATTRIBUTIONS_LONG
73                                 | PackageManager.GET_SIGNATURES
74                                 | PackageManager.GET_UNINSTALLED_PACKAGES));
75     }
76 
77     @Test
testSimple()78     public void testSimple() {
79         PackageInfo info = new PackageInfo();
80         assertNotNull(info.toString());
81     }
82 
83     @Test
testPackageInfoOp()84     public void testPackageInfoOp() {
85         // Test constructor, describeContents, toString
86         final PackageInfo packageInfo = new PackageInfo();
87         assertEquals(0, packageInfo.describeContents());
88         assertNotNull(packageInfo.toString());
89 
90         // Test writeToParcel
91         Parcel p = Parcel.obtain();
92         packageInfo.writeToParcel(p, 0);
93         p.setDataPosition(0);
94         final PackageInfo packageInfoCmp = PackageInfo.CREATOR.createFromParcel(p);
95         checkPkgInfoSame(packageInfo, packageInfoCmp);
96         p.recycle();
97     }
98 
99     @Test
100     @DisabledOnRavenwood(blockedBy = PackageManager.class)
testApplicationInfoSame()101     public void testApplicationInfoSame() throws Exception {
102         PackageInfo packageInfo = getPackageInfo();
103         ApplicationInfo ai = packageInfo.applicationInfo;
104 
105         // Make sure all the components in it has the same ApplicationInfo.
106         for (ComponentInfo[] ar : new ComponentInfo[][]{
107                 packageInfo.activities, packageInfo.services, packageInfo.providers,
108                 packageInfo.receivers}) {
109             for (ComponentInfo ci : ar) {
110                 assertSame("component=" + ci.getComponentName(), ai, ci.applicationInfo);
111             }
112         }
113     }
114 
checkPkgInfoSame(PackageInfo expected, PackageInfo actual)115     private void checkPkgInfoSame(PackageInfo expected, PackageInfo actual) {
116         assertEquals(expected.packageName, actual.packageName);
117         assertEquals(expected.getLongVersionCode(), actual.getLongVersionCode());
118         assertEquals(expected.versionName, actual.versionName);
119         assertEquals(expected.sharedUserId, actual.sharedUserId);
120         assertEquals(expected.sharedUserLabel, actual.sharedUserLabel);
121         if (Flags.provideInfoOfApkInApex()) {
122             assertThat(expected.getApexPackageName()).isEqualTo(
123                     actual.getApexPackageName());
124         }
125         if (expected.applicationInfo != null) {
126             assertNotNull(actual.applicationInfo);
127             checkAppInfo(expected.applicationInfo, actual.applicationInfo);
128         } else {
129             assertNull(actual.applicationInfo);
130         }
131         assertTrue(Arrays.equals(expected.gids, actual.gids));
132         checkInfoArray(expected.activities, actual.activities);
133         checkInfoArray(expected.receivers, actual.receivers);
134         checkInfoArray(expected.services, actual.services);
135         checkInfoArray(expected.providers, actual.providers);
136         checkInfoArray(expected.instrumentation, actual.instrumentation);
137         checkInfoArray(expected.permissions, actual.permissions);
138         assertTrue(Arrays.equals(expected.requestedPermissions, actual.requestedPermissions));
139         checkSignatureInfo(expected.signatures, actual.signatures);
140         checkConfigInfo(expected.configPreferences, actual.configPreferences);
141         checkAttributionInfo(expected.attributions, actual.attributions);
142     }
143 
checkAppInfo(ApplicationInfo expected, ApplicationInfo actual)144     private void checkAppInfo(ApplicationInfo expected, ApplicationInfo actual) {
145         assertEquals(expected.taskAffinity, actual.taskAffinity);
146         assertEquals(expected.permission, actual.permission);
147         assertEquals(expected.processName, actual.processName);
148         assertEquals(expected.className, actual.className);
149         assertEquals(expected.theme, actual.theme);
150         assertEquals(expected.flags, actual.flags);
151         assertEquals(expected.sourceDir, actual.sourceDir);
152         assertEquals(expected.publicSourceDir, actual.publicSourceDir);
153         assertArrayEquals(expected.sharedLibraryFiles, actual.sharedLibraryFiles);
154         assertEquals(expected.dataDir, actual.dataDir);
155         assertEquals(expected.uid, actual.uid);
156         assertEquals(expected.enabled, actual.enabled);
157         assertEquals(expected.manageSpaceActivityName, actual.manageSpaceActivityName);
158         assertEquals(expected.descriptionRes, actual.descriptionRes);
159     }
160 
checkInfoArray(PackageItemInfo[] expected, PackageItemInfo[] actual)161     private void checkInfoArray(PackageItemInfo[] expected, PackageItemInfo[] actual) {
162         if (expected != null && expected.length > 0) {
163             assertNotNull(actual);
164             assertEquals(expected.length, actual.length);
165             for (int i = 0; i < expected.length; i++) {
166                 assertEquals(expected[i].name, actual[i].name);
167             }
168         } else if (expected == null) {
169             assertNull(actual);
170         } else {
171             assertEquals(0, actual.length);
172         }
173     }
174 
checkSignatureInfo(Signature[] expected, Signature[] actual)175     private void checkSignatureInfo(Signature[] expected, Signature[] actual) {
176         if (expected != null && expected.length > 0) {
177             assertNotNull(actual);
178             assertEquals(expected.length, actual.length);
179             for (int i = 0; i < expected.length; i++) {
180                 assertEquals(expected[i], actual[i]);
181             }
182         } else if (expected == null) {
183             assertNull(actual);
184         } else {
185             assertEquals(0, actual.length);
186         }
187     }
188 
checkConfigInfo(ConfigurationInfo[] expected, ConfigurationInfo[] actual)189     private void checkConfigInfo(ConfigurationInfo[] expected, ConfigurationInfo[] actual) {
190         if (expected != null && expected.length > 0) {
191             assertNotNull(actual);
192             assertEquals(expected.length, actual.length);
193             for (int i = 0; i < expected.length; i++) {
194                 assertEquals(expected[i].reqKeyboardType, actual[i].reqKeyboardType);
195                 assertEquals(expected[i].reqTouchScreen, actual[i].reqTouchScreen);
196                 assertEquals(expected[i].reqInputFeatures, actual[i].reqInputFeatures);
197                 assertEquals(expected[i].reqNavigation, actual[i].reqNavigation);
198             }
199         } else if (expected == null) {
200             assertNull(actual);
201         } else {
202             assertEquals(0, actual.length);
203         }
204     }
205 
checkAttributionInfo(Attribution[] expected, Attribution[] actual)206     private void checkAttributionInfo(Attribution[] expected, Attribution[] actual) {
207         if (expected != null && expected.length > 0) {
208             assertNotNull(actual);
209             assertEquals(expected.length, actual.length);
210             for (int i = 0; i < expected.length; i++) {
211                 assertEquals(actual[i].getTag(), expected[i].getTag());
212                 assertEquals(actual[i].getTag(), expected[i].getTag());
213             }
214         } else if (expected == null) {
215             assertNull(actual);
216         } else {
217             assertEquals(0, actual.length);
218         }
219     }
220 }
221