set up fast-forward from froyo-release to gingerbread

Change-Id: I3db260aa3aa968bdcacab2add7756d666d4a8a25
diff --git a/Android.mk b/Android.mk
index 6e20ab8..e015dda 100644
--- a/Android.mk
+++ b/Android.mk
@@ -12,6 +12,8 @@
 
 LOCAL_PACKAGE_NAME := Browser
 
+LOCAL_EMMA_COVERAGE_FILTER := *,-com.android.common.*
+
 include $(BUILD_PACKAGE)
 
 # additionally, build tests in sub-folders in a separate .apk
diff --git a/res/values-ko/strings.xml b/res/values-ko/strings.xml
index c91c317..bc15b43 100644
--- a/res/values-ko/strings.xml
+++ b/res/values-ko/strings.xml
@@ -117,7 +117,7 @@
     <string name="contextmenu_add_contact" msgid="3183511922223645716">"연락처 추가"</string>
     <string name="contextmenu_send_mail" msgid="1014513374828775660">"이메일 보내기"</string>
     <string name="contextmenu_map" msgid="7471390435434034912">"지도"</string>
-    <string name="choosertitle_sharevia" msgid="4600490613341909086">"공유에 사용할 응용프로그램:"</string>
+    <string name="choosertitle_sharevia" msgid="4600490613341909086">"공유에 사용할 애플리케이션:"</string>
     <string name="clear" msgid="7070043081700011461">"지우기"</string>
     <string name="replace" msgid="4843033491070384047">"교체"</string>
     <string name="browser_bookmarks_page_bookmarks_text" msgid="6787605028726162673">"북마크"</string>
diff --git a/src/com/android/browser/BrowserBackupAgent.java b/src/com/android/browser/BrowserBackupAgent.java
index 6f6e829..c968ce5 100644
--- a/src/com/android/browser/BrowserBackupAgent.java
+++ b/src/com/android/browser/BrowserBackupAgent.java
@@ -84,6 +84,10 @@
             savedVersion = in.readInt();
         } catch (EOFException e) {
             // It means we had no previous state; that's fine
+        } finally {
+            if (in != null) {
+                in.close();
+            }
         }
 
         // Build a flattened representation of the bookmarks table
@@ -174,6 +178,10 @@
                     } catch (IOException ioe) {
                         Log.w(TAG, "Bad backup data; not restoring");
                         crc = -1;
+                    } finally {
+                        if (in != null) {
+                            in.close();
+                        }
                     }
                 }
 
@@ -187,7 +195,7 @@
         }
     }
 
