various fixes for browser test harness

* the definitive way to determine page load finish seems to be
  onPageLoadFinished AND onProgressChanged==100, changing test
  harness logic to reflect that

* webview does not seem to be happen if ClearCache is called too
  often (or for certain pages?). It may complain that failed to
  delete blahblah, then the test harness goes crazy. So changing
  cache clear to after an entire iteration of web pages have been
  loaded

* browser will load default home page, which may be disruptive
  to the page load process. So for the test, the browser is now
  started with initial intent to visit about:blank, which does
  not trigger any page load at all

Change-Id: I1cfbd8d0196a05caea6a1c5137f45f1f59cc4c5f
diff --git a/tests/src/com/android/browser/PopularUrlsTest.java b/tests/src/com/android/browser/PopularUrlsTest.java
index 5b8cb86..b1760e9 100644
--- a/tests/src/com/android/browser/PopularUrlsTest.java
+++ b/tests/src/com/android/browser/PopularUrlsTest.java
@@ -63,6 +63,7 @@
     private Instrumentation mInst = null;
     private CountDownLatch mLatch = new CountDownLatch(1);
     private RunStatus mStatus;
+    private boolean pageLoadFinishCalled, pageProgressFull;
 
     public PopularUrlsTest() {
         super(BrowserActivity.class);
@@ -72,6 +73,8 @@
     protected void setUp() throws Exception {
         super.setUp();
 
+        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("about:blank"));
+        setActivityIntent(i);
         mActivity = getActivity();
         mInst = getInstrumentation();
         mInst.waitForIdleSync();
@@ -122,14 +125,18 @@
 
         webView.setWebChromeClient(new TestWebChromeClient(webView.getWebChromeClient()) {
 
-            /**
-             * Reset the latch whenever page progress reaches 100%.
-             */
             @Override
             public void onProgressChanged(WebView view, int newProgress) {
                 super.onProgressChanged(view, newProgress);
                 if (newProgress >= 100) {
-                    resetLatch();
+                    if (!pageProgressFull) {
+                        // void duplicate calls
+                        pageProgressFull  = true;
+                        if (pageLoadFinishCalled) {
+                            //reset latch and move forward only if both indicators are true
+                            resetLatch();
+                        }
+                    }
                 }
             }
 
@@ -206,20 +213,38 @@
                     String host, String realm) {
                 handler.proceed("user", "passwd");
             }
+
+            /* (non-Javadoc)
+             * @see com.android.browser.TestWebViewClient#onPageFinished(android.webkit.WebView, java.lang.String)
+             */
+            @Override
+            public void onPageFinished(WebView view, String url) {
+                if (!pageLoadFinishCalled) {
+                    pageLoadFinishCalled = true;
+                    if (pageProgressFull) {
+                        //reset latch and move forward only if both indicators are true
+                        resetLatch();
+                    }
+                }
+            }
+
         });
     }
 
     void resetLatch() {
-        CountDownLatch temp = mLatch;
-        mLatch = new CountDownLatch(1);
-        if (temp != null) {
-            // Notify existing latch that it's done.
-            while (temp.getCount() > 0) {
-                temp.countDown();
-            }
+        if (mLatch.getCount() != 1) {
+            Log.w(TAG, "Expecting latch to be 1, but it's not!");
+        } else {
+            mLatch.countDown();
         }
     }
 
+    void resetForNewPage() {
+        mLatch = new CountDownLatch(1);
+        pageLoadFinishCalled = false;
+        pageProgressFull = false;
+    }
+
     void waitForLoad() throws InterruptedException {
         boolean timedout = !mLatch.await(PAGE_LOAD_TIMEOUT, TimeUnit.MILLISECONDS);
         if (timedout) {
@@ -372,18 +397,19 @@
         }
 
         while (mStatus.getIteration() < loopCount) {
+            if (clearCache) {
+                webView.clearCache(true);
+            }
             while(iterator.hasNext()) {
                 page = iterator.next();
                 mStatus.setUrl(page);
                 mStatus.write();
                 Log.i(TAG, "start: " + page);
                 Uri uri = Uri.parse(page);
-                if (clearCache) {
-                    webView.clearCache(true);
-                }
                 final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 
                 long startTime = System.currentTimeMillis();
+                resetForNewPage();
                 mInst.runOnMainSync(new Runnable() {
 
                     public void run() {