Demo of Sending Arguments to a Test

Related to: https://review.source.android.com/#change,22431,patchset=1

Instead of writing an argument to a file, this
implementation sends arguments via the am command line and
then setting the arguments via a system property to
be used by a test.

Change-Id: I1424cff8d726e08dc78cbcbe7bfc8483b4c823c4
diff --git a/tests/core/runner/src/android/test/InstrumentationCtsTestRunner.java b/tests/core/runner/src/android/test/InstrumentationCtsTestRunner.java
index 5aa2c3b..dbb4f7f 100644
--- a/tests/core/runner/src/android/test/InstrumentationCtsTestRunner.java
+++ b/tests/core/runner/src/android/test/InstrumentationCtsTestRunner.java
@@ -59,7 +59,7 @@
      */
     private static final String TAG = "InstrumentationCtsTestRunner";
 
-    private static final String REPORT_VALUE_ID = "InstrumentationCtsTestRunner";
+    private static final String ARGUMENT_PHONE_NUMBER = "phoneNumber";
 
     /**
      * True if (and only if) we are running in single-test mode (as opposed to
@@ -88,6 +88,9 @@
         if (arguments != null) {
             String classArg = arguments.getString(ARGUMENT_TEST_CLASS);
             mSingleTest = classArg != null && classArg.contains("#");
+
+            System.setProperty("cts.phoneNumber",
+                    arguments.getString(ARGUMENT_PHONE_NUMBER));
         }
 
         // attempt to disable keyguard,  if current test has permission to do so
diff --git a/tests/tests/telephony/src/android/telephony/cts/SmsManagerTest.java b/tests/tests/telephony/src/android/telephony/cts/SmsManagerTest.java
index 44af455..7e9af21 100755
--- a/tests/tests/telephony/src/android/telephony/cts/SmsManagerTest.java
+++ b/tests/tests/telephony/src/android/telephony/cts/SmsManagerTest.java
@@ -110,8 +110,12 @@
         super.setUp();
         mTelephonyManager =
             (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
-        mPackageManager = mContext.getPackageManager();
+        mPackageManager = getContext().getPackageManager();
         mDestAddr = mTelephonyManager.getLine1Number();
+        if (mDestAddr == null || mDestAddr.isEmpty()) {
+           mDestAddr = System.getProperty("cts.phoneNumber", "");
+        }
+
         mText = "This is a test message";
 
         if (!mPackageManager.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
diff --git a/tools/host/src/com/android/cts/HostConfig.java b/tools/host/src/com/android/cts/HostConfig.java
index fbea3a5..2051abd 100644
--- a/tools/host/src/com/android/cts/HostConfig.java
+++ b/tools/host/src/com/android/cts/HostConfig.java
@@ -15,6 +15,11 @@
  */
 package com.android.cts;
 
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+
 import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -35,11 +40,6 @@
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-import org.xml.sax.SAXException;
-
 /**
  * Holds CTS host configuration information, such as:
  * <ul>
@@ -105,6 +105,25 @@
         }
     }
 
+    enum Strings {
+        // Phone number that will be used in tests if not available from the SIM card
+        phoneNumber ("");
+
+        private String value;
+
+        Strings(String value) {
+            this.value = value;
+        }
+
+        String value() {
+            return value;
+        }
+
+        void setValue(String value) {
+            this.value = value;
+        }
+    }
+
     private final static HostConfig sInstance = new HostConfig();
 
     private HostConfig() {
@@ -169,7 +188,8 @@
             return false;
         }
 
-        getConfigValues(doc);
+        loadIntConfigValues(doc);
+        loadStringConfigValues(doc);
 
         String caseRoot = repositoryRoot + File.separator + caseCfg;
         String planRoot = repositoryRoot + File.separator + planCfg;
@@ -360,24 +380,35 @@
         return cfgStr;
     }
 
-    /**
-     * Load configuration values from config file.
-     *
-     * @param doc The document from which to load the values.
-     */
-    private void getConfigValues(final Document doc) {
+    /** Load the integer configuration values from the config. */
+    private void loadIntConfigValues(final Document doc) {
         NodeList intValues = doc.getElementsByTagName("IntValue");
         for (int i = 0; i < intValues.getLength(); i++) {
-            Node n = intValues.item(i);
-            String name = getStringAttributeValue(n, "name");
-            String value = getStringAttributeValue(n, "value");
+            Node node = intValues.item(i);
+            String name = getStringAttributeValue(node, "name");
+            String value = getStringAttributeValue(node, "value");
             try {
-                Integer v = Integer.parseInt(value);
-                Ints.valueOf(name).setValue(v);
+                Integer intValue = Integer.parseInt(value);
+                Ints.valueOf(name).setValue(intValue);
             } catch (NumberFormatException e) {
                 Log.e("Configuration error. Illegal value for " + name, e);
             } catch (IllegalArgumentException e) {
-                Log.e("Unknown configuration value " + name, e);
+                Log.e("Unknown int configuration value " + name, e);
+            }
+        }
+    }
+
+    /** Load the String configuration values from the config. */
+    private void loadStringConfigValues(final Document doc) {
+        NodeList stringValues = doc.getElementsByTagName("StringValue");
+        for (int i = 0; i < stringValues.getLength(); i++) {
+            Node node = stringValues.item(i);
+            String name = getStringAttributeValue(node, "name");
+            String value = getStringAttributeValue(node, "value");
+            try {
+                Strings.valueOf(name).setValue(value);
+            } catch (IllegalArgumentException e) {
+                Log.e("Unknown string configuration value " + name, e);
             }
         }
     }
diff --git a/tools/host/src/com/android/cts/TestDevice.java b/tools/host/src/com/android/cts/TestDevice.java
index 672ed37..b8a0dc2 100644
--- a/tools/host/src/com/android/cts/TestDevice.java
+++ b/tools/host/src/com/android/cts/TestDevice.java
@@ -16,6 +16,7 @@
 
 package com.android.cts;
 
+import com.android.cts.HostConfig.Strings;
 import com.android.ddmlib.AdbCommandRejectedException;
 import com.android.ddmlib.Client;
 import com.android.ddmlib.ClientData;
@@ -891,7 +892,6 @@
      * @param test The test to be run.
      */
     public void runTest(Test test) throws DeviceDisconnectedException {
-
         final String appNameSpace = test.getAppNameSpace();
         String runner = test.getInstrumentationRunner();
         if (runner == null) {
@@ -903,7 +903,7 @@
         final String testName = test.getFullName().replaceAll("\\$", "\\\\\\$");
 
         final String commandStr = "am instrument -w -r -e class " + testName
-                + " " + appNameSpace + "/" + runner;
+                + getTestArguments() + " " + appNameSpace + "/" + runner;
         Log.d(commandStr);
         executeShellCommand(commandStr, new IndividualModeResultParser(test));
     }
@@ -928,7 +928,8 @@
             name = javaPkgName;
         }
 
-        String cmdHeader = "am instrument -w -r -e package " + name + " ";
+        String cmdHeader = "am instrument -w -r -e package " + name + " "
+                + getTestArguments() + " ";
         final String commandStr = cmdHeader + appNameSpace + "/" + runner;
         Log.d(commandStr);
 
@@ -936,31 +937,8 @@
         executeShellCommand(commandStr, mBatchModeResultParser);
     }
 
