Bug 1547295 - Make use of browser.test.assertRejects and browser.test.assertThrows wherever possible r=robwu,rpl
authorMichael Krasnov <mihalKrasnov@gmail.com>
Wed, 26 Jun 2019 13:40:31 +0000
changeset 543000 378f90fe8bfa897ccb2af0753cfcc6a129a4ba93
parent 542999 dad6a1b392f7a0b8db957ca90c8503b729693112
child 543001 ab93b6bc57590d7f965c1ec72981419171a840c8
push id2131
push userffxbld-merge
push dateMon, 26 Aug 2019 18:30:20 +0000
treeherdermozilla-release@b19ffb3ca153 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrobwu, rpl
bugs1547295
milestone69.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1547295 - Make use of browser.test.assertRejects and browser.test.assertThrows wherever possible r=robwu,rpl Differential Revision: https://phabricator.services.mozilla.com/D32623
browser/components/extensions/test/xpcshell/test_ext_bookmarks.js
--- a/browser/components/extensions/test/xpcshell/test_ext_bookmarks.js
+++ b/browser/components/extensions/test/xpcshell/test_ext_bookmarks.js
@@ -1,17 +1,17 @@
 /* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim: set sts=2 sw=2 et tw=80: */
 "use strict";
 
 ChromeUtils.defineModuleGetter(this, "PlacesUtils",
                                "resource://gre/modules/PlacesUtils.jsm");
 
 add_task(async function test_bookmarks() {
-  function background() {
+  async function background() {
     let unsortedId, ourId;
     let initialBookmarkCount = 0;
     let createdBookmarks = new Set();
     let createdFolderId;
     let createdSeparatorId;
     let collectedEvents = [];
     const nonExistentId = "000000000000";
     const bookmarkGuids = {
@@ -38,20 +38,16 @@ add_task(async function test_bookmarks()
       browser.test.assertEq(expected.title, bookmark.title, "Bookmark has the expected title");
       browser.test.assertEq(expected.index, bookmark.index, "Bookmark has expected index");
       browser.test.assertEq("bookmark", bookmark.type, "Bookmark is of type bookmark");
       if ("parentId" in expected) {
         browser.test.assertEq(expected.parentId, bookmark.parentId, "Bookmark has the expected parentId");
       }
     }
 
-    function expectedError() {
-      browser.test.fail("Did not get expected error");
-    }
-
     function checkOnCreated(id, parentId, index, title, url, dateAdded, type = "bookmark") {
       let createdData = collectedEvents.pop();
       browser.test.assertEq("onCreated", createdData.event, "onCreated was the last event received");
       browser.test.assertEq(id, createdData.id, "onCreated event received the expected id");
       let bookmark = createdData.bookmark;
       browser.test.assertEq(id, bookmark.id, "onCreated event received the expected bookmark id");
       browser.test.assertEq(parentId, bookmark.parentId, "onCreated event received the expected bookmark parentId");
       browser.test.assertEq(index, bookmark.index, "onCreated event received the expected bookmark index");
@@ -111,29 +107,26 @@ add_task(async function test_bookmarks()
     browser.bookmarks.onMoved.addListener((id, info) => {
       collectedEvents.push({event: "onMoved", id, info});
     });
 
     browser.bookmarks.onRemoved.addListener((id, info) => {
       collectedEvents.push({event: "onRemoved", id, info});
     });
 
-    browser.bookmarks.get(["not-a-bookmark-guid"]).then(expectedError, invalidGuidError => {
-      browser.test.assertTrue(
-        invalidGuidError.message.includes("Invalid value for property 'guid': \"not-a-bookmark-guid\""),
-        "Expected error thrown when trying to get a bookmark using an invalid guid"
-      );
+    await browser.test.assertRejects(
+      browser.bookmarks.get(["not-a-bookmark-guid"]),
+      /Invalid value for property 'guid': "not-a-bookmark-guid"/,
+      "Expected error thrown when trying to get a bookmark using an invalid guid");
 
-      return browser.bookmarks.get([nonExistentId]).then(expectedError, nonExistentIdError => {
-        browser.test.assertTrue(
-          nonExistentIdError.message.includes("Bookmark not found"),
-          "Expected error thrown when trying to get a bookmark using a non-existent Id"
-        );
-      });
-    }).then(() => {
+    await browser.test.assertRejects(
+      browser.bookmarks.get([nonExistentId]),
+      /Bookmark not found/,
+      "Expected error thrown when trying to get a bookmark using a non-existent Id")
+    .then(() => {
       return browser.bookmarks.search({});
     }).then(results => {
       initialBookmarkCount = results.length;
       return browser.bookmarks.create({title: "test bookmark", url: "http://example.org", type: "bookmark"});
     }).then(result => {
       ourId = result.id;
       checkOurBookmark(result);
       browser.test.assertEq(1, collectedEvents.length, "1 expected event received");
@@ -156,86 +149,70 @@ add_task(async function test_bookmarks()
       browser.test.assertEq(undefined, folder.url, "Folder does not have a url");
       browser.test.assertEq("Other Bookmarks", folder.title, "Folder has the expected title");
       browser.test.assertTrue("dateAdded" in folder, "Folder has a dateAdded");
       browser.test.assertTrue("dateGroupModified" in folder, "Folder has a dateGroupModified");
       browser.test.assertFalse("unmodifiable" in folder, "Folder is not unmodifiable"); // TODO: Do we want to enable this?
       browser.test.assertEq("folder", folder.type, "Folder has a type of folder");
 
       return browser.bookmarks.getChildren(unsortedId);
-    }).then(results => {
+    }).then(async results => {
       browser.test.assertEq(1, results.length, "The folder has one child");
       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", url: "http://example.com/"});
-      });
-    }).then(result => {
+      await browser.test.assertRejects(
+        browser.bookmarks.update(nonExistentId, {title: "new test title"}),
+        /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", url: "http://example.com/"});
+    }).then(async 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");
       browser.test.assertEq("bookmark", result.type, "Updated bookmark has a type of bookmark");
 
       browser.test.assertEq(2, collectedEvents.length, "2 expected events received");
       checkOnChanged(ourId, "http://example.com/", "new test title");
 
-      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();
-      });
+      await browser.test.assertRejects(
+        browser.bookmarks.update(ourId, {url: "this is not a valid url"}),
+        /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(bookmarkItem => bookmarkItem.id == unsortedId);
       browser.test.assertEq(
         "Other Bookmarks",
         bookmark.title,
         "Folder returned from getTree has the expected title");
       browser.test.assertEq("folder", bookmark.type,
                             "Folder returned from getTree has the expected type");
 
-      return browser.bookmarks.create({parentId: "invalid"}).then(expectedError, error => {
-        browser.test.assertTrue(
-          error.message.includes("Invalid bookmark"),
-          "Expected error thrown when trying to create a bookmark with an invalid parentId"
-        );
-        browser.test.assertTrue(
-          error.message.includes(`"parentGuid":"invalid"`),
-          "Expected error thrown when trying to create a bookmark with an invalid parentId");
-      });
+      return browser.test.assertRejects(
+        browser.bookmarks.create({parentId: "invalid"}),
+        error => error.message.includes("Invalid bookmark") && error.message.includes(`"parentGuid":"invalid"`),
+        "Expected error thrown when trying to create a bookmark with an invalid parentId");
     }).then(() => {
       return browser.bookmarks.remove(ourId);
     }).then(result => {
       browser.test.assertEq(undefined, result, "Removing a bookmark returns undefined");
 
       browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
       checkOnRemoved(ourId, bookmarkGuids.unfiledGuid, 0, "http://example.com/", "bookmark");
 
-      return browser.bookmarks.get(ourId).then(expectedError, error => {
-        browser.test.assertTrue(
-          error.message.includes("Bookmark not found"),
-          "Expected error thrown when trying to get a removed bookmark"
-        );
-      });
+      return browser.test.assertRejects(
+        browser.bookmarks.get(ourId),
+        /Bookmark not found/,
+        "Expected error thrown when trying to get a removed bookmark");
     }).then(() => {
-      return browser.bookmarks.remove(nonExistentId).then(expectedError, error => {
-        browser.test.assertTrue(
-          error.message.includes("No bookmarks found for the provided GUID"),
-          "Expected error thrown when trying removed a non-existent bookmark"
-        );
-      });
+      return browser.test.assertRejects(
+        browser.bookmarks.remove(nonExistentId),
+        /No bookmarks found for the provided GUID/,
+        "Expected error thrown when trying removed a non-existent bookmark");
     }).then(() => {
       // test bookmarks.search
       return Promise.all([
         browser.bookmarks.create({title: "MØzillä", url: "http://møzîllä.örg/"}),
         browser.bookmarks.create({title: "Example", url: "http://example.org/"}),
         browser.bookmarks.create({title: "Mozilla Folder", type: "folder"}),
         browser.bookmarks.create({title: "EFF", url: "http://eff.org/"}),
         browser.bookmarks.create({title: "Menu Item", url: "http://menu.org/", parentId: bookmarkGuids.menuGuid}),
@@ -276,28 +253,24 @@ add_task(async function test_bookmarks()
           index: 1,
         });
       }).then(result => {
         browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
         checkOnCreated(result.id, createdFolderId, 1, "About Mozilla", "http://allizom.org/about/", result.dateAdded);
 
         // returns all items on empty object
         return browser.bookmarks.search({});
-      }).then(bookmarksSearchResults => {
+      }).then(async bookmarksSearchResults => {
         browser.test.assertTrue(bookmarksSearchResults.length >= 10, "At least as many bookmarks as added were returned by search({})");
 
-        return Promise.resolve().then(() => {
-          return browser.bookmarks.remove(createdFolderId);
-        }).then(expectedError, error => {
-          browser.test.assertTrue(
-            error.message.includes("Cannot remove a non-empty folder"),
-            "Expected error thrown when trying to remove a non-empty folder"
-          );
-          return browser.bookmarks.getSubTree(createdFolderId);
-        });
+        await browser.test.assertRejects(
+          browser.bookmarks.remove(createdFolderId),
+          /Cannot remove a non-empty folder/,
+          "Expected error thrown when trying to remove a non-empty folder");
+        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");
       browser.test.assertEq(bookmarkGuids.unfiledGuid, results[0].parentId, "Folder has the expected parentId");
       browser.test.assertEq("folder", results[0].type, "Folder has the expected type");
       let children = results[0].children;
       browser.test.assertEq(5, children.length, "Expected number of bookmarks returned by getSubTree");
@@ -308,61 +281,40 @@ add_task(async function test_bookmarks()
       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("", children[3].title, "Separator has the expected title");
       browser.test.assertEq("data:", children[3].url, "Separator has the expected url");
       browser.test.assertEq("separator", children[3].type, "Separator has the expected type");
       browser.test.assertEq("Mozilla", children[4].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"
-        );
-      });
+      browser.test.assertThrows(
+        () => browser.bookmarks.search(),
+        /Incorrect argument types for bookmarks.search/,
+        "Expected error thrown when trying to search with no arguments");
 
-      Promise.resolve().then(() => {
-        return browser.bookmarks.search(null);
-      }).then(expectedError, error => {
-        browser.test.assertTrue(
-          error.message.includes("Incorrect argument types for bookmarks.search"),
-          "Expected error thrown when trying to search with null as an argument"
-        );
-      });
+      browser.test.assertThrows(
+        () => browser.bookmarks.search(null),
+        /Incorrect argument types for bookmarks.search/,
+        "Expected error thrown when trying to search with null as an argument");
 
-      Promise.resolve().then(() => {
-        return browser.bookmarks.search(function() {});
-      }).then(expectedError, error => {
-        browser.test.assertTrue(
-          error.message.includes("Incorrect argument types for bookmarks.search"),
-          "Expected error thrown when trying to search with a function as an argument"
-        );
-      });
+      browser.test.assertThrows(
+        () => browser.bookmarks.search(() => {}),
+        /Incorrect argument types for bookmarks.search/,
+        "Expected error thrown when trying to search with a function as an argument");
 
-      Promise.resolve().then(() => {
-        return browser.bookmarks.search({banana: "banana"});
-      }).then(expectedError, error => {
-        let substr = `an unexpected "banana" property`;
-        browser.test.assertTrue(
-          error.message.includes(substr),
-          `Expected error ${JSON.stringify(error.message)} to contain ${JSON.stringify(substr)}`);
-      });
+      browser.test.assertThrows(
+        () => browser.bookmarks.search({banana: "banana"}),
+        /an unexpected "banana" property/,
+        "Expected error thrown when trying to search with a banana as an argument");
 
-      Promise.resolve().then(() => {
-        return browser.bookmarks.search({url: "spider-man vs. batman"});
-      }).then(expectedError, error => {
-        let substr = 'must match the format "url"';
-        browser.test.assertTrue(
-          error.message.includes(substr),
-          `Expected error ${JSON.stringify(error.message)} to contain ${JSON.stringify(substr)}`);
-      });
-
+      browser.test.assertThrows(
+        () => browser.bookmarks.search({url: "spider-man vs. batman"}),
+        /must match the format "url"/,
+        "Expected error thrown when trying to search with a illegally formatted URL");
       // queries the full url
       return browser.bookmarks.search("http://example.org/");
     }).then(results => {
       browser.test.assertEq(1, results.length, "Expected number of results returned for url search");
       checkBookmark({title: "Example", url: "http://example.org/", index: 2}, results[0]);
 
       // queries a partial url
       return browser.bookmarks.search("example.org");
@@ -455,33 +407,25 @@ add_task(async function test_bookmarks()
         "Expected number of results returned for non-matching title and query fields"
       );
 
       // returns an empty array on item not found
       return browser.bookmarks.search("microsoft");
     }).then(results => {
       browser.test.assertEq(0, results.length, "Expected number of results returned for non-matching search");
 
-      return Promise.resolve().then(() => {
-        return browser.bookmarks.getRecent("");
-      }).then(expectedError, error => {
-        browser.test.assertTrue(
-          error.message.includes("Incorrect argument types for bookmarks.getRecent"),
-          "Expected error thrown when calling getRecent with an empty string"
-        );
-      });
+      browser.test.assertThrows(
+        () => browser.bookmarks.getRecent(""),
+        /Incorrect argument types for bookmarks.getRecent/,
+        "Expected error thrown when calling getRecent with an empty string");
     }).then(() => {
-      return Promise.resolve().then(() => {
-        return browser.bookmarks.getRecent(1.234);
-      }).then(expectedError, error => {
-        browser.test.assertTrue(
-          error.message.includes("Incorrect argument types for bookmarks.getRecent"),
-          "Expected error thrown when calling getRecent with a decimal number"
-        );
-      });
+      browser.test.assertThrows(
+        () => browser.bookmarks.getRecent(1.234),
+        /Incorrect argument types for bookmarks.getRecent/,
+        "Expected error thrown when calling getRecent with a decimal number");
     }).then(() => {
       return Promise.all([
         browser.bookmarks.search("corporation"),
         browser.bookmarks.getChildren(bookmarkGuids.menuGuid),
       ]);
     }).then(results => {
       let corporationBookmark = results[0][0];
       let childCount = results[1].length;
@@ -575,38 +519,30 @@ add_task(async function test_bookmarks()
       browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
       checkOnRemoved(createdSeparatorId, createdFolderId, 0, "data:", "separator");
 
       return browser.bookmarks.remove(createdFolderId);
     }).then(() => {
       browser.test.assertEq(1, collectedEvents.length, "1 expected events received");
       checkOnRemoved(createdFolderId, bookmarkGuids.unfiledGuid, 3);
 
-      return browser.bookmarks.get(createdFolderId).then(expectedError, error => {
-        browser.test.assertTrue(
-          error.message.includes("Bookmark not found"),
-          "Expected error thrown when trying to get a removed folder"
-        );
-      });
+      return browser.test.assertRejects(
+        browser.bookmarks.get(createdFolderId),
+        /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"
-        );
-      });
+      return browser.test.assertRejects(
+        browser.bookmarks.getChildren(nonExistentId),
+        /root is null/,
+        "Expected error thrown when trying to getChildren for a non-existent folder");
     }).then(() => {
-      return Promise.resolve().then(() => {
-        return browser.bookmarks.move(nonExistentId, {});
-      }).then(expectedError, error => {
-        browser.test.assertTrue(
-          error.message.includes("No bookmarks found for the provided GUID"),
-          "Expected error thrown when calling move with a non-existent bookmark"
-        );
-      });
+      return browser.test.assertRejects(
+        browser.bookmarks.move(nonExistentId, {}),
+        /No bookmarks found for the provided GUID/,
+        "Expected error thrown when calling move with a non-existent bookmark");
     }).then(() => {
       return browser.test.assertRejects(
         browser.bookmarks.create({title: "test root folder", parentId: bookmarkGuids.rootGuid}),
         "The bookmark root cannot be modified",
         "Expected error thrown when creating bookmark folder at the root"
       );
     }).then(() => {
       return browser.test.assertRejects(