Simplify CTS testing for policies that apply to DO and PO

Currently, code for policies that apply to device and profile owner
situations has to be duplicated or added to a shared library. This
change introduces one app that can act as device and profile owner and
holds all common test classes. Added the runtime permission tests to
this app as a first example.

Bug: 22192363
Change-Id: I6a7a8bf9f64a44f5aed4d8f70c3868a603b98757
diff --git a/CtsTestCaseList.mk b/CtsTestCaseList.mk
index 8e0d83d..29c6480b 100644
--- a/CtsTestCaseList.mk
+++ b/CtsTestCaseList.mk
@@ -76,6 +76,7 @@
     CtsDeviceAdmin \
     CtsDeviceOpenGl \
     CtsWifiConfigCreator \
+    CtsDeviceAndProfileOwnerApp \
     CtsDeviceOwnerApp \
     CtsDeviceTaskswitchingAppA \
     CtsDeviceTaskswitchingAppB \
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/Android.mk b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/Android.mk
new file mode 100644
index 0000000..0548af6
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/Android.mk
@@ -0,0 +1,33 @@
+# Copyright (C) 2015 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.
+
+LOCAL_PATH:= $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_PACKAGE_NAME := CtsDeviceAndProfileOwnerApp
+
+LOCAL_MODULE_TAGS := optional
+
+LOCAL_MODULE_PATH := $(TARGET_OUT_DATA_APPS)
+
+LOCAL_SRC_FILES := $(call all-java-files-under, src)
+
+LOCAL_JAVA_LIBRARIES := android.test.runner cts-junit
+
+LOCAL_STATIC_JAVA_LIBRARIES = android-support-v4 ctstestrunner ub-uiautomator
+
+LOCAL_SDK_VERSION := current
+
+include $(BUILD_CTS_PACKAGE)
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/AndroidManifest.xml b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/AndroidManifest.xml
new file mode 100644
index 0000000..484dd6e
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/AndroidManifest.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2015 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.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.android.cts.deviceandprofileowner">
+
+    <uses-sdk android:minSdkVersion="23"/>
+
+    <application>
+        <uses-library android:name="android.test.runner" />
+        <receiver
+            android:name="com.android.cts.deviceandprofileowner.BaseDeviceAdminTest$BasicAdminReceiver"
+            android:permission="android.permission.BIND_DEVICE_ADMIN">
+            <meta-data android:name="android.app.device_admin"
+                       android:resource="@xml/device_admin" />
+            <intent-filter>
+                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
+            </intent-filter>
+        </receiver>
+    </application>
+
+    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
+                     android:targetPackage="com.android.cts.deviceandprofileowner"
+                     android:label="Profile and Device Owner CTS Tests"/>
+</manifest>
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/res/xml/device_admin.xml b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/res/xml/device_admin.xml
new file mode 100644
index 0000000..69a661c
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/res/xml/device_admin.xml
@@ -0,0 +1,18 @@
+<!-- Copyright (C) 2015 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.
+-->
+<device-admin xmlns:android="http://schemas.android.com/apk/res/android" android:visible="false">
+    <uses-policies>
+    </uses-policies>
+</device-admin>
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/BaseDeviceAdminTest.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/BaseDeviceAdminTest.java
new file mode 100644
index 0000000..148d8a8
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/BaseDeviceAdminTest.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2015 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.deviceandprofileowner;
+
+import android.app.admin.DeviceAdminReceiver;
+import android.app.admin.DevicePolicyManager;
+import android.content.ComponentName;
+import android.content.Context;
+import android.test.InstrumentationTestCase;
+
+/**
+ * Base class for profile and device based tests.
+ *
+ * This class handles making sure that the test is the profile or device owner and that it has an
+ * active admin registered, so that all tests may assume these are done.
+ */
+public class BaseDeviceAdminTest extends InstrumentationTestCase {
+
+    public static class BasicAdminReceiver extends DeviceAdminReceiver {
+    }
+
+    public static final String PACKAGE_NAME = BasicAdminReceiver.class.getPackage().getName();
+    public static final ComponentName ADMIN_RECEIVER_COMPONENT = new ComponentName(
+            PACKAGE_NAME, BasicAdminReceiver.class.getName());
+
+    protected DevicePolicyManager mDevicePolicyManager;
+    protected Context mContext;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mContext = getInstrumentation().getContext();
+
+        mDevicePolicyManager = (DevicePolicyManager)
+            mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
+        assertNotNull(mDevicePolicyManager);
+
+        assertTrue(mDevicePolicyManager.isAdminActive(ADMIN_RECEIVER_COMPONENT));
+        assertTrue("App is neither device nor profile owner",
+                mDevicePolicyManager.isProfileOwnerApp(PACKAGE_NAME) ||
+                mDevicePolicyManager.isDeviceOwnerApp(PACKAGE_NAME));
+    }
+}
diff --git a/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ClearDeviceOwnerTest.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ClearDeviceOwnerTest.java
new file mode 100644
index 0000000..eefe781
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/ClearDeviceOwnerTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2015 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.deviceandprofileowner;
+
+import android.app.admin.DevicePolicyManager;
+import android.content.ComponentName;
+import android.content.Context;
+import android.test.AndroidTestCase;
+
+public class ClearDeviceOwnerTest extends BaseDeviceAdminTest {
+
+    private DevicePolicyManager mDevicePolicyManager;
+
+    @Override
+    protected void tearDown() throws Exception {
+        mDevicePolicyManager = (DevicePolicyManager)
+                mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
+        if (mDevicePolicyManager != null) {
+            removeActiveAdmin(ADMIN_RECEIVER_COMPONENT);
+            if (mDevicePolicyManager.isDeviceOwnerApp(PACKAGE_NAME)) {
+                mDevicePolicyManager.clearDeviceOwnerApp(PACKAGE_NAME);
+            }
+            assertFalse(mDevicePolicyManager.isAdminActive(ADMIN_RECEIVER_COMPONENT));
+            assertFalse(mDevicePolicyManager.isDeviceOwnerApp(PACKAGE_NAME));
+        }
+
+        super.tearDown();
+    }
+
+    // This test clears the device owner and active admin on tearDown(). To be called from the host
+    // side test once a test case is finished.
+    public void testClearDeviceOwner() {
+    }
+
+    private void removeActiveAdmin(ComponentName cn) throws InterruptedException {
+        if (mDevicePolicyManager.isAdminActive(cn)) {
+            mDevicePolicyManager.removeActiveAdmin(cn);
+            for (int i = 0; i < 1000 && mDevicePolicyManager.isAdminActive(cn); i++) {
+                Thread.sleep(100);
+            }
+        }
+    }
+}
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/PermissionsTest.java b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/PermissionsTest.java
similarity index 96%
rename from hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/PermissionsTest.java
rename to hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/PermissionsTest.java
index 7ddf77f..8b3a975 100644
--- a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/PermissionsTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceAndProfileOwner/src/com/android/cts/deviceandprofileowner/PermissionsTest.java
@@ -13,7 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package com.android.cts.managedprofile;
+package com.android.cts.deviceandprofileowner;
 
 import android.app.admin.DevicePolicyManager;
 import android.content.BroadcastReceiver;
