Bug 1236121 - Complete test coverage for the WebExtension bookmarks API, r?kmag draft
authorbsilverberg <bsilverberg@mozilla.com>
Wed, 09 Mar 2016 10:02:40 -0500
changeset 338601 c0764c13b2bf6f070b0bcbbdbd34155141fc597c
parent 337431 5a8c334ac296c94faf804e12a05c3e5c6f555155
child 339479 375f70af08d191c965624e64ace6fee8b66f21ed
push id12541
push userbmo:bob.silverberg@gmail.com
push dateWed, 09 Mar 2016 15:03:36 +0000
reviewerskmag
bugs1236121
milestone47.0a1
Bug 1236121 - Complete test coverage for the WebExtension bookmarks API, r?kmag Add coverage for getChildren() when the promise is rejected Add coverage for create() specifying index Add coverage for update() specifying a URL Add coverage for update() when the promise is rejected MozReview-Commit-ID: 7ANRDWRublL
browser/components/extensions/ext-bookmarks.js
toolkit/components/extensions/test/mochitest/test_ext_bookmarks.html
--- a/browser/components/extensions/ext-bookmarks.js
+++ b/browser/components/extensions/ext-bookmarks.js
@@ -209,9 +209,8 @@ extensions.registerSchemaAPI("bookmarks"
           return Bookmarks.remove(info).then(result => {});
         } catch (e) {
           return Promise.reject({message: `Invalid bookmark: ${JSON.stringify(info)}`});
         }
       },
     },
   };
 });
