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 com.android.cts.stub.R; 20 21 22 import android.content.ComponentName; 23 import android.content.Intent; 24 import android.content.IntentFilter; 25 import android.content.pm.ApplicationInfo; 26 import android.content.pm.InstrumentationInfo; 27 import android.content.pm.PackageInfo; 28 import android.content.pm.PackageManager; 29 import android.content.pm.PermissionGroupInfo; 30 import android.content.pm.PermissionInfo; 31 import android.content.pm.ProviderInfo; 32 import android.content.pm.ResolveInfo; 33 import android.content.pm.PackageManager.NameNotFoundException; 34 import android.test.AndroidTestCase; 35 36 import java.util.ArrayList; 37 import java.util.Iterator; 38 import java.util.List; 39 40 /** 41 * This test is based on the declarations in AndroidManifest.xml. We create mock declarations 42 * in AndroidManifest.xml just for test of PackageManager, and there are no corresponding parts 43 * of these declarations in test project. 44 */ 45 public class PackageManagerTest extends AndroidTestCase { 46 private PackageManager mPackageManager; 47 private static final String PACKAGE_NAME = "com.android.cts.stub"; 48 private static final String CONTENT_PKG_NAME = "com.android.cts.content"; 49 private static final String ACTIVITY_ACTION_NAME = "android.intent.action.PMTEST"; 50 private static final String MAIN_ACTION_NAME = "android.intent.action.MAIN"; 51 private static final String SERVICE_ACTION_NAME = 52 "android.content.pm.cts.activity.PMTEST_SERVICE"; 53 private static final String PERMISSION_NAME = "android.permission.INTERNET"; 54 private static final String ACTIVITY_NAME = "android.content.pm.cts.TestPmActivity"; 55 private static final String SERVICE_NAME = "android.content.pm.cts.TestPmService"; 56 private static final String RECEIVER_NAME = "android.content.pm.cts.PmTestReceiver"; 57 private static final String INSTRUMENT_NAME = "android.content.pm.cts.TestPmInstrumentation"; 58 private static final String PROVIDER_NAME = "android.content.cts.MockContentProvider"; 59 private static final String PERMISSIONGROUP_NAME = "android.permission-group.COST_MONEY"; 60 61 @Override 62 protected void setUp() throws Exception { 63 super.setUp(); 64 mPackageManager = getContext().getPackageManager(); 65 } 66 67 public void testQuery() throws NameNotFoundException { 68 // Test query Intent Activity related methods 69 70 Intent activityIntent = new Intent(ACTIVITY_ACTION_NAME); 71 String cmpActivityName = "android.content.pm.cts.TestPmCompare"; 72 // List with different activities and the filter doesn't work, 73 List<ResolveInfo> listWithDiff = mPackageManager.queryIntentActivityOptions( 74 new ComponentName(PACKAGE_NAME, cmpActivityName), null, activityIntent, 0); 75 checkActivityInfoName(ACTIVITY_NAME, listWithDiff); 76 77 // List with the same activities to make filter work 78 List<ResolveInfo> listInSame = mPackageManager.queryIntentActivityOptions( 79 new ComponentName(PACKAGE_NAME, ACTIVITY_NAME), null, activityIntent, 0); 80 assertEquals(0, listInSame.size()); 81 82 // Test queryIntentActivities 83 List<ResolveInfo> intentActivities = 84 mPackageManager.queryIntentActivities(activityIntent, 0); 85 assertTrue(intentActivities.size() > 0); 86 checkActivityInfoName(ACTIVITY_NAME, intentActivities); 87 88 // End of Test query Intent Activity related methods 89 90 // Test queryInstrumentation 91 String targetPackage = "android"; 92 List<InstrumentationInfo> instrumentations = mPackageManager.queryInstrumentation( 93 targetPackage, 0); 94 checkInstrumentationInfoName(INSTRUMENT_NAME, instrumentations); 95 96 // Test queryIntentServices 97 Intent serviceIntent = new Intent(SERVICE_ACTION_NAME); 98 List<ResolveInfo> services = mPackageManager.queryIntentServices(serviceIntent, 99 PackageManager.GET_INTENT_FILTERS); 100 checkServiceInfoName(SERVICE_NAME, services); 101 102 // Test queryBroadcastReceivers 103 String receiverActionName = "android.content.pm.cts.PackageManagerTest.PMTEST_RECEIVER"; 104 Intent broadcastIntent = new Intent(receiverActionName); 105 List<ResolveInfo> broadcastReceivers = new ArrayList<ResolveInfo>(); 106 broadcastReceivers = mPackageManager.queryBroadcastReceivers(broadcastIntent, 0); 107 checkActivityInfoName(RECEIVER_NAME, broadcastReceivers); 108 109 // Test queryPermissionsByGroup, queryContentProviders 110 String testPermissionsGroup = "android.permission-group.NETWORK"; 111 List<PermissionInfo> permissions = mPackageManager.queryPermissionsByGroup( 112 testPermissionsGroup, PackageManager.GET_META_DATA); 113 checkPermissionInfoName(PERMISSION_NAME, permissions); 114 115 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(PACKAGE_NAME, 0); 116 List<ProviderInfo> providers = mPackageManager.queryContentProviders(PACKAGE_NAME, 117 appInfo.uid, 0); 118 checkProviderInfoName(PROVIDER_NAME, providers); 119 } 120 121 private void checkActivityInfoName(String expectedName, List<ResolveInfo> resolves) { 122 // Flag for checking if the name is contained in list array. 123 boolean isContained = false; 124 Iterator<ResolveInfo> infoIterator = resolves.iterator(); 125 String current; 126 while (infoIterator.hasNext()) { 127 current = infoIterator.next().activityInfo.name; 128 if (current.equals(expectedName)) { 129 isContained = true; 130 break; 131 } 132 } 133 assertTrue(isContained); 134 } 135 136 private void checkServiceInfoName(String expectedName, List<ResolveInfo> resolves) { 137 boolean isContained = false; 138 Iterator<ResolveInfo> infoIterator = resolves.iterator(); 139 String current; 140 while (infoIterator.hasNext()) { 141 current = infoIterator.next().serviceInfo.name; 142 if (current.equals(expectedName)) { 143 isContained = true; 144 break; 145 } 146 } 147 assertTrue(isContained); 148 } 149 150 private void checkPermissionInfoName(String expectedName, List<PermissionInfo> permissions) { 151 boolean isContained = false; 152 Iterator<PermissionInfo> infoIterator = permissions.iterator(); 153 String current; 154 while (infoIterator.hasNext()) { 155 current = infoIterator.next().name; 156 if (current.equals(expectedName)) { 157 isContained = true; 158 break; 159 } 160 } 161 assertTrue(isContained); 162 } 163 164 private void checkProviderInfoName(String expectedName, List<ProviderInfo> providers) { 165 boolean isContained = false; 166 Iterator<ProviderInfo> infoIterator = providers.iterator(); 167 String current; 168 while (infoIterator.hasNext()) { 169 current = infoIterator.next().name; 170 if (current.equals(expectedName)) { 171 isContained = true; 172 break; 173 } 174 } 175 assertTrue(isContained); 176 } 177 178 private void checkInstrumentationInfoName(String expectedName, 179 List<InstrumentationInfo> instrumentations) { 180 boolean isContained = false; 181 Iterator<InstrumentationInfo> infoIterator = instrumentations.iterator(); 182 String current; 183 while (infoIterator.hasNext()) { 184 current = infoIterator.next().name; 185 if (current.equals(expectedName)) { 186 isContained = true; 187 break; 188 } 189 } 190 assertTrue(isContained); 191 } 192 193 public void testGetInfo() throws NameNotFoundException { 194 // Test getApplicationInfo, getText 195 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(PACKAGE_NAME, 0); 196 int discriptionRes = R.string.hello_android; 197 String expectedDisciptionRes = "Hello, Android!"; 198 CharSequence appText = mPackageManager.getText(PACKAGE_NAME, discriptionRes, appInfo); 199 assertEquals(expectedDisciptionRes, appText); 200 ComponentName activityName = new ComponentName(PACKAGE_NAME, ACTIVITY_NAME); 201 ComponentName serviceName = new ComponentName(PACKAGE_NAME, SERVICE_NAME); 202 ComponentName receiverName = new ComponentName(PACKAGE_NAME, RECEIVER_NAME); 203 ComponentName instrName = new ComponentName(PACKAGE_NAME, INSTRUMENT_NAME); 204 205 // Test getPackageInfo 206 PackageInfo packageInfo = mPackageManager.getPackageInfo(PACKAGE_NAME, 207 PackageManager.GET_INSTRUMENTATION); 208 assertEquals(PACKAGE_NAME, packageInfo.packageName); 209 210 // Test getApplicationInfo, getApplicationLabel 211 String appLabel = "Android TestCase"; 212 assertEquals(appLabel, mPackageManager.getApplicationLabel(appInfo)); 213 assertEquals(PACKAGE_NAME, appInfo.processName); 214 215 // Test getServiceInfo 216 assertEquals(SERVICE_NAME, mPackageManager.getServiceInfo(serviceName, 217 PackageManager.GET_META_DATA).name); 218 219 // Test getReceiverInfo 220 assertEquals(RECEIVER_NAME, mPackageManager.getReceiverInfo(receiverName, 0).name); 221 222 // Test getPackageArchiveInfo 223 final String apkRoute = getContext().getPackageCodePath(); 224 final String apkName = getContext().getPackageName(); 225 assertEquals(apkName, mPackageManager.getPackageArchiveInfo(apkRoute, 0).packageName); 226 227 // Test getPackagesForUid, getNameForUid 228 checkPackagesNameForUid(PACKAGE_NAME, mPackageManager.getPackagesForUid(appInfo.uid)); 229 assertEquals(PACKAGE_NAME, mPackageManager.getNameForUid(appInfo.uid)); 230 231 // Test getActivityInfo 232 assertEquals(ACTIVITY_NAME, mPackageManager.getActivityInfo(activityName, 0).name); 233 234 // Test getPackageGids 235 assertTrue(mPackageManager.getPackageGids(PACKAGE_NAME).length > 0); 236 237 // Test getPermissionInfo 238 assertEquals(PERMISSION_NAME, mPackageManager.getPermissionInfo(PERMISSION_NAME, 0).name); 239 240 // Test getPermissionGroupInfo 241 assertEquals(PERMISSIONGROUP_NAME, mPackageManager.getPermissionGroupInfo( 242 PERMISSIONGROUP_NAME, 0).name); 243 244 // Test getAllPermissionGroups 245 List<PermissionGroupInfo> permissionGroups = mPackageManager.getAllPermissionGroups(0); 246 checkPermissionGroupInfoName(PERMISSIONGROUP_NAME, permissionGroups); 247 248 // Test getInstalledApplications 249 assertTrue(mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA).size() > 0); 250 251 // Test getInstalledPacakge 252 assertTrue(mPackageManager.getInstalledPackages(0).size() > 0); 253 254 // Test getInstrumentationInfo 255 assertEquals(INSTRUMENT_NAME, mPackageManager.getInstrumentationInfo(instrName, 0).name); 256 257 // Test getSystemSharedLibraryNames, in javadoc, String array and null 258 // are all OK as return value. 259 mPackageManager.getSystemSharedLibraryNames(); 260 261 // Test getLaunchIntentForPackage, Intent of activity 262 // android.content.pm.cts.TestPmCompare is set to match the condition 263 // to make sure the return of this method is not null. 264 assertEquals(MAIN_ACTION_NAME, mPackageManager.getLaunchIntentForPackage(PACKAGE_NAME) 265 .getAction()); 266 267 // Test isSafeMode. Because the test case will not run in safe mode, so 268 // the return will be false. 269 assertFalse(mPackageManager.isSafeMode()); 270 } 271 272 private void checkPackagesNameForUid(String expectedName, String[] uid) { 273 boolean isContained = false; 274 for (int i = 0; i < uid.length; i++) { 275 if (uid[i].equals(expectedName)) { 276 isContained = true; 277 break; 278 } 279 } 280 assertTrue(isContained); 281 } 282 283 private void checkPermissionGroupInfoName(String expectedName, 284 List<PermissionGroupInfo> permissionGroups) { 285 boolean isContained = false; 286 Iterator<PermissionGroupInfo> infoIterator = permissionGroups.iterator(); 287 String current; 288 while (infoIterator.hasNext()) { 289 current = infoIterator.next().name; 290 if (current.equals(expectedName)) { 291 isContained = true; 292 break; 293 } 294 } 295 assertTrue(isContained); 296 } 297 298 299 /** 300 * Simple test for {@link PackageManager#getPreferredActivities(List, List, String)} that tests 301 * calling it has no effect. The method is essentially a no-op because no preferred activities 302 * can be added. 303 * @see PackageManager#addPreferredActivity(IntentFilter, int, ComponentName[], ComponentName) 304 */ 305 public void testGetPreferredActivities() { 306 assertNoPreferredActivities(); 307 } 308 309 /** 310 * Helper method to test that {@link PackageManager#getPreferredActivities(List, List, String)} 311 * returns empty lists. 312 */ 313 private void assertNoPreferredActivities() { 314 List<ComponentName> outActivities = new ArrayList<ComponentName>(); 315 List<IntentFilter> outFilters = new ArrayList<IntentFilter>(); 316 mPackageManager.getPreferredActivities(outFilters, outActivities, PACKAGE_NAME); 317 assertEquals(0, outActivities.size()); 318 assertEquals(0, outFilters.size()); 319 } 320 321 /** 322 * Test that calling {@link PackageManager#addPreferredActivity(IntentFilter, int, 323 * ComponentName[], ComponentName)} throws a {@link SecurityException}. 324 * <p/> 325 * The method is protected by the {@link android.permission.SET_PREFERRED_APPLICATIONS} 326 * signature permission. Even though this app declares that permission, it still should not be 327 * able to call this method because it is not signed with the platform certificate. 328 */ 329 public void testAddPreferredActivity() { 330 IntentFilter intentFilter = new IntentFilter(ACTIVITY_ACTION_NAME); 331 ComponentName[] componentName = {new ComponentName(PACKAGE_NAME, ACTIVITY_NAME)}; 332 try { 333 mPackageManager.addPreferredActivity(intentFilter, IntentFilter.MATCH_CATEGORY_HOST, 334 componentName, componentName[0]); 335 fail("addPreferredActivity unexpectedly succeeded"); 336 } catch (SecurityException e) { 337 // expected 338 } 339 assertNoPreferredActivities(); 340 } 341 342 /** 343 * Test that calling {@link PackageManager#clearPackagePreferredActivities(String)} has no 344 * effect. 345 */ 346 public void testClearPackagePreferredActivities() { 347 // just ensure no unexpected exceptions are thrown, nothing else to do 348 mPackageManager.clearPackagePreferredActivities(PACKAGE_NAME); 349 } 350 351 private void checkComponentName(String expectedName, List<ComponentName> componentNames) { 352 boolean isContained = false; 353 Iterator<ComponentName> nameIterator = componentNames.iterator(); 354 String current; 355 while (nameIterator.hasNext()) { 356 current = nameIterator.next().getClassName(); 357 if (current.equals(expectedName)) { 358 isContained = true; 359 break; 360 } 361 } 362 assertTrue(isContained); 363 } 364 365 private void checkIntentFilterAction(String expectedName, List<IntentFilter> intentFilters) { 366 boolean isContained = false; 367 Iterator<IntentFilter> filterIterator = intentFilters.iterator(); 368 IntentFilter currentFilter; 369 String currentAction; 370 while (filterIterator.hasNext()) { 371 currentFilter = filterIterator.next(); 372 for (int i = 0; i < currentFilter.countActions(); i++) { 373 currentAction = currentFilter.getAction(i); 374 if (currentAction.equals(expectedName)) { 375 isContained = true; 376 break; 377 } 378 } 379 } 380 assertTrue(isContained); 381 } 382 383 public void testAccessEnabledSetting() { 384 mPackageManager.setApplicationEnabledSetting(PACKAGE_NAME, 385 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 386 assertEquals(PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 387 mPackageManager.getApplicationEnabledSetting(PACKAGE_NAME)); 388 389 ComponentName componentName = new ComponentName(PACKAGE_NAME, ACTIVITY_NAME); 390 mPackageManager.setComponentEnabledSetting(componentName, 391 PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); 392 assertEquals(PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 393 mPackageManager.getComponentEnabledSetting(componentName)); 394 } 395 396 public void testOpPermission() { 397 PermissionInfo permissionInfo = new PermissionInfo(); 398 String permissionName = "com.android.cts.stub.permission.TEST_DYNAMIC.ADD"; 399 permissionInfo.name = permissionName; 400 permissionInfo.labelRes = R.string.permlab_testDynamic; 401 permissionInfo.nonLocalizedLabel = "Test Tree"; 402 403 // TODO: Bug ID 1561181. 404 // Can't add permission in dynamic way 405 } 406 407 public void testGetIcon() throws NameNotFoundException { 408 assertNotNull(mPackageManager.getApplicationIcon(PACKAGE_NAME)); 409 assertNotNull(mPackageManager.getApplicationIcon(mPackageManager.getApplicationInfo( 410 PACKAGE_NAME, 0))); 411 assertNotNull(mPackageManager 412 .getActivityIcon(new ComponentName(PACKAGE_NAME, ACTIVITY_NAME))); 413 assertNotNull(mPackageManager.getActivityIcon(new Intent(MAIN_ACTION_NAME))); 414 assertNotNull(mPackageManager.getDefaultActivityIcon()); 415 416 // getDrawable is called by ComponentInfo.loadIcon() which called by getActivityIcon() 417 // method of PackageMaganer. Here is just assurance for its functionality. 418 int iconRes = R.drawable.start; 419 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(PACKAGE_NAME, 0); 420 assertNotNull(mPackageManager.getDrawable(PACKAGE_NAME, iconRes, appInfo)); 421 } 422 423 public void testCheckMethods() { 424 assertEquals(PackageManager.SIGNATURE_MATCH, mPackageManager.checkSignatures(PACKAGE_NAME, 425 CONTENT_PKG_NAME)); 426 assertEquals(PackageManager.PERMISSION_GRANTED, 427 mPackageManager.checkPermission(PERMISSION_NAME, PACKAGE_NAME)); 428 } 429 430 public void testResolveMethods() { 431 // Test resolveActivity 432 Intent intent = new Intent(ACTIVITY_ACTION_NAME); 433 intent.setComponent(new ComponentName(PACKAGE_NAME, ACTIVITY_NAME)); 434 assertEquals(ACTIVITY_NAME, mPackageManager.resolveActivity(intent, 435 PackageManager.MATCH_DEFAULT_ONLY).activityInfo.name); 436 437 // Test resolveService 438 intent = new Intent(SERVICE_ACTION_NAME); 439 intent.setComponent(new ComponentName(PACKAGE_NAME, SERVICE_NAME)); 440 ResolveInfo resolveInfo = mPackageManager.resolveService(intent, 441 PackageManager.GET_INTENT_FILTERS); 442 assertEquals(SERVICE_NAME, resolveInfo.serviceInfo.name); 443 444 // Test resolveContentProvider 445 String providerAuthorities = "ctstest"; 446 assertEquals(PROVIDER_NAME, 447 mPackageManager.resolveContentProvider(providerAuthorities, 0).name); 448 } 449 450 public void testGetResources() throws NameNotFoundException { 451 ComponentName componentName = new ComponentName(PACKAGE_NAME, ACTIVITY_NAME); 452 int resourceId = R.xml.pm_test; 453 String xmlName = "com.android.cts.stub:xml/pm_test"; 454 ApplicationInfo appInfo = mPackageManager.getApplicationInfo(PACKAGE_NAME, 0); 455 assertNotNull(mPackageManager.getXml(PACKAGE_NAME, resourceId, appInfo)); 456 assertEquals(xmlName, mPackageManager.getResourcesForActivity(componentName) 457 .getResourceName(resourceId)); 458 assertEquals(xmlName, mPackageManager.getResourcesForApplication(appInfo).getResourceName( 459 resourceId)); 460 assertEquals(xmlName, mPackageManager.getResourcesForApplication(PACKAGE_NAME) 461 .getResourceName(resourceId)); 462 } 463 464 public void testGetPackageArchiveInfo() throws Exception { 465 final String apkPath = getContext().getPackageCodePath(); 466 final String apkName = getContext().getPackageName(); 467 468 final int flags = PackageManager.GET_SIGNATURES; 469 470 final PackageInfo pkgInfo = mPackageManager.getPackageArchiveInfo(apkPath, flags); 471 472 assertEquals("getPackageArchiveInfo should return the correct package name", 473 apkName, pkgInfo.packageName); 474 475 assertNotNull("Signatures should have been collected when GET_SIGNATURES flag specified", 476 pkgInfo.signatures); 477 } 478 } 479