@@ -38,7 +38,7 @@
 /**
  * Test Runtime Permissions APIs in DevicePolicyManager.
  */
-public class PermissionsTest extends BaseManagedProfileTest {
+public class PermissionsTest extends BaseDeviceAdminTest {
     private static final String TAG = "PermissionsTest";
 
     private static final String PERMISSION_APP_PACKAGE_NAME
@@ -76,11 +76,6 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        // Make sure we are running in a managed profile, otherwise risk wiping the primary user's
-        // data.
-        assertTrue(mDevicePolicyManager.isAdminActive(ADMIN_RECEIVER_COMPONENT));
-        assertTrue(mDevicePolicyManager.isProfileOwnerApp(
-                ADMIN_RECEIVER_COMPONENT.getPackageName()));
         mReceiver = new PermissionBroadcastReceiver();
         mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_PERMISSION_RESULT));
         mPackageManager = mContext.getPackageManager();
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
index de7c16b..e47ec9e 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/BaseDevicePolicyTest.java
@@ -49,13 +49,9 @@
 
     private static final String RUNNER = "android.support.test.runner.AndroidJUnitRunner";
 
-    protected static final String MANAGED_PROFILE_PKG = "com.android.cts.managedprofile";
-    protected static final String MANAGED_PROFILE_APK = "CtsManagedProfileApp.apk";
-    protected static final String ADMIN_RECEIVER_TEST_CLASS =
-            MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver";
-
     protected CtsBuildHelper mCtsBuild;
 
+    private String mPackageVerifier;
     private HashSet<String> mAvailableFeatures;
     protected boolean mHasFeature;
 
