am 7dd3337b: Merge change I8179742a into eclair-mr2

Merge commit '7dd3337b8e3ef27fd64df9afdbd0f658de77f3ab' into eclair-mr2-plus-aosp

* commit '7dd3337b8e3ef27fd64df9afdbd0f658de77f3ab':
  Add support for extracting render time and image in page cycler
diff --git a/tests/DumpRenderTree/assets/run_page_cycler.py b/tests/DumpRenderTree/assets/run_page_cycler.py
index 7dd4a88..8b8fb38 100755
--- a/tests/DumpRenderTree/assets/run_page_cycler.py
+++ b/tests/DumpRenderTree/assets/run_page_cycler.py
@@ -20,7 +20,7 @@
 
 def main(options, args):
   """Run the tests. Will call sys.exit when complete.
-  
+
   """
 
   # Set up logging format.
@@ -56,7 +56,15 @@
   run_load_test_cmd_postfix = " -w com.android.dumprendertree/.LayoutTestsAutoRunner"
 
   # Call LoadTestsAutoTest::runTest.
-  run_load_test_cmd = run_load_test_cmd_prefix + " -e class com.android.dumprendertree.LoadTestsAutoTest#runPageCyclerTest -e path \"" + path + "\" -e timeout " + timeout_ms + run_load_test_cmd_postfix
+  run_load_test_cmd = run_load_test_cmd_prefix + " -e class com.android.dumprendertree.LoadTestsAutoTest#runPageCyclerTest -e path \"" + path + "\" -e timeout " + timeout_ms
+
+  if options.drawtime:
+    run_load_test_cmd += " -e drawtime true "
+
+  if options.save_image:
+    run_load_test_cmd += " -e saveimage \"%s\"" % options.save_image
+
+  run_load_test_cmd += run_load_test_cmd_postfix
 
   (adb_output, adb_error) = subprocess.Popen(run_load_test_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()
   fail_flag = False
@@ -101,18 +109,26 @@
 
 if '__main__' == __name__:
   option_parser = optparse.OptionParser()
-  option_parser.add_option("", "--time-out-ms",
+  option_parser.add_option("-t", "--time-out-ms",
                            default=None,
                            help="set the timeout for each test")
-  option_parser.add_option("", "--verbose", action="store_true",
+  option_parser.add_option("-v", "--verbose", action="store_true",
                            default=False,
                            help="include debug-level logging")
-  option_parser.add_option("", "--adb-options",
+  option_parser.add_option("-a", "--adb-options",
                            default=None,
                            help="pass options to adb, such as -d -e, etc");
-  option_parser.add_option("", "--results-directory",
+  option_parser.add_option("-r", "--results-directory",
                            default="layout-test-results",
                            help="directory which results are stored.")
 
+  option_parser.add_option("-d", "--drawtime", action="store_true",
+                           default=False,
+                           help="log draw time for each page rendered.")
+
+  option_parser.add_option("-s", "--save-image",
+                           default=None,
+                           help="stores rendered page to a location on device.")
+
   options, args = option_parser.parse_args();
   main(options, args)
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/FsUtils.java b/tests/DumpRenderTree/src/com/android/dumprendertree/FsUtils.java
index fea366c..1a265e8 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/FsUtils.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/FsUtils.java
@@ -25,6 +25,7 @@
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.util.regex.Pattern;
 
@@ -156,4 +157,43 @@
         return same;
     }
 
+    public static boolean isTestPageUrl(String url) {
+        int qmPostion = url.indexOf('?');
+        int slashPostion = url.lastIndexOf('/');
+        if (slashPostion < qmPostion) {
+            String fileName = url.substring(slashPostion + 1, qmPostion);
+            if ("index.html".equals(fileName)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public static String getLastSegmentInPath(String path) {
+        int endPos = path.lastIndexOf('/');
+        path = path.substring(0, endPos);
+        endPos = path.lastIndexOf('/');
+        return path.substring(endPos + 1);
+    }
+
+    public static void writeDrawTime(String fileName, String url, long[] times) {
+        StringBuffer lineBuffer = new StringBuffer();
+        // grab the last segment of path in url
+        lineBuffer.append(getLastSegmentInPath(url));
+        for (long time : times) {
+            lineBuffer.append('\t');
+            lineBuffer.append(time);
+        }
+        lineBuffer.append('\n');
+        String line = lineBuffer.toString();
+        Log.v(LOGTAG, "logging draw times: " + line);
+        try {
+            FileWriter fw = new FileWriter(fileName, true);
+            fw.write(line);
+            fw.close();
+        } catch (IOException ioe) {
+            Log.e(LOGTAG, "Failed to log draw times", ioe);
+        }
+    }
+
 }
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoRunner.java b/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoRunner.java
index 283b98c..539d551 100755
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoRunner.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/LayoutTestsAutoRunner.java
@@ -73,13 +73,20 @@
         this.mLogtime = (logtime != null
                 && logtime.toLowerCase().equals("true"));
 
+        String drawTime = (String) icicle.get("drawtime");
+        this.mGetDrawTime = (drawTime != null
+                && drawTime.toLowerCase().equals("true"));
+
+        mSaveImagePath = (String) icicle.get("saveimage");
+
         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;
 }
-
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/LoadTestsAutoTest.java b/tests/DumpRenderTree/src/com/android/dumprendertree/LoadTestsAutoTest.java
index ba46197..2ef342f 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/LoadTestsAutoTest.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/LoadTestsAutoTest.java
@@ -70,7 +70,8 @@
         freeMem();
 
         // Run tests
-        runTestAndWaitUntilDone(activity, runner.mTestPath, runner.mTimeoutInMillis);
+        runTestAndWaitUntilDone(activity, runner.mTestPath, runner.mTimeoutInMillis,
+                runner.mGetDrawTime, runner.mSaveImagePath);
 
         activity.clearCache();
         try {
@@ -161,7 +162,8 @@
     }
 
     // A convenient method to be called by another activity.
-    private void runTestAndWaitUntilDone(TestShellActivity activity, String url, int timeout) {
+    private void runTestAndWaitUntilDone(TestShellActivity activity, String url, int timeout,
+            boolean getDrawTime, String saveImagePath) {
         activity.setCallback(new TestShellCallback() {
             public void finished() {
                 synchronized (LoadTestsAutoTest.this) {
@@ -181,6 +183,9 @@
         intent.putExtra(TestShellActivity.TEST_URL, url);
         intent.putExtra(TestShellActivity.TIMEOUT_IN_MILLIS, timeout);
         intent.putExtra(TestShellActivity.RESULT_FILE, LOAD_TEST_RESULT);
+        intent.putExtra(TestShellActivity.GET_DRAW_TIME, getDrawTime);
+        if (saveImagePath != null)
+            intent.putExtra(TestShellActivity.SAVE_IMAGE, saveImagePath);
         activity.startActivity(intent);
 
         // Wait until done.
diff --git a/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java b/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java
index aeb55b4..b6b1661 100644
--- a/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java
+++ b/tests/DumpRenderTree/src/com/android/dumprendertree/TestShellActivity.java
@@ -25,6 +25,9 @@
 import android.content.Intent;
 import android.content.DialogInterface.OnClickListener;
 import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Bitmap.CompressFormat;
+import android.graphics.Bitmap.Config;
 import android.net.http.SslError;
 import android.os.Bundle;
 import android.os.Handler;
@@ -163,6 +166,8 @@
 
         mResultFile = intent.getStringExtra(RESULT_FILE);
         mTimeoutInMillis = intent.getIntExtra(TIMEOUT_IN_MILLIS, 0);
+        mGetDrawtime = intent.getBooleanExtra(GET_DRAW_TIME, false);
+        mSaveImagePath = intent.getStringExtra(SAVE_IMAGE);
 
         Log.v(LOGTAG, "  Loading " + mTestUrl);
         mWebView.loadUrl(mTestUrl);
@@ -459,6 +464,18 @@
         public void onPageFinished(WebView view, String url) {
             Log.v(LOGTAG, "onPageFinished, url=" + url);
             mPageFinished = true;
+            // get page draw time
+            if (FsUtils.isTestPageUrl(url)) {
+                if (mGetDrawtime) {
+                    long[] times = new long[DRAW_RUNS];
+                    times = getDrawWebViewTime(mWebView, DRAW_RUNS);
+                    FsUtils.writeDrawTime(DRAW_TIME_LOG, url, times);
+                }
+                if (mSaveImagePath != null) {
+                    String name = FsUtils.getLastSegmentInPath(url);
+                    drawPageToFile(mSaveImagePath + "/" + name + ".png", mWebView);
+                }
+            }
             // Calling finished() will check if we've met all the conditions for completing
             // this test and move to the next one if we are ready.
             if (finished()) {
@@ -691,6 +708,41 @@
         mPageFinished = false;
         mOneHundredPercentComplete = false;
         mDumpWebKitData = false;
+        mGetDrawtime = false;
+        mSaveImagePath = null;
+    }
+
+    private long[] getDrawWebViewTime(WebView view, int count) {
+        if (count == 0)
+            return null;
+        long[] ret = new long[count];
+        long start;
+        Canvas canvas = new Canvas();
+        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
+        canvas.setBitmap(bitmap);
+        for (int i = 0; i < count; i++) {
+            start = System.currentTimeMillis();
+            view.draw(canvas);
+            ret[i] = System.currentTimeMillis() - start;
+        }
+        return ret;
+    }
+
+    private void drawPageToFile(String fileName, WebView view) {
+        Canvas canvas = new Canvas();
+        Bitmap bitmap = Bitmap.createBitmap(view.getContentWidth(), view.getContentHeight(),
+                Config.ARGB_8888);
+        canvas.setBitmap(bitmap);
+        view.drawPage(canvas);
+        try {
+            FileOutputStream fos = new FileOutputStream(fileName);
+            if(!bitmap.compress(CompressFormat.PNG, 90, fos)) {
+                Log.w(LOGTAG, "Failed to compress and save image.");
+            }
+        } catch (IOException ioe) {
+            Log.e(LOGTAG, "", ioe);
+        }
+        bitmap.recycle();
     }
 
     private boolean canMoveToNextTest() {
@@ -730,7 +782,9 @@
     private String mResultFile;
     private int mTimeoutInMillis;
     private String mUiAutoTestPath;
+    private String mSaveImagePath;
     private BufferedReader mTestListReader;
+    private boolean mGetDrawtime;
 
     // States
     private boolean mTimedOut;
@@ -766,6 +820,11 @@
     static final String RESULT_FILE = "ResultFile";
     static final String TIMEOUT_IN_MILLIS = "TimeoutInMillis";
     static final String UI_AUTO_TEST = "UiAutoTest";
+    static final String GET_DRAW_TIME = "GetDrawTime";
+    static final String SAVE_IMAGE = "SaveImage";
+
+    static final int DRAW_RUNS = 5;
+    static final String DRAW_TIME_LOG = "/sdcard/android/page_draw_time.txt";
 
     private boolean mGeolocationPermissionSet;
     private boolean mGeolocationPermission;