diff options
author | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-17 23:05:38 +0000 |
---|---|---|
committer | joth@chromium.org <joth@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-10-17 23:05:38 +0000 |
commit | 14890487066c93f15e23a984efd2fd90e6133dad (patch) | |
tree | f3c84c2e58146d5f92abf3b4f02a903493de1f36 /android_webview | |
parent | 34a7a61e937a6949e6e89743bb84974c142fa34a (diff) | |
download | chromium_src-14890487066c93f15e23a984efd2fd90e6133dad.zip chromium_src-14890487066c93f15e23a984efd2fd90e6133dad.tar.gz chromium_src-14890487066c93f15e23a984efd2fd90e6133dad.tar.bz2 |
Make HttpAuthDatabaseTest more robust
Currently it uses the system default auth-db, and leaves junk in the DB from one run to
the next and was giving me some spurious failures.
Now we use a temporary DB that can be blown away between test runs
BUG=
Review URL: https://chromiumcodereview.appspot.com/11137028
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@162564 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'android_webview')
-rw-r--r-- | android_webview/java/src/org/chromium/android_webview/HttpAuthDatabase.java | 91 | ||||
-rw-r--r-- | android_webview/javatests/src/org/chromium/android_webview/test/HttpAuthDatabaseTest.java | 14 |
2 files changed, 61 insertions, 44 deletions
diff --git a/android_webview/java/src/org/chromium/android_webview/HttpAuthDatabase.java b/android_webview/java/src/org/chromium/android_webview/HttpAuthDatabase.java index b19dcab..0286a67 100644 --- a/android_webview/java/src/org/chromium/android_webview/HttpAuthDatabase.java +++ b/android_webview/java/src/org/chromium/android_webview/HttpAuthDatabase.java @@ -10,19 +10,21 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.util.Log; -import android.webkit.WebView; /** - * This database is used to support - * {@link WebView#setHttpAuthUsernamePassword(String, String, String, String)} and - * {@link WebView#getHttpAuthUsernamePassword(String, String)}. + * This database is used to support WebView's setHttpAuthUsernamePassword and + * getHttpAuthUsernamePassword methods, and WebViewDatabase's clearHttpAuthUsernamePassword and + * hasHttpAuthUsernamePassword methods. * - * This class is a singleton and should be accessed only via - * {@link HttpAuthDatabase#getInstance(Context)}. Calling this method will create an instance on - * demand and start the process of opening or creating the database in the background. + * While this class is intended to be used as a singleton, this property is not enforced in this + * layer, primarily for ease of testing. To line up with the classic implementation and behavior, + * there is no specific handling and reporting when SQL errors occur. * - * Unfortunately, to line up with the WebViewClassic API and behavior, there isn't much we can do - * when SQL errors occur. + * Note on thread-safety: As per the classic implementation, most API functions have thread safety + * provided by the underlying SQLiteDatabase instance. The exception is database opening: this + * is handled in the dedicated background thread, which also provides a performance gain + * if triggered early on (e.g. as a side effect of CookieSyncManager.createInstance() call), + * sufficiently in advance of the first blocking usage of the API. */ public class HttpAuthDatabase { @@ -34,7 +36,7 @@ public class HttpAuthDatabase { private static HttpAuthDatabase sInstance = null; - private static SQLiteDatabase sDatabase = null; + private SQLiteDatabase mDatabase = null; private static final String ID_COL = "_id"; @@ -54,25 +56,29 @@ public class HttpAuthDatabase { */ private boolean mInitialized = false; - private HttpAuthDatabase(final Context context) { - // Singleton only, use getInstance() + /** + * Create an instance of HttpAuthDatabase for the named file, and kick-off background + * initialization of that database. + * + * @param context the Context to use for opening the database + * @param databaseFile Name of the file to be initialized. + */ + public HttpAuthDatabase(final Context context, final String databaseFile) { new Thread() { @Override public void run() { - init(context); + initOnBackgroundThread(context, databaseFile); } }.start(); } /** - * Fetches the singleton instance of HttpAuthDatabase, creating an instance on demand. - * - * @param context the Context to use for opening the database - * @return The singleton instance + * @deprecated Retained for merge convenience. TODO(joth): remove in next patch. */ + @Deprecated public static synchronized HttpAuthDatabase getInstance(Context context) { if (sInstance == null) { - sInstance = new HttpAuthDatabase(context); + sInstance = new HttpAuthDatabase(context, DATABASE_FILE); } return sInstance; } @@ -81,52 +87,55 @@ public class HttpAuthDatabase { * Initializes the databases and notifies any callers waiting on waitForInit. * * @param context the Context to use for opening the database + * @param databaseFile Name of the file to be initialized. */ - private synchronized void init(Context context) { + private synchronized void initOnBackgroundThread(Context context, String databaseFile) { if (mInitialized) { return; } - initDatabase(context); + initDatabase(context, databaseFile); // Thread done, notify. mInitialized = true; - notify(); + notifyAll(); } /** * Opens the database, and upgrades it if necessary. * * @param context the Context to use for opening the database + * @param databaseFile Name of the file to be initialized. */ - private static void initDatabase(Context context) { + private void initDatabase(Context context, String databaseFile) { try { - sDatabase = context.openOrCreateDatabase(DATABASE_FILE, 0, null); + mDatabase = context.openOrCreateDatabase(databaseFile, 0, null); } catch (SQLiteException e) { // try again by deleting the old db and create a new one - if (context.deleteDatabase(DATABASE_FILE)) { - sDatabase = context.openOrCreateDatabase(DATABASE_FILE, 0, null); + if (context.deleteDatabase(databaseFile)) { + mDatabase = context.openOrCreateDatabase(databaseFile, 0, null); } } - if (sDatabase == null) { + if (mDatabase == null) { // Not much we can do to recover at this point - Log.e(LOGTAG, "Unable to open or create " + DATABASE_FILE); + Log.e(LOGTAG, "Unable to open or create " + databaseFile); + return; } - if (sDatabase.getVersion() != DATABASE_VERSION) { - sDatabase.beginTransactionNonExclusive(); + if (mDatabase.getVersion() != DATABASE_VERSION) { + mDatabase.beginTransactionNonExclusive(); try { createTable(); - sDatabase.setTransactionSuccessful(); + mDatabase.setTransactionSuccessful(); } finally { - sDatabase.endTransaction(); + mDatabase.endTransaction(); } } } - private static void createTable() { - sDatabase.execSQL("CREATE TABLE " + HTTPAUTH_TABLE_NAME + private void createTable() { + mDatabase.execSQL("CREATE TABLE " + HTTPAUTH_TABLE_NAME + " (" + ID_COL + " INTEGER PRIMARY KEY, " + HTTPAUTH_HOST_COL + " TEXT, " + HTTPAUTH_REALM_COL + " TEXT, " + HTTPAUTH_USERNAME_COL + " TEXT, " @@ -134,7 +143,7 @@ public class HttpAuthDatabase { + HTTPAUTH_HOST_COL + ", " + HTTPAUTH_REALM_COL + ") ON CONFLICT REPLACE);"); - sDatabase.setVersion(DATABASE_VERSION); + mDatabase.setVersion(DATABASE_VERSION); } /** @@ -153,7 +162,7 @@ public class HttpAuthDatabase { } } } - return sDatabase != null; + return mDatabase != null; } /** @@ -165,8 +174,6 @@ public class HttpAuthDatabase { * @param username the username for the password. * @param password the password */ - // TODO(leandrogracia): remove public when @VisibleForTesting works. - // @VisibleForTesting public void setHttpAuthUsernamePassword(String host, String realm, String username, String password) { if (host == null || realm == null || !waitForInit()) { @@ -178,7 +185,7 @@ public class HttpAuthDatabase { c.put(HTTPAUTH_REALM_COL, realm); c.put(HTTPAUTH_USERNAME_COL, username); c.put(HTTPAUTH_PASSWORD_COL, password); - sDatabase.insert(HTTPAUTH_TABLE_NAME, HTTPAUTH_HOST_COL, c); + mDatabase.insert(HTTPAUTH_TABLE_NAME, HTTPAUTH_HOST_COL, c); } /** @@ -191,8 +198,6 @@ public class HttpAuthDatabase { * @return a String[] if found where String[0] is username (which can be null) and * String[1] is password. Null is returned if it can't find anything. */ - // TODO(leandrogracia): remove public when @VisibleForTesting works. - // @VisibleForTesting public String[] getHttpAuthUsernamePassword(String host, String realm) { if (host == null || realm == null || !waitForInit()){ return null; @@ -207,7 +212,7 @@ public class HttpAuthDatabase { String[] ret = null; Cursor cursor = null; try { - cursor = sDatabase.query(HTTPAUTH_TABLE_NAME, columns, selection, + cursor = mDatabase.query(HTTPAUTH_TABLE_NAME, columns, selection, new String[] { host, realm }, null, null, null); if (cursor.moveToFirst()) { ret = new String[] { @@ -236,7 +241,7 @@ public class HttpAuthDatabase { Cursor cursor = null; boolean ret = false; try { - cursor = sDatabase.query(HTTPAUTH_TABLE_NAME, ID_PROJECTION, null, null, null, null, + cursor = mDatabase.query(HTTPAUTH_TABLE_NAME, ID_PROJECTION, null, null, null, null, null); ret = cursor.moveToFirst(); } catch (IllegalStateException e) { @@ -254,6 +259,6 @@ public class HttpAuthDatabase { if (!waitForInit()) { return; } - sDatabase.delete(HTTPAUTH_TABLE_NAME, null, null); + mDatabase.delete(HTTPAUTH_TABLE_NAME, null, null); } } diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/HttpAuthDatabaseTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/HttpAuthDatabaseTest.java index 57f269a..30b5da1 100644 --- a/android_webview/javatests/src/org/chromium/android_webview/test/HttpAuthDatabaseTest.java +++ b/android_webview/javatests/src/org/chromium/android_webview/test/HttpAuthDatabaseTest.java @@ -15,10 +15,22 @@ import org.chromium.base.test.util.Feature; */ public class HttpAuthDatabaseTest extends AndroidTestCase { + private static final String TEST_DATABASE = "http_auth_for_HttpAuthDatabaseTest.db"; + + @Override + protected void setUp() { + getContext().deleteDatabase(TEST_DATABASE); + } + + @Override + protected void tearDown() { + getContext().deleteDatabase(TEST_DATABASE); + } + @SmallTest @Feature({"Android-WebView"}) public void testAccessHttpAuthUsernamePassword() throws Exception { - HttpAuthDatabase instance = HttpAuthDatabase.getInstance(getContext()); + HttpAuthDatabase instance = new HttpAuthDatabase(getContext(), TEST_DATABASE); String host = "http://localhost:8080"; String realm = "testrealm"; |