michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import java.util.ArrayList; michael@0: michael@0: import org.mozilla.gecko.AppConstants; michael@0: import org.mozilla.gecko.Assert; michael@0: import org.mozilla.gecko.GeckoProfile; michael@0: import org.mozilla.gecko.db.BrowserDB; michael@0: michael@0: import android.app.Activity; michael@0: import android.content.ContentResolver; michael@0: import android.database.Cursor; michael@0: import android.net.Uri; michael@0: michael@0: class DatabaseHelper { michael@0: protected enum BrowserDataType {BOOKMARKS, HISTORY}; michael@0: private Activity mActivity; michael@0: private Assert mAsserter; michael@0: michael@0: public DatabaseHelper(Activity activity, Assert asserter) { michael@0: mActivity = activity; michael@0: mAsserter = asserter; michael@0: } michael@0: /** michael@0: * This method can be used to check if an URL is present in the bookmarks database michael@0: */ michael@0: protected boolean isBookmark(String url) { michael@0: final ContentResolver resolver = mActivity.getContentResolver(); michael@0: return BrowserDB.isBookmark(resolver, url); michael@0: } michael@0: michael@0: protected Uri buildUri(BrowserDataType dataType) { michael@0: Uri uri = null; michael@0: if (dataType == BrowserDataType.BOOKMARKS || dataType == BrowserDataType.HISTORY) { michael@0: uri = Uri.parse("content://" + AppConstants.ANDROID_PACKAGE_NAME + ".db.browser/" + dataType.toString().toLowerCase()); michael@0: } else { michael@0: mAsserter.ok(false, "The wrong data type has been provided = " + dataType.toString(), "Please provide the correct data type"); michael@0: } michael@0: uri = uri.buildUpon().appendQueryParameter("profile", GeckoProfile.DEFAULT_PROFILE) michael@0: .appendQueryParameter("sync", "true").build(); michael@0: return uri; michael@0: } michael@0: michael@0: /** michael@0: * Adds a bookmark, or updates the bookmark title if the url already exists. michael@0: * michael@0: * The LocalBrowserDB.addBookmark implementation handles updating existing bookmarks. michael@0: */ michael@0: protected void addOrUpdateMobileBookmark(String title, String url) { michael@0: final ContentResolver resolver = mActivity.getContentResolver(); michael@0: BrowserDB.addBookmark(resolver, title, url); michael@0: mAsserter.ok(true, "Inserting/updating a new bookmark", "Inserting/updating the bookmark with the title = " + title + " and the url = " + url); michael@0: } michael@0: michael@0: /** michael@0: * Updates the title and keyword of a bookmark with the given URL. michael@0: * michael@0: * Warning: This method assumes that there's only one bookmark with the given URL. michael@0: */ michael@0: protected void updateBookmark(String url, String title, String keyword) { michael@0: final ContentResolver resolver = mActivity.getContentResolver(); michael@0: // Get the id for the bookmark with the given URL. michael@0: Cursor c = null; michael@0: try { michael@0: c = BrowserDB.getBookmarkForUrl(resolver, url); michael@0: if (!c.moveToFirst()) { michael@0: mAsserter.ok(false, "Getting bookmark with url", "Couldn't find bookmark with url = " + url); michael@0: return; michael@0: } michael@0: michael@0: int id = c.getInt(c.getColumnIndexOrThrow("_id")); michael@0: BrowserDB.updateBookmark(resolver, id, url, title, keyword); michael@0: michael@0: mAsserter.ok(true, "Updating bookmark", "Updating bookmark with url = " + url); michael@0: } finally { michael@0: if (c != null) { michael@0: c.close(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: protected void deleteBookmark(String url) { michael@0: final ContentResolver resolver = mActivity.getContentResolver(); michael@0: BrowserDB.removeBookmarksWithURL(resolver, url); michael@0: } michael@0: michael@0: protected void deleteHistoryItem(String url) { michael@0: final ContentResolver resolver = mActivity.getContentResolver(); michael@0: BrowserDB.removeHistoryEntry(resolver, url); michael@0: } michael@0: michael@0: // About the same implementation as getFolderIdFromGuid from LocalBrowserDB because it is declared private and we can't use reflections to access it michael@0: protected long getFolderIdFromGuid(String guid) { michael@0: ContentResolver resolver = mActivity.getContentResolver(); michael@0: long folderId = Long.valueOf(-1); michael@0: Uri bookmarksUri = buildUri(BrowserDataType.BOOKMARKS); michael@0: Cursor c = null; michael@0: try { michael@0: c = resolver.query(bookmarksUri, michael@0: new String[] { "_id" }, michael@0: "guid = ?", michael@0: new String[] { guid }, michael@0: null); michael@0: if (c.moveToFirst()) { michael@0: folderId = c.getLong(c.getColumnIndexOrThrow("_id")); michael@0: } michael@0: if (folderId == -1) { michael@0: mAsserter.ok(false, "Trying to get the folder id" ,"We did not get the correct folder id"); michael@0: } michael@0: } finally { michael@0: if (c != null) { michael@0: c.close(); michael@0: } michael@0: } michael@0: return folderId; michael@0: } michael@0: michael@0: /** michael@0: * @param a BrowserDataType value - either HISTORY or BOOKMARKS michael@0: * @return an ArrayList of the urls in the Firefox for Android Bookmarks or History databases michael@0: */ michael@0: protected ArrayList getBrowserDBUrls(BrowserDataType dataType) { michael@0: ArrayList browserData = new ArrayList(); michael@0: ContentResolver resolver = mActivity.getContentResolver(); michael@0: Cursor cursor = null; michael@0: Uri uri = buildUri(dataType); michael@0: if (dataType == BrowserDataType.HISTORY) { michael@0: cursor = BrowserDB.getAllVisitedHistory(resolver); michael@0: } else if (dataType == BrowserDataType.BOOKMARKS) { michael@0: cursor = BrowserDB.getBookmarksInFolder(resolver, getFolderIdFromGuid("mobile")); michael@0: } michael@0: if (cursor != null) { michael@0: cursor.moveToFirst(); michael@0: for (int i = 0; i < cursor.getCount(); i++ ) { michael@0: // The url field may be null for folders in the structure of the Bookmarks table for Firefox so we should eliminate those michael@0: if (cursor.getString(cursor.getColumnIndex("url")) != null) { michael@0: browserData.add(cursor.getString(cursor.getColumnIndex("url"))); michael@0: } michael@0: if(!cursor.isLast()) { michael@0: cursor.moveToNext(); michael@0: } michael@0: } michael@0: } else { michael@0: mAsserter.ok(false, "We could not retrieve any data from the database", "The cursor was null"); michael@0: } michael@0: if (cursor != null) { michael@0: cursor.close(); michael@0: } michael@0: return browserData; michael@0: } michael@0: }