Bug 1267517 - Allow for sharing of Places test utils that are currently in head_common.js, r?mak
MozReview-Commit-ID: E3Kb0m3vJDT
--- a/toolkit/components/places/tests/PlacesTestUtils.jsm
+++ b/toolkit/components/places/tests/PlacesTestUtils.jsm
@@ -132,11 +132,48 @@ this.PlacesTestUtils = Object.freeze({
handleError: function () {},
handleCompletion: function(aReason)
{
resolve();
}
});
commit.finalize();
});
- }
+ },
+
+ /**
+ * Asynchronously checks if an address is found in the database.
+ * @param aURI
+ * nsIURI or address to look for.
+ *
+ * @return {Promise}
+ * @resolves Returns true if the page is found.
+ * @rejects JavaScript exception.
+ */
+ isPageInDB: Task.async(function* (aURI) {
+ let url = aURI instanceof Ci.nsIURI ? aURI.spec : aURI;
+ let db = yield PlacesUtils.promiseDBConnection();
+ let rows = yield db.executeCached(
+ "SELECT id FROM moz_places WHERE url = :url",
+ { url });
+ return rows.length > 0;
+ }),
+ /**
+ * Asynchronously checks how many visits exist for a specified page.
+ * @param aURI
+ * nsIURI or address to look for.
+ *
+ * @return {Promise}
+ * @resolves Returns the number of visits found.
+ * @rejects JavaScript exception.
+ */
+ visitsInDB: Task.async(function* (aURI) {
+ let url = aURI instanceof Ci.nsIURI ? aURI.spec : aURI;
+ let db = yield PlacesUtils.promiseDBConnection();
+ let rows = yield db.executeCached(
+ `SELECT count(*) FROM moz_historyvisits v
+ JOIN moz_places h ON h.id = v.place_id
+ WHERE url = :url`,
+ { url });
+ return rows[0].getResultByIndex(0);
+ })
});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/places/tests/unit/test_isPageInDB.js
@@ -0,0 +1,10 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+
+add_task(function* test_execute() {
+ var good_uri = uri("http://mozilla.com");
+ var bad_uri = uri("http://google.com");
+ yield PlacesTestUtils.addVisits({uri: good_uri});
+ do_check_true(yield PlacesTestUtils.isPageInDB(good_uri));
+ do_check_false(yield PlacesTestUtils.isPageInDB(bad_uri));
+});
new file mode 100644
--- /dev/null
+++ b/toolkit/components/places/tests/unit/test_visitsInDB.js
@@ -0,0 +1,12 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim:set ts=2 sw=2 sts=2 et: */
+
+add_task(function* test_execute() {
+ const TEST_URI = uri("http://mozilla.com");
+
+ do_check_eq(0, yield PlacesTestUtils.visitsInDB(TEST_URI));
+ yield PlacesTestUtils.addVisits({uri: TEST_URI});
+ do_check_eq(1, yield PlacesTestUtils.visitsInDB(TEST_URI));
+ yield PlacesTestUtils.addVisits({uri: TEST_URI});
+ do_check_eq(2, yield PlacesTestUtils.visitsInDB(TEST_URI));
+});
--- a/toolkit/components/places/tests/unit/xpcshell.ini
+++ b/toolkit/components/places/tests/unit/xpcshell.ini
@@ -98,16 +98,17 @@ skip-if = os == "android"
[test_history_catobs.js]
[test_history_clear.js]
[test_history_notifications.js]
[test_history_observer.js]
# Bug 676989: test hangs consistently on Android
skip-if = os == "android"
[test_history_sidebar.js]
[test_hosts_triggers.js]
+[test_isPageInDB.js]
[test_isURIVisited.js]
[test_isvisited.js]
[test_keywords.js]
[test_lastModified.js]
[test_markpageas.js]
[test_mozIAsyncLivemarks.js]
[test_multi_queries.js]
# Bug 676989: test fails consistently on Android
@@ -144,8 +145,9 @@ skip-if = os == "android"
[test_tagging.js]
[test_telemetry.js]
[test_update_frecency_after_delete.js]
# Bug 676989: test hangs consistently on Android
skip-if = os == "android"
[test_utils_backups_create.js]
[test_utils_getURLsForContainerNode.js]
[test_utils_setAnnotationsFor.js]
+[test_visitsInDB.js]