Bug 1448064 - (part 2) Avoid using updatePlaces in sync's test_history_engine.js r?kitcambridge
MozReview-Commit-ID: C8X00W4ZA2N
--- 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.