Put cts-suite up to date with CompatibilityTest

Ensure the latest relevant change of CompatibilityTest
are carried to suites.

Test: unit tests
Bug: 63804674
Change-Id: Ia26b59378df214838ad0b39325eb411e73a4bdf6
diff --git a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/testtype/suite/CompatibilityTestSuite.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/testtype/suite/CompatibilityTestSuite.java
index 0bde834..62b7af9 100644
--- a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/testtype/suite/CompatibilityTestSuite.java
+++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/testtype/suite/CompatibilityTestSuite.java
@@ -96,14 +96,14 @@
     private String mTestName = null;
 
     @Option(name = MODULE_ARG_OPTION,
-            description = "the arguments to pass to a module. The expected format is "
-                    + "\"<module-name>:<arg-name>:<arg-value>\"",
+            description = "the arguments to pass to a module. The expected format is"
+                    + "\"<module-name>:<arg-name>:[<arg-key>:=]<arg-value>\"",
             importance = Importance.ALWAYS)
     private List<String> mModuleArgs = new ArrayList<>();
 
     @Option(name = TEST_ARG_OPTION,
-            description = "the arguments to pass to a test. The expected format is "
-                    + "\"<test-class>:<arg-name>:<arg-value>\"",
+            description = "the arguments to pass to a test. The expected format is"
+                    + "\"<test-class>:<arg-name>:[<arg-key>:=]<arg-value>\"",
             importance = Importance.ALWAYS)
     private List<String> mTestArgs = new ArrayList<>();
 
diff --git a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/testtype/suite/ModuleRepoSuite.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/testtype/suite/ModuleRepoSuite.java
index 10810e9..07c65af 100644
--- a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/testtype/suite/ModuleRepoSuite.java
+++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/testtype/suite/ModuleRepoSuite.java
@@ -116,12 +116,7 @@
                     if (mModuleArgs.containsKey(id)) {
                         args.putAll(mModuleArgs.get(id));
                     }
-                    for (Entry<String, List<String>> entry : args.entrySet()) {
-                        for (String value : entry.getValue()) {
-                            // Collection-type options can be injected with multiple values
-                            config.injectOptionValue(entry.getKey(), value);
-                        }
-                    }
+                    injectOptionsToConfig(args, config);
 
                     List<IRemoteTest> tests = config.getTests();
                     for (IRemoteTest test : tests) {
@@ -130,11 +125,7 @@
                         if (mTestArgs.containsKey(className)) {
                             testArgsMap.putAll(mTestArgs.get(className));
                         }
-                        for (Entry<String, List<String>> entry : testArgsMap.entrySet()) {
-                            for (String value : entry.getValue()) {
-                                config.injectOptionValue(entry.getKey(), value);
-                            }
-                        }
+                        injectOptionsToConfig(testArgsMap, config);
                         addFiltersToTest(test, abi, name);
                         if (test instanceof IAbiReceiver) {
                             ((IAbiReceiver)test).setAbi(abi);
@@ -158,6 +149,28 @@
         return toRun;
     }
 
+    /**
+     * Helper to inject options to a config.
+     */
+    @VisibleForTesting
+    void injectOptionsToConfig(Map<String, List<String>> optionMap, IConfiguration config)
+            throws ConfigurationException{
+        for (Entry<String, List<String>> entry : optionMap.entrySet()) {
+            for (String entryValue : entry.getValue()) {
+                String entryName = entry.getKey();
+                if (entryValue.contains(":=")) {
+                    // entryValue is key-value pair
+                    String key = entryValue.substring(0, entryValue.indexOf(":="));
+                    String value = entryValue.substring(entryValue.indexOf(":=") + 2);
+                    config.injectOptionValue(entryName, key, value);
+                } else {
+                    // entryValue is just the argument value
+                    config.injectOptionValue(entryName, entryValue);
+                }
+            }
+        }
+    }
+
     @VisibleForTesting
     protected boolean filterByConfigMetadata(IConfiguration config,
             MultiMap<String, String> include, MultiMap<String, String> exclude) {
@@ -325,7 +338,6 @@
          */
         @Override
         public boolean accept(File dir, String name) {
-            CLog.d("%s/%s", dir.getAbsolutePath(), name);
             return name.endsWith(CONFIG_EXT);
         }
     }
diff --git a/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/testtype/suite/ModuleRepoSuiteTest.java b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/testtype/suite/ModuleRepoSuiteTest.java
index bdcf5ea..c67afc0 100644
--- a/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/testtype/suite/ModuleRepoSuiteTest.java
+++ b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/testtype/suite/ModuleRepoSuiteTest.java
@@ -15,11 +15,17 @@
  */
 package com.android.compatibility.common.tradefed.testtype.suite;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import com.android.tradefed.config.Configuration;
 import com.android.tradefed.config.ConfigurationDescriptor;
 import com.android.tradefed.config.IConfiguration;
+import com.android.tradefed.config.Option;
+import com.android.tradefed.device.DeviceNotAvailableException;
+import com.android.tradefed.result.ITestInvocationListener;
+import com.android.tradefed.testtype.IRemoteTest;
 import com.android.tradefed.util.MultiMap;
 
 import org.junit.Before;
@@ -27,6 +33,11 @@
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  * Unit tests for {@link ModuleRepoSuite}.
  */
@@ -296,4 +307,50 @@
         assertFalse("config not excluded with matching inclusion and exclusion filters",
                 mRepo.filterByConfigMetadata(config, includeFilter, excludeFilter));
     }
+
+    public static class TestInject implements IRemoteTest {
+        @Option(name = "simple-string")
+        public String test = null;
+        @Option(name = "list-string")
+        public List<String> testList = new ArrayList<>();
+        @Option(name = "map-string")
+        public Map<String, String> testMap = new HashMap<>();
+
+        @Override
+        public void run(ITestInvocationListener listener) throws DeviceNotAvailableException {
+        }
+    }
+
+    /**
+     * Test that the different format for module-arg and test-arg can properly be passed to the
+     * configuration.
+     */
+    @Test
+    public void testInjectConfig() throws Exception {
+        IConfiguration config = new Configuration("foo", "bar");
+        TestInject checker = new TestInject();
+        config.setTest(checker);
+        Map<String, List<String>> optionMap = new HashMap<String, List<String>>();
+        List<String> option1 = new ArrayList<>();
+        option1.add("value1");
+        optionMap.put("simple-string", option1);
+
+        List<String> option2 = new ArrayList<>();
+        option2.add("value2");
+        option2.add("value3");
+        option2.add("set-option:moreoption");
+        optionMap.put("list-string", option2);
+
+        List<String> option3 = new ArrayList<>();
+        option3.add("set-option:=moreoption");
+        optionMap.put("map-string", option3);
+
+        mRepo.injectOptionsToConfig(optionMap, config);
+
+        assertEquals("value1", checker.test);
+        assertEquals(option2, checker.testList);
+        Map<String, String> resMap = new HashMap<>();
+        resMap.put("set-option", "moreoption");
+        assertEquals(resMap, checker.testMap);
+    }
 }