-
--- a/toolkit/components/extensions/test/mochitest/test_ext_bookmarks.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_bookmarks.html
@@ -85,23 +85,32 @@ function backgroundScript() {
     checkOurBookmark(results[0]);
 
     return browser.bookmarks.update(nonExistentId, {title: "new test title"}).then(expectedError, error => {
       browser.test.assertTrue(
         error.message.includes("No bookmarks found for the provided GUID"),
         "Expected error thrown when trying to update a non-existent bookmark"
       );
 
-      return browser.bookmarks.update(ourId, {title: "new test title"});
+      return browser.bookmarks.update(ourId, {title: "new test title", url: "http://example.com/"});
     });
   }).then(result => {
     browser.test.assertEq("new test title", result.title, "Updated bookmark has the expected title");
+    browser.test.assertEq("http://example.com/", result.url, "Updated bookmark has the expected URL");
     browser.test.assertEq(ourId, result.id, "Updated bookmark has the expected id");
 
-    return browser.bookmarks.getTree();
+    return Promise.resolve().then(() => {
+      return browser.bookmarks.update(ourId, {url: "this is not a valid url"});
+    }).then(expectedError, error => {
+      browser.test.assertTrue(
+        error.message.includes("Invalid bookmark:"),
+        "Expected error thrown when trying update with an invalid url"
+      );
+      return browser.bookmarks.getTree();
+    });
   }).then(results => {
     browser.test.assertEq(1, results.length, "getTree returns one result");
     let bookmark = results[0].children.find(bookmark => bookmark.id == unsortedId);
     browser.test.assertEq(
         "Unsorted Bookmarks",
         bookmark.title,
         "Folder returned from getTree has the expected title"
     );
@@ -145,17 +154,24 @@ function backgroundScript() {
       browser.bookmarks.create({title: "Toolbar Item", url: "http://toolbar.org", parentId: "toolbar_____"}),
     ]);
   }).then(results => {
     let createdFolderId = results[2].id;
     return Promise.all([
       browser.bookmarks.create({title: "Mozilla", url: "http://allizom.org", parentId: createdFolderId}),
       browser.bookmarks.create({title: "Mozilla Corporation", url: "http://allizom.com", parentId: createdFolderId}),
       browser.bookmarks.create({title: "Firefox", url: "http://allizom.org/firefox", parentId: createdFolderId}),
-    ]).then(() => {
+    ]).then(results => {
+      return browser.bookmarks.create({
+        title: "About Mozilla",
+        url: "http://allizom.org/about",
+        parentId: createdFolderId,
+        index: 1,
+      });
+    }).then(() => {
       // returns all items on empty object
       return browser.bookmarks.search({});
     }).then(results => {
       browser.test.assertTrue(results.length >= 9, "At least as many bookmarks as added were returned by search({})");
 
       return Promise.resolve().then(() => {
         return browser.bookmarks.remove(createdFolderId);
       }).then(expectedError, error => {
@@ -165,20 +181,22 @@ function backgroundScript() {
         );
         return browser.bookmarks.getSubTree(createdFolderId);
       });
     });
   }).then(results => {
     browser.test.assertEq(1, results.length, "Expected number of nodes returned by getSubTree");
     browser.test.assertEq("Mozilla Folder", results[0].title, "Folder has the expected title");
     let children = results[0].children;
-    browser.test.assertEq(3, children.length, "Expected number of bookmarks returned by getSubTree");
+    browser.test.assertEq(4, children.length, "Expected number of bookmarks returned by getSubTree");
     browser.test.assertEq("Firefox", children[0].title, "Bookmark has the expected title");
-    browser.test.assertEq("Mozilla Corporation", children[1].title, "Bookmark has the expected title");
-    browser.test.assertEq("Mozilla", children[2].title, "Bookmark has the expected title");
+    browser.test.assertEq("About Mozilla", children[1].title, "Bookmark has the expected title");
+    browser.test.assertEq(1, children[1].index, "Bookmark has the expected index");
+    browser.test.assertEq("Mozilla Corporation", children[2].title, "Bookmark has the expected title");
+    browser.test.assertEq("Mozilla", children[3].title, "Bookmark has the expected title");
 
     // throws an error for invalid query objects
     Promise.resolve().then(() => {
       return browser.bookmarks.search();
     }).then(expectedError, error => {
       browser.test.assertTrue(
         error.message.includes("Incorrect argument types for bookmarks.search"),
         "Expected error thrown when trying to search with no arguments"
@@ -261,50 +279,51 @@ function backgroundScript() {
     return browser.bookmarks.search("MøZILLÄ");
   }).then(results => {
     browser.test.assertEq(1, results.length, "Expected number of results returned for non-ascii search");
     browser.test.assertEq("MØzillä", results[0].title, "Bookmark has the expected title");
 
     // returns multiple results
     return browser.bookmarks.search("allizom");
   }).then(results => {
-    browser.test.assertEq(3, results.length, "Expected number of multiple results returned");
+    browser.test.assertEq(4, results.length, "Expected number of multiple results returned");
     browser.test.assertEq("Mozilla", results[0].title, "Bookmark has the expected title");
     browser.test.assertEq("Mozilla Corporation", results[1].title, "Bookmark has the expected title");
     browser.test.assertEq("Firefox", results[2].title, "Bookmark has the expected title");
+    browser.test.assertEq("About Mozilla", results[3].title, "Bookmark has the expected title");
 
     // accepts a url field
     return browser.bookmarks.search({url: "http://allizom.com/"});
   }).then(results => {
     browser.test.assertEq(1, results.length, "Expected number of results returned for url field");
-    checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 1}, results[0]);
+    checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 2}, results[0]);
 
     // normalizes urls
     return browser.bookmarks.search({url: "http://allizom.com"});
   }).then(results => {
     browser.test.assertEq(results.length, 1, "Expected number of results returned for normalized url field");
-    checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 1}, results[0]);
+    checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 2}, results[0]);
 
     // normalizes urls even more
     return browser.bookmarks.search({url: "http:allizom.com"});
   }).then(results => {
     browser.test.assertEq(results.length, 1, "Expected number of results returned for normalized url field");
-    checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 1}, results[0]);
+    checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 2}, results[0]);
 
     // accepts a title field
     return browser.bookmarks.search({title: "Mozilla"});
   }).then(results => {
     browser.test.assertEq(results.length, 1, "Expected number of results returned for title field");
-    checkBookmark({title: "Mozilla", url: "http://allizom.org/", index: 2}, results[0]);
+    checkBookmark({title: "Mozilla", url: "http://allizom.org/", index: 3}, results[0]);
 
     // can combine title and query
     return browser.bookmarks.search({title: "Mozilla", query: "allizom"});
   }).then(results => {
     browser.test.assertEq(1, results.length, "Expected number of results returned for title and query fields");
-    checkBookmark({title: "Mozilla", url: "http://allizom.org/", index: 2}, results[0]);
+    checkBookmark({title: "Mozilla", url: "http://allizom.org/", index: 3}, results[0]);
 
     // uses AND conditions
     return browser.bookmarks.search({title: "EFF", query: "allizom"});
   }).then(results => {
     browser.test.assertEq(
       0,
       results.length,
       "Expected number of results returned for non-matching title and query fields"
@@ -331,31 +350,31 @@ function backgroundScript() {
         error.message.includes("Incorrect argument types for bookmarks.getRecent"),
         "Expected error thrown when calling getRecent with a decimal number"
       );
     });
   }).then(() => {
     return browser.bookmarks.getRecent(5);
   }).then(results => {
     browser.test.assertEq(5, results.length, "Expected number of results returned by getRecent");
-    browser.test.assertEq("Firefox", results[0].title, "Bookmark has the expected title");
-    browser.test.assertEq("Mozilla Corporation", results[1].title, "Bookmark has the expected title");
-    browser.test.assertEq("Mozilla", results[2].title, "Bookmark has the expected title");
-    browser.test.assertEq("Toolbar Item", results[3].title, "Bookmark has the expected title");
-    browser.test.assertEq("Menu Item", results[4].title, "Bookmark has the expected title");
+    browser.test.assertEq("About Mozilla", results[0].title, "Bookmark has the expected title");
+    browser.test.assertEq("Firefox", results[1].title, "Bookmark has the expected title");
+    browser.test.assertEq("Mozilla Corporation", results[2].title, "Bookmark has the expected title");
+    browser.test.assertEq("Mozilla", results[3].title, "Bookmark has the expected title");
+    browser.test.assertEq("Toolbar Item", results[4].title, "Bookmark has the expected title");
 
     return browser.bookmarks.search({});
   }).then(results => {
     let startBookmarkCount = results.length;
     return browser.bookmarks.search({title: "Mozilla Folder"}).then(result => {
       return browser.bookmarks.removeTree(result[0].id);
     }).then(() => {
       return browser.bookmarks.search({}).then(results => {
         browser.test.assertEq(
-          startBookmarkCount - 4,
+          startBookmarkCount - 5,
           results.length,
           "Expected number of results returned after removeTree");
       });
     });
   }).then(() => {
     return browser.bookmarks.create({title: "Empty Folder"});
   }).then(result => {
     let emptyFolderId = result.id;
@@ -364,16 +383,23 @@ function backgroundScript() {
       return browser.bookmarks.get(emptyFolderId).then(expectedError, error => {
         browser.test.assertTrue(
           error.message.includes("Bookmark not found"),
           "Expected error thrown when trying to get a removed folder"
         );
       });
     });
   }).then(() => {
+    return browser.bookmarks.getChildren(nonExistentId).then(expectedError, error => {
+      browser.test.assertTrue(
+        error.message.includes("root is null"),
+        "Expected error thrown when trying to getChildren for a non-existent folder"
+      );
+    });
+  }).then(() => {
     return browser.test.notifyPass("bookmarks");
   }).catch(error => {
     browser.test.fail(`Error: ${String(error)} :: ${error.stack}`);
     browser.test.notifyFail("bookmarks");
   });
 }
 
 let extensionData = {