Bug 1431470 - Add getTotalBookmarksCount to NewTabUtils.jsm
MozReview-Commit-ID: BlcBcMap2Xw
--- a/toolkit/modules/NewTabUtils.jsm
+++ b/toolkit/modules/NewTabUtils.jsm
@@ -1094,16 +1094,40 @@ var ActivityStreamProvider = {
columns: this._highlightsColumns,
params: this._getCommonParams(options, {
dateAddedThreshold: (Date.now() - options.bookmarkSecondsAgo * 1000) * 1000
})
}), options, "bookmark");
},
/**
+ * Get total count of all bookmarks.
+ * Note: this includes default bookmarks
+ *
+ * @return {int} The number bookmarks in the places DB.
+ */
+ async getTotalBookmarksCount() {
+ let sqlQuery = `
+ SELECT count(*) FROM moz_bookmarks b
+ JOIN moz_bookmarks t ON t.id = b.parent
+ AND t.parent <> :tags_folder
+ WHERE b.type = :type_bookmark
+ `;
+
+ const result = await this.executePlacesQuery(sqlQuery, {
+ params: {
+ tags_folder: PlacesUtils.tagsFolderId,
+ type_bookmark: PlacesUtils.bookmarks.TYPE_BOOKMARK,
+ }
+ });
+
+ return result[0][0];
+ },
+
+ /**
* Get most-recently-visited history with metadata for Activity Stream.
*
* @param {Object} aOptions
* {bool} ignoreBlocked: Do not filter out blocked links.
* {int} numItems: Maximum number of items to return.
*/
async getRecentHistory(aOptions) {
const options = Object.assign({
--- a/toolkit/modules/tests/xpcshell/test_NewTabUtils.js
+++ b/toolkit/modules/tests/xpcshell/test_NewTabUtils.js
@@ -915,16 +915,36 @@ add_task(async function activityStream_b
let sizeQueryResult;
// bookmarks
sizeQueryResult = await getBookmarksSize();
Assert.equal(sizeQueryResult, 1, "got the correct bookmark size");
});
+add_task(async function activityStream_getTotalBookmarksCount() {
+ await setUpActivityStreamTest();
+
+ let provider = NewTabUtils.activityStreamProvider;
+ let bookmarks = [
+ {url: "https://mozilla1.com/0", parentGuid: PlacesUtils.bookmarks.unfiledGuid},
+ {url: "https://mozilla1.com/1", parentGuid: PlacesUtils.bookmarks.unfiledGuid}
+ ];
+
+ let bookmarksSize = await provider.getTotalBookmarksCount();
+ Assert.equal(bookmarksSize, 0, ".getTotalBookmarksCount() returns 0 for an empty bookmarks table");
+
+ for (const bookmark of bookmarks) {
+ await PlacesUtils.bookmarks.insert(bookmark);
+ }
+
+ bookmarksSize = await provider.getTotalBookmarksCount();
+ Assert.equal(bookmarksSize, 2, ".getTotalBookmarksCount() returns 2 after 2 bookmarks are inserted");
+});
+
function TestProvider(getLinksFn) {
this.getLinks = getLinksFn;
this._observers = new Set();
}
TestProvider.prototype = {
addObserver(observer) {
this._observers.add(observer);