@@ -70,6 +66,18 @@
         assertNotNull(mCtsBuild);  // ensure build has been set before test is run.
         mHasFeature = getDevice().getApiLevel() >= 21 /* Build.VERSION_CODES.L */
                 && hasDeviceFeature("android.software.device_admin");
+        // disable the package verifier to avoid the dialog when installing an app
+        mPackageVerifier = getDevice().executeShellCommand(
+                "settings get global package_verifier_enable");
+        getDevice().executeShellCommand("settings put global package_verifier_enable 0");
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        // reset the package verifier setting to its original value
+        getDevice().executeShellCommand("settings put global package_verifier_enable "
+                + mPackageVerifier);
+        super.tearDown();
     }
 
     protected void installApp(String fileName)
@@ -341,4 +349,12 @@
         assertTrue(commandOutput + " expected to start with \"Success:\"",
                 commandOutput.startsWith("Success:"));
     }
+
+    protected void setDeviceOwner(String componentName) throws DeviceNotAvailableException {
+        String command = "dpm set-device-owner '" + componentName + "'";
+        String commandOutput = getDevice().executeShellCommand(command);
+        CLog.logAndDisplay(LogLevel.INFO, "Output for command " + command + ": " + commandOutput);
+        assertTrue(commandOutput + " expected to start with \"Success:\"",
+                commandOutput.startsWith("Success:"));
+    }
 }
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
new file mode 100644
index 0000000..b0918bc
--- /dev/null
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceAndProfileOwnerTest.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2015 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.devicepolicy;
+
+import com.android.ddmlib.Log.LogLevel;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.log.LogUtil.CLog;
+
+import junit.framework.AssertionFailedError;
+
+/**
+ * Set of tests for usecases that apply to profile and device owner.
+ * This class is the base class of MixedProfileOwnerTest and MixedDeviceOwnerTest and is abstract
+ * to avoid running spurious tests.
+ */
+public abstract class DeviceAndProfileOwnerTest extends BaseDevicePolicyTest {
+
+    protected static final String DEVICE_ADMIN_PKG = "com.android.cts.deviceandprofileowner";
+    protected static final String DEVICE_ADMIN_APK = "CtsDeviceAndProfileOwnerApp.apk";
+    protected static final String ADMIN_RECEIVER_TEST_CLASS
+            = ".BaseDeviceAdminTest$BasicAdminReceiver";
+
+    private static final String PERMISSIONS_APP_PKG = "com.android.cts.permission.permissionapp";
+    private static final String PERMISSIONS_APP_APK = "CtsPermissionApp.apk";
+
+    private static final String SIMPLE_PRE_M_APP_PKG = "com.android.cts.launcherapps.simplepremapp";
+    private static final String SIMPLE_PRE_M_APP_APK = "CtsSimplePreMApp.apk";
+
+    // ID of the user all tests are run as. For device owner this will be 0, for profile owner it
+    // is the user id of the created profile.
+    protected int mUserId;
+
+    protected void tearDown() throws Exception {
+        if (mHasFeature) {
+            getDevice().uninstallPackage(PERMISSIONS_APP_PKG);
+            getDevice().uninstallPackage(SIMPLE_PRE_M_APP_PKG);
+        }
+        super.tearDown();
+    }
+
+    public void testPermissionGrant() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionGrantState", mUserId));
+    }
+
+    public void testPermissionPolicy() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionPolicy", mUserId));
+    }
+
+    public void testPermissionMixedPolicies() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionMixedPolicies", mUserId));
+    }
+
+    public void testPermissionPrompts() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        try {
+            // unlock device and ensure that the screen stays on
+            getDevice().executeShellCommand("input keyevent 82");
+            getDevice().executeShellCommand("settings put global stay_on_while_plugged_in 2");
+            installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+            assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                    "testPermissionPrompts", mUserId));
+        } finally {
+            getDevice().executeShellCommand("settings put global stay_on_while_plugged_in 0");
+        }
+    }
+
+    public void testPermissionAppUpdate() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_setDeniedState", mUserId));
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_checkDenied", mUserId));
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_checkDenied", mUserId));
+
+        assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_setGrantedState", mUserId));
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_checkGranted", mUserId));
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_checkGranted", mUserId));
+
+        assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_setAutoDeniedPolicy", mUserId));
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_checkDenied", mUserId));
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_checkDenied", mUserId));
+
+        assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_setAutoGrantedPolicy", mUserId));
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_checkGranted", mUserId));
+        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionUpdate_checkGranted", mUserId));
+    }
+
+    public void testPermissionGrantPreMApp() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        installAppAsUser(SIMPLE_PRE_M_APP_APK, mUserId);
+        assertTrue(runDeviceTestsAsUser(DEVICE_ADMIN_PKG, ".PermissionsTest",
+                "testPermissionGrantStatePreMApp", mUserId));
+    }
+}
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
index 66a123a1..26d0d233 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
@@ -135,13 +135,4 @@
         String testClass = DEVICE_OWNER_PKG + "." + testClassName;
         assertTrue(testClass + " failed.", runDeviceTests(DEVICE_OWNER_PKG, testClass));
     }