-    class Bookmark {
+    static class Bookmark {
         public String url;
         public int visits;
         public long date;
@@ -258,13 +266,18 @@
         data.writeEntityHeader(key, toCopy);
 
         FileInputStream in = new FileInputStream(file);
-        int nRead;
-        while (toCopy > 0) {
-            nRead = in.read(buf, 0, CHUNK);
-            data.writeEntityData(buf, nRead);
-            toCopy -= nRead;
+        try {
+            int nRead;
+            while (toCopy > 0) {
+                nRead = in.read(buf, 0, CHUNK);
+                data.writeEntityData(buf, nRead);
+                toCopy -= nRead;
+            }
+        } finally {
+            if (in != null) {
+                in.close();
+            }
         }
-        in.close();
     }
 
     // Read the given file from backup to a file, calculating a CRC32 along the way
@@ -275,14 +288,18 @@
         CRC32 crc = new CRC32();
         FileOutputStream out = new FileOutputStream(file);
 
-        while (toRead > 0) {
-            int numRead = data.readEntityData(buf, 0, CHUNK);
-            crc.update(buf, 0, numRead);
-            out.write(buf, 0, numRead);
-            toRead -= numRead;
+        try {
+            while (toRead > 0) {
+                int numRead = data.readEntityData(buf, 0, CHUNK);
+                crc.update(buf, 0, numRead);
+                out.write(buf, 0, numRead);
+                toRead -= numRead;
+            }
+        } finally {
+            if (out != null) {
+                out.close();
+            }
         }
-
-        out.close();
         return crc.getValue();
     }
 
@@ -291,8 +308,14 @@
             throws IOException {
         DataOutputStream out = new DataOutputStream(
                 new FileOutputStream(stateFile.getFileDescriptor()));
-        out.writeLong(fileSize);
-        out.writeLong(crc);
-        out.writeInt(BACKUP_AGENT_VERSION);
+        try {
+            out.writeLong(fileSize);
+            out.writeLong(crc);
+            out.writeInt(BACKUP_AGENT_VERSION);
+        } finally {
+            if (out != null) {
+                out.close();
+            }
+        }
     }
 }
diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.java
index 51b4eaa..3d5ca03 100644
--- a/src/com/android/browser/BrowserSettings.java
+++ b/src/com/android/browser/BrowserSettings.java
@@ -503,7 +503,10 @@
     /* package */ void clearFormData(Context context) {
         WebViewDatabase.getInstance(context).clearFormData();
         if (mTabControl != null) {
-            mTabControl.getCurrentTopWebView().clearFormData();
+            WebView currentTopView = mTabControl.getCurrentTopWebView();
+            if (currentTopView != null) {
+                currentTopView.clearFormData();
+            }
         }
     }
 
diff --git a/src/com/android/browser/ErrorConsoleView.java b/src/com/android/browser/ErrorConsoleView.java
index 0f87cb5..ca5fed4 100644
--- a/src/com/android/browser/ErrorConsoleView.java
+++ b/src/com/android/browser/ErrorConsoleView.java
@@ -230,7 +230,7 @@
          * This class is an adapter for ErrorConsoleListView that contains the error console
          * message data.
          */
-        private class ErrorConsoleMessageList extends android.widget.BaseAdapter
+        private static class ErrorConsoleMessageList extends android.widget.BaseAdapter
                 implements android.widget.ListAdapter {
 
             private Vector<ConsoleMessage> mMessages;
diff --git a/src/com/android/browser/Tab.java b/src/com/android/browser/Tab.java
index 2dac050..07310bf 100644
--- a/src/com/android/browser/Tab.java
+++ b/src/com/android/browser/Tab.java
@@ -960,6 +960,9 @@
                     } else if (url.startsWith("http://")) {
                         url = url.substring(4);
                     }
+                    // Escape wildcards for LIKE operator.
+                    url = url.replace("\\", "\\\\").replace("%", "\\%")
+                            .replace("_", "\\_");
                     Cursor c = null;
                     try {
                         final ContentResolver cr
@@ -967,7 +970,7 @@
                         url = "%" + url;
                         String [] selArgs = new String[] { url };
                         String where = Browser.BookmarkColumns.URL
-                                + " LIKE ? AND "
+                                + " LIKE ? ESCAPE '\\' AND "
                                 + Browser.BookmarkColumns.BOOKMARK + " = 0";
                         c = cr.query(Browser.BOOKMARKS_URI, new String[]
                                 { Browser.BookmarkColumns._ID }, where, selArgs,
@@ -1430,7 +1433,7 @@
                     if (mSubView.copyBackForwardList().getSize() == 0) {
                         // This subwindow was opened for the sole purpose of
                         // downloading a file. Remove it.
-                        dismissSubWindow();
+                        mActivity.dismissSubWindow(Tab.this);
                     }
                 }
             });
diff --git a/src/com/android/browser/WebsiteSettingsActivity.java b/src/com/android/browser/WebsiteSettingsActivity.java
index 430286f..1e27092 100644
--- a/src/com/android/browser/WebsiteSettingsActivity.java
+++ b/src/com/android/browser/WebsiteSettingsActivity.java
@@ -62,7 +62,7 @@
     private static String sMBStored = null;
     private SiteAdapter mAdapter = null;
 
