Add CTS test to verify kiosk mode works after reboot

Bug: 38220702
Bug: 34811243
Test: cts-tradefed run cts -m CtsDevicePolicyManagerTestCases -t
com.android.cts.devicepolicy.DeviceOwnerTest#testLockTaskAfterReboot_deviceOwnerUser

Change-Id: I08840a5c449917c3723da5013b8492c6eddf8f6b
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml b/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml
index 01967dc..9a09007 100644
--- a/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml
@@ -61,7 +61,14 @@
             android:name="com.android.cts.deviceowner.LockTaskUtilityActivity" />
         <activity
             android:name="com.android.cts.deviceowner.LockTaskUtilityActivityIfWhitelisted"
-            android:lockTaskMode="if_whitelisted" />
+            android:launchMode="singleInstance"
+            android:lockTaskMode="if_whitelisted">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN"/>
+                <category android:name="android.intent.category.HOME"/>
+                <category android:name="android.intent.category.DEFAULT"/>
+            </intent-filter>
+        </activity>
 
         <!-- we need to give a different taskAffinity so that when we use
              FLAG_ACTIVITY_NEW_TASK, the system tries to start it in a different task
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/LockTaskHostDrivenTest.java b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/LockTaskHostDrivenTest.java
new file mode 100644
index 0000000..ce79922
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/LockTaskHostDrivenTest.java
@@ -0,0 +1,131 @@
+/*
+ * Copyright (C) 2017 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.deviceowner;
+
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+
+import android.app.ActivityManager;
+import android.app.admin.DevicePolicyManager;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+import android.support.test.uiautomator.UiDevice;
+import android.util.Log;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Test class that is meant to be driven from the host and can't be run alone, which is required
+ * for tests that include rebooting or other connection-breaking steps. For this reason, this class
+ * does not override tearDown and setUp just initializes the test state, changing nothing in the
+ * device. Therefore, the host is responsible for making sure the tests leave the device in a clean
+ * state after running.
+ */
+@RunWith(AndroidJUnit4.class)
+public class LockTaskHostDrivenTest {
+
+    private static final String TAG = LockTaskHostDrivenTest.class.getName();
+
+    private static final String PACKAGE_NAME = LockTaskHostDrivenTest.class.getPackage().getName();
+    private static final ComponentName ADMIN_COMPONENT =
+            new ComponentName(PACKAGE_NAME, BaseDeviceOwnerTest.BasicAdminReceiver.class.getName());
+
+    private static final String LOCK_TASK_ACTIVITY
+            = LockTaskUtilityActivityIfWhitelisted.class.getName();
+
+    private UiDevice mUiDevice;
+    private Context mContext;
+    private ActivityManager mActivityManager;
+    private DevicePolicyManager mDevicePolicyManager;
+
+    @Before
+    public void setUp() {
+        mContext = InstrumentationRegistry.getContext();
+        mDevicePolicyManager =  mContext.getSystemService(DevicePolicyManager.class);
+        mActivityManager = mContext.getSystemService(ActivityManager.class);
+        mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
+    }
+
+    @Test
+    public void startLockTask() throws Exception {
+        Log.d(TAG, "startLockTask on host-driven test (no cleanup)");
+        setDefaultHomeIntentReceiver();
+        launchLockTaskActivity();
+        mUiDevice.waitForIdle();
+    }
+
+    @Test
+    public void testLockTaskIsActiveAndCantBeInterrupted() throws Exception {
+        mUiDevice.waitForIdle();
+
+        checkLockedActivityIsRunning();
+
+        mUiDevice.pressBack();
+        mUiDevice.waitForIdle();
+        checkLockedActivityIsRunning();
+
+        mUiDevice.pressHome();
+        mUiDevice.waitForIdle();
+        checkLockedActivityIsRunning();
+
+        mUiDevice.pressRecentApps();
+        mUiDevice.waitForIdle();
+        checkLockedActivityIsRunning();
+
+        mUiDevice.waitForIdle();
+    }
+
+    @Test
+    public void clearDefaultHomeIntentReceiver() {
+        mDevicePolicyManager.clearPackagePersistentPreferredActivities(ADMIN_COMPONENT,
+                PACKAGE_NAME);
+        mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[0]);
+    }
+
+    private void checkLockedActivityIsRunning() throws Exception {
+        assertTrue(isActivityOnTop());
+        assertEquals(ActivityManager.LOCK_TASK_MODE_LOCKED,
+                mActivityManager.getLockTaskModeState());
+    }
+
+    private boolean isActivityOnTop() {
+        return mActivityManager.getAppTasks().get(0).getTaskInfo().topActivity
+                .getClassName().equals(LOCK_TASK_ACTIVITY);
+    }
+
+    private void launchLockTaskActivity() {
+        Intent intent = new Intent();
+        intent.setClassName(PACKAGE_NAME, LOCK_TASK_ACTIVITY);
+        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
+        intent.putExtra(LockTaskUtilityActivity.START_LOCK_TASK, true);
+        mContext.startActivity(intent);
+    }
+
+    private void setDefaultHomeIntentReceiver() {
+        mDevicePolicyManager.setLockTaskPackages(ADMIN_COMPONENT, new String[] { PACKAGE_NAME });
+        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
+        intentFilter.addCategory(Intent.CATEGORY_HOME);
+        intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
+        mDevicePolicyManager.addPersistentPreferredActivity(ADMIN_COMPONENT, intentFilter,
+                new ComponentName(PACKAGE_NAME, LOCK_TASK_ACTIVITY));
+    }
+}
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
index ac846a3..b5d33ad 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/DeviceOwnerTest.java
@@ -372,6 +372,35 @@
         }
     }
 
+    public void testLockTaskAfterReboot_deviceOwnerUser() throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+
+        try {
+            // Just start kiosk mode
+            runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".LockTaskHostDrivenTest", "startLockTask",
+                    mPrimaryUserId);
+
+            // Reboot while in kiosk mode and then unlock the device
+            getDevice().reboot();
+
+            // Check that kiosk mode is working and can't be interrupted
+            runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".LockTaskHostDrivenTest",
+                    "testLockTaskIsActiveAndCantBeInterrupted", mPrimaryUserId);
+
+            // Try to open settings via adb
+            executeShellCommand("am start -a android.settings.SETTINGS");
+
+            // Check again
+            runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".LockTaskHostDrivenTest",
+                    "testLockTaskIsActiveAndCantBeInterrupted", mPrimaryUserId);
+        } finally {
+            runDeviceTestsAsUser(DEVICE_OWNER_PKG, ".LockTaskHostDrivenTest",
+                    "clearDefaultHomeIntentReceiver", mPrimaryUserId);
+        }
+    }
+
     public void testLockTask_unaffiliatedUser() throws Exception {
         if (!mHasFeature || !canCreateAdditionalUsers(1)) {
             return;
@@ -565,4 +594,4 @@
         runDeviceTestsAsUser(DEVICE_OWNER_PKG, className, testName,
                 /* deviceOwnerUserId */ mPrimaryUserId);
     }
-}
+}
\ No newline at end of file