Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit 34e3c99

Browse files
committed
Bug 820763 - Stop using addvisit() in toolkit tests. r=mak
1 parent 435c17f commit 34e3c99

4 files changed

Lines changed: 191 additions & 58 deletions

File tree

toolkit/components/downloads/test/unit/head_download_manager.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ do_get_profile();
1414
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
1515
Cu.import("resource://gre/modules/Services.jsm");
1616
Cu.import("resource://testing-common/httpd.js");
17+
Cu.import("resource://gre/modules/PlacesUtils.jsm");
18+
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
19+
"resource://gre/modules/commonjs/promise/core.js");
1720

1821
var downloadUtils = { };
1922
XPCOMUtils.defineLazyServiceGetter(downloadUtils,
@@ -158,6 +161,70 @@ function getDownloadListener()
158161
};
159162
}
160163

164+
/**
165+
* Asynchronously adds visits to a page.
166+
*
167+
* @param aPlaceInfo
168+
* Can be an nsIURI, in such a case a single LINK visit will be added.
169+
* Otherwise can be an object describing the visit to add, or an array
170+
* of these objects:
171+
* { uri: nsIURI of the page,
172+
* transition: one of the TRANSITION_* from nsINavHistoryService,
173+
* [optional] title: title of the page,
174+
* [optional] visitDate: visit date in microseconds from the epoch
175+
* [optional] referrer: nsIURI of the referrer for this visit
176+
* }
177+
*
178+
* @return {Promise}
179+
* @resolves When all visits have been added successfully.
180+
* @rejects JavaScript exception.
181+
*/
182+
function promiseAddVisits(aPlaceInfo)
183+
{
184+
let deferred = Promise.defer();
185+
let places = [];
186+
if (aPlaceInfo instanceof Ci.nsIURI) {
187+
places.push({ uri: aPlaceInfo });
188+
}
189+
else if (Array.isArray(aPlaceInfo)) {
190+
places = places.concat(aPlaceInfo);
191+
} else {
192+
places.push(aPlaceInfo)
193+
}
194+
195+
// Create mozIVisitInfo for each entry.
196+
let now = Date.now();
197+
for (let i = 0; i < places.length; i++) {
198+
if (!places[i].title) {
199+
places[i].title = "test visit for " + places[i].uri.spec;
200+
}
201+
places[i].visits = [{
202+
transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK
203+
: places[i].transition,
204+
visitDate: places[i].visitDate || (now++) * 1000,
205+
referrerURI: places[i].referrer
206+
}];
207+
}
208+
209+
PlacesUtils.asyncHistory.updatePlaces(
210+
places,
211+
{
212+
handleError: function handleError(aResultCode, aPlaceInfo) {
213+
let ex = new Components.Exception("Unexpected error in adding visits.",
214+
aResultCode);
215+
deferred.reject(ex);
216+
},
217+
handleResult: function () {},
218+
handleCompletion: function handleCompletion() {
219+
deferred.resolve();
220+
}
221+
}
222+
);
223+
224+
return deferred.promise;
225+
}
226+
227+
161228
XPCOMUtils.defineLazyGetter(this, "Services", function() {
162229
Cu.import("resource://gre/modules/Services.jsm");
163230
return Services;

toolkit/components/downloads/test/unit/test_history_expiration.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ function getExpirablePRTime() {
2525
}
2626

2727
function run_test()
28+
{
29+
run_next_test();
30+
}
31+
32+
add_task(function test_execute()
2833
{
2934
// Like the code, we check to see if nav-history-service exists
3035
// (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()
7984
// Add an expirable visit to this download.
8085
let histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
8186
getService(Ci.nsINavHistoryService);
82-
histsvc.addVisit(theURI, getExpirablePRTime(), null,
83-
histsvc.TRANSITION_DOWNLOAD, false, 0);
87+
yield promiseAddVisits({uri: theURI, visitDate: getExpirablePRTime(),
88+
transition: histsvc.TRANSITION_DOWNLOAD});
8489

8590
// Get the download manager as history observer and batch expirations
8691
let histobs = dm.QueryInterface(Ci.nsINavHistoryObserver);
@@ -116,4 +121,5 @@ function run_test()
116121

117122
// Expiration happens on a timeout, about 3.5s after we set the pref
118123
do_test_pending();
119-
}
124+
});
125+

toolkit/components/places/tests/browser/browser_bug248970.js

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,19 @@ function test() {
6868
is(PlacesUtils.history.hasHistoryEntries, false,
6969
"History database should be empty");
7070
// Create a handful of history items with various visit types
71-
fillHistoryVisitedURI(window);
72-
placeItemsCount += 7;
73-
// History database should have entries
74-
is(PlacesUtils.history.hasHistoryEntries, true,
75-
"History database should have entries");
76-
// We added 7 new items to history.
77-
is(getPlacesItemsCount(window), placeItemsCount,
78-
"Check the total items count");
79-
// Test on windows.
80-
testOnWindow(false, function() {
81-
testOnWindow(true, function() {
82-
testOnWindow(false, finish);
71+
fillHistoryVisitedURI(window, function() {
72+
placeItemsCount += 7;
73+
// History database should have entries
74+
is(PlacesUtils.history.hasHistoryEntries, true,
75+
"History database should have entries");
76+
// We added 7 new items to history.
77+
is(getPlacesItemsCount(window), placeItemsCount,
78+
"Check the total items count");
79+
// Test on windows.
80+
testOnWindow(false, function() {
81+
testOnWindow(true, function() {
82+
testOnWindow(false, finish);
83+
});
8384
});
8485
});
8586
});
@@ -127,24 +128,17 @@ function getPlacesItemsCount(aWin){
127128
return cc;
128129
}
129130

130-
function addVisit(aWin, aURI, aType) {
131-
aWin.PlacesUtils.history.addVisit(
132-
NetUtil.newURI(aURI), Date.now() * 1000, null, aType, false, 0);
133-
}
134-
135-
function fillHistoryVisitedURI(aWin) {
136-
aWin.PlacesUtils.history.runInBatchMode({
137-
runBatched: function (aUserData) {
138-
addVisit(aWin, visitedURIs[0], PlacesUtils.history.TRANSITION_LINK);
139-
addVisit(aWin, visitedURIs[1], PlacesUtils.history.TRANSITION_TYPED);
140-
addVisit(aWin, visitedURIs[2], PlacesUtils.history.TRANSITION_BOOKMARK);
141-
addVisit(aWin, visitedURIs[3], PlacesUtils.history.TRANSITION_REDIRECT_PERMANENT);
142-
addVisit(aWin, visitedURIs[4], PlacesUtils.history.TRANSITION_REDIRECT_TEMPORARY);
143-
addVisit(aWin, visitedURIs[5], PlacesUtils.history.TRANSITION_EMBED);
144-
addVisit(aWin, visitedURIs[6], PlacesUtils.history.TRANSITION_FRAMED_LINK);
145-
addVisit(aWin, visitedURIs[7], PlacesUtils.history.TRANSITION_DOWNLOAD);
146-
}
147-
}, null);
131+
function fillHistoryVisitedURI(aWin, aCallback) {
132+
addVisits([
133+
{uri: NetUtil.newURI(visitedURIs[0]), transition: PlacesUtils.history.TRANSITION_LINK},
134+
{uri: NetUtil.newURI(visitedURIs[1]), transition: PlacesUtils.history.TRANSITION_TYPED},
135+
{uri: NetUtil.newURI(visitedURIs[2]), transition: PlacesUtils.history.TRANSITION_BOOKMARK},
136+
{uri: NetUtil.newURI(visitedURIs[3]), transition: PlacesUtils.history.TRANSITION_REDIRECT_PERMANENT},
137+
{uri: NetUtil.newURI(visitedURIs[4]), transition: PlacesUtils.history.TRANSITION_REDIRECT_TEMPORARY},
138+
{uri: NetUtil.newURI(visitedURIs[5]), transition: PlacesUtils.history.TRANSITION_EMBED},
139+
{uri: NetUtil.newURI(visitedURIs[6]), transition: PlacesUtils.history.TRANSITION_FRAMED_LINK},
140+
{uri: NetUtil.newURI(visitedURIs[7]), transition: PlacesUtils.history.TRANSITION_DOWNLOAD}],
141+
aWin, aCallback);
148142
}
149143

150144
function checkHistoryItems(aWin) {

toolkit/forgetaboutsite/test/unit/test_removeDataFromDomain.js

Lines changed: 91 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@
1212
////////////////////////////////////////////////////////////////////////////////
1313
//// Globals
1414

15+
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
1516
Cu.import("resource://gre/modules/Services.jsm");
1617
Cu.import("resource://gre/modules/PlacesUtils.jsm");
1718
Cu.import("resource://gre/modules/ForgetAboutSite.jsm");
1819

20+
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
21+
"resource://gre/modules/commonjs/promise/core.js");
22+
1923
const COOKIE_EXPIRY = Math.round(Date.now() / 1000) + 60;
2024
const COOKIE_NAME = "testcookie";
2125
const COOKIE_PATH = "/";
@@ -48,32 +52,87 @@ function uri(aURIString)
4852
}
4953

5054
/**
51-
* Adds a visit to history.
55+
* Asynchronously adds visits to a page.
5256
*
53-
* @param aURI
54-
* The URI to add.
57+
* @param aPlaceInfo
58+
* Can be an nsIURI, in such a case a single LINK visit will be added.
59+
* Otherwise can be an object describing the visit to add, or an array
60+
* of these objects:
61+
* { uri: nsIURI of the page,
62+
* transition: one of the TRANSITION_* from nsINavHistoryService,
63+
* [optional] title: title of the page,
64+
* [optional] visitDate: visit date in microseconds from the epoch
65+
* [optional] referrer: nsIURI of the referrer for this visit
66+
* }
67+
*
68+
* @return {Promise}
69+
* @resolves When all visits have been added successfully.
70+
* @rejects JavaScript exception.
5571
*/
56-
function add_visit(aURI)
72+
function promiseAddVisits(aPlaceInfo)
5773
{
58-
check_visited(aURI, false);
59-
PlacesUtils.history.addVisit(aURI, Date.now() * 1000, null,
60-
Ci.nsINavHistoryService.TRANSITION_LINK, false,
61-
0);
62-
check_visited(aURI, true);
74+
let deferred = Promise.defer();
75+
let places = [];
76+
if (aPlaceInfo instanceof Ci.nsIURI) {
77+
places.push({ uri: aPlaceInfo });
78+
}
79+
else if (Array.isArray(aPlaceInfo)) {
80+
places = places.concat(aPlaceInfo);
81+
} else {
82+
places.push(aPlaceInfo)
83+
}
84+
85+
// Create mozIVisitInfo for each entry.
86+
let now = Date.now();
87+
for (let i = 0; i < places.length; i++) {
88+
if (!places[i].title) {
89+
places[i].title = "test visit for " + places[i].uri.spec;
90+
}
91+
places[i].visits = [{
92+
transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK
93+
: places[i].transition,
94+
visitDate: places[i].visitDate || (now++) * 1000,
95+
referrerURI: places[i].referrer
96+
}];
97+
}
98+
99+
PlacesUtils.asyncHistory.updatePlaces(
100+
places,
101+
{
102+
handleError: function handleError(aResultCode, aPlaceInfo) {
103+
let ex = new Components.Exception("Unexpected error in adding visits.",
104+
aResultCode);
105+
deferred.reject(ex);
106+
},
107+
handleResult: function () {},
108+
handleCompletion: function handleCompletion() {
109+
deferred.resolve();
110+
}
111+
}
112+
);
113+
114+
return deferred.promise;
63115
}
64116

117+
65118
/**
66-
* Checks to ensure a URI string is visited or not.
119+
* Asynchronously check a url is visited.
67120
*
68121
* @param aURI
69-
* The URI to check.
70-
* @param aIsVisited
71-
* True if the URI should be visited, false otherwise.
122+
* The URI.
123+
*
124+
* @return {Promise}
125+
* @resolves When the check has been added successfully.
126+
* @rejects JavaScript exception.
72127
*/
73-
function check_visited(aURI, aIsVisited)
128+
function promiseIsURIVisited(aURI)
74129
{
75-
let checker = aIsVisited ? do_check_true : do_check_false;
76-
checker(PlacesUtils.ghistory2.isVisited(aURI));
130+
let deferred = Promise.defer();
131+
PlacesUtils.asyncHistory.isURIVisited(aURI, function(aURI, aIsVisited) {
132+
deferred.resolve(aIsVisited);
133+
});
134+
135+
return deferred.promise;
77136
}
78137

79138
/**
@@ -323,25 +382,31 @@ function check_preference_exists(aURI, aExists)
323382
function test_history_cleared_with_direct_match()
324383
{
325384
const TEST_URI = uri("http://mozilla.org/foo");
326-
add_visit(TEST_URI);
385+
do_check_false(yield promiseIsURIVisited(TEST_URI));
386+
yield promiseAddVisits(TEST_URI);
387+
do_check_true(yield promiseIsURIVisited(TEST_URI));
327388
ForgetAboutSite.removeDataFromDomain("mozilla.org");
328-
check_visited(TEST_URI, false);
389+
do_check_false(yield promiseIsURIVisited(TEST_URI));
329390
}
330391

331392
function test_history_cleared_with_subdomain()
332393
{
333394
const TEST_URI = uri("http://www.mozilla.org/foo");
334-
add_visit(TEST_URI);
395+
do_check_false(yield promiseIsURIVisited(TEST_URI));
396+
yield promiseAddVisits(TEST_URI);
397+
do_check_true(yield promiseIsURIVisited(TEST_URI));
335398
ForgetAboutSite.removeDataFromDomain("mozilla.org");
336-
check_visited(TEST_URI, false);
399+
do_check_false(yield promiseIsURIVisited(TEST_URI));
337400
}
338401

339402
function test_history_not_cleared_with_uri_contains_domain()
340403
{
341404
const TEST_URI = uri("http://ilovemozilla.org/foo");
342-
add_visit(TEST_URI);
405+
do_check_false(yield promiseIsURIVisited(TEST_URI));
406+
yield promiseAddVisits(TEST_URI);
407+
do_check_true(yield promiseIsURIVisited(TEST_URI));
343408
ForgetAboutSite.removeDataFromDomain("mozilla.org");
344-
check_visited(TEST_URI, true);
409+
do_check_true(yield promiseIsURIVisited(TEST_URI));
345410

346411
// Clear history since we left something there from this test.
347412
PlacesUtils.bhistory.removeAllPages();
@@ -544,6 +609,8 @@ function test_cache_cleared()
544609
observe: function(aSubject, aTopic, aData)
545610
{
546611
os.removeObserver(observer, "cacheservice:empty-cache");
612+
// Shutdown the download manager.
613+
Services.obs.notifyObservers(null, "quit-application", null);
547614
do_test_finished();
548615
}
549616
};
@@ -635,8 +702,7 @@ function run_test()
635702
Services.prefs.setBoolPref("places.history.enabled", true);
636703

637704
for (let i = 0; i < tests.length; i++)
638-
tests[i]();
705+
add_task(tests[i]);
639706

640-
// Shutdown the download manager.
641-
Services.obs.notifyObservers(null, "quit-application", null);
707+
run_next_test();
642708
}

0 commit comments

Comments
 (0)