Bug 1267517 - Allow for sharing of Places test utils that are currently in head_common.js, r?mak draft
authorBob Silverberg <bsilverberg@mozilla.com>
Wed, 27 Apr 2016 13:11:04 -0400
changeset 356939 9496419166c1f105c46936f92492ea7bb1003b70
parent 356126 352d365eb4eda3aab7c3680b0e80ce17e178b5ee
child 356959 e99fd03186e738fd669c43b6c064cfd4fb0dd3cc
child 357280 e033a372cdfe4e83ba2577f71c7790174eca89ad
child 357283 47b9ef4da37be2f2e471eb97ad552defc211cfa7
child 357284 c0e671a84bc74efd3a310a987cbafe7f9cf98ca7
push id16651
push userbmo:bob.silverberg@gmail.com
push dateWed, 27 Apr 2016 17:12:38 +0000
reviewersmak
bugs1267517
milestone48.0a1
Bug 1267517 - Allow for sharing of Places test utils that are currently in head_common.js, r?mak MozReview-Commit-ID: E3Kb0m3vJDT
toolkit/components/places/tests/PlacesTestUtils.jsm
toolkit/components/places/tests/unit/test_isPageInDB.js
toolkit/components/places/tests/unit/test_visitsInDB.js
toolkit/components/places/tests/unit/xpcshell.ini
--- 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]