|
1 package org.mozilla.gecko.tests; |
|
2 |
|
3 import java.util.ArrayList; |
|
4 |
|
5 import org.mozilla.gecko.AppConstants; |
|
6 import org.mozilla.gecko.Assert; |
|
7 import org.mozilla.gecko.GeckoProfile; |
|
8 import org.mozilla.gecko.db.BrowserDB; |
|
9 |
|
10 import android.app.Activity; |
|
11 import android.content.ContentResolver; |
|
12 import android.database.Cursor; |
|
13 import android.net.Uri; |
|
14 |
|
15 class DatabaseHelper { |
|
16 protected enum BrowserDataType {BOOKMARKS, HISTORY}; |
|
17 private Activity mActivity; |
|
18 private Assert mAsserter; |
|
19 |
|
20 public DatabaseHelper(Activity activity, Assert asserter) { |
|
21 mActivity = activity; |
|
22 mAsserter = asserter; |
|
23 } |
|
24 /** |
|
25 * This method can be used to check if an URL is present in the bookmarks database |
|
26 */ |
|
27 protected boolean isBookmark(String url) { |
|
28 final ContentResolver resolver = mActivity.getContentResolver(); |
|
29 return BrowserDB.isBookmark(resolver, url); |
|
30 } |
|
31 |
|
32 protected Uri buildUri(BrowserDataType dataType) { |
|
33 Uri uri = null; |
|
34 if (dataType == BrowserDataType.BOOKMARKS || dataType == BrowserDataType.HISTORY) { |
|
35 uri = Uri.parse("content://" + AppConstants.ANDROID_PACKAGE_NAME + ".db.browser/" + dataType.toString().toLowerCase()); |
|
36 } else { |
|
37 mAsserter.ok(false, "The wrong data type has been provided = " + dataType.toString(), "Please provide the correct data type"); |
|
38 } |
|
39 uri = uri.buildUpon().appendQueryParameter("profile", GeckoProfile.DEFAULT_PROFILE) |
|
40 .appendQueryParameter("sync", "true").build(); |
|
41 return uri; |
|
42 } |
|
43 |
|
44 /** |
|
45 * Adds a bookmark, or updates the bookmark title if the url already exists. |
|
46 * |
|
47 * The LocalBrowserDB.addBookmark implementation handles updating existing bookmarks. |
|
48 */ |
|
49 protected void addOrUpdateMobileBookmark(String title, String url) { |
|
50 final ContentResolver resolver = mActivity.getContentResolver(); |
|
51 BrowserDB.addBookmark(resolver, title, url); |
|
52 mAsserter.ok(true, "Inserting/updating a new bookmark", "Inserting/updating the bookmark with the title = " + title + " and the url = " + url); |
|
53 } |
|
54 |
|
55 /** |
|
56 * Updates the title and keyword of a bookmark with the given URL. |
|
57 * |
|
58 * Warning: This method assumes that there's only one bookmark with the given URL. |
|
59 */ |
|
60 protected void updateBookmark(String url, String title, String keyword) { |
|
61 final ContentResolver resolver = mActivity.getContentResolver(); |
|
62 // Get the id for the bookmark with the given URL. |
|
63 Cursor c = null; |
|
64 try { |
|
65 c = BrowserDB.getBookmarkForUrl(resolver, url); |
|
66 if (!c.moveToFirst()) { |
|
67 mAsserter.ok(false, "Getting bookmark with url", "Couldn't find bookmark with url = " + url); |
|
68 return; |
|
69 } |
|
70 |
|
71 int id = c.getInt(c.getColumnIndexOrThrow("_id")); |
|
72 BrowserDB.updateBookmark(resolver, id, url, title, keyword); |
|
73 |
|
74 mAsserter.ok(true, "Updating bookmark", "Updating bookmark with url = " + url); |
|
75 } finally { |
|
76 if (c != null) { |
|
77 c.close(); |
|
78 } |
|
79 } |
|
80 } |
|
81 |
|
82 protected void deleteBookmark(String url) { |
|
83 final ContentResolver resolver = mActivity.getContentResolver(); |
|
84 BrowserDB.removeBookmarksWithURL(resolver, url); |
|
85 } |
|
86 |
|
87 protected void deleteHistoryItem(String url) { |
|
88 final ContentResolver resolver = mActivity.getContentResolver(); |
|
89 BrowserDB.removeHistoryEntry(resolver, url); |
|
90 } |
|
91 |
|
92 // About the same implementation as getFolderIdFromGuid from LocalBrowserDB because it is declared private and we can't use reflections to access it |
|
93 protected long getFolderIdFromGuid(String guid) { |
|
94 ContentResolver resolver = mActivity.getContentResolver(); |
|
95 long folderId = Long.valueOf(-1); |
|
96 Uri bookmarksUri = buildUri(BrowserDataType.BOOKMARKS); |
|
97 Cursor c = null; |
|
98 try { |
|
99 c = resolver.query(bookmarksUri, |
|
100 new String[] { "_id" }, |
|
101 "guid = ?", |
|
102 new String[] { guid }, |
|
103 null); |
|
104 if (c.moveToFirst()) { |
|
105 folderId = c.getLong(c.getColumnIndexOrThrow("_id")); |
|
106 } |
|
107 if (folderId == -1) { |
|
108 mAsserter.ok(false, "Trying to get the folder id" ,"We did not get the correct folder id"); |
|
109 } |
|
110 } finally { |
|
111 if (c != null) { |
|
112 c.close(); |
|
113 } |
|
114 } |
|
115 return folderId; |
|
116 } |
|
117 |
|
118 /** |
|
119 * @param a BrowserDataType value - either HISTORY or BOOKMARKS |
|
120 * @return an ArrayList of the urls in the Firefox for Android Bookmarks or History databases |
|
121 */ |
|
122 protected ArrayList<String> getBrowserDBUrls(BrowserDataType dataType) { |
|
123 ArrayList<String> browserData = new ArrayList<String>(); |
|
124 ContentResolver resolver = mActivity.getContentResolver(); |
|
125 Cursor cursor = null; |
|
126 Uri uri = buildUri(dataType); |
|
127 if (dataType == BrowserDataType.HISTORY) { |
|
128 cursor = BrowserDB.getAllVisitedHistory(resolver); |
|
129 } else if (dataType == BrowserDataType.BOOKMARKS) { |
|
130 cursor = BrowserDB.getBookmarksInFolder(resolver, getFolderIdFromGuid("mobile")); |
|
131 } |
|
132 if (cursor != null) { |
|
133 cursor.moveToFirst(); |
|
134 for (int i = 0; i < cursor.getCount(); i++ ) { |
|
135 // The url field may be null for folders in the structure of the Bookmarks table for Firefox so we should eliminate those |
|
136 if (cursor.getString(cursor.getColumnIndex("url")) != null) { |
|
137 browserData.add(cursor.getString(cursor.getColumnIndex("url"))); |
|
138 } |
|
139 if(!cursor.isLast()) { |
|
140 cursor.moveToNext(); |
|
141 } |
|
142 } |
|
143 } else { |
|
144 mAsserter.ok(false, "We could not retrieve any data from the database", "The cursor was null"); |
|
145 } |
|
146 if (cursor != null) { |
|
147 cursor.close(); |
|
148 } |
|
149 return browserData; |
|
150 } |
|
151 } |