mobile/android/base/tests/DatabaseHelper.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/tests/DatabaseHelper.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,151 @@
     1.4 +package org.mozilla.gecko.tests;
     1.5 +
     1.6 +import java.util.ArrayList;
     1.7 +
     1.8 +import org.mozilla.gecko.AppConstants;
     1.9 +import org.mozilla.gecko.Assert;
    1.10 +import org.mozilla.gecko.GeckoProfile;
    1.11 +import org.mozilla.gecko.db.BrowserDB;
    1.12 +
    1.13 +import android.app.Activity;
    1.14 +import android.content.ContentResolver;
    1.15 +import android.database.Cursor;
    1.16 +import android.net.Uri;
    1.17 +
    1.18 +class DatabaseHelper {
    1.19 +    protected enum BrowserDataType {BOOKMARKS, HISTORY};
    1.20 +    private Activity mActivity;
    1.21 +    private Assert mAsserter;
    1.22 +
    1.23 +    public DatabaseHelper(Activity activity, Assert asserter) {
    1.24 +        mActivity = activity;
    1.25 +        mAsserter = asserter;
    1.26 +    }
    1.27 +    /**
    1.28 +    * This method can be used to check if an URL is present in the bookmarks database
    1.29 +    */
    1.30 +    protected boolean isBookmark(String url) {
    1.31 +        final ContentResolver resolver = mActivity.getContentResolver();
    1.32 +        return BrowserDB.isBookmark(resolver, url);
    1.33 +    }
    1.34 +
    1.35 +    protected Uri buildUri(BrowserDataType dataType) {
    1.36 +        Uri uri = null;
    1.37 +        if (dataType == BrowserDataType.BOOKMARKS || dataType == BrowserDataType.HISTORY) {
    1.38 +            uri = Uri.parse("content://" + AppConstants.ANDROID_PACKAGE_NAME + ".db.browser/" + dataType.toString().toLowerCase());
    1.39 +        } else {
    1.40 +           mAsserter.ok(false, "The wrong data type has been provided = " + dataType.toString(), "Please provide the correct data type");
    1.41 +        }
    1.42 +        uri = uri.buildUpon().appendQueryParameter("profile", GeckoProfile.DEFAULT_PROFILE)
    1.43 +                             .appendQueryParameter("sync", "true").build();
    1.44 +        return uri;
    1.45 +    }
    1.46 +
    1.47 +    /**
    1.48 +     * Adds a bookmark, or updates the bookmark title if the url already exists.
    1.49 +     *
    1.50 +     * The LocalBrowserDB.addBookmark implementation handles updating existing bookmarks.
    1.51 +     */
    1.52 +    protected void addOrUpdateMobileBookmark(String title, String url) {
    1.53 +        final ContentResolver resolver = mActivity.getContentResolver();
    1.54 +        BrowserDB.addBookmark(resolver, title, url);
    1.55 +        mAsserter.ok(true, "Inserting/updating a new bookmark", "Inserting/updating the bookmark with the title = " + title + " and the url = " + url);
    1.56 +    }
    1.57 +
    1.58 +    /**
    1.59 +     * Updates the title and keyword of a bookmark with the given URL.
    1.60 +     *
    1.61 +     * Warning: This method assumes that there's only one bookmark with the given URL.
    1.62 +     */
    1.63 +    protected void updateBookmark(String url, String title, String keyword) {
    1.64 +        final ContentResolver resolver = mActivity.getContentResolver();
    1.65 +        // Get the id for the bookmark with the given URL.
    1.66 +        Cursor c = null;
    1.67 +        try {
    1.68 +            c = BrowserDB.getBookmarkForUrl(resolver, url);
    1.69 +            if (!c.moveToFirst()) {
    1.70 +                mAsserter.ok(false, "Getting bookmark with url", "Couldn't find bookmark with url = " + url);
    1.71 +                return;
    1.72 +            }
    1.73 +
    1.74 +            int id = c.getInt(c.getColumnIndexOrThrow("_id"));
    1.75 +            BrowserDB.updateBookmark(resolver, id, url, title, keyword);
    1.76 +
    1.77 +            mAsserter.ok(true, "Updating bookmark", "Updating bookmark with url = " + url);
    1.78 +        } finally {
    1.79 +            if (c != null) {
    1.80 +                c.close();
    1.81 +            }
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +    protected void deleteBookmark(String url) {
    1.86 +        final ContentResolver resolver = mActivity.getContentResolver();
    1.87 +        BrowserDB.removeBookmarksWithURL(resolver, url);
    1.88 +    }
    1.89 +
    1.90 +    protected void deleteHistoryItem(String url) {
    1.91 +        final ContentResolver resolver = mActivity.getContentResolver();
    1.92 +        BrowserDB.removeHistoryEntry(resolver, url);
    1.93 +    }
    1.94 +
    1.95 +    // About the same implementation as getFolderIdFromGuid from LocalBrowserDB because it is declared private and we can't use reflections to access it
    1.96 +    protected long getFolderIdFromGuid(String guid) {
    1.97 +        ContentResolver resolver = mActivity.getContentResolver();
    1.98 +        long folderId = Long.valueOf(-1);
    1.99 +        Uri bookmarksUri = buildUri(BrowserDataType.BOOKMARKS);
   1.100 +        Cursor c = null;
   1.101 +        try {
   1.102 +            c = resolver.query(bookmarksUri,
   1.103 +                               new String[] { "_id" },
   1.104 +                               "guid = ?",
   1.105 +                               new String[] { guid },
   1.106 +                               null);
   1.107 +            if (c.moveToFirst()) {
   1.108 +                folderId = c.getLong(c.getColumnIndexOrThrow("_id"));
   1.109 +            }
   1.110 +            if (folderId == -1) {
   1.111 +                mAsserter.ok(false, "Trying to get the folder id" ,"We did not get the correct folder id");
   1.112 +            }
   1.113 +        } finally {
   1.114 +            if (c != null) {
   1.115 +                c.close();
   1.116 +            }
   1.117 +        }
   1.118 +        return folderId;
   1.119 +    }
   1.120 +
   1.121 +     /**
   1.122 +     * @param  a BrowserDataType value - either HISTORY or BOOKMARKS
   1.123 +     * @return an ArrayList of the urls in the Firefox for Android Bookmarks or History databases
   1.124 +     */
   1.125 +    protected ArrayList<String> getBrowserDBUrls(BrowserDataType dataType) {
   1.126 +        ArrayList<String> browserData = new ArrayList<String>();
   1.127 +        ContentResolver resolver = mActivity.getContentResolver();
   1.128 +        Cursor cursor = null;
   1.129 +        Uri uri = buildUri(dataType);
   1.130 +        if (dataType == BrowserDataType.HISTORY) {
   1.131 +            cursor = BrowserDB.getAllVisitedHistory(resolver);
   1.132 +        } else if (dataType == BrowserDataType.BOOKMARKS) {
   1.133 +            cursor = BrowserDB.getBookmarksInFolder(resolver, getFolderIdFromGuid("mobile"));
   1.134 +        }
   1.135 +        if (cursor != null) {
   1.136 +            cursor.moveToFirst();
   1.137 +            for (int i = 0; i < cursor.getCount(); i++ ) {
   1.138 +                 // The url field may be null for folders in the structure of the Bookmarks table for Firefox so we should eliminate those
   1.139 +                if (cursor.getString(cursor.getColumnIndex("url")) != null) {
   1.140 +                    browserData.add(cursor.getString(cursor.getColumnIndex("url")));
   1.141 +                }
   1.142 +                if(!cursor.isLast()) {
   1.143 +                    cursor.moveToNext();
   1.144 +                }
   1.145 +            }
   1.146 +        } else {
   1.147 +             mAsserter.ok(false, "We could not retrieve any data from the database", "The cursor was null");
   1.148 +        }
   1.149 +        if (cursor != null) {
   1.150 +            cursor.close();
   1.151 +        }
   1.152 +        return browserData;
   1.153 +    }
   1.154 +}

mercurial