Bug 1448081 - Replaced tests using updatePlaces with PlaceTestUtils.addVisits. r=Standard8

PlacesTestUtils.addVisits provides a better api for adding history visits to the database for testing.

MozReview-Commit-ID: 3VIeWz59ErM
This commit is contained in:
sreeise 2018-04-20 11:00:17 +01:00 committed by Ed Lee
parent 870a86ad8e
commit 470642124d
5 changed files with 141 additions and 172 deletions

View File

@ -1,5 +1,8 @@
requestLongerTimeout(2);
const {PlacesTestUtils} =
ChromeUtils.import("resource://testing-common/PlacesTestUtils.jsm", {});
// Bug 453440 - Test the timespan-based logic of the sanitizer code
var now_mSec = Date.now();
var now_uSec = now_mSec * 1000;
@ -426,46 +429,37 @@ async function onHistoryReady() {
ok(!(await downloadExists(publicList, "fakefile-old")), "Year old download should now be deleted");
}
function setupHistory() {
return new Promise(resolve => {
async function setupHistory() {
let places = [];
let places = [];
function addPlace(aURI, aTitle, aVisitDate) {
places.push({
uri: aURI,
title: aTitle,
visits: [{
visitDate: aVisitDate,
transitionType: Ci.nsINavHistoryService.TRANSITION_LINK
}]
});
}
addPlace(makeURI("http://10minutes.com/"), "10 minutes ago", now_uSec - 10 * kUsecPerMin);
addPlace(makeURI("http://1hour.com/"), "Less than 1 hour ago", now_uSec - 45 * kUsecPerMin);
addPlace(makeURI("http://1hour10minutes.com/"), "1 hour 10 minutes ago", now_uSec - 70 * kUsecPerMin);
addPlace(makeURI("http://2hour.com/"), "Less than 2 hours ago", now_uSec - 90 * kUsecPerMin);
addPlace(makeURI("http://2hour10minutes.com/"), "2 hours 10 minutes ago", now_uSec - 130 * kUsecPerMin);
addPlace(makeURI("http://4hour.com/"), "Less than 4 hours ago", now_uSec - 180 * kUsecPerMin);
addPlace(makeURI("http://4hour10minutes.com/"), "4 hours 10 minutesago", now_uSec - 250 * kUsecPerMin);
let today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(1);
addPlace(makeURI("http://today.com/"), "Today", today.getTime() * 1000);
let lastYear = new Date();
lastYear.setFullYear(lastYear.getFullYear() - 1);
addPlace(makeURI("http://before-today.com/"), "Before Today", lastYear.getTime() * 1000);
PlacesUtils.asyncHistory.updatePlaces(places, {
handleError: () => ok(false, "Unexpected error in adding visit."),
handleResult: () => { },
handleCompletion: () => resolve()
function addPlace(aURI, aTitle, aVisitDate) {
places.push({
uri: aURI,
title: aTitle,
visitDate: aVisitDate,
transition: Ci.nsINavHistoryService.TRANSITION_LINK
});
}
});
addPlace("http://10minutes.com/", "10 minutes ago", now_uSec - 10 * kUsecPerMin);
addPlace("http://1hour.com/", "Less than 1 hour ago", now_uSec - 45 * kUsecPerMin);
addPlace("http://1hour10minutes.com/", "1 hour 10 minutes ago", now_uSec - 70 * kUsecPerMin);
addPlace("http://2hour.com/", "Less than 2 hours ago", now_uSec - 90 * kUsecPerMin);
addPlace("http://2hour10minutes.com/", "2 hours 10 minutes ago", now_uSec - 130 * kUsecPerMin);
addPlace("http://4hour.com/", "Less than 4 hours ago", now_uSec - 180 * kUsecPerMin);
addPlace("http://4hour10minutes.com/", "4 hours 10 minutesago", now_uSec - 250 * kUsecPerMin);
let today = new Date();
today.setHours(0);
today.setMinutes(0);
today.setSeconds(1);
addPlace("http://today.com/", "Today", today.getTime() * 1000);
let lastYear = new Date();
lastYear.setFullYear(lastYear.getFullYear() - 1);
addPlace("http://before-today.com/", "Before Today", lastYear.getTime() * 1000);
await PlacesTestUtils.addVisits(places);
}
async function setupFormHistory() {

View File

@ -10,59 +10,42 @@
* are shown in it.
*/
var now = Date.now();
add_task(async function test() {
// Add visits.
await PlacesTestUtils.addVisits([{
uri: "http://mozilla.org",
transition: PlacesUtils.history.TRANSITION_TYPED
}, {
uri: "http://google.com",
transition: PlacesUtils.history.TRANSITION_DOWNLOAD
}, {
uri: "http://en.wikipedia.org",
transition: PlacesUtils.history.TRANSITION_TYPED
}, {
uri: "http://ubuntu.org",
transition: PlacesUtils.history.TRANSITION_DOWNLOAD
}]);
function test() {
waitForExplicitFinish();
let library = await promiseLibrary("Downloads");
let onLibraryReady = function(win) {
// Add visits to compare contents with.
let places = [
{ uri: NetUtil.newURI("http://mozilla.com"),
visits: [ new VisitInfo(PlacesUtils.history.TRANSITION_TYPED) ]
},
{ uri: NetUtil.newURI("http://google.com"),
visits: [ new VisitInfo(PlacesUtils.history.TRANSITION_DOWNLOAD) ]
},
{ uri: NetUtil.newURI("http://en.wikipedia.org"),
visits: [ new VisitInfo(PlacesUtils.history.TRANSITION_TYPED) ]
},
{ uri: NetUtil.newURI("http://ubuntu.org"),
visits: [ new VisitInfo(PlacesUtils.history.TRANSITION_DOWNLOAD) ]
},
];
PlacesUtils.asyncHistory.updatePlaces(places, {
handleResult() {},
handleError() {
ok(false, "gHistory.updatePlaces() failed");
},
handleCompletion() {
// Make sure Downloads is present.
isnot(win.PlacesOrganizer._places.selectedNode, null,
"Downloads is present and selected");
registerCleanupFunction(async () => {
await library.close();
await PlacesUtils.history.clear();
});
// Make sure Downloads is present.
Assert.notEqual(library.PlacesOrganizer._places.selectedNode, null,
"Downloads is present and selected");
// Check results.
let testURIs = ["http://ubuntu.org/", "http://google.com/"];
for (let element of win.ContentArea.currentView
.associatedElement.children) {
is(element._shell.download.source.url, testURIs.shift(),
"URI matches");
}
// Check results.
let testURIs = ["http://ubuntu.org/", "http://google.com/"];
win.close();
PlacesUtils.history.clear().then(finish);
}
});
};
await BrowserTestUtils.waitForCondition(() =>
library.ContentArea.currentView.associatedElement.children.length == testURIs.length);
openLibrary(onLibraryReady, "Downloads");
}
function VisitInfo(aTransitionType) {
this.transitionType =
aTransitionType === undefined ?
PlacesUtils.history.TRANSITION_LINK : aTransitionType;
this.visitDate = now++ * 1000;
}
VisitInfo.prototype = {};
for (let element of library.ContentArea.currentView
.associatedElement.children) {
Assert.equal(element._shell.download.source.url, testURIs.shift(),
"URI matches");
}
});

View File

@ -9,25 +9,23 @@ var visit_count = 0;
// Returns the Place ID corresponding to an added visit.
async function task_add_visit(aURI, aVisitType) {
// Add the visit asynchronously, and save its visit ID.
let deferUpdatePlaces = new Promise((resolve, reject) => {
PlacesUtils.asyncHistory.updatePlaces({
uri: aURI,
visits: [{ transitionType: aVisitType, visitDate: Date.now() * 1000 }]
}, {
handleError: function TAV_handleError() {
reject(new Error("Unexpected error in adding visit."));
},
handleResult(aPlaceInfo) {
this.visitId = aPlaceInfo.visits[0].visitId;
},
handleCompletion: function TAV_handleCompletion() {
resolve(this.visitId);
}
});
});
// Wait for a visits notification and get the visitId.
let visitId;
let visitsPromise = PlacesTestUtils.waitForNotification("onVisits", visits => {
visitId = visits[0].visitId;
let {uri} = visits[0];
return uri.equals(aURI);
}, "history");
let visitId = await deferUpdatePlaces;
// Add visits.
await PlacesTestUtils.addVisits([{
uri: aURI,
transition: aVisitType
}]);
if (aVisitType != TRANSITION_EMBED) {
await visitsPromise;
}
// Increase visit_count if applicable
if (aVisitType != 0 &&

View File

@ -154,34 +154,41 @@ add_task(async function test_dh_addBookmarkRemoveDownload() {
});
});
add_test(function test_dh_addDownload_referrer() {
waitForOnVisit(function DHAD_prepareReferrer(aURI, aVisitID) {
Assert.ok(aURI.equals(REFERRER_URI));
let referrerVisitId = aVisitID;
add_task(async function test_dh_addDownload_referrer() {
// Wait for visits notification and get the visit id.
let visitId;
let referrerPromise = PlacesTestUtils.waitForNotification("onVisits", visits => {
visitId = visits[0].visitId;
let {uri} = visits[0];
return uri.equals(REFERRER_URI);
}, "history");
waitForOnVisit(function DHAD_onVisit(aVisitedURI, unused, unused2, unused3,
aReferringID) {
Assert.ok(aVisitedURI.equals(DOWNLOAD_URI));
Assert.equal(aReferringID, referrerVisitId);
// Verify that the URI is already available in results at this time.
Assert.ok(!!page_in_database(DOWNLOAD_URI));
PlacesUtils.history.clear().then(run_next_test);
});
gDownloadHistory.addDownload(DOWNLOAD_URI, REFERRER_URI, Date.now() * 1000);
});
// Note that we don't pass the optional callback argument here because we must
// ensure that we receive the onVisits notification before we call addDownload.
PlacesUtils.asyncHistory.updatePlaces({
await PlacesTestUtils.addVisits([{
uri: REFERRER_URI,
visits: [{
transitionType: Ci.nsINavHistoryService.TRANSITION_TYPED,
visitDate: Date.now() * 1000
}]
});
transition: Ci.nsINavHistoryService.TRANSITION_TYPED
}]);
await referrerPromise;
// Verify results for referrer uri.
Assert.ok(!!PlacesTestUtils.isPageInDB(REFERRER_URI));
Assert.equal(visitId, 1);
// Wait for visits notification and get the referrer Id.
let referrerId;
let downloadPromise = PlacesTestUtils.waitForNotification("onVisits", visits => {
referrerId = visits[0].referrerId;
let {uri} = visits[0];
return uri.equals(DOWNLOAD_URI);
}, "history");
gDownloadHistory.addDownload(DOWNLOAD_URI, REFERRER_URI, Date.now() * 1000);
await downloadPromise;
// Verify results for download uri.
Assert.ok(!!PlacesTestUtils.isPageInDB(DOWNLOAD_URI));
Assert.equal(visitId, referrerId);
await PlacesUtils.history.clear();
});
add_test(function test_dh_addDownload_disabledHistory() {

View File

@ -23,59 +23,46 @@ const SCHEMES = {
"javascript:": false,
};
var gRunner;
function run_test() {
do_test_pending();
gRunner = step();
gRunner.next();
}
function* step() {
add_task(async function test_isURIVisited() {
let history = Cc["@mozilla.org/browser/history;1"]
.getService(Ci.mozIAsyncHistory);
function visitsPromise(uri) {
return new Promise(resolve => {
history.isURIVisited(uri, (receivedURI, visited) => {
resolve([receivedURI, visited]);
});
});
}
for (let scheme in SCHEMES) {
info("Testing scheme " + scheme);
for (let t in PlacesUtils.history.TRANSITIONS) {
info("With transition " + t);
let transition = PlacesUtils.history.TRANSITIONS[t];
let aTransition = PlacesUtils.history.TRANSITIONS[t];
let uri = NetUtil.newURI(scheme + "mozilla.org/");
let aURI = Services.io.newURI(scheme + "mozilla.org/");
history.isURIVisited(uri, function(aURI, aIsVisited) {
Assert.ok(uri.equals(aURI));
Assert.ok(!aIsVisited);
let [receivedURI1, visited1] = await visitsPromise(aURI);
Assert.ok(aURI.equals(receivedURI1));
Assert.ok(!visited1);
let callback = {
handleError() {},
handleResult() {},
handleCompletion() {
info("Added visit to " + uri.spec);
if (PlacesUtils.history.canAddURI(aURI)) {
await PlacesTestUtils.addVisits([{
uri: aURI,
transition: aTransition
}]);
info("Added visit for " + aURI.spec);
}
history.isURIVisited(uri, function(aURI2, aIsVisited2) {
Assert.ok(uri.equals(aURI2));
Assert.ok(SCHEMES[scheme] ? aIsVisited2 : !aIsVisited2);
let [receivedURI2, visited2] = await visitsPromise(aURI);
Assert.ok(aURI.equals(receivedURI2));
Assert.equal(SCHEMES[scheme], visited2);
PlacesUtils.history.clear().then(function() {
history.isURIVisited(uri, function(aURI3, aIsVisited3) {
Assert.ok(uri.equals(aURI3));
Assert.ok(!aIsVisited3);
gRunner.next();
});
});
});
},
};
history.updatePlaces({ uri,
visits: [ { transitionType: transition,
visitDate: Date.now() * 1000
} ]
}, callback);
});
yield undefined;
await PlacesUtils.history.clear();
let [receivedURI3, visited3] = await visitsPromise(aURI);
Assert.ok(aURI.equals(receivedURI3));
Assert.ok(!visited3);
}
}
do_test_finished();
}
});