Bug 1448064 - (part 2) Avoid using updatePlaces in sync's test_history_engine.js r?kitcambridge draft
authorThom Chiovoloni <tchiovoloni@mozilla.com>
Tue, 27 Mar 2018 15:02:11 -0700
changeset 773413 a7bab57ec5264ec9242ad048d80aee76ee5360ab
parent 773412 bfa1fc59a4f7e230790ae4c4d72bf656c5cdca98
push id104227
push userbmo:tchiovoloni@mozilla.com
push dateTue, 27 Mar 2018 22:04:31 +0000
reviewerskitcambridge
bugs1448064
milestone61.0a1
Bug 1448064 - (part 2) Avoid using updatePlaces in sync's test_history_engine.js r?kitcambridge MozReview-Commit-ID: C8X00W4ZA2N
services/sync/tests/unit/test_history_engine.js
--- a/services/sync/tests/unit/test_history_engine.js
+++ b/services/sync/tests/unit/test_history_engine.js
@@ -1,34 +1,39 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 ChromeUtils.import("resource://services-sync/service.js");
 ChromeUtils.import("resource://services-sync/engines/history.js");
 ChromeUtils.import("resource://services-common/utils.js");
 
-async function rawAddVisit(id, uri, visitPRTime, transitionType) {
-  return new Promise((resolve, reject) => {
-    let results = [];
-    let handler = {
-      handleResult(result) {
-        results.push(result);
-      },
-      handleError(resultCode, placeInfo) {
-        do_throw(`updatePlaces gave error ${resultCode}!`);
-      },
-      handleCompletion(count) {
-        resolve({ results, count });
-      }
-    };
-    PlacesUtils.asyncHistory.updatePlaces([{
-      guid: id,
-      uri: typeof uri == "string" ? CommonUtils.makeURI(uri) : uri,
-      visits: [{ visitDate: visitPRTime, transitionType }]
-    }], handler);
+// Unlike insert/insertMany, this allows setting the visit date with microsecond
+// precision.
+async function rawAddVisit(guid, uri, visitPRTime, transitionType) {
+  let roundedDate = PlacesUtils.toDate(visitPRTime);
+  // Insert the visit with a rounded date.
+  await PlacesUtils.history.insert({
+    guid,
+    url: CommonUtils.makeURI(uri),
+    visits: [{
+      transition: transitionType,
+      date: roundedDate
+    }]
+  });
+  // Update the visit we just added to have the date with microsecond precision.
+  await PlacesUtils.withConnectionWrapper("test_history_engine.rawAddVisit", async function(db) {
+    let rows = await db.execute(`SELECT id FROM moz_places WHERE guid = :guid`, { guid });
+    equal(rows.length, 1);
+    let placeId = rows[0].getResultByName("id");
+
+    await db.execute(
+      `UPDATE moz_historyvisits SET visit_date = :visitPRTime
+       WHERE place_id = :placeId
+         AND visit_date = :roundedVisitDate`,
+      { placeId, visitPRTime, roundedVisitDate: roundedDate.getTime() * 1000 });
   });
 }
 
 
 add_task(async function test_history_download_limit() {
   let engine = new HistoryEngine(Service);
   await engine.initialize();
 
@@ -143,19 +148,18 @@ add_task(async function test_history_vis
 
   let id = "aaaaaaaaaaaa";
   let oneHourMS = 60 * 60 * 1000;
   // Insert a visit with a non-round microsecond timestamp (e.g. it's not evenly
   // divisible by 1000). This will typically be the case for visits that occur
   // during normal navigation.
   let time = (Date.now() - oneHourMS) * 1000 + 555;
   // We use the low level updatePlaces api since it lets us provide microseconds
-  let {count} = await rawAddVisit(id, "https://www.example.com", time,
-                                  PlacesUtils.history.TRANSITIONS.TYPED);
-  equal(count, 1);
+  await rawAddVisit(id, "https://www.example.com", time,
+                    PlacesUtils.history.TRANSITIONS.TYPED);
   // Check that it was inserted and that we didn't round on the insert.
   let visits = await PlacesSyncUtils.history.fetchVisitsForURL("https://www.example.com");
   equal(visits.length, 1);
   equal(visits[0].date, time);
 
   let collection = server.user("foo").collection("history");
 
   // Sync the visit up to the server.