-    /**
-     * Run a in batch mode of a TestPackage.
-     *
-     * @param testPackage The testPackage to be run.
-     * @param javaClassName The java class name.
-     */
-    public void runTestCaseInBatchMode(TestPackage testPackage, final String javaClassName,
-            String profile) throws DeviceDisconnectedException {
-        if (javaClassName == null) {
-            return;
-        }
-
-        String appNameSpace = testPackage.getAppNameSpace();
-        String runner = testPackage.getInstrumentationRunner();
-        if (runner == null) {
-            runner = DEFAULT_TEST_RUNNER_NAME;
-        }
-
-        String cmdHeader = "am instrument -w -r -e class " + javaClassName
-                + " -e profile " + profile + " ";
-        final String commandStr = cmdHeader + appNameSpace + "/" + runner;
-        Log.d(commandStr);
-
-        mBatchModeResultParser = new BatchModeResultParser(testPackage);
-        executeShellCommand(commandStr, mBatchModeResultParser);
+    private String getTestArguments() {
+        return "-e phoneNumber " + Strings.phoneNumber.value();
     }
 
     /**
diff --git a/tools/utils/host_config.xml b/tools/utils/host_config.xml
index 7d5360c..9c43528 100644
--- a/tools/utils/host_config.xml
+++ b/tools/utils/host_config.xml
@@ -42,4 +42,7 @@
     <!-- Time to wait [ms] after a package installation or removal. -->
     <IntValue name="postInstallWaitMs" value="10000" />
 
+    <!-- Phone number that will be used in tests if not available from the SIM card. -->
+    <StringValue name="phoneNumber" value="" />
+
 </HostConfiguration>