Add more filtering logic for CtsDeqpTestCases on retry

bug:33090757
bug:33211104
Test: run cts, disconnect device after first round CtsDeqpTestCases
Change-Id: Ic7cb9aadf9b4f6f504634eebfa7ef1e787904a75
diff --git a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/result/SubPlanCreator.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/result/SubPlanCreator.java
index 4b4c0be..6b9b5e4 100644
--- a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/result/SubPlanCreator.java
+++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/result/SubPlanCreator.java
@@ -191,10 +191,51 @@
         if (mModuleName != null) {
             subPlan.addIncludeFilter(new TestFilter(mAbiName, mModuleName, mTestName).toString());
         }
+        Set<TestStatus> statusesToRun = getStatusesToRun();
 
         for (IModuleResult module : mResult.getModules()) {
+
+            // TODO(aaronholden): remove this special case from SubPlanCreator, and filter
+            // individual tests only when the module should run. Tracked by b/33211104
+            if (MULTITEST_MODULES.contains(module.getName())) {
+                // cannot check module.isDone() since this value is not accurate for modules
+                // with multiple test configs. If we should run not-executed tests, include module
+                // and exclude tests with status not in mResultTypes.
+                TestFilter moduleFilter =
+                            new TestFilter(module.getAbi(), module.getName(), null /*test*/);
+                if (mResultTypes.contains(NOT_EXECUTED)) {
+                    subPlan.addIncludeFilter(moduleFilter.toString());
+                    for (ICaseResult caseResult : module.getResults()) {
+                        for (ITestResult testResult : caseResult.getResults()) {
+                            if (!statusesToRun.contains(testResult.getResultStatus())) {
+                                TestFilter testExclude = new TestFilter(module.getAbi(),
+                                        module.getName(), testResult.getFullName());
+                                subPlan.addExcludeFilter(testExclude.toString());
+                            }
+                        }
+                    }
+                } else {
+                    // not running not-executed tests, only include executed tests
+                    if (shouldRunModule(module)) {
+                        // at least some executed tests will be included
+                        for (ICaseResult caseResult : module.getResults()) {
+                            for (ITestResult testResult : caseResult.getResults()) {
+                                if (statusesToRun.contains(testResult.getResultStatus())) {
+                                    TestFilter testInclude = new TestFilter(module.getAbi(),
+                                            module.getName(), testResult.getFullName());
+                                    subPlan.addIncludeFilter(testInclude.toString());
+                                }
+                            }
+                        }
+                    } else {
+                        // no executed tests will be included, so exclude entire module
+                        subPlan.addExcludeFilter(moduleFilter.toString());
+                    }
+                }
+                continue;
+            }
+
             if (shouldRunModule(module)) {
-                Set<TestStatus> statusesToRun = getStatusesToRun();
                 TestFilter moduleInclude =
                             new TestFilter(module.getAbi(), module.getName(), null /*test*/);
                 if (shouldRunEntireModule(module)) {
@@ -227,21 +268,9 @@
                 }
             } else {
                 // module should not run, exclude entire module
-                // TODO(aaronholden): remove this special case from SubPlanCreator, and filter
-                // individual tests only when the module should run. Tracked by b/33211104
-                if (MULTITEST_MODULES.contains(module.getName())) {
-                    for (ICaseResult caseResult : module.getResults()) {
-                        for (ITestResult testResult : caseResult.getResults()) {
-                            TestFilter testExclude = new TestFilter(module.getAbi(),
-                                    module.getName(), testResult.getFullName());
-                            subPlan.addExcludeFilter(testExclude.toString());
-                        }
-                    }
-                } else {
-                    TestFilter moduleExclude =
-                            new TestFilter(module.getAbi(), module.getName(), null /*test*/);
-                    subPlan.addExcludeFilter(moduleExclude.toString());
-                }
+                TestFilter moduleExclude =
+                        new TestFilter(module.getAbi(), module.getName(), null /*test*/);
+                subPlan.addExcludeFilter(moduleExclude.toString());
             }
         }
         return subPlan;