AI 147545: CTS: Handle timeout issues in unexpected tests
  If a test is included in a test APK, but not in the test plan, it will
  still run in batch mode. However, the result notifications for unexpected
  test were not previously passed on to the TestPackage instance, which meant
  that no timeout task was started for the test.
  This change restarts the timeout timer when any test result (expected or
  not) comes in. This properly catches timeouts in unexpected tests, as well
  as timeouts that occur between two tests.
  BUG=1804876

Automated import of CL 147545
diff --git a/tools/host/src/com/android/cts/TestDevice.java b/tools/host/src/com/android/cts/TestDevice.java
index 70f671d..f4693cb 100644
--- a/tools/host/src/com/android/cts/TestDevice.java
+++ b/tools/host/src/com/android/cts/TestDevice.java
@@ -1409,8 +1409,9 @@
                             CtsTestResult.CODE_FAIL, mFailedMsg, mStackTrace));
                     break;
                 }
-                mTestPackage.notifyTestStatus(mTest, status);
             }
+            // report status even if no matching test was found
+            mTestPackage.notifyTestStatus(mTest, status);
         }
 
         /**
diff --git a/tools/host/src/com/android/cts/TestPackage.java b/tools/host/src/com/android/cts/TestPackage.java
index 719001f..a67a88c 100644
--- a/tools/host/src/com/android/cts/TestPackage.java
+++ b/tools/host/src/com/android/cts/TestPackage.java
@@ -492,7 +492,8 @@
     /**
      * Update Test running status when running in batch mode.
      *
-     * @param test The Test to update.
+     * @param test The Test to update. May be null if a status gets reported on a test that is not
+     * in the test plan.
      * @param status The status to be updated.
      */
     public void notifyTestStatus(final Test test, final String status) {
@@ -503,18 +504,20 @@
         if (mIsInBatchMode) {
             if (status.equals(START)) {
                 if ((mCurrentTest != null) && (mCurrentTest.getResult().isNotExecuted())) {
-                    Log.d("Err: not received FINISH msg for test " + mCurrentTest.getFullName());
+                    Log.d("Err: Missing FINISH msg for test " + mCurrentTest.getFullName());
                     handleMissingFinishEvent();
                 }
                 mCurrentTest = test;
-                print(mCurrentTest.getFullName() + "...");
-                mProgressObserver.start();
-                mTimeOutTimer.restart(new TimeOutTask(this), TimeOutTask.DELAY);
-            } else{
+                if (test != null) {
+                    print(mCurrentTest.getFullName() + "...");
+                    mProgressObserver.start();
+                }
+            } else {
                 mProgressObserver.stop();
-                mTimeOutTimer.cancel(false);
                 mCurrentTest = null;
             }
+            // restart the timer even for unexpected tests
+            mTimeOutTimer.restart(new TimeOutTask(this), TimeOutTask.DELAY);
         }
     }