Merge "Add an option for package name in CompilationFilterRule" into main
diff --git a/libraries/health/rules/src/android/platform/test/rule/CompilationFilterRule.java b/libraries/health/rules/src/android/platform/test/rule/CompilationFilterRule.java
index 1b495f1..e06eba1 100644
--- a/libraries/health/rules/src/android/platform/test/rule/CompilationFilterRule.java
+++ b/libraries/health/rules/src/android/platform/test/rule/CompilationFilterRule.java
@@ -20,12 +20,12 @@
 
 import android.os.SystemClock;
 import android.util.Log;
+
 import androidx.annotation.VisibleForTesting;
 
 import com.google.common.collect.ImmutableList;
 
 import org.junit.runner.Description;
-import org.junit.runners.model.InitializationError;
 
 import java.util.HashSet;
 import java.util.Set;
@@ -42,14 +42,16 @@
     @VisibleForTesting static final String SPEED_PROFILE_FILTER = "speed-profile";
     private static final String PROFILE_SAVE_TIMEOUT = "profile-save-timeout";
     @VisibleForTesting static final String COMPILE_FILTER_OPTION = "compilation-filter";
+
+    @VisibleForTesting
+    static final String COMPILE_PACKAGE_NAMES_OPTION = "compilation-package-names";
+
     @VisibleForTesting static final String COMPILE_SUCCESS = "Success";
     @VisibleForTesting static Set<String> mCompiledTests = new HashSet<>();
 
-    private final String[] mApplications;
+    private String[] mApplications;
 
-    public CompilationFilterRule() throws InitializationError {
-        throw new InitializationError("Must supply an application to compile.");
-    }
+    public CompilationFilterRule() {}
 
     public CompilationFilterRule(String... applications) {
         mApplications = applications;
@@ -83,6 +85,10 @@
                             "Unknown compiler filter: %s, not part of %s", filter, filterOptions));
         }
 
+        if (getArguments().getString(COMPILE_PACKAGE_NAMES_OPTION) != null) {
+            mApplications = getArguments().getString(COMPILE_PACKAGE_NAMES_OPTION).split(",");
+        }
+
         for (String app : mApplications) {
             // For speed profile compilation, ART team recommended to wait for 5 secs when app
             // is in the foreground, dump the profile, wait for another 5 secs before
diff --git a/libraries/health/rules/tests/src/android/platform/test/rule/CompilationFilterRuleTest.java b/libraries/health/rules/tests/src/android/platform/test/rule/CompilationFilterRuleTest.java
index 8d9b818..e04f1f8 100644
--- a/libraries/health/rules/tests/src/android/platform/test/rule/CompilationFilterRuleTest.java
+++ b/libraries/health/rules/tests/src/android/platform/test/rule/CompilationFilterRuleTest.java
@@ -15,9 +15,10 @@
  */
 package android.platform.test.rule;
 
-import static org.junit.Assert.fail;
 import static com.google.common.truth.Truth.assertThat;
 
+import static org.junit.Assert.fail;
+
 import android.os.Bundle;
 
 import org.junit.After;
@@ -25,7 +26,6 @@
 import org.junit.runner.Description;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
-import org.junit.runners.model.InitializationError;
 import org.junit.runners.model.Statement;
 
 import java.util.ArrayList;
@@ -41,14 +41,43 @@
         CompilationFilterRule.mCompiledTests.clear();
     }
 
-    /** Tests that this rule will fail to register if no apps are supplied. */
+    /** Tests that this rule will do nothing if no package are supplied. */
     @Test
-    public void testNoAppToCompileFails() throws InitializationError {
-        try {
-            CompilationFilterRule rule = new CompilationFilterRule();
-            fail("An initialization error should have been thrown, but wasn't.");
-        } catch (InitializationError e) {
-        }
+    public void testNoAppToCompile() throws Throwable {
+        Bundle filterBundle = new Bundle();
+        filterBundle.putString(CompilationFilterRule.COMPILE_PACKAGE_NAMES_OPTION, "");
+        TestableCompilationFilterRule rule =
+                new TestableCompilationFilterRule(filterBundle) {
+                    @Override
+                    protected String executeShellCommand(String cmd) {
+                        super.executeShellCommand(cmd);
+                        return CompilationFilterRule.COMPILE_SUCCESS;
+                    }
+                };
+        rule.apply(rule.getTestStatement(), Description.createTestDescription("clzz", "mthd1"))
+                .evaluate();
+    }
+
+    /** Tests that this rule will compile the app after the test, if supplied. */
+    @Test
+    public void testSingleAppToCompile() throws Throwable {
+        Bundle filterBundle = new Bundle();
+        filterBundle.putString(CompilationFilterRule.COMPILE_FILTER_OPTION, "speed");
+        filterBundle.putString(
+                CompilationFilterRule.COMPILE_PACKAGE_NAMES_OPTION, "example.package");
+        TestableCompilationFilterRule rule =
+                new TestableCompilationFilterRule(filterBundle) {
+                    @Override
+                    protected String executeShellCommand(String cmd) {
+                        super.executeShellCommand(cmd);
+                        return CompilationFilterRule.COMPILE_SUCCESS;
+                    }
+                };
+        rule.apply(rule.getTestStatement(), Description.createTestDescription("clzz", "mthd2"))
+                .evaluate();
+        String compileCmd =
+                String.format(CompilationFilterRule.COMPILE_CMD_FORMAT, "speed", "example.package");
+        assertThat(rule.getOperations()).containsExactly("test", compileCmd).inOrder();
     }
 
     /** Tests that this rule will fail to run and throw if the option is bad. */