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