1 /* 2 * Copyright (C) 2016 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.privilegedupdate; 18 19 import java.util.List; 20 21 import android.content.ComponentName; 22 import android.content.Intent; 23 import android.content.pm.ApplicationInfo; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ResolveInfo; 26 import android.content.pm.PackageManager.NameNotFoundException; 27 import android.net.Uri; 28 import android.test.InstrumentationTestCase; 29 30 /** 31 * Tests for system app type and enabled state 32 */ 33 public class PrivilegedAppDisableTest extends InstrumentationTestCase { 34 /** Package name of the privileged CTS shim */ 35 private static final String PRIVILEGED_SHIM_PKG = "com.android.cts.priv.ctsshim"; 36 37 public void testPrivAppAndEnabled() throws Exception { 38 assertEquals((getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP), 39 0); 40 assertPackageEnabledState(PRIVILEGED_SHIM_PKG, 41 PackageManager.COMPONENT_ENABLED_STATE_ENABLED); 42 } 43 44 public void testPrivAppAndDisabled() throws Exception { 45 assertEquals((getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP), 46 0); 47 assertPackageEnabledState(PRIVILEGED_SHIM_PKG, 48 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER); 49 } 50 51 public void testUpdatedPrivAppAndEnabled() throws Exception { 52 assertEquals((getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP), 53 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP); 54 assertPackageEnabledState(PRIVILEGED_SHIM_PKG, 55 PackageManager.COMPONENT_ENABLED_STATE_ENABLED); 56 } 57 58 public void testUpdatedPrivAppAndDisabled() throws Exception { 59 assertEquals((getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP), 60 ApplicationInfo.FLAG_UPDATED_SYSTEM_APP); 61 assertPackageEnabledState(PRIVILEGED_SHIM_PKG, 62 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER); 63 } 64 65 private int getApplicationFlags() { 66 PackageManager pm = getInstrumentation().getContext().getPackageManager(); 67 try { 68 ApplicationInfo aInfo = pm.getApplicationInfo(PRIVILEGED_SHIM_PKG, 69 PackageManager.MATCH_DISABLED_COMPONENTS); 70 return aInfo.flags; 71 } catch (NameNotFoundException e) { 72 return 0; 73 } 74 } 75 76 private void assertPackageEnabledState(String packageName, int expectedState) { 77 PackageManager pm = getInstrumentation().getContext().getPackageManager(); 78 int state = pm.getApplicationEnabledSetting(packageName); 79 if (state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { 80 state = PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 81 } 82 assertEquals(expectedState, state); 83 } 84 } 85