Bug 1366231 - Implement History.hasVisits as a wrapper of asyncHistory.isURIVisited; r?mak draft
authormeetshah <meet1995@gmail.com>
Mon, 29 May 2017 23:41:56 +0530
changeset 590342 bb466ce338f5f1421704c96b3d10928abab3bbd4
parent 586049 34ac1a5d6576d6775491c8a882710a1520551da6
child 632198 8fb0ca9d6ba014fffcafc7e3d27732a7e03ddf0d
push id62714
push userbmo:meet1995@gmail.com
push dateWed, 07 Jun 2017 17:49:18 +0000
reviewersmak
bugs1366231
milestone55.0a1
Bug 1366231 - Implement History.hasVisits as a wrapper of asyncHistory.isURIVisited; r?mak Tests for the wrapper implementation added in test_hasVisits.js in toolkit/components/places/tests/history. MozReview-Commit-ID: KEW2bDTSWVY
toolkit/components/places/History.jsm
toolkit/components/places/tests/history/test_hasVisits.js
--- a/toolkit/components/places/History.jsm
+++ b/toolkit/components/places/History.jsm
@@ -509,31 +509,34 @@ this.History = Object.freeze({
       "History.jsm: removeByFilter",
       db => removeByFilter(db, filter, onResult)
     );
   },
 
   /**
    * Determine if a page has been visited.
    *
-   * @param pages: (URL or nsIURI)
-   *      The full URI of the page.
-   *            or (string)
-   *      The full URI of the page or the GUID of the page.
-   *
+   * @param guidOrURI: (string) or (URL, nsIURI or href)
+   *      Either the full URI of the page or the GUID of the page.
    * @return (Promise)
    *      A promise resolved once the operation is complete.
    * @resolve (bool)
    *      `true` if the page has been visited, `false` otherwise.
    * @throws (Error)
-   *      If `pages` has an unexpected type or if a string provided
+   *      If `guidOrURI` has an unexpected type or if a string provided
    *      is neither not a valid GUID nor a valid URI.
    */
-  hasVisits(page, onResult) {
-    throw new Error("Method not implemented");
+  hasVisits(guidOrURI) {
+    guidOrURI = PlacesUtils.normalizeToURLOrGUID(guidOrURI);
+
+    return new Promise(resolve => {
+      PlacesUtils.asyncHistory.isURIVisited(guidOrURI, (aURI, aIsVisited) => {
+        resolve(aIsVisited);
+      });
+    });
   },
 
   /**
    * Clear all history.
    *
    * @return (Promise)
    *      A promise resolved once the operation is complete.
    */
new file mode 100644
--- /dev/null
+++ b/toolkit/components/places/tests/history/test_hasVisits.js
@@ -0,0 +1,36 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+// Tests for `History.hasVisits` as implemented in History.jsm
+
+"use strict";
+
+add_task(async function test_has_visits_error_cases() {
+  Assert.throws(
+    () => PlacesUtils.history.hasVisits(),
+    /TypeError: Invalid url or guid: undefined/,
+    "passing a null into History.hasVisits should throw a TypeError"
+  );
+  Assert.throws(
+    () => PlacesUtils.history.hasVisits(1),
+    /TypeError: Invalid url or guid: 1/,
+    "passing an invalid url into History.hasVisits should throw a TypeError"
+  );
+  Assert.throws(
+    () => PlacesUtils.history.hasVisits({}),
+    /TypeError: Invalid url or guid: [object Object]/,
+    `passing an invalid (not of type URI or nsIURI) object to History.hasVisits
+     should throw a TypeError`
+  );
+});
+
+add_task(async function test_history_has_visits() {
+  const TEST_URL = "http://mozilla.com/";
+  await PlacesTestUtils.clearHistory();
+  Assert.equal(await PlacesUtils.history.hasVisits(TEST_URL), false,
+               "Test Url should not be in history.");
+  await PlacesTestUtils.addVisits(TEST_URL);
+  Assert.equal(await PlacesUtils.history.hasVisits(TEST_URL), true,
+               "Test Url should be in history.");
+  await PlacesTestUtils.clearHistory();
+});