Bug 1431470 - Add getTotalBookmarksCount to NewTabUtils.jsm draft
authork88hudson <k88hudson@gmail.com>
Tue, 27 Feb 2018 17:50:16 -0500
changeset 760650 611005d62864e9ffbcce4bc4f8ca15e24408a834
parent 760648 5297541590781af40ff09e067646f3115960af75
push id100704
push userbmo:khudson@mozilla.com
push dateTue, 27 Feb 2018 22:51:11 +0000
bugs1431470
milestone60.0a1
Bug 1431470 - Add getTotalBookmarksCount to NewTabUtils.jsm MozReview-Commit-ID: BlcBcMap2Xw
toolkit/modules/NewTabUtils.jsm
toolkit/modules/tests/xpcshell/test_NewTabUtils.js
--- 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);