-
-    private void setDeviceOwner(String componentName) throws DeviceNotAvailableException {
-        String command = "dpm set-device-owner '" + componentName + "'";
-        String commandOutput = getDevice().executeShellCommand(command);
-        CLog.logAndDisplay(LogLevel.INFO, "Output for command " + command + ": " + commandOutput);
-        assertTrue(commandOutput + " expected to start with \"Success:\"",
-                commandOutput.startsWith("Success:"));
-    }
-
 }
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsProfileTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsProfileTest.java
index 43f1f5a..c65d443 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsProfileTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/LauncherAppsProfileTest.java
@@ -25,6 +25,11 @@
  */
 public class LauncherAppsProfileTest extends BaseLauncherAppsTest {
 
+    private static final String MANAGED_PROFILE_PKG = "com.android.cts.managedprofile";
+    private static final String MANAGED_PROFILE_APK = "CtsManagedProfileApp.apk";
+    private static final String ADMIN_RECEIVER_TEST_CLASS =
+            MANAGED_PROFILE_PKG + ".BaseManagedProfileTest$BasicAdminReceiver";
+
     private int mProfileUserId;
     private int mProfileSerialNumber;
     private int mMainUserSerialNumber;
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
index 52e1e75..ade4807 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
@@ -30,12 +30,6 @@
     private static final String MANAGED_PROFILE_PKG = "com.android.cts.managedprofile";
     private static final String MANAGED_PROFILE_APK = "CtsManagedProfileApp.apk";
 
-    private static final String SIMPLE_PRE_M_APP_PKG = "com.android.cts.launcherapps.simplepremapp";
-    private static final String SIMPLE_PRE_M_APP_APK = "CtsSimplePreMApp.apk";
-
-    private static final String PERMISSIONS_APP_PKG = "com.android.cts.permission.permissionapp";
-    private static final String PERMISSIONS_APP_APK = "CtsPermissionApp.apk";
-
     private static final String INTENT_SENDER_PKG = "com.android.cts.intent.sender";
     private static final String INTENT_SENDER_APK = "CtsIntentSenderApp.apk";
 
@@ -75,11 +69,6 @@
             removeTestUsers();
             mUserId = createManagedProfile();
 
-            // disable the package verifier to avoid the dialog when installing an app
-            mPackageVerifier = getDevice().executeShellCommand(
-                    "settings get global package_verifier_enable");
-            getDevice().executeShellCommand("settings put global package_verifier_enable 0");
-
             installApp(MANAGED_PROFILE_APK);
             setProfileOwner(MANAGED_PROFILE_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, mUserId);
             startUser(mUserId);
@@ -94,9 +83,6 @@
             getDevice().uninstallPackage(INTENT_SENDER_PKG);
             getDevice().uninstallPackage(INTENT_RECEIVER_PKG);
             getDevice().uninstallPackage(CERT_INSTALLER_PKG);
-            // reset the package verifier setting to its original value
-            getDevice().executeShellCommand("settings put global package_verifier_enable "
-                    + mPackageVerifier);
         }
         super.tearDown();
     }
@@ -549,102 +535,6 @@
         }
     }
 
