Fix the lock task cts test.

This test used to rely on onPause being called as a sign that a new activity
had been launched.
But onPause may be called without a new activity being launched.

To fix this test:
The activity that is launched signals that it has been launched with a
broadcast intent.

Also: the main event that can take a long time is to destroy the activity.
So, giving a long timeout when destroying the activity, and a shorter timeout
for the rest.

BUG:17890673

Change-Id: Ieb51ee64dafe26d8633f280c6554bc8e4ebf3bad
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml b/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml
index 7a196bf..dfc7e9c 100644
--- a/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/AndroidManifest.xml
@@ -49,6 +49,14 @@
 
         <activity
             android:name="com.android.cts.deviceowner.LockTaskUtilityActivity" />
+
+        <!-- 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
+        -->
+        <activity
+            android:name="com.android.cts.deviceowner.LockTaskTest$IntentReceivingActivity"
+            android:taskAffinity="com.android.cts.deviceowner.LockTaskTest.IntentReceivingActivity"
+            />
         <activity
             android:name="com.android.cts.deviceowner.ApplicationRestrictionActivity" />
     </application>
diff --git a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/LockTaskTest.java b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/LockTaskTest.java
index 42aa847..69c5bf7 100644
--- a/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/LockTaskTest.java
+++ b/hostsidetests/devicepolicy/app/DeviceOwner/src/com/android/cts/deviceowner/LockTaskTest.java
@@ -15,12 +15,14 @@
  */
 package com.android.cts.deviceowner;
 
+import android.app.Activity;
 import android.app.ActivityManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.provider.Settings;
+import android.os.Bundle;
 
 // This is not a standard test of an android activity (such as
 // ActivityInstrumentationTestCase2) as it is attempting to test the actual
@@ -30,9 +32,12 @@
 
     private static final String TEST_PACKAGE = "com.google.android.example.somepackage";
 
-    private static final int ACTIVITY_RESUMED_TIMEOUT_MILLIS = 60000;  // 60 seconds
-    private static final int ACTIVITY_RUNNING_TIMEOUT_MILLIS = 20000;  // 20 seconds
+    private static final int ACTIVITY_RESUMED_TIMEOUT_MILLIS = 20000;  // 20 seconds
+    private static final int ACTIVITY_RUNNING_TIMEOUT_MILLIS = 10000;  // 10 seconds
+    private static final int ACTIVITY_DESTROYED_TIMEOUT_MILLIS = 60000;  // 60 seconds
 
