merge reboot after / before from mr1 DO NOT MERGE

- INJECT_EVENTS permission failures happens in mr0 as well

Change-Id: Id1ca13d8af9e4b9954c8b601f01c2be211a07a02
diff --git a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/CtsTest.java b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/CtsTest.java
index 78f7412..a79b84e 100644
--- a/tools/tradefed-host/src/com/android/cts/tradefed/testtype/CtsTest.java
+++ b/tools/tradefed-host/src/com/android/cts/tradefed/testtype/CtsTest.java
@@ -51,6 +51,7 @@
 import java.lang.System;
 import java.lang.Thread;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -148,6 +149,9 @@
             "Interval between each reboot in min. Meaningful only with reboot-per-package option")
     private int mRebootIntervalMin = 30;
 
+
+    private long mPrevRebootTime; // last reboot time
+
     /** data structure for a {@link IRemoteTest} and its known tests */
     class TestPackage {
         private final IRemoteTest mTestForPackage;
@@ -350,8 +354,8 @@
                 Log.i(LOG_TAG, "Initial reboot for multiple packages");
                 rebootDevice();
             }
-            long prevTime = System.currentTimeMillis();
-            long intervalInMSec = mRebootIntervalMin * 60 * 1000;
+            mPrevRebootTime = System.currentTimeMillis();
+
             while (!mRemainingTestPkgs.isEmpty()) {
                 TestPackage knownTests = mRemainingTestPkgs.get(0);
 
@@ -367,15 +371,7 @@
                 test.run(filter);
                 mRemainingTestPkgs.remove(0);
                 if (mRemainingTestPkgs.size() > 0) {
-                    if (mRebootPerPackage) {
-                        long currentTime = System.currentTimeMillis();
-                        if ((currentTime - prevTime) > intervalInMSec) {
-                            Log.i(LOG_TAG, String.format("Rebooting after running package %s",
-                                    knownTests.getPackageDef().getName()));
-                            rebootDevice();
-                            prevTime = System.currentTimeMillis();
-                        }
-                    }
+                    rebootIfNecessary(knownTests, mRemainingTestPkgs.get(0));
                     // remove artifacts like status bar from the previous test.
                     // But this cannot dismiss dialog popped-up.
                     changeToHomeScreen();
@@ -398,6 +394,33 @@
         }
     }
 
+    private void rebootIfNecessary(TestPackage testFinished, TestPackage testToRun)
+            throws DeviceNotAvailableException {
+        // If there comes spurious failure like INJECT_EVENTS for a package,
+        // reboot it before running it.
+        // Also reboot after package which is know to leave pop-up behind
+        final List<String> rebootAfterList = Arrays.asList("CtsWebkitSecurityTestCases");
+        final List<String> rebootBeforeList = Arrays.asList("CtsAnimationTestCases",
+                "CtsGraphicsTestCases",
+                "CtsViewTestCases",
+                "CtsWebkitSecurityTestCases",
+                "CtsWidgetTestCases" );
+        long intervalInMSec = mRebootIntervalMin * 60 * 1000;
+        if (mRebootPerPackage) {
+            long currentTime = System.currentTimeMillis();
+            if (((currentTime - mPrevRebootTime) > intervalInMSec) ||
+                    rebootAfterList.contains(testFinished.getPackageDef().getName()) ||
+                    rebootBeforeList.contains(testToRun.getPackageDef().getName()) ) {
+                Log.i(LOG_TAG,
+                        String.format("Rebooting after running package %s, before package %s",
+                                testFinished.getPackageDef().getName(),
+                                testToRun.getPackageDef().getName()));
+                rebootDevice();
+                mPrevRebootTime = System.currentTimeMillis();
+            }
+        }
+    }
+
     private void rebootDevice() throws DeviceNotAvailableException {
         final int TIMEOUT_MS = 4 * 60 * 1000;
         TestDeviceOptions options = mDevice.getOptions();