CTS tests to allow updated system apps to be disabled

This change also fixes a priv-app upgrade CTS test
that wasn't properly running due to a package name typo.

Bug: 27924655
Change-Id: I1e4b9d3065d92e0b7e8bc0754cea1af2432df7f1
diff --git a/hostsidetests/appsecurity/src/android/appsecurity/cts/PrivilegedUpdateTests.java b/hostsidetests/appsecurity/src/android/appsecurity/cts/PrivilegedUpdateTests.java
index b2b4476..c726fb7 100644
--- a/hostsidetests/appsecurity/src/android/appsecurity/cts/PrivilegedUpdateTests.java
+++ b/hostsidetests/appsecurity/src/android/appsecurity/cts/PrivilegedUpdateTests.java
@@ -64,6 +64,7 @@
 
         assertNull(getDevice().installPackage(
                 MigrationHelper.getTestFile(mCtsBuild, TEST_APK), false));
+        getDevice().executeShellCommand("pm enable " + SHIM_PKG);
     }
 
     @Override
@@ -72,6 +73,7 @@
 
         getDevice().uninstallPackage(SHIM_PKG);
         getDevice().uninstallPackage(TEST_PKG);
+        getDevice().executeShellCommand("pm enable " + SHIM_PKG);
     }
 
     public void testSystemAppPriorities() throws Exception {
@@ -94,6 +96,28 @@
         }
     }
 
+    public void testDisableSystemApp() throws Exception {
+        getDevice().executeShellCommand("pm enable " + SHIM_PKG);
+        runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testPrivAppAndEnabled");
+        getDevice().executeShellCommand("pm disable-user " + SHIM_PKG);
+        runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testPrivAppAndDisabled");
+    }
+
+    public void testDisableUpdatedSystemApp() throws Exception {
+        getDevice().executeShellCommand("pm enable " + SHIM_PKG);
+        runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testPrivAppAndEnabled");
+        try {
+            assertNull(getDevice().installPackage(
+                    MigrationHelper.getTestFile(mCtsBuild, SHIM_UPDATE_APK), true));
+            getDevice().executeShellCommand("pm disable-user " + SHIM_PKG);
+            runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testUpdatedPrivAppAndDisabled");
+            getDevice().executeShellCommand("pm enable " + SHIM_PKG);
+            runDeviceTests(TEST_PKG, ".PrivilegedAppDisableTest", "testUpdatedPrivAppAndEnabled");
+        } finally {
+            getDevice().uninstallPackage(SHIM_PKG);
+        }
+    }
+
     private void runDeviceTests(String packageName, String testClassName, String testMethodName)
             throws DeviceNotAvailableException {
         Utils.runDeviceTests(getDevice(), packageName, testClassName, testMethodName);
diff --git a/hostsidetests/appsecurity/src/android/appsecurity/cts/Utils.java b/hostsidetests/appsecurity/src/android/appsecurity/cts/Utils.java
index 7048074..37aaeaf 100644
--- a/hostsidetests/appsecurity/src/android/appsecurity/cts/Utils.java
+++ b/hostsidetests/appsecurity/src/android/appsecurity/cts/Utils.java
@@ -85,7 +85,9 @@
             throw new AssertionError("Failed to successfully run device tests for "
                     + result.getName() + ": " + result.getRunFailureMessage());
         }
-
+        if (result.getNumTests() == 0) {
+            throw new AssertionError("No tests were run on the device");
+        }
         if (result.hasFailedTests()) {
             // build a meaningful error message
             StringBuilder errorBuilder = new StringBuilder("on-device tests failed:\n");
diff --git a/hostsidetests/appsecurity/test-apps/PrivilegedUpdateApp/src/com/android/cts/privilegedupdate/PrivilegedAppDisableTest.java b/hostsidetests/appsecurity/test-apps/PrivilegedUpdateApp/src/com/android/cts/privilegedupdate/PrivilegedAppDisableTest.java
new file mode 100644
index 0000000..606ada1
--- /dev/null
+++ b/hostsidetests/appsecurity/test-apps/PrivilegedUpdateApp/src/com/android/cts/privilegedupdate/PrivilegedAppDisableTest.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.cts.privilegedupdate;
+
+import java.util.List;
+
+import android.content.ComponentName;
+import android.content.Intent;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.content.pm.PackageManager.NameNotFoundException;
+import android.net.Uri;
+import android.test.InstrumentationTestCase;
+
+/**
+ * Tests for system app type and enabled state
+ */
+public class PrivilegedAppDisableTest extends InstrumentationTestCase {
+    /** Package name of the privileged CTS shim */
+    private static final String PRIVILEGED_SHIM_PKG = "com.android.cts.priv.ctsshim";
+
+    public void testPrivAppAndEnabled() throws Exception {
+        assertEquals((getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP),
+                0);
+        assertPackageEnabledState(PRIVILEGED_SHIM_PKG,
+                PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
+    }
+
+    public void testPrivAppAndDisabled() throws Exception {
+        assertEquals((getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP),
+                0);
+        assertPackageEnabledState(PRIVILEGED_SHIM_PKG,
+                PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER);
+    }
+
+    public void testUpdatedPrivAppAndEnabled() throws Exception {
+        assertEquals((getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP),
+                ApplicationInfo.FLAG_UPDATED_SYSTEM_APP);
+        assertPackageEnabledState(PRIVILEGED_SHIM_PKG,
+                PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
+    }
+
+    public void testUpdatedPrivAppAndDisabled() throws Exception {
+        assertEquals((getApplicationFlags() & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP),
+                ApplicationInfo.FLAG_UPDATED_SYSTEM_APP);
+        assertPackageEnabledState(PRIVILEGED_SHIM_PKG,
+                PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER);
+    }
+
+    private int getApplicationFlags() {
+        PackageManager pm = getInstrumentation().getContext().getPackageManager();
+        try {
+            ApplicationInfo aInfo = pm.getApplicationInfo(PRIVILEGED_SHIM_PKG,
+                    PackageManager.MATCH_DISABLED_COMPONENTS);
+            return aInfo.flags;
+        } catch (NameNotFoundException e) {
+            return 0;
+        }
+    }
+
+    private void assertPackageEnabledState(String packageName, int expectedState) {
+        PackageManager pm = getInstrumentation().getContext().getPackageManager();
+        int state = pm.getApplicationEnabledSetting(packageName);
+        if (state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
+            state = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
+        }
+        assertEquals(expectedState, state);
+    }
+}
diff --git a/hostsidetests/appsecurity/test-apps/PrivilegedUpdateApp/src/com/android/cts/privilegedupdate/PrivilegedUpdateTest.java b/hostsidetests/appsecurity/test-apps/PrivilegedUpdateApp/src/com/android/cts/privilegedupdate/PrivilegedUpdateTest.java
index 7b92bea..9246256 100644
--- a/hostsidetests/appsecurity/test-apps/PrivilegedUpdateApp/src/com/android/cts/privilegedupdate/PrivilegedUpdateTest.java
+++ b/hostsidetests/appsecurity/test-apps/PrivilegedUpdateApp/src/com/android/cts/privilegedupdate/PrivilegedUpdateTest.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.cts.intentfilter;
+package com.android.cts.privilegedupdate;
 
 import java.util.List;