-    public void testPermissionGrant() throws Exception {
-        if (!mHasFeature) {
-            return;
-        }
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionGrantState", mUserId));
-    }
-
-    public void testPermissionPolicy() throws Exception {
-        if (!mHasFeature) {
-            return;
-        }
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionPolicy", mUserId));
-    }
-
-    public void testPermissionMixedPolicies() throws Exception {
-        if (!mHasFeature) {
-            return;
-        }
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionMixedPolicies", mUserId));
-    }
-
-    public void testPermissionPrompts() throws Exception {
-        if (!mHasFeature) {
-            return;
-        }
-        try {
-            // unlock device and ensure that the screen stays on
-            getDevice().executeShellCommand("input keyevent 82");
-            getDevice().executeShellCommand("settings put global stay_on_while_plugged_in 2");
-            installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-            assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                    "testPermissionPrompts", mUserId));
-        } finally {
-            getDevice().executeShellCommand("settings put global stay_on_while_plugged_in 0");
-        }
-    }
-
-    public void testPermissionAppUpdate() throws Exception {
-        if (!mHasFeature) {
-            return;
-        }
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_setDeniedState", mUserId));
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_checkDenied", mUserId));
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_checkDenied", mUserId));
-
-        assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_setGrantedState", mUserId));
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_checkGranted", mUserId));
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_checkGranted", mUserId));
-
-        assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_setAutoDeniedPolicy", mUserId));
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_checkDenied", mUserId));
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_checkDenied", mUserId));
-
-        assertNull(getDevice().uninstallPackage(PERMISSIONS_APP_PKG));
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_setAutoGrantedPolicy", mUserId));
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_checkGranted", mUserId));
-        installAppAsUser(PERMISSIONS_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionUpdate_checkGranted", mUserId));
-    }
-
-    public void testPermissionGrantPreMApp() throws Exception {
-        if (!mHasFeature) {
-            return;
-        }
-        installAppAsUser(SIMPLE_PRE_M_APP_APK, mUserId);
-        assertTrue(runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".PermissionsTest",
-                "testPermissionGrantStatePreMApp", mUserId));
-    }
-
     private void disableActivityForUser(String activityName, int userId)
             throws DeviceNotAvailableException {
         String command = "am start -W --user " + userId
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedDeviceOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedDeviceOwnerTest.java
new file mode 100644
index 0000000..97851d4
--- /dev/null
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedDeviceOwnerTest.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2015 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.devicepolicy;
+
+import com.android.ddmlib.Log.LogLevel;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.log.LogUtil.CLog;
+
+import junit.framework.AssertionFailedError;
+
+/**
+ * Set of tests for device owner use cases that also apply to profile owners.
+ * Tests that should be run identically in both cases are added in DeviceAndProfileOwnerTest.
+ */
+public class MixedDeviceOwnerTest extends DeviceAndProfileOwnerTest {
+
+    private static final String CLEAR_DEVICE_OWNER_TEST_CLASS =
+            DEVICE_ADMIN_PKG + ".ClearDeviceOwnerTest";
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        if (mHasFeature) {
+            mUserId = 0;
+
+            installApp(DEVICE_ADMIN_APK);
+            setDeviceOwner(DEVICE_ADMIN_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS);
+        }
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        if (mHasFeature) {
+            assertTrue("Failed to remove device owner.",
+                    runDeviceTests(DEVICE_ADMIN_PKG, CLEAR_DEVICE_OWNER_TEST_CLASS));
+            getDevice().uninstallPackage(DEVICE_ADMIN_PKG);
+        }
+        super.tearDown();
+    }
+
+    // All tests for this class are defined in DeviceAndProfileOwnerTest
+}
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedProfileOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedProfileOwnerTest.java
new file mode 100644
index 0000000..7f47227
--- /dev/null
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/MixedProfileOwnerTest.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2015 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.devicepolicy;
+
+import com.android.ddmlib.Log.LogLevel;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.log.LogUtil.CLog;
+
+import junit.framework.AssertionFailedError;
+
+/**
+ * Set of tests for profile owner use cases that also apply to device owners.
+ * Tests that should be run identically in both cases are added in DeviceAndProfileOwnerTest.
+ */
+public class MixedProfileOwnerTest extends DeviceAndProfileOwnerTest {
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        // We need managed users to be supported in order to create a profile of the user owner.
+        mHasFeature &= hasDeviceFeature("android.software.managed_users");
+
+        if (mHasFeature) {
+            removeTestUsers();
+            mUserId = createManagedProfile();
+
+            installApp(DEVICE_ADMIN_APK);
+            setProfileOwner(DEVICE_ADMIN_PKG + "/" + ADMIN_RECEIVER_TEST_CLASS, mUserId);
+            startUser(mUserId);
+        }
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        if (mHasFeature) {
+            removeUser(mUserId);
+            getDevice().uninstallPackage(DEVICE_ADMIN_PKG);
+        }
+        super.tearDown();
+    }
+
+    // All tests for this class are defined in DeviceAndProfileOwnerTest
+}