Bug 820763 - Stop using addvisit() in toolkit tests. r=mak

This commit is contained in:
Raymond Lee 2013-01-18 10:13:09 +08:00
parent 4681b29bd2
commit c089ae192d
4 changed files with 191 additions and 58 deletions

View File

@ -14,6 +14,9 @@ do_get_profile();
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://testing-common/httpd.js");
Cu.import("resource://gre/modules/PlacesUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/commonjs/promise/core.js");
var downloadUtils = { };
XPCOMUtils.defineLazyServiceGetter(downloadUtils,
@ -158,6 +161,70 @@ function getDownloadListener()
};
}
/**
* Asynchronously adds visits to a page.
*
* @param aPlaceInfo
* Can be an nsIURI, in such a case a single LINK visit will be added.
* Otherwise can be an object describing the visit to add, or an array
* of these objects:
* { uri: nsIURI of the page,
* transition: one of the TRANSITION_* from nsINavHistoryService,
* [optional] title: title of the page,
* [optional] visitDate: visit date in microseconds from the epoch
* [optional] referrer: nsIURI of the referrer for this visit
* }
*
* @return {Promise}
* @resolves When all visits have been added successfully.
* @rejects JavaScript exception.
*/
function promiseAddVisits(aPlaceInfo)
{
let deferred = Promise.defer();
let places = [];
if (aPlaceInfo instanceof Ci.nsIURI) {
places.push({ uri: aPlaceInfo });
}
else if (Array.isArray(aPlaceInfo)) {
places = places.concat(aPlaceInfo);
} else {
places.push(aPlaceInfo)
}
// Create mozIVisitInfo for each entry.
let now = Date.now();
for (let i = 0; i < places.length; i++) {
if (!places[i].title) {
places[i].title = "test visit for " + places[i].uri.spec;
}
places[i].visits = [{
transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK
: places[i].transition,
visitDate: places[i].visitDate || (now++) * 1000,
referrerURI: places[i].referrer
}];
}
PlacesUtils.asyncHistory.updatePlaces(
places,
{
handleError: function handleError(aResultCode, aPlaceInfo) {
let ex = new Components.Exception("Unexpected error in adding visits.",
aResultCode);
deferred.reject(ex);
},
handleResult: function () {},
handleCompletion: function handleCompletion() {
deferred.resolve();
}
}
);
return deferred.promise;
}
XPCOMUtils.defineLazyGetter(this, "Services", function() {
Cu.import("resource://gre/modules/Services.jsm");
return Services;

View File

@ -25,6 +25,11 @@ function getExpirablePRTime() {
}
function run_test()
{
run_next_test();
}
add_task(function test_execute()
{
// Like the code, we check to see if nav-history-service exists
// (i.e MOZ_PLACES is enabled), so that we don't run this test if it doesn't.
@ -79,8 +84,8 @@ function run_test()
// Add an expirable visit to this download.
let histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
getService(Ci.nsINavHistoryService);
histsvc.addVisit(theURI, getExpirablePRTime(), null,
histsvc.TRANSITION_DOWNLOAD, false, 0);
yield promiseAddVisits({uri: theURI, visitDate: getExpirablePRTime(),
transition: histsvc.TRANSITION_DOWNLOAD});
// Get the download manager as history observer and batch expirations
let histobs = dm.QueryInterface(Ci.nsINavHistoryObserver);
@ -116,4 +121,5 @@ function run_test()
// Expiration happens on a timeout, about 3.5s after we set the pref
do_test_pending();
}
});

View File

@ -68,18 +68,19 @@ function test() {
is(PlacesUtils.history.hasHistoryEntries, false,
"History database should be empty");
// Create a handful of history items with various visit types
fillHistoryVisitedURI(window);
placeItemsCount += 7;
// History database should have entries
is(PlacesUtils.history.hasHistoryEntries, true,
"History database should have entries");
// We added 7 new items to history.
is(getPlacesItemsCount(window), placeItemsCount,
"Check the total items count");
// Test on windows.
testOnWindow(false, function() {
testOnWindow(true, function() {
testOnWindow(false, finish);
fillHistoryVisitedURI(window, function() {
placeItemsCount += 7;
// History database should have entries
is(PlacesUtils.history.hasHistoryEntries, true,
"History database should have entries");
// We added 7 new items to history.
is(getPlacesItemsCount(window), placeItemsCount,
"Check the total items count");
// Test on windows.
testOnWindow(false, function() {
testOnWindow(true, function() {
testOnWindow(false, finish);
});
});
});
});
@ -127,24 +128,17 @@ function getPlacesItemsCount(aWin){
return cc;
}
function addVisit(aWin, aURI, aType) {
aWin.PlacesUtils.history.addVisit(
NetUtil.newURI(aURI), Date.now() * 1000, null, aType, false, 0);
}
function fillHistoryVisitedURI(aWin) {
aWin.PlacesUtils.history.runInBatchMode({
runBatched: function (aUserData) {
addVisit(aWin, visitedURIs[0], PlacesUtils.history.TRANSITION_LINK);
addVisit(aWin, visitedURIs[1], PlacesUtils.history.TRANSITION_TYPED);
addVisit(aWin, visitedURIs[2], PlacesUtils.history.TRANSITION_BOOKMARK);
addVisit(aWin, visitedURIs[3], PlacesUtils.history.TRANSITION_REDIRECT_PERMANENT);
addVisit(aWin, visitedURIs[4], PlacesUtils.history.TRANSITION_REDIRECT_TEMPORARY);
addVisit(aWin, visitedURIs[5], PlacesUtils.history.TRANSITION_EMBED);
addVisit(aWin, visitedURIs[6], PlacesUtils.history.TRANSITION_FRAMED_LINK);
addVisit(aWin, visitedURIs[7], PlacesUtils.history.TRANSITION_DOWNLOAD);
}
}, null);
function fillHistoryVisitedURI(aWin, aCallback) {
addVisits([
{uri: NetUtil.newURI(visitedURIs[0]), transition: PlacesUtils.history.TRANSITION_LINK},
{uri: NetUtil.newURI(visitedURIs[1]), transition: PlacesUtils.history.TRANSITION_TYPED},
{uri: NetUtil.newURI(visitedURIs[2]), transition: PlacesUtils.history.TRANSITION_BOOKMARK},
{uri: NetUtil.newURI(visitedURIs[3]), transition: PlacesUtils.history.TRANSITION_REDIRECT_PERMANENT},
{uri: NetUtil.newURI(visitedURIs[4]), transition: PlacesUtils.history.TRANSITION_REDIRECT_TEMPORARY},
{uri: NetUtil.newURI(visitedURIs[5]), transition: PlacesUtils.history.TRANSITION_EMBED},
{uri: NetUtil.newURI(visitedURIs[6]), transition: PlacesUtils.history.TRANSITION_FRAMED_LINK},
{uri: NetUtil.newURI(visitedURIs[7]), transition: PlacesUtils.history.TRANSITION_DOWNLOAD}],
aWin, aCallback);
}
function checkHistoryItems(aWin) {

View File

@ -12,10 +12,14 @@
////////////////////////////////////////////////////////////////////////////////
//// Globals
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/PlacesUtils.jsm");
Cu.import("resource://gre/modules/ForgetAboutSite.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/commonjs/promise/core.js");
const COOKIE_EXPIRY = Math.round(Date.now() / 1000) + 60;
const COOKIE_NAME = "testcookie";
const COOKIE_PATH = "/";
@ -48,32 +52,87 @@ function uri(aURIString)
}
/**
* Adds a visit to history.
* Asynchronously adds visits to a page.
*
* @param aURI
* The URI to add.
* @param aPlaceInfo
* Can be an nsIURI, in such a case a single LINK visit will be added.
* Otherwise can be an object describing the visit to add, or an array
* of these objects:
* { uri: nsIURI of the page,
* transition: one of the TRANSITION_* from nsINavHistoryService,
* [optional] title: title of the page,
* [optional] visitDate: visit date in microseconds from the epoch
* [optional] referrer: nsIURI of the referrer for this visit
* }
*
* @return {Promise}
* @resolves When all visits have been added successfully.
* @rejects JavaScript exception.
*/
function add_visit(aURI)
function promiseAddVisits(aPlaceInfo)
{
check_visited(aURI, false);
PlacesUtils.history.addVisit(aURI, Date.now() * 1000, null,
Ci.nsINavHistoryService.TRANSITION_LINK, false,
0);
check_visited(aURI, true);
let deferred = Promise.defer();
let places = [];
if (aPlaceInfo instanceof Ci.nsIURI) {
places.push({ uri: aPlaceInfo });
}
else if (Array.isArray(aPlaceInfo)) {
places = places.concat(aPlaceInfo);
} else {
places.push(aPlaceInfo)
}
// Create mozIVisitInfo for each entry.
let now = Date.now();
for (let i = 0; i < places.length; i++) {
if (!places[i].title) {
places[i].title = "test visit for " + places[i].uri.spec;
}
places[i].visits = [{
transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK
: places[i].transition,
visitDate: places[i].visitDate || (now++) * 1000,
referrerURI: places[i].referrer
}];
}
PlacesUtils.asyncHistory.updatePlaces(
places,
{
handleError: function handleError(aResultCode, aPlaceInfo) {
let ex = new Components.Exception("Unexpected error in adding visits.",
aResultCode);
deferred.reject(ex);
},
handleResult: function () {},
handleCompletion: function handleCompletion() {
deferred.resolve();
}
}
);
return deferred.promise;
}
/**
* Checks to ensure a URI string is visited or not.
* Asynchronously check a url is visited.
*
* @param aURI
* The URI to check.
* @param aIsVisited
* True if the URI should be visited, false otherwise.
* The URI.
*
* @return {Promise}
* @resolves When the check has been added successfully.
* @rejects JavaScript exception.
*/
function check_visited(aURI, aIsVisited)
function promiseIsURIVisited(aURI)
{
let checker = aIsVisited ? do_check_true : do_check_false;
checker(PlacesUtils.ghistory2.isVisited(aURI));
let deferred = Promise.defer();
PlacesUtils.asyncHistory.isURIVisited(aURI, function(aURI, aIsVisited) {
deferred.resolve(aIsVisited);
});
return deferred.promise;
}
/**
@ -323,25 +382,31 @@ function check_preference_exists(aURI, aExists)
function test_history_cleared_with_direct_match()
{
const TEST_URI = uri("http://mozilla.org/foo");
add_visit(TEST_URI);
do_check_false(yield promiseIsURIVisited(TEST_URI));
yield promiseAddVisits(TEST_URI);
do_check_true(yield promiseIsURIVisited(TEST_URI));
ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_visited(TEST_URI, false);
do_check_false(yield promiseIsURIVisited(TEST_URI));
}
function test_history_cleared_with_subdomain()
{
const TEST_URI = uri("http://www.mozilla.org/foo");
add_visit(TEST_URI);
do_check_false(yield promiseIsURIVisited(TEST_URI));
yield promiseAddVisits(TEST_URI);
do_check_true(yield promiseIsURIVisited(TEST_URI));
ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_visited(TEST_URI, false);
do_check_false(yield promiseIsURIVisited(TEST_URI));
}
function test_history_not_cleared_with_uri_contains_domain()
{
const TEST_URI = uri("http://ilovemozilla.org/foo");
add_visit(TEST_URI);
do_check_false(yield promiseIsURIVisited(TEST_URI));
yield promiseAddVisits(TEST_URI);
do_check_true(yield promiseIsURIVisited(TEST_URI));
ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_visited(TEST_URI, true);
do_check_true(yield promiseIsURIVisited(TEST_URI));
// Clear history since we left something there from this test.
PlacesUtils.bhistory.removeAllPages();
@ -544,6 +609,8 @@ function test_cache_cleared()
observe: function(aSubject, aTopic, aData)
{
os.removeObserver(observer, "cacheservice:empty-cache");
// Shutdown the download manager.
Services.obs.notifyObservers(null, "quit-application", null);
do_test_finished();
}
};
@ -635,8 +702,7 @@ function run_test()
Services.prefs.setBoolPref("places.history.enabled", true);
for (let i = 0; i < tests.length; i++)
tests[i]();
add_task(tests[i]);
// Shutdown the download manager.
Services.obs.notifyObservers(null, "quit-application", null);
run_next_test();
}