-    class Site {
+    static class Site {
         private String mOrigin;
         private String mTitle;
         private Bitmap mIcon;
@@ -190,7 +190,7 @@
          * Adds the specified feature to the site corresponding to supplied
          * origin in the map. Creates the site if it does not already exist.
          */
-        private void addFeatureToSite(Map sites, String origin, int feature) {
+        private void addFeatureToSite(Map<String, Site> sites, String origin, int feature) {
             Site site = null;
             if (sites.containsKey(origin)) {
                 site = (Site) sites.get(origin);
@@ -213,7 +213,7 @@
 
             WebStorage.getInstance().getOrigins(new ValueCallback<Map>() {
                 public void onReceiveValue(Map origins) {
-                    Map sites = new HashMap<String, Site>();
+                    Map<String, Site> sites = new HashMap<String, Site>();
                     if (origins != null) {
                         Iterator<String> iter = origins.keySet().iterator();
                         while (iter.hasNext()) {
@@ -225,7 +225,7 @@
             });
         }
 
-        public void askForGeolocation(final Map sites) {
+        public void askForGeolocation(final Map<String, Site> sites) {
             GeolocationPermissions.getInstance().getOrigins(new ValueCallback<Set<String> >() {
                 public void onReceiveValue(Set<String> origins) {
                     if (origins != null) {
@@ -240,19 +240,19 @@
             });
         }
 
-        public void populateIcons(Map sites) {
+        public void populateIcons(Map<String, Site> sites) {
             // Create a map from host to origin. This is used to add metadata
             // (title, icon) for this origin from the bookmarks DB.
-            HashMap hosts = new HashMap<String, Set<Site> >();
-            Set keys = sites.keySet();
-            Iterator<String> originIter = keys.iterator();
+            HashMap<String, Set<Site>> hosts = new HashMap<String, Set<Site>>();
+            Set<Map.Entry<String, Site>> elements = sites.entrySet();
+            Iterator<Map.Entry<String, Site>> originIter = elements.iterator();
             while (originIter.hasNext()) {
-                String origin = originIter.next();
-                Site site = (Site) sites.get(origin);
-                String host = Uri.parse(origin).getHost();
-                Set hostSites = null;
+                Map.Entry<String, Site> entry = originIter.next();
+                Site site = entry.getValue();
+                String host = Uri.parse(entry.getKey()).getHost();
+                Set<Site> hostSites = null;
                 if (hosts.containsKey(host)) {
-                    hostSites = (Set) hosts.get(host);
+                    hostSites = (Set<Site>)hosts.get(host);
                 } else {
                     hostSites = new HashSet<Site>();
                     hosts.put(host, hostSites);
@@ -266,55 +266,56 @@
                     new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE,
                     Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null);
 
-            if ((c != null) && c.moveToFirst()) {
-                int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
-                int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
-                int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
-                do {
-                    String url = c.getString(urlIndex);
-                    String host = Uri.parse(url).getHost();
-                    if (hosts.containsKey(host)) {
-                        String title = c.getString(titleIndex);
-                        Bitmap bmp = null;
-                        byte[] data = c.getBlob(faviconIndex);
-                        if (data != null) {
-                            bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
-                        }
-                        Set matchingSites = (Set) hosts.get(host);
-                        Iterator<Site> sitesIter = matchingSites.iterator();
-                        while (sitesIter.hasNext()) {
-                            Site site = sitesIter.next();
-                            // We should only set the title if the bookmark is for the root
-                            // (i.e. www.google.com), as website settings act on the origin
-                            // as a whole rather than a single page under that origin. If the
-                            // user has bookmarked a page under the root but *not* the root,
-                            // then we risk displaying the title of that page which may or
-                            // may not have any relevance to the origin.
-                            if (url.equals(site.getOrigin()) ||
-                                    (new String(site.getOrigin()+"/")).equals(url)) {
-                                site.setTitle(title);
+            if (c != null) {
+                if (c.moveToFirst()) {
+                    int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
+                    int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
+                    int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
+                    do {
+                        String url = c.getString(urlIndex);
+                        String host = Uri.parse(url).getHost();
+                        if (hosts.containsKey(host)) {
+                            String title = c.getString(titleIndex);
+                            Bitmap bmp = null;
+                            byte[] data = c.getBlob(faviconIndex);
+                            if (data != null) {
+                                bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                             }
-                            if (bmp != null) {
-                                site.setIcon(bmp);
+                            Set matchingSites = (Set) hosts.get(host);
+                            Iterator<Site> sitesIter = matchingSites.iterator();
+                            while (sitesIter.hasNext()) {
+                                Site site = sitesIter.next();
+                                // We should only set the title if the bookmark is for the root
+                                // (i.e. www.google.com), as website settings act on the origin
+                                // as a whole rather than a single page under that origin. If the
+                                // user has bookmarked a page under the root but *not* the root,
+                                // then we risk displaying the title of that page which may or
+                                // may not have any relevance to the origin.
+                                if (url.equals(site.getOrigin()) ||
+                                        (new String(site.getOrigin()+"/")).equals(url)) {
+                                    site.setTitle(title);
+                                }
+                                if (bmp != null) {
+                                    site.setIcon(bmp);
+                                }
                             }
                         }
-                    }
-                } while (c.moveToNext());
+                    } while (c.moveToNext());
+                }
+                c.close();
             }
-
-            c.close();
         }
 
 
-        public void populateOrigins(Map sites) {
+        public void populateOrigins(Map<String, Site> sites) {
             clear();
 
             // We can now simply populate our array with Site instances
-            Set keys = sites.keySet();
-            Iterator<String> originIter = keys.iterator();
-            while (originIter.hasNext()) {
-                String origin = originIter.next();
-                Site site = (Site) sites.get(origin);
+            Set<Map.Entry<String, Site>> elements = sites.entrySet();
+            Iterator<Map.Entry<String, Site>> entryIterator = elements.iterator();
+            while (entryIterator.hasNext()) {
+                Map.Entry<String, Site> entry = entryIterator.next();
+                Site site = entry.getValue();
                 add(site);
             }
 
diff --git a/tests/assets/bindings_test.html b/tests/assets/bindings_test.html
index c20ccec..71f3438 100755
--- a/tests/assets/bindings_test.html
+++ b/tests/assets/bindings_test.html
@@ -175,7 +175,7 @@
 function runTests() {
 
   // Assume that if the test isn't done after 10s that we failed.
-  window.setTimeout(function() { JNIBindingsTest.testComplete(); }, 10000);
+  window.setTimeout(function() { JNIBindingsTest.notifyComplete(); }, 10000);
 
   if (testPrimitiveTypes()) {
     appendLog("testPrimitiveTypes passed!");
@@ -231,7 +231,7 @@
     appendLog("testParameterTypeMismatch failed!");
   }
 
-  JNIBindingsTest.testComplete();
+  JNIBindingsTest.notifyComplete();
 }
 </script>
 
diff --git a/tests/src/com/android/browser/JNIBindingsTest.java b/tests/src/com/android/browser/JNIBindingsTest.java
index bfa3ac1..ba3c66a 100644
--- a/tests/src/com/android/browser/JNIBindingsTest.java
+++ b/tests/src/com/android/browser/JNIBindingsTest.java
@@ -20,6 +20,7 @@
 import android.util.Log;
 
 import java.util.Arrays;
+
 import junit.framework.AssertionFailedError;
 
 public class JNIBindingsTest extends AndroidTestCase {
@@ -34,9 +35,9 @@
         mTestApp = testApp;
     }
 
-    public void testComplete() {
+    public void notifyComplete() {
         Log.v(LOGTAG, "Completing the test.");
-        mTestApp.testComplete();
+        mTestApp.notifyComplete();
     }
 
     public void printAssertionFailed(AssertionFailedError e) {
@@ -232,7 +233,7 @@
             assertEquals(expectedIntParam, intParam);
             assertEquals(expectedDoubleParam, doubleParam);
             assertEquals(expectedBooleanParam, booleanParam);
-            assertEquals(expectedCharParam, charParam);;
+            assertEquals(expectedCharParam, charParam);
 
             // EMULATE_JSC_BINDINGS  JSC passes "undefined" for undefined types.
             assertEquals(expectedUndefinedParam, undefinedParam);
diff --git a/tests/src/com/android/browser/JNIBindingsTestApp.java b/tests/src/com/android/browser/JNIBindingsTestApp.java
index e01aca2..4f083f6 100644
--- a/tests/src/com/android/browser/JNIBindingsTestApp.java
+++ b/tests/src/com/android/browser/JNIBindingsTestApp.java
@@ -18,7 +18,6 @@
 
 import android.app.Instrumentation;
 import android.net.http.SslError;
-import android.os.Environment;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
@@ -29,6 +28,12 @@
 import android.webkit.SslErrorHandler;
 import android.webkit.WebView;
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
 /**
  * Adds a JavaScript interface to the webview and calls functions on it to verify variables
  * are passed from JS to Java correctly.
@@ -37,6 +42,8 @@
 
     private final static String TAG = "JNIBindingsTest";
 
+    private static final String SDCARD_BINDINGS_TEST_HTML = "/sdcard/bindings_test.html";
+
     private static final int MSG_WEBKIT_DATA_READY = 101;
 
     private BrowserActivity mActivity = null;
@@ -67,9 +74,11 @@
             mWebView = webView;
         }
 
+        @Override
         public void run() {
             Looper.prepare();
             mHandler = new Handler() {
+                @Override
                 public void handleMessage(Message msg) {
                     switch (msg.what) {
                         case MSG_WEBKIT_DATA_READY: {
@@ -102,6 +111,32 @@
         mInst = getInstrumentation();
         mInst.waitForIdleSync();
 
+        extractAsset();
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        removeAsset();
+        super.tearDown();
+    }
+
+    protected void extractAsset() throws IOException {
+        InputStream in = getInstrumentation().getContext().getAssets().open("bindings_test.html");
+        OutputStream out = new FileOutputStream(SDCARD_BINDINGS_TEST_HTML);
+
+        byte[] buf = new byte[2048];
+        int len;
+
+        while ((len = in.read(buf)) >= 0 ) {
+            out.write(buf, 0, len);
+        }
+        out.close();
+        in.close();
+    }
+
+    protected void removeAsset(){
+        File fileToDelete = new File(SDCARD_BINDINGS_TEST_HTML);
+        fileToDelete.delete();
     }
 
     /**
@@ -183,7 +218,7 @@
         });
     }
 
-    public synchronized void testComplete() {
+    public synchronized void notifyComplete() {
         mTestDone = true;
         notify();
     }
@@ -193,7 +228,7 @@
 
         Tab tab = mActivity.getTabControl().getCurrentTab();
         WebView webView = tab.getWebView();
-        webView.loadUrl("file:///sdcard/bindings_test.html");
+        webView.loadUrl("file://" + SDCARD_BINDINGS_TEST_HTML);
         synchronized(this) {
             while(!mTestDone) {
                 try {
diff --git a/tests/src/com/android/browser/PopularUrlsTest.java b/tests/src/com/android/browser/PopularUrlsTest.java
index d3806a0..5b8cb86 100644
--- a/tests/src/com/android/browser/PopularUrlsTest.java
+++ b/tests/src/com/android/browser/PopularUrlsTest.java
@@ -16,19 +16,6 @@
 
 package com.android.browser;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.FileWriter;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-
 import android.app.Instrumentation;
 import android.content.Intent;
 import android.net.Uri;
@@ -42,6 +29,19 @@
 import android.webkit.SslErrorHandler;
 import android.webkit.WebView;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
 /**
  *
  * Iterates over a list of URLs from a file and outputs the time to load each.
@@ -423,6 +423,9 @@
                     bufferedReader.close();
                 }
             }
+        } catch (FileNotFoundException fnfe) {
+            Log.e(TAG, fnfe.getMessage(), fnfe);
+            fail("Test environment not setup correctly");
         } finally {
             if (writer != null) {
                 writer.close();
@@ -436,6 +439,9 @@
         BufferedReader bufferedReader = getInputStream();
         try {
             loopUrls(bufferedReader, null, true, STABILITY_LOOPCOUNT);
+        } catch (FileNotFoundException fnfe) {
+            Log.e(TAG, fnfe.getMessage(), fnfe);
+            fail("Test environment not setup correctly");
         } finally {
             if (bufferedReader != null) {
                 bufferedReader.close();