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
--- 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();
+});