Clean up the filter directory from device

In case of runs as root/unroot the directory created on
device, might not have appropriate permission to be
re-used. Deleting it completely after the run to ensure
we always start fresh.

Cherry pick note:
This was cherry picked out of order to clean up the cherry pick of
I4a6a401638d2407ec68ee60eafb8d5f34fffeca0

Bug: 123529934
Test: unit tests
./cts-tradefed run cts-dev -m CtsGesture
Bug: 79421394

Change-Id: Ifec86e790595a9f7d0bbae3e802f137dc9bcb004
Merged-In: Ifec86e790595a9f7d0bbae3e802f137dc9bcb004
diff --git a/src/com/android/tradefed/testtype/AndroidJUnitTest.java b/src/com/android/tradefed/testtype/AndroidJUnitTest.java
index db50108..e1f984a 100644
--- a/src/com/android/tradefed/testtype/AndroidJUnitTest.java
+++ b/src/com/android/tradefed/testtype/AndroidJUnitTest.java
@@ -240,16 +240,19 @@
         if (getDevice() == null) {
             throw new IllegalArgumentException("Device has not been set");
         }
+        boolean pushedFile = false;
         // if mIncludeTestFile is set, perform filtering with this file
         if (mIncludeTestFile != null) {
             mDeviceIncludeFile = mTestFilterDir.replaceAll("/$", "") + "/" + INCLUDE_FILE;
             pushTestFile(mIncludeTestFile, mDeviceIncludeFile, listener);
+            pushedFile = true;
         }
 
         // if mExcludeTestFile is set, perform filtering with this file
         if (mExcludeTestFile != null) {
             mDeviceExcludeFile = mTestFilterDir.replaceAll("/$", "") + "/" + EXCLUDE_FILE;
             pushTestFile(mExcludeTestFile, mDeviceExcludeFile, listener);
+            pushedFile = true;
         }
         if (mTotalShards > 0 && !isShardable() && mShardIndex != 0) {
             // If not shardable, only first shard can run.
@@ -257,11 +260,9 @@
             return;
         }
         super.run(listener);
-        if (mIncludeTestFile != null) {
-            removeTestFile(mDeviceIncludeFile);
-        }
-        if (mExcludeTestFile != null) {
-            removeTestFile(mDeviceExcludeFile);
+        if (pushedFile) {
+            // Remove the directory where the files where pushed
+            removeTestFilterDir();
         }
     }
 
@@ -354,6 +355,7 @@
         }
         ITestDevice device = getDevice();
         try {
+            CLog.d("Attempting to push filters to %s", destination);
             if (!device.pushFile(testFile, destination)) {
                 String message =
                         String.format(
@@ -370,9 +372,9 @@
         }
     }
 
-    private void removeTestFile(String deviceTestFile) throws DeviceNotAvailableException {
+    private void removeTestFilterDir() throws DeviceNotAvailableException {
         ITestDevice device = getDevice();
-        device.executeShellCommand(String.format("rm %s", deviceTestFile));
+        device.executeShellCommand(String.format("rm -r %s", mTestFilterDir));
     }
 
     private void reportEarlyFailure(ITestInvocationListener listener, String errorMessage) {
diff --git a/tests/src/com/android/tradefed/testtype/AndroidJUnitTestTest.java b/tests/src/com/android/tradefed/testtype/AndroidJUnitTestTest.java
index aaefa60..893145d 100644
--- a/tests/src/com/android/tradefed/testtype/AndroidJUnitTestTest.java
+++ b/tests/src/com/android/tradefed/testtype/AndroidJUnitTestTest.java
@@ -256,7 +256,7 @@
                 EasyMock.<String>anyObject())).andReturn(Boolean.TRUE).times(2);
         EasyMock.expect(mMockTestDevice.executeShellCommand(EasyMock.<String>anyObject()))
                 .andReturn("")
-                .times(4);
+                .times(3);
         EasyMock.replay(mMockRemoteRunner, mMockTestDevice);
 
         File tmpFileInclude = FileUtil.createTempFile("includeFile", ".txt");
@@ -324,7 +324,7 @@
                 .times(2);
         EasyMock.expect(mMockTestDevice.executeShellCommand(EasyMock.<String>anyObject()))
                 .andReturn("")
-                .times(4);
+                .times(3);
         EasyMock.replay(mMockRemoteRunner, mMockTestDevice);
 
         File tmpFileInclude = FileUtil.createTempFile("includeFile", ".txt");