Support setting an empty object list in Configuration.

If an invocation is started with non-zero shard index and
if there is no IStrictShardableTest, it was failing with NPE
since Configuration.setTests() does not support setting an empty list.
This change is to fix the behavior.

BUG: 28554194
Change-Id: I102900ac485fd9508776c20d1d8c371bd3dd1eb0
diff --git a/src/com/android/tradefed/config/Configuration.java b/src/com/android/tradefed/config/Configuration.java
index 5ed5209..3ee6099 100644
--- a/src/com/android/tradefed/config/Configuration.java
+++ b/src/com/android/tradefed/config/Configuration.java
@@ -648,6 +648,7 @@
             throw new IllegalArgumentException("configList cannot be null");
         }
         mConfigMap.remove(typeName);
+        mConfigMap.put(typeName, new ArrayList<Object>(1));
         for (Object configObject : configList) {
             addObject(typeName, configObject);
         }
diff --git a/tests/src/com/android/tradefed/invoker/TestInvocationTest.java b/tests/src/com/android/tradefed/invoker/TestInvocationTest.java
index af2956e..29b23fc 100644
--- a/tests/src/com/android/tradefed/invoker/TestInvocationTest.java
+++ b/tests/src/com/android/tradefed/invoker/TestInvocationTest.java
@@ -683,6 +683,65 @@
     }
 
     /**
+     * Test the
+     * {@link TestInvocation#invoke(ITestDevice, IConfiguration, IRescheduler, ITestInvocationListener[])}
+     * scenario with non-{@link IStrictShardableTest} when shard index 0 is given.
+     */
+    public void testInvoke_nonStrictShardableTest_withShardIndexZero() throws Throwable {
+        String[] commandLine = {"config", "arg"};
+        int shardCount = 10;
+        int shardIndex = 0;
+        IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
+        mStubConfiguration.setTest(test);
+        mStubConfiguration.setCommandLine(commandLine);
+        mStubConfiguration.getCommandOptions().setShardCount(shardCount);
+        mStubConfiguration.getCommandOptions().setShardIndex(shardIndex);
+
+        setupInvokeWithBuild();
+        setupMockSuccessListeners();
+        EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
+        mMockBuildInfo.addBuildAttribute("command_line_args", "config arg");
+        mMockBuildInfo.addBuildAttribute("shard_count", "10");
+        mMockBuildInfo.addBuildAttribute("shard_index", "0");
+        test.run((ITestInvocationListener)EasyMock.anyObject());
+        mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
+        replayMocks(test);
+
+        mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
+
+        verifyMocks(test);
+    }
+
+    /**
+     * Test the
+     * {@link TestInvocation#invoke(ITestDevice, IConfiguration, IRescheduler, ITestInvocationListener[])}
+     * scenario with non-{@link IStrictShardableTest} when a shard index non-0 is given.
+     */
+    public void testInvoke_nonStrictShardableTest_withShardIndexNonZero() throws Throwable {
+        String[] commandLine = {"config", "arg"};
+        int shardCount = 10;
+        int shardIndex = 1;
+        IRemoteTest test = EasyMock.createMock(IRemoteTest.class);
+        mStubConfiguration.setTest(test);
+        mStubConfiguration.setCommandLine(commandLine);
+        mStubConfiguration.getCommandOptions().setShardCount(shardCount);
+        mStubConfiguration.getCommandOptions().setShardIndex(shardIndex);
+
+        setupInvokeWithBuild();
+        setupMockSuccessListeners();
+        EasyMock.expect(mMockBuildProvider.getBuild()).andReturn(mMockBuildInfo);
+        mMockBuildInfo.addBuildAttribute("command_line_args", "config arg");
+        mMockBuildInfo.addBuildAttribute("shard_count", "10");
+        mMockBuildInfo.addBuildAttribute("shard_index", "1");
+        mMockPreparer.setUp(mMockDevice, mMockBuildInfo);
+        replayMocks(test);
+
+        mTestInvocation.invoke(mMockDevice, mStubConfiguration, mockRescheduler);
+
+        verifyMocks(test);
+    }
+
+    /**
      * Set up expected conditions for normal run up to the part where tests are run.
      *
      * @param test the {@link Test} to use.