DO NOT MERGE: Add wait between stopping and starting user.

User switching for create and manage user test take time.
If the CTS restricted policy related test gets executed
before the process completion, it results in test failure.
So, a wait is added before the removeUser operation.

Test: run cts -m CtsDevicePolicyManagerTestCases -t com.android.cts.devicepolicy.DeviceOwnerTest#
testCreateAndManageUser_StopEphemeralUser_DisallowRemoveUser

Bug: 120816879
Change-Id: I49725d33bde4e7cf2cdf7659f6637648d0766737
Signed-off-by: Xinghua Yang <xinghua.yang@mediatek.com>
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/CreateAndManageUserTest.java b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/CreateAndManageUserTest.java
index 3227770..0280bdf 100755
--- a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/CreateAndManageUserTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/CreateAndManageUserTest.java
@@ -411,8 +411,13 @@
 
         LocalBroadcastReceiver broadcastReceiver = new LocalBroadcastReceiver();
         localBroadcastManager.registerReceiver(broadcastReceiver,
-                new IntentFilter(BasicAdminReceiver.ACTION_USER_REMOVED));
+                new IntentFilter(BasicAdminReceiver.ACTION_USER_STARTED));
+        broadcastReceiver.waitForBroadcastReceived();
+        localBroadcastManager.unregisterReceiver(broadcastReceiver);
 
+        // Register broadcast receiver for the remove action.
+        localBroadcastManager.registerReceiver(broadcastReceiver,
+                new IntentFilter(BasicAdminReceiver.ACTION_USER_REMOVED));
         try {
             assertEquals(UserManager.USER_OPERATION_SUCCESS,
                     mDevicePolicyManager.stopUser(getWho(), userHandle));