Adds to DumpRenderTree the ability to look for Android-specific results

These Android-specific results will be added to
external/webkit/LayoutTests/platform/android-<js-engine> and are used in
preference to the generic expected results.

The JavaScript engine to use is read from the JS_ENGINE environment variable
used by the build system or can be overridden on the command line. If neither
is set, it defaults to JSC.

Change-Id: Ia8d107ced3968a5c061fd6f0f57451419bab6b27
diff --git a/tests/DumpRenderTree/assets/run_layout_tests.py b/tests/DumpRenderTree/assets/run_layout_tests.py
index c3627bb..7f3ef2d 100755
--- a/tests/DumpRenderTree/assets/run_layout_tests.py
+++ b/tests/DumpRenderTree/assets/run_layout_tests.py
@@ -26,6 +26,7 @@
     --time-out-ms (default is 8000 millis) for each test
     --adb-options="-e" passes option string to adb
     --results-directory=..., (default is ./layout-test-results) directory name under which results are stored.
+    --js-engine the JavaScript engine currently in use, determines which set of Android-specific expected results we should use, should be 'jsc' or 'v8'
 """
 
 import logging
@@ -186,6 +187,16 @@
   run_layout_test_cmd_postfix = " -e path \"" + path + "\" -e timeout " + timeout_ms
   if options.rebaseline:
     run_layout_test_cmd_postfix += " -e rebaseline true"
+
+  # If the JS engine is not specified on the command line, try reading the
+  # JS_ENGINE environment  variable, which is used by the build system in
+  # external/webkit/Android.mk.
+  js_engine = options.js_engine
+  if not js_engine:
+    js_engine = os.environ['JS_ENGINE']
+  if js_engine:
+    run_layout_test_cmd_postfix += " -e jsengine " + js_engine
+
   run_layout_test_cmd_postfix += " -w com.android.dumprendertree/.LayoutTestsAutoRunner"
 
   # Call LayoutTestsAutoTest::startLayoutTests.
@@ -297,6 +308,9 @@
                            default=None,
                            dest="ref_directory",
                            help="directory where reference results are stored.")
+  option_parser.add_option("", "--js-engine",
+                           default=None,
+                           help="The JavaScript engine currently in use, which determines which set of Android-specific expected results we should use. Should be 'jsc' or 'v8'.");
 
   options, args = option_parser.parse_args();
   main(options, args)
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoRunner.java b/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoRunner.java
index 539d551..e058f32 100755
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoRunner.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoRunner.java
@@ -79,14 +79,17 @@
 
         mSaveImagePath = (String) icicle.get("saveimage");
 
+        mJsEngine = (String) icicle.get("jsengine");
+
         super.onCreate(icicle);
     }
     
-    public String mTestPath = null;
-    public String mSaveImagePath = null;
-    public int mTimeoutInMillis = 0;
-    public int mDelay = 0;
-    public boolean mRebaseline = false;
-    public boolean mLogtime = false;
-    public boolean mGetDrawTime = false;
+    public String mTestPath;
+    public String mSaveImagePath;
+    public int mTimeoutInMillis;
+    public int mDelay;
+    public boolean mRebaseline;
+    public boolean mLogtime;
+    public boolean mGetDrawTime;
+    public String mJsEngine;
 }
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoTest.java b/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoTest.java
index 634d683..d9ec3fa 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoTest.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoTest.java
@@ -147,6 +147,9 @@
     private MyTestRecorder mResultRecorder;
     private Vector<String> mTestList;
     private boolean mRebaselineResults;
+    // The JavaScript engine currently in use. This determines which set of Android-specific
+    // expected test results we use.
+    private String mJsEngine;
     private String mTestPathPrefix;
     private boolean mFinished;
 
@@ -214,14 +217,24 @@
         return shortName.replaceFirst(LAYOUT_TESTS_ROOT, LAYOUT_TESTS_RESULT_DIR) + "-result.txt";
     }
 
+    // Gets the file which contains WebKit's expected results for this test.
     private String getExpectedResultFile(String test) {
+        // The generic result is at <path>/<name>-expected.txt
+        // First try the Android-specific result at
+        // platform/android-<js-engine>/<path>/<name>-expected.txt
         int pos = test.lastIndexOf('.');
-        if(pos == -1)
+        if (pos == -1)
             return null;
-        String shortName = test.substring(0, pos);
-        return shortName + "-expected.txt";
+        String genericExpectedResult = test.substring(0, pos) + "-expected.txt";
+        String androidExpectedResultsDir = "platform/android-" + mJsEngine + "/";
+        String androidExpectedResult =
+            genericExpectedResult.replaceFirst(LAYOUT_TESTS_ROOT, LAYOUT_TESTS_ROOT + androidExpectedResultsDir);
+        File f = new File(androidExpectedResult);
+        return f.exists() ? androidExpectedResult : genericExpectedResult;
     }
 
+    // Gets the file which contains the actual results of running the test on
+    // Android, generated by a previous run which set a new baseline.
     private String getAndroidExpectedResultFile(String expectedResultFile) {
         return expectedResultFile.replaceFirst(LAYOUT_TESTS_ROOT, ANDROID_EXPECTED_RESULT_DIR);
     }
@@ -282,8 +295,8 @@
         });
 
         String resultFile = getResultFile(test);
-        if(resultFile == null) {
-            //simply ignore this test
+        if (resultFile == null) {
+            // Simply ignore this test.
             return;
         }
         if (mRebaselineResults) {
@@ -339,8 +352,10 @@
         this.mTestList = new Vector<String>();
 
         // Read settings
-        this.mTestPathPrefix = (new File(LAYOUT_TESTS_ROOT + runner.mTestPath)).getAbsolutePath();
-        this.mRebaselineResults = runner.mRebaseline;
+        mTestPathPrefix = (new File(LAYOUT_TESTS_ROOT + runner.mTestPath)).getAbsolutePath();
+        mRebaselineResults = runner.mRebaseline;
+        // JSC is the default JavaScript engine.
+        mJsEngine = runner.mJsEngine == null ? "jsc" : runner.mJsEngine;
 
         int timeout = runner.mTimeoutInMillis;
         if (timeout <= 0) {