Fix Wipe Data CTS Tests on GSI

Use broadcast to trigger wipe of the work profile instead of running the
test code directly in the work profile user, so that the test doesn't
get killed when the work profile is destroyed.

Change-Id: I32aa3dc61be574d9ce5bb06c168f57c46db7f111
Bug: 110357404
Test: com.android.cts.devicepolicy.ManagedProfileTest
(cherry picked from commit 32ddc2509a448d885df655d2ae7f19535bfea5ca)
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml b/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml
index 3cb0bce..58e232b 100644
--- a/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/AndroidManifest.xml
@@ -191,6 +191,13 @@
           </intent-filter>
         </receiver>
 
+        <receiver android:name=".WipeDataReceiver">
+            <intent-filter>
+                <action android:name="com.android.cts.managedprofile.WIPE_DATA" />
+                <action android:name="com.android.cts.managedprofile.WIPE_DATA_WITH_REASON" />
+            </intent-filter>
+        </receiver>
+
     </application>
 
     <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/WipeDataReceiver.java b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/WipeDataReceiver.java
new file mode 100644
index 0000000..0abea7d
--- /dev/null
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/WipeDataReceiver.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2018 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.managedprofile;
+
+import android.app.admin.DevicePolicyManager;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+
+/**
+ * Invoke wipeData(), optionally including a reason. This is used by host side tests to destroy the
+ * work profile. The call is triggered via a broadcast instead of run as a normal test case, since
+ * the test process will be killed after calling wipeData() which will result in a test failure if
+ * this were run as a test case.
+ */
+public class WipeDataReceiver extends BroadcastReceiver {
+
+    private static final String ACTION_WIPE_DATA = "com.android.cts.managedprofile.WIPE_DATA";
+    private static final String ACTION_WIPE_DATA_WITH_REASON =
+            "com.android.cts.managedprofile.WIPE_DATA_WITH_REASON";
+    private static final String TEST_WIPE_DATA_REASON = "cts test for WipeDataWithReason";
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        final DevicePolicyManager dpm = context.getSystemService(DevicePolicyManager.class);
+        if (ACTION_WIPE_DATA.equals(intent.getAction())) {
+            dpm.wipeData(0);
+        } else if (ACTION_WIPE_DATA_WITH_REASON.equals(intent.getAction())) {
+            dpm.wipeData(0, TEST_WIPE_DATA_REASON);
+        }
+    }
+}
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
index 2dd5a2a..360e141 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
@@ -160,11 +160,7 @@
             return;
         }
         assertTrue(listUsers().contains(mProfileUserId));
-        runDeviceTestsAsUser(
-                MANAGED_PROFILE_PKG,
-                ".WipeDataTest",
-                "testWipeDataWithReason",
-                mProfileUserId);
+        sendWipeProfileBroadcast("com.android.cts.managedprofile.WIPE_DATA_WITH_REASON");
         // Note: the managed profile is removed by this test, which will make removeUserCommand in
         // tearDown() to complain, but that should be OK since its result is not asserted.
         assertUserGetsRemoved(mProfileUserId);
@@ -186,14 +182,19 @@
             return;
         }
         assertTrue(listUsers().contains(mProfileUserId));
-        runDeviceTestsAsUser(
-                MANAGED_PROFILE_PKG, ".WipeDataTest",
-                "testWipeData", mProfileUserId);
+        sendWipeProfileBroadcast("com.android.cts.managedprofile.WIPE_DATA");
         // Note: the managed profile is removed by this test, which will make removeUserCommand in
         // tearDown() to complain, but that should be OK since its result is not asserted.
         assertUserGetsRemoved(mProfileUserId);
     }
 
+    private void sendWipeProfileBroadcast(String action) throws Exception {
+        final String cmd = "am broadcast --receiver-foreground --user " + mProfileUserId
+            + " -a " + action
+            + " com.android.cts.managedprofile/.WipeDataReceiver";
+        getDevice().executeShellCommand(cmd);
+    }
+
     public void testLockNowWithKeyEviction() throws Exception {
         if (!mHasFeature || !mSupportsFbe) {
             return;