• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
testPrivAppAndEnabled()37     public void testPrivAppAndEnabled() throws Exception {
38         assertEquals(0, (getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP));
39         assertPackageEnabledState(PRIVILEGED_SHIM_PKG,
40                 PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
41     }
42 
testPrivAppAndDisabled()43     public void testPrivAppAndDisabled() throws Exception {
44         assertEquals(0, (getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP));
45         assertPackageEnabledState(PRIVILEGED_SHIM_PKG,
46                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER);
47     }
48 
testUpdatedPrivAppAndEnabled()49     public void testUpdatedPrivAppAndEnabled() throws Exception {
50         assertEquals(ApplicationInfo.FLAG_UPDATED_SYSTEM_APP,
51                 (getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP));
52         assertPackageEnabledState(PRIVILEGED_SHIM_PKG,
53                 PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
54     }
55 
testUpdatedPrivAppAndDisabled()56     public void testUpdatedPrivAppAndDisabled() throws Exception {
57         assertEquals(ApplicationInfo.FLAG_UPDATED_SYSTEM_APP,
58                 (getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP));
59         assertPackageEnabledState(PRIVILEGED_SHIM_PKG,
60                 PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER);
61     }
62 
getApplicationFlags()63     private int getApplicationFlags() {
64         PackageManager pm = getInstrumentation().getContext().getPackageManager();
65         try {
66             ApplicationInfo aInfo = pm.getApplicationInfo(PRIVILEGED_SHIM_PKG,
67                     PackageManager.MATCH_DISABLED_COMPONENTS);
68             return aInfo.flags;
69         } catch (NameNotFoundException e) {
70             return 0;
71         }
72     }
73 
assertPackageEnabledState(String packageName, int expectedState)74     private void assertPackageEnabledState(String packageName, int expectedState) {
75         PackageManager pm = getInstrumentation().getContext().getPackageManager();
76         int state = pm.getApplicationEnabledSetting(packageName);
77         if (state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
78             state = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
79         }
80         assertEquals(expectedState, state);
81     }
82 }
83