+    public static final String RECEIVING_ACTIVITY_CREATED_ACTION
+            = "com.android.cts.deviceowner.RECEIVER_ACTIVITY_STARTED_ACTION";
     /**
      * The tests below need to keep detailed track of the state of the activity
      * that is started and stopped frequently.  To do this it sends a number of
@@ -71,14 +76,30 @@
                     mIntentHandled = true;
                     LockTaskTest.this.notify();
                 }
+            } else if (RECEIVING_ACTIVITY_CREATED_ACTION.equals(action)) {
+                synchronized(mReceivingActivityCreatedLock) {
+                    mReceivingActivityWasCreated = true;
+                    mReceivingActivityCreatedLock.notify();
+                }
             }
         }
     };
 
+    public static class IntentReceivingActivity extends Activity {
+        @Override
+        public void onCreate(Bundle savedInstanceState) {
+            super.onCreate(savedInstanceState);
+            sendBroadcast(new Intent(RECEIVING_ACTIVITY_CREATED_ACTION));
+            finish();
+        }
+    }
+
     private boolean mIsActivityRunning;
     private boolean mIsActivityResumed;
+    private boolean mReceivingActivityWasCreated;
     private final Object mActivityRunningLock = new Object();
     private final Object mActivityResumedLock = new Object();
+    private final Object mReceivingActivityCreatedLock = new Object();
     private Boolean mIntentHandled;
 
     @Override
@@ -90,6 +111,7 @@
         filter.addAction(LockTaskUtilityActivity.INTENT_ACTION);
         filter.addAction(LockTaskUtilityActivity.RESUME_ACTION);
         filter.addAction(LockTaskUtilityActivity.PAUSE_ACTION);
+        filter.addAction(RECEIVING_ACTIVITY_CREATED_ACTION);
         mContext.registerReceiver(mReceiver, filter);
     }
 
@@ -139,49 +161,46 @@
         stopAndFinish(activityManager);
     }
 
-    // This test has the UtilityActivity trigger starting another activity (settings)
+    // This launches an activity that is in the current task.
     // this should be permitted as a part of lock task (since it isn't a new task).
-    // As a result onPause should be called as it goes to a new activity.
     public void testStartActivityWithinTask() {
         mDevicePolicyManager.setLockTaskPackages(getWho(), new String[] { PACKAGE_NAME });
         startLockTask();
         waitForResume();
 
-        Intent launchIntent = new Intent(Settings.ACTION_SETTINGS);
+        mReceivingActivityWasCreated = false;
+        Intent launchIntent = new Intent(mContext, IntentReceivingActivity.class);
         Intent lockTaskUtility = getLockTaskUtility();
         lockTaskUtility.putExtra(LockTaskUtilityActivity.START_ACTIVITY, launchIntent);
         mContext.startActivity(lockTaskUtility);
 
-        synchronized (mActivityResumedLock) {
-            if (mIsActivityResumed) {
-                try {
-                    mActivityResumedLock.wait(ACTIVITY_RESUMED_TIMEOUT_MILLIS);
-                } catch (InterruptedException e) {
-                }
-                assertFalse(mIsActivityResumed);
+        synchronized (mReceivingActivityCreatedLock) {
+            try {
+                mReceivingActivityCreatedLock.wait(ACTIVITY_RESUMED_TIMEOUT_MILLIS);
+            } catch (InterruptedException e) {
             }
+            assertTrue(mReceivingActivityWasCreated);
         }
         stopAndFinish(null);
     }
 
     // This launches an activity that is not part of the current task and therefore
-    // should be blocked.  This is verified by making sure that the activity does
-    // not get a call to onPause.
+    // should be blocked.
     public void testCannotStartActivityOutsideTask() {
         mDevicePolicyManager.setLockTaskPackages(getWho(), new String[] { PACKAGE_NAME });
         startLockTask();
         waitForResume();
 
-        Intent launchIntent = new Intent(Settings.ACTION_SETTINGS);
+        mReceivingActivityWasCreated = false;
+        Intent launchIntent = new Intent(mContext, IntentReceivingActivity.class);
         launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         mContext.startActivity(launchIntent);
-
-        synchronized (mActivityResumedLock) {
+        synchronized (mReceivingActivityCreatedLock) {
             try {
-                mActivityResumedLock.wait(ACTIVITY_RESUMED_TIMEOUT_MILLIS);
+                mReceivingActivityCreatedLock.wait(ACTIVITY_RESUMED_TIMEOUT_MILLIS);
             } catch (InterruptedException e) {
             }
-            assertTrue(mIsActivityResumed);
+            assertFalse(mReceivingActivityWasCreated);
         }
         stopAndFinish(null);
     }
@@ -212,7 +231,7 @@
             finish();
             if (mIsActivityRunning) {
                 try {
-                    mActivityRunningLock.wait(ACTIVITY_RUNNING_TIMEOUT_MILLIS);
+                    mActivityRunningLock.wait(ACTIVITY_DESTROYED_TIMEOUT_MILLIS);
                 } catch (InterruptedException e) {
                 }
             }
@@ -220,7 +239,7 @@
     }
 
     /**
-     * Wait for onPause to be called on the LockTaskUtilityActivity.
+     * Wait for onResume to be called on the LockTaskUtilityActivity.
      */
     private void waitForResume() {
         // It may take a moment for the resume to come in.
diff --git a/tests/expectations/knownfailures.txt b/tests/expectations/knownfailures.txt
index 3574900..0b86f9c 100644
--- a/tests/expectations/knownfailures.txt
+++ b/tests/expectations/knownfailures.txt
@@ -166,15 +166,9 @@
     "com.android.cts.devicepolicy.ManagedProfileTest#testWipeData",
     "com.android.cts.devicepolicy.ManagedProfileTest#testCrossProfileIntentFilters",
     "com.android.cts.devicepolicy.ManagedProfileTest#testCrossProfileContent",
-    "com.android.cts.devicepolicy.ManagedProfileTest#testNoDebuggingFeaturesRestriction"
-  ]
-},
-{
-  description: "Flaky test which ocassionally fails",
-  names: [
+    "com.android.cts.devicepolicy.ManagedProfileTest#testNoDebuggingFeaturesRestriction",
     "com.android.cts.devicepolicy.DeviceOwnerTest#testLockTask"
-  ],
-  bug: 17890673
+  ]
 },
 {