1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/components/places/tests/chrome/head.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,69 @@ 1.4 +/** 1.5 + * Asynchronously adds visits to a page, invoking a callback function when done. 1.6 + * 1.7 + * @param aPlaceInfo 1.8 + * Can be an nsIURI, in such a case a single LINK visit will be added. 1.9 + * Otherwise can be an object describing the visit to add, or an array 1.10 + * of these objects: 1.11 + * { uri: nsIURI of the page, 1.12 + * transition: one of the TRANSITION_* from nsINavHistoryService, 1.13 + * [optional] title: title of the page, 1.14 + * [optional] visitDate: visit date in microseconds from the epoch 1.15 + * [optional] referrer: nsIURI of the referrer for this visit 1.16 + * } 1.17 + * @param [optional] aCallback 1.18 + * Function to be invoked on completion. 1.19 + */ 1.20 +function addVisits(aPlaceInfo, aCallback) { 1.21 + let places = []; 1.22 + if (aPlaceInfo instanceof Ci.nsIURI) { 1.23 + places.push({ uri: aPlaceInfo }); 1.24 + } 1.25 + else if (Array.isArray(aPlaceInfo)) { 1.26 + places = places.concat(aPlaceInfo); 1.27 + } else { 1.28 + places.push(aPlaceInfo) 1.29 + } 1.30 + 1.31 + // Create mozIVisitInfo for each entry. 1.32 + let now = Date.now(); 1.33 + for (let i = 0; i < places.length; i++) { 1.34 + if (!places[i].title) { 1.35 + places[i].title = "test visit for " + places[i].uri.spec; 1.36 + } 1.37 + places[i].visits = [{ 1.38 + transitionType: places[i].transition === undefined ? PlacesUtils.history.TRANSITION_LINK 1.39 + : places[i].transition, 1.40 + visitDate: places[i].visitDate || (now++) * 1000, 1.41 + referrerURI: places[i].referrer 1.42 + }]; 1.43 + } 1.44 + 1.45 + PlacesUtils.asyncHistory.updatePlaces( 1.46 + places, 1.47 + { 1.48 + handleError: function AAV_handleError() { 1.49 + throw("Unexpected error in adding visit."); 1.50 + }, 1.51 + handleResult: function () {}, 1.52 + handleCompletion: function UP_handleCompletion() { 1.53 + if (aCallback) 1.54 + aCallback(); 1.55 + } 1.56 + } 1.57 + ); 1.58 +} 1.59 + 1.60 +/** 1.61 + * Clears history invoking callback when done. 1.62 + */ 1.63 +function waitForClearHistory(aCallback) { 1.64 + const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished"; 1.65 + Services.obs.addObserver(function observer(aSubject, aTopic, aData) { 1.66 + Services.obs.removeObserver(observer, TOPIC_EXPIRATION_FINISHED); 1.67 + aCallback(); 1.68 + }, TOPIC_EXPIRATION_FINISHED, false); 1.69 + Cc["@mozilla.org/browser/nav-history-service;1"] 1.70 + .getService(Ci.nsINavHistoryService) 1.71 + .QueryInterface(Ci.nsIBrowserHistory).removeAllPages(); 1.72 +}