Snap for 4832339 from 93acd18a75fefd41a0b61cf5f686725d87856689 to pi-release

Change-Id: Id814452eb4cb78a3f3e66829c32a667426ddf0e5
diff --git a/src/com/android/tradefed/device/DeviceSelectionOptions.java b/src/com/android/tradefed/device/DeviceSelectionOptions.java
index 06f953e..5c28520 100644
--- a/src/com/android/tradefed/device/DeviceSelectionOptions.java
+++ b/src/com/android/tradefed/device/DeviceSelectionOptions.java
@@ -164,15 +164,13 @@
         mPropertyMap.put(propertyKey, propValue);
     }
 
-    /**
-     * {@inheritDoc}
-     */
+    /** {@inheritDoc} */
     @Override
-    public Collection<String> getSerials() {
+    public Collection<String> getSerials(IDevice device) {
         // If no serial was explicitly set, use the environment variable ANDROID_SERIAL.
         if (mSerials.isEmpty() && !mFetchedEnvVariable) {
             String env_serial = fetchEnvironmentVariable("ANDROID_SERIAL");
-            if (env_serial != null) {
+            if (env_serial != null && !(device instanceof StubDevice)) {
                 mSerials.add(env_serial);
             }
             mFetchedEnvVariable = true;
@@ -360,7 +358,7 @@
      */
     @Override
     public boolean matches(IDevice device) {
-        Collection<String> serials = getSerials();
+        Collection<String> serials = getSerials(device);
         Collection<String> excludeSerials = getExcludeSerials();
         Map<String, Collection<String>> productVariants = splitOnVariant(getProductTypes());
         Collection<String> productTypes = productVariants.keySet();
diff --git a/src/com/android/tradefed/device/IDeviceSelection.java b/src/com/android/tradefed/device/IDeviceSelection.java
index 2d05362..a031d3b 100644
--- a/src/com/android/tradefed/device/IDeviceSelection.java
+++ b/src/com/android/tradefed/device/IDeviceSelection.java
@@ -30,9 +30,10 @@
     /**
      * Gets a copy of the serial numbers
      *
+     * @param device The {@link IDevice} representing the device considered for selection.
      * @return a {@link Collection} of serial numbers
      */
-    public Collection<String> getSerials();
+    public Collection<String> getSerials(IDevice device);
 
     /**
      * Gets a copy of the serial numbers exclusion list
diff --git a/tests/src/com/android/tradefed/config/ConfigurationFactoryTest.java b/tests/src/com/android/tradefed/config/ConfigurationFactoryTest.java
index 78e3bc7..2ff9bfd 100644
--- a/tests/src/com/android/tradefed/config/ConfigurationFactoryTest.java
+++ b/tests/src/com/android/tradefed/config/ConfigurationFactoryTest.java
@@ -1235,9 +1235,12 @@
                 .getTargetPreparers().size());
         List<String> serials = new ArrayList<String>();
         serials.add("test");
-        assertEquals(serials, config.getDeviceRequirements().getSerials());
-        assertEquals(serials, config.getDeviceConfigByName(ConfigurationDef.DEFAULT_DEVICE_NAME)
-                .getDeviceRequirements().getSerials());
+        assertEquals(serials, config.getDeviceRequirements().getSerials(null));
+        assertEquals(
+                serials,
+                config.getDeviceConfigByName(ConfigurationDef.DEFAULT_DEVICE_NAME)
+                        .getDeviceRequirements()
+                        .getSerials(null));
     }
 
     /**
diff --git a/tests/src/com/android/tradefed/device/DeviceSelectionOptionsTest.java b/tests/src/com/android/tradefed/device/DeviceSelectionOptionsTest.java
index 96bca72..0efaee5 100644
--- a/tests/src/com/android/tradefed/device/DeviceSelectionOptionsTest.java
+++ b/tests/src/com/android/tradefed/device/DeviceSelectionOptionsTest.java
@@ -15,6 +15,10 @@
  */
 package com.android.tradefed.device;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
 import com.android.ddmlib.IDevice;
 import com.android.tradefed.config.ArgsOptionParser;
 import com.android.tradefed.config.OptionSetter;
@@ -22,14 +26,15 @@
 
 import com.google.common.util.concurrent.SettableFuture;
 
-import junit.framework.TestCase;
-
 import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
 
-/**
- * Unit tests for {@link DeviceSelectionOptions}
- */
-public class DeviceSelectionOptionsTest extends TestCase {
+/** Unit tests for {@link DeviceSelectionOptions}. */
+@RunWith(JUnit4.class)
+public class DeviceSelectionOptionsTest {
 
     // DEVICE_SERIAL and DEVICE_ENV_SERIAL need to be different.
     private static final String DEVICE_SERIAL = "12345";
@@ -60,11 +65,8 @@
                     + "  temperature: %s\n"
                     + "  technology: Li-ion\n";
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    protected void setUp() throws Exception {
+    @Before
+    public void setUp() throws Exception {
         mMockDevice = EasyMock.createMock(IDevice.class);
         EasyMock.expect(mMockDevice.getSerialNumber()).andStubReturn(DEVICE_SERIAL);
         EasyMock.expect(mMockDevice.isEmulator()).andStubReturn(Boolean.FALSE);
@@ -73,45 +75,58 @@
         EasyMock.expect(mMockEmulatorDevice.isEmulator()).andStubReturn(Boolean.TRUE);
     }
 
-    /**
-     * Test for {@link DeviceSelectionOptions#getSerials()}
-     */
+    /** Test for {@link DeviceSelectionOptions#getSerials(IDevice)} */
+    @Test
     public void testGetSerials() {
         DeviceSelectionOptions options = getDeviceSelectionOptionsWithEnvVar(DEVICE_ENV_SERIAL);
         // If no serial is available, the environment variable will be used instead.
-        assertEquals(1, options.getSerials().size());
-        assertTrue(options.getSerials().contains(DEVICE_ENV_SERIAL));
-        assertFalse(options.getSerials().contains(DEVICE_SERIAL));
+        assertEquals(1, options.getSerials(mMockDevice).size());
+        assertTrue(options.getSerials(mMockDevice).contains(DEVICE_ENV_SERIAL));
+        assertFalse(options.getSerials(mMockDevice).contains(DEVICE_SERIAL));
+    }
+
+    /** Test matching a stub device when ANDROID_SERIAL is set. */
+    @Test
+    public void testGetSerials_envVariable_nullDevice() {
+        DeviceSelectionOptions options = getDeviceSelectionOptionsWithEnvVar(DEVICE_ENV_SERIAL);
+        options.setNullDeviceRequested(true);
+        // If no serial is available, the environment variable will be used instead.
+        IDevice device = new NullDevice("serial");
+        assertEquals(0, options.getSerials(device).size());
+        assertTrue(options.matches(device));
     }
 
     /**
-     * Test that {@link DeviceSelectionOptions#getSerials()} does not override the values.
+     * Test that {@link DeviceSelectionOptions#getSerials(IDevice)} does not override the values.
      */
+    @Test
     public void testGetSerialsDoesNotOverride() {
         DeviceSelectionOptions options = getDeviceSelectionOptionsWithEnvVar(DEVICE_ENV_SERIAL);
         options.addSerial(DEVICE_SERIAL);
 
         // Check that now we do not override the serial with the environment variable.
-        assertEquals(1, options.getSerials().size());
-        assertFalse(options.getSerials().contains(DEVICE_ENV_SERIAL));
-        assertTrue(options.getSerials().contains(DEVICE_SERIAL));
+        assertEquals(1, options.getSerials(mMockDevice).size());
+        assertFalse(options.getSerials(mMockDevice).contains(DEVICE_ENV_SERIAL));
+        assertTrue(options.getSerials(mMockDevice).contains(DEVICE_SERIAL));
     }
 
     /**
-     * Test for {@link DeviceSelectionOptions#getSerials()} without the environment variable set.
+     * Test for {@link DeviceSelectionOptions#getSerials(IDevice)} without the environment variable
+     * set.
      */
+    @Test
     public void testGetSerialsWithNoEnvValue() {
         DeviceSelectionOptions options = getDeviceSelectionOptionsWithEnvVar(null);
         // An empty list will cause it to fetch the
-        assertTrue(options.getSerials().isEmpty());
+        assertTrue(options.getSerials(mMockDevice).isEmpty());
         // If no serial is available and the environment variable is not set, nothing happens.
-        assertEquals(0, options.getSerials().size());
+        assertEquals(0, options.getSerials(mMockDevice).size());
 
         options.addSerial(DEVICE_SERIAL);
         // Check that now we do not override the serial.
-        assertEquals(1, options.getSerials().size());
-        assertFalse(options.getSerials().contains(DEVICE_ENV_SERIAL));
-        assertTrue(options.getSerials().contains(DEVICE_SERIAL));
+        assertEquals(1, options.getSerials(mMockDevice).size());
+        assertFalse(options.getSerials(mMockDevice).contains(DEVICE_ENV_SERIAL));
+        assertTrue(options.getSerials(mMockDevice).contains(DEVICE_SERIAL));
     }
 
     /**
@@ -131,6 +146,7 @@
         };
     }
 
+    @Test
     public void testGetProductVariant_legacy() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
 
@@ -142,6 +158,7 @@
         assertEquals("legacy", options.getDeviceProductVariant(mMockDevice));
     }
 
+    @Test
     public void testGetProductVariant_vendor() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
 
@@ -153,6 +170,7 @@
         assertEquals("variant", options.getDeviceProductVariant(mMockDevice));
     }
 
+    @Test
     public void testGetProductType_mismatch() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.addProductType(OTHER_DEVICE_TYPE);
@@ -164,6 +182,7 @@
         assertFalse(options.matches(mMockDevice));
     }
 
+    @Test
     public void testGetProductType_match() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.addProductType(DEVICE_TYPE);
@@ -176,9 +195,10 @@
     }
 
     /**
-     * Test scenario where device does not return a valid product type. For now, this will result
-     * in device not being matched.
+     * Test scenario where device does not return a valid product type. For now, this will result in
+     * device not being matched.
      */
+    @Test
     public void testGetProductType_missingProduct() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.addProductType(DEVICE_TYPE);
@@ -190,9 +210,8 @@
         assertTrue(options.matches(mMockDevice));
     }
 
-    /**
-     * Test matching by property
-     */
+    /** Test matching by property */
+    @Test
     public void testMatches_property() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.addProperty("prop1", "propvalue");
@@ -203,9 +222,8 @@
         assertTrue(options.matches(mMockDevice));
     }
 
-    /**
-     * Test negative case for matching by property
-     */
+    /** Test negative case for matching by property */
+    @Test
     public void testMatches_propertyNotMatch() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.addProperty("prop1", "propvalue");
@@ -216,9 +234,8 @@
         EasyMock.verify(mMockDevice);
     }
 
-    /**
-     * Test for matching by multiple properties
-     */
+    /** Test for matching by multiple properties */
+    @Test
     public void testMatches_multipleProperty() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.addProperty("prop1", "propvalue");
@@ -231,9 +248,8 @@
         EasyMock.verify(mMockDevice);
     }
 
-    /**
-     * Test for matching by multiple properties, when one property does not match
-     */
+    /** Test for matching by multiple properties, when one property does not match */
+    @Test
     public void testMatches_notMultipleProperty() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.addProperty("prop1", "propvalue");
@@ -247,9 +263,8 @@
         // EasyMock.verify(mMockDevice);
     }
 
-    /**
-     * Test for matching with an srtub emulator
-     */
+    /** Test for matching with an srtub emulator */
+    @Test
     public void testMatches_stubEmulator() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setStubEmulatorRequested(true);
@@ -257,18 +272,16 @@
         assertTrue(options.matches(emulatorDevice));
     }
 
-    /**
-     * Test that an stub emulator is not matched by default
-     */
+    /** Test that an stub emulator is not matched by default */
+    @Test
     public void testMatches_stubEmulatorNotDefault() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         IDevice emulatorDevice = new StubDevice("emulator", true);
         assertFalse(options.matches(emulatorDevice));
     }
 
-    /**
-     * Test for matching with null device requested flag
-     */
+    /** Test for matching with null device requested flag */
+    @Test
     public void testMatches_nullDevice() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setNullDeviceRequested(true);
@@ -277,9 +290,8 @@
     }
 
 
-    /**
-     * Test for matching with tcp device requested flag
-     */
+    /** Test for matching with tcp device requested flag */
+    @Test
     public void testMatches_tcpDevice() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setTcpDeviceRequested(true);
@@ -287,9 +299,8 @@
         assertTrue(options.matches(stubDevice));
     }
 
-    /**
-     * Test that a real device is not matched if the 'null device requested' flag is set
-     */
+    /** Test that a real device is not matched if the 'null device requested' flag is set */
+    @Test
     public void testMatches_notNullDevice() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setNullDeviceRequested(true);
@@ -297,9 +308,8 @@
         assertFalse(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that a real device is matched when requested
-     */
+    /** Test that a real device is matched when requested */
+    @Test
     public void testMatches_device() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setDeviceRequested(true);
@@ -308,9 +318,8 @@
         assertFalse(options.matches(mMockEmulatorDevice));
     }
 
-    /**
-     * Test that a emulator is matched when requested
-     */
+    /** Test that a emulator is matched when requested */
+    @Test
     public void testMatches_emulator() {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setEmulatorRequested(true);
@@ -319,9 +328,8 @@
         assertTrue(options.matches(mMockEmulatorDevice));
     }
 
-    /**
-     * Test that battery checking works
-     */
+    /** Test that battery checking works */
+    @Test
     public void testMatches_minBatteryPass() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setMinBatteryLevel(25);
@@ -330,9 +338,8 @@
         assertTrue(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that battery checking works
-     */
+    /** Test that battery checking works */
+    @Test
     public void testMatches_minBatteryFail() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setMinBatteryLevel(75);
@@ -341,9 +348,8 @@
         assertFalse(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that battery checking works
-     */
+    /** Test that battery checking works */
+    @Test
     public void testMatches_maxBatteryPass() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setMaxBatteryLevel(75);
@@ -352,9 +358,8 @@
         assertTrue(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that battery checking works
-     */
+    /** Test that battery checking works */
+    @Test
     public void testMatches_maxBatteryFail() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setMaxBatteryLevel(25);
@@ -363,9 +368,8 @@
         assertFalse(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that battery checking works
-     */
+    /** Test that battery checking works */
+    @Test
     public void testMatches_forceBatteryCheckTrue() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setRequireBatteryCheck(true);
@@ -380,6 +384,7 @@
      * Test that when battery checking is disabled, if a min-battery is requested, no battery check
      * occurs.
      */
+    @Test
     public void testMatches_forceBatteryCheckFalse() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         options.setRequireBatteryCheck(false);
@@ -392,6 +397,7 @@
     }
 
     /** Test that battery temperature checking works */
+    @Test
     public void testMatches_maxBatteryTempPass() throws Exception {
         // 50 < 100, test should pass
         DeviceSelectionOptions options = mockBatteryTemperatureCheck(50, 100, true);
@@ -400,6 +406,7 @@
     }
 
     /** Test that battery temperature checking works */
+    @Test
     public void testMatches_maxBatteryTempFail() throws Exception {
         // 150 > 100, test should fail
         DeviceSelectionOptions options = mockBatteryTemperatureCheck(150, 100, true);
@@ -408,6 +415,7 @@
     }
 
     /** Test that battery temperature checking works */
+    @Test
     public void testMatches_forceBatteryTempCheckTrue() throws Exception {
         // temperature unavailable, should fail
         DeviceSelectionOptions options = mockBatteryTemperatureCheck(0, 100, true);
@@ -416,6 +424,7 @@
     }
 
     /** Test that battery temperature checking works */
+    @Test
     public void testMatches_forceBatteryTempCheckFalse() throws Exception {
         // temperature unavailable, should pass
         DeviceSelectionOptions options = mockBatteryTemperatureCheck(0, 100, false);
@@ -423,9 +432,8 @@
         assertTrue(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that min sdk checking works for negative case
-     */
+    /** Test that min sdk checking works for negative case */
+    @Test
     public void testMatches_minSdkFail() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         ArgsOptionParser p = new ArgsOptionParser(options);
@@ -437,9 +445,8 @@
         assertFalse(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that min sdk checking works for positive case
-     */
+    /** Test that min sdk checking works for positive case */
+    @Test
     public void testMatches_minSdkPass() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         ArgsOptionParser p = new ArgsOptionParser(options);
@@ -451,9 +458,8 @@
         assertTrue(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that device is not matched if device api cannot be determined
-     */
+    /** Test that device is not matched if device api cannot be determined */
+    @Test
     public void testMatches_minSdkNull() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         ArgsOptionParser p = new ArgsOptionParser(options);
@@ -465,9 +471,8 @@
         assertFalse(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that max sdk checking works for negative case
-     */
+    /** Test that max sdk checking works for negative case */
+    @Test
     public void testMatches_maxSdkFail() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         ArgsOptionParser p = new ArgsOptionParser(options);
@@ -479,9 +484,8 @@
         assertFalse(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that max sdk checking works for positive case
-     */
+    /** Test that max sdk checking works for positive case */
+    @Test
     public void testMatches_maxSdkPass() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         ArgsOptionParser p = new ArgsOptionParser(options);
@@ -493,9 +497,8 @@
         assertTrue(options.matches(mMockDevice));
     }
 
-    /**
-     * Test that device is not matched if device api cannot be determined
-     */
+    /** Test that device is not matched if device api cannot be determined */
+    @Test
     public void testMatches_maxSdkNull() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         ArgsOptionParser p = new ArgsOptionParser(options);
@@ -511,6 +514,7 @@
      * Test that min-battery is not used to check non physical devices otherwise they will never
      * match.
      */
+    @Test
     public void testStubDevice_minBattery() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         OptionSetter setter = new OptionSetter(options);
@@ -523,6 +527,7 @@
      * A FastbootDevice does not expose a battery level so if a battery is specified we cannot match
      * it.
      */
+    @Test
     public void testFastbootDevice_minBattery() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         OptionSetter setter = new OptionSetter(options);
@@ -533,6 +538,7 @@
     /**
      * Ensure that a fastboot device without any special condition can be matched for allocation.
      */
+    @Test
     public void testFastbootDevice() throws Exception {
         DeviceSelectionOptions options = new DeviceSelectionOptions();
         assertTrue(options.matches(new FastbootDevice("serial")));