Bug 680550 - Handle removeAllPages more sanely in tests.

r=dietrich
This commit is contained in:
Marco Bonardo 2011-08-23 14:34:15 +02:00
parent addae4eaec
commit fd2c793a9a
18 changed files with 197 additions and 172 deletions

View File

@ -56,8 +56,6 @@ Cc["@mozilla.org/moz/jssubscript-loader;1"].
const dm = Cc["@mozilla.org/download-manager;1"].
getService(Ci.nsIDownloadManager);
const bhist = Cc["@mozilla.org/browser/global-history;2"].
getService(Ci.nsIBrowserHistory);
const formhist = Cc["@mozilla.org/satchel/form-history;1"].
getService(Ci.nsIFormHistory2);
@ -583,7 +581,7 @@ WindowHelper.prototype = {
try {
if (wh.onunload)
wh.onunload();
doNextTest();
waitForAsyncUpdates(doNextTest);
}
catch (exc) {
win.close();
@ -698,10 +696,11 @@ function addFormEntryWithMinutesAgo(aMinutesAgo) {
*/
function addHistoryWithMinutesAgo(aMinutesAgo) {
let pURI = makeURI("http://" + aMinutesAgo + "-minutes-ago.com/");
bhist.addPageWithDetails(pURI,
aMinutesAgo + " minutes ago",
now_uSec - (aMinutesAgo * 60 * 1000 * 1000));
is(bhist.isVisited(pURI), true,
PlacesUtils.bhistory
.addPageWithDetails(pURI,
aMinutesAgo + " minutes ago",
now_uSec - (aMinutesAgo * 60 * 1000 * 1000));
is(PlacesUtils.bhistory.isVisited(pURI), true,
"Sanity check: history visit " + pURI.spec +
" should exist after creating it");
return pURI;
@ -711,11 +710,45 @@ function addHistoryWithMinutesAgo(aMinutesAgo) {
* Removes all history visits, downloads, and form entries.
*/
function blankSlate() {
bhist.removeAllPages();
PlacesUtils.bhistory.removeAllPages();
dm.cleanUp();
formhist.removeAllEntries();
}
/**
* Waits for all pending async statements on the default connection, before
* proceeding with aCallback.
*
* @param aCallback
* Function to be called when done.
* @param aScope
* Scope for the callback.
* @param aArguments
* Arguments array for the callback.
*
* @note The result is achieved by asynchronously executing a query requiring
* a write lock. Since all statements on the same connection are
* serialized, the end of this write operation means that all writes are
* complete. Note that WAL makes so that writers don't block readers, but
* this is a problem only across different connections.
*/
function waitForAsyncUpdates(aCallback, aScope, aArguments)
{
let scope = aScope || this;
let args = aArguments || [];
let db = PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase)
.DBConnection;
db.createAsyncStatement("BEGIN EXCLUSIVE").executeAsync();
db.createAsyncStatement("COMMIT").executeAsync({
handleResult: function() {},
handleError: function() {},
handleCompletion: function(aReason)
{
aCallback.apply(scope, args);
}
});
}
/**
* Ensures that the given pref is the expected value.
*
@ -758,7 +791,7 @@ function downloadExists(aID)
function doNextTest() {
if (gAllTests.length <= gCurrTest) {
blankSlate();
finish();
waitForAsyncUpdates(finish);
}
else {
let ct = gCurrTest;
@ -810,7 +843,7 @@ function ensureFormEntriesClearedState(aFormEntries, aShouldBeCleared) {
function ensureHistoryClearedState(aURIs, aShouldBeCleared) {
let niceStr = aShouldBeCleared ? "no longer" : "still";
aURIs.forEach(function (aURI) {
is(bhist.isVisited(aURI), !aShouldBeCleared,
is(PlacesUtils.bhistory.isVisited(aURI), !aShouldBeCleared,
"history visit " + aURI.spec + " should " + niceStr + " exist");
});
}
@ -836,5 +869,5 @@ function test() {
blankSlate();
waitForExplicitFinish();
// Kick off all the tests in the gAllTests array.
doNextTest();
waitForAsyncUpdates(doNextTest);
}

View File

@ -55,8 +55,6 @@ Cc["@mozilla.org/moz/jssubscript-loader;1"].
const dm = Cc["@mozilla.org/download-manager;1"].
getService(Ci.nsIDownloadManager);
const bhist = Cc["@mozilla.org/browser/global-history;2"].
getService(Ci.nsIBrowserHistory);
const formhist = Cc["@mozilla.org/satchel/form-history;1"].
getService(Ci.nsIFormHistory2);
@ -502,10 +500,11 @@ function addFormEntryWithMinutesAgo(aMinutesAgo) {
*/
function addHistoryWithMinutesAgo(aMinutesAgo) {
let pURI = makeURI("http://" + aMinutesAgo + "-minutes-ago.com/");
bhist.addPageWithDetails(pURI,
aMinutesAgo + " minutes ago",
now_uSec - (aMinutesAgo * 60 * 1000 * 1000));
is(bhist.isVisited(pURI), true,
PlacesUtils.bhistory
.addPageWithDetails(pURI,
aMinutesAgo + " minutes ago",
now_uSec - (aMinutesAgo * 60 * 1000 * 1000));
is(PlacesUtils.bhistory.isVisited(pURI), true,
"Sanity check: history visit " + pURI.spec +
" should exist after creating it");
return pURI;
@ -515,11 +514,45 @@ function addHistoryWithMinutesAgo(aMinutesAgo) {
* Removes all history visits, downloads, and form entries.
*/
function blankSlate() {
bhist.removeAllPages();
PlacesUtils.bhistory.removeAllPages();
dm.cleanUp();
formhist.removeAllEntries();
}
/**
* Waits for all pending async statements on the default connection, before
* proceeding with aCallback.
*
* @param aCallback
* Function to be called when done.
* @param aScope
* Scope for the callback.
* @param aArguments
* Arguments array for the callback.
*
* @note The result is achieved by asynchronously executing a query requiring
* a write lock. Since all statements on the same connection are
* serialized, the end of this write operation means that all writes are
* complete. Note that WAL makes so that writers don't block readers, but
* this is a problem only across different connections.
*/
function waitForAsyncUpdates(aCallback, aScope, aArguments)
{
let scope = aScope || this;
let args = aArguments || [];
let db = PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase)
.DBConnection;
db.createAsyncStatement("BEGIN EXCLUSIVE").executeAsync();
db.createAsyncStatement("COMMIT").executeAsync({
handleResult: function() {},
handleError: function() {},
handleCompletion: function(aReason)
{
aCallback.apply(scope, args);
}
});
}
/**
* Checks to see if the download with the specified ID exists.
*
@ -548,7 +581,7 @@ function downloadExists(aID)
function doNextTest() {
if (gAllTests.length <= gCurrTest) {
blankSlate();
finish();
waitForAsyncUpdates(finish);
}
else {
let ct = gCurrTest;
@ -600,7 +633,7 @@ function ensureFormEntriesClearedState(aFormEntries, aShouldBeCleared) {
function ensureHistoryClearedState(aURIs, aShouldBeCleared) {
let niceStr = aShouldBeCleared ? "no longer" : "still";
aURIs.forEach(function (aURI) {
is(bhist.isVisited(aURI), !aShouldBeCleared,
is(PlacesUtils.bhistory.isVisited(aURI), !aShouldBeCleared,
"history visit " + aURI.spec + " should " + niceStr + " exist");
});
}
@ -625,7 +658,7 @@ function openWindow(aOnloadCallback) {
// ok()/is() do...
try {
aOnloadCallback(win);
doNextTest();
waitForAsyncUpdates(doNextTest);
}
catch (exc) {
win.close();
@ -649,5 +682,5 @@ function test() {
blankSlate();
waitForExplicitFinish();
// Kick off all the tests in the gAllTests array.
doNextTest();
waitForAsyncUpdates(doNextTest);
}

View File

@ -536,7 +536,7 @@ function runNextTest() {
gCurrentTest.cleanup();
info("End of test: " + gCurrentTest.desc);
gCurrentTest = null;
executeSoon(runNextTest);
waitForAsyncUpdates(runNextTest);
return;
}

View File

@ -146,8 +146,7 @@ gTests.push({
menuNode.containerOpen = false;
bhist.removeAllPages();
nextTest();
waitForClearHistory(nextTest);
}
});

View File

@ -29,6 +29,13 @@ function openLibrary(callback, aLeftPaneRoot) {
return library;
}
/**
* Waits for completion of a clear history operation, before
* proceeding with aCallback.
*
* @param aCallback
* Function to be called when done.
*/
function waitForClearHistory(aCallback) {
Services.obs.addObserver(function observeCH(aSubject, aTopic, aData) {
Services.obs.removeObserver(observeCH, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
@ -36,3 +43,37 @@ function waitForClearHistory(aCallback) {
}, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
PlacesUtils.bhistory.removeAllPages();
}
/**
* Waits for all pending async statements on the default connection, before
* proceeding with aCallback.
*
* @param aCallback
* Function to be called when done.
* @param aScope
* Scope for the callback.
* @param aArguments
* Arguments array for the callback.
*
* @note The result is achieved by asynchronously executing a query requiring
* a write lock. Since all statements on the same connection are
* serialized, the end of this write operation means that all writes are
* complete. Note that WAL makes so that writers don't block readers, but
* this is a problem only across different connections.
*/
function waitForAsyncUpdates(aCallback, aScope, aArguments)
{
let scope = aScope || this;
let args = aArguments || [];
let db = PlacesUtils.history.QueryInterface(Ci.nsPIPlacesDatabase)
.DBConnection;
db.createAsyncStatement("BEGIN EXCLUSIVE").executeAsync();
db.createAsyncStatement("COMMIT").executeAsync({
handleResult: function() {},
handleError: function() {},
handleCompletion: function(aReason)
{
aCallback.apply(scope, args);
}
});
}

View File

@ -42,34 +42,21 @@ function test() {
// initialization
let pb = Cc["@mozilla.org/privatebrowsing;1"].
getService(Ci.nsIPrivateBrowsingService);
let bhist = Cc["@mozilla.org/browser/global-history;2"].
getService(Ci.nsIBrowserHistory);
let histsvc = Cc["@mozilla.org/browser/nav-history-service;1"].
getService(Ci.nsINavHistoryService).
QueryInterface(Ci.nsPIPlacesDatabase);
let cm = Cc["@mozilla.org/cookiemanager;1"].
getService(Ci.nsICookieManager);
waitForExplicitFinish();
const TEST_URL = "http://mochi.test:8888/browser/browser/components/privatebrowsing/test/browser/title.sjs";
function cleanup() {
// delete all history items
bhist.removeAllPages();
function waitForCleanup(aCallback) {
// delete all cookies
cm.removeAll();
// delete all history items
waitForClearHistory(aCallback);
}
cleanup();
let observer = {
pass: 1,
onBeginUpdateBatch: function() {
},
onEndUpdateBatch: function() {
},
onVisit: function(aURI, aVisitID, aTime, aSessionId, aReferringId,
aTransitionType, _added) {
},
onTitleChanged: function(aURI, aPageTitle) {
if (aURI.spec != TEST_URL)
return;
@ -80,8 +67,9 @@ function test() {
break;
case 2: // the second time that the page is loaded
is(aPageTitle, "Cookie", "The page should be loaded with a cookie for the second time");
cleanup();
gBrowser.selectedTab = gBrowser.addTab(TEST_URL);
waitForCleanup(function () {
gBrowser.selectedTab = gBrowser.addTab(TEST_URL);
});
break;
case 3: // before entering the private browsing mode
is(aPageTitle, "No Cookie", "The page should be loaded without any cookie again");
@ -89,37 +77,33 @@ function test() {
pb.privateBrowsingEnabled = true;
gBrowser.selectedTab = gBrowser.addTab(TEST_URL);
executeSoon(function() {
histsvc.removeObserver(observer);
PlacesUtils.history.removeObserver(observer);
pb.privateBrowsingEnabled = false;
while (gBrowser.browsers.length > 1)
while (gBrowser.browsers.length > 1) {
gBrowser.removeCurrentTab();
cleanup();
finish();
}
waitForCleanup(finish);
});
break;
default:
ok(false, "Unexpected pass: " + (this.pass - 1));
}
},
onBeforeDeleteURI: function(aURI) {
},
onDeleteURI: function(aURI) {
},
onClearHistory: function() {
},
onPageChanged: function(aURI, aWhat, aValue) {
},
onDeleteVisits: function() {
},
QueryInterface: function(iid) {
if (iid.equals(Ci.nsINavHistoryObserver) ||
iid.equals(Ci.nsISupports)) {
return this;
}
throw Cr.NS_ERROR_NO_INTERFACE;
}
};
histsvc.addObserver(observer, false);
gBrowser.selectedTab = gBrowser.addTab(TEST_URL);
onBeginUpdateBatch: function () {},
onEndUpdateBatch: function () {},
onVisit: function () {},
onBeforeDeleteURI: function () {},
onDeleteURI: function () {},
onClearHistory: function () {},
onPageChanged: function () {},
onDeleteVisits: function() {},
QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver])
};
PlacesUtils.history.addObserver(observer, false);
waitForCleanup(function () {
gBrowser.selectedTab = gBrowser.addTab(TEST_URL);
});
}

View File

@ -9,3 +9,17 @@ registerCleanupFunction(function() {
} catch(e) {}
});
/**
* Waits for completion of a clear history operation, before
* proceeding with aCallback.
*
* @param aCallback
* Function to be called when done.
*/
function waitForClearHistory(aCallback) {
Services.obs.addObserver(function observeCH(aSubject, aTopic, aData) {
Services.obs.removeObserver(observeCH, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
aCallback();
}, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
PlacesUtils.bhistory.removeAllPages();
}

View File

@ -46,6 +46,7 @@ include $(DEPTH)/config/autoconf.mk
include $(topsrcdir)/config/rules.mk
_BROWSER_FILES = \
head.js \
browser_bug399606.js \
browser_visituri.js \
browser_visituri_nohistory.js \

View File

@ -75,22 +75,6 @@ function test() {
};
hs.addObserver(historyObserver, false);
/**
* Clears history invoking callback when done.
*/
function waitForClearHistory(aCallback)
{
let observer = {
observe: function(aSubject, aTopic, aData)
{
Services.obs.removeObserver(this, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
aCallback(aSubject, aTopic, aData);
}
};
Services.obs.addObserver(observer, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
PlacesUtils.bhistory.removeAllPages();
}
function confirm_results() {
gBrowser.removeCurrentTab();
hs.removeObserver(historyObserver, false);

View File

@ -46,24 +46,6 @@ function getColumn(table, column, fromColumnName, fromColumnValue)
}
}
/**
* Clears history invoking callback when done.
*/
function waitForClearHistory(aCallback) {
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
let observer = {
observe: function(aSubject, aTopic, aData) {
Services.obs.removeObserver(this, TOPIC_EXPIRATION_FINISHED);
aCallback();
}
};
Services.obs.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
getService(Ci.nsINavHistoryService);
hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
}
function test()
{
// Make sure titles are correctly saved for a URI with the proper

View File

@ -63,24 +63,6 @@ function getColumn(table, column, fromColumnName, fromColumnValue)
}
}
/**
* Clears history invoking callback when done.
*/
function waitForClearHistory(aCallback) {
const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished";
let observer = {
observe: function(aSubject, aTopic, aData) {
Services.obs.removeObserver(this, TOPIC_EXPIRATION_FINISHED);
aCallback();
}
};
Services.obs.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false);
let hs = Cc["@mozilla.org/browser/nav-history-service;1"].
getService(Ci.nsINavHistoryService);
hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
}
function test()
{
// Make sure places visit chains are saved correctly with a redirect

View File

@ -37,22 +37,6 @@ function waitForLoad(callback)
}, true);
}
/**
* Clears history invoking callback when done.
*/
function waitForClearHistory(aCallback)
{
let observer = {
observe: function(aSubject, aTopic, aData)
{
Services.obs.removeObserver(this, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
aCallback(aSubject, aTopic, aData);
}
};
Services.obs.addObserver(observer, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
PlacesUtils.bhistory.removeAllPages();
}
function test()
{
waitForExplicitFinish();

View File

@ -37,22 +37,6 @@ function waitForLoad(callback)
}, true);
}
/**
* Clears history invoking callback when done.
*/
function waitForClearHistory(aCallback)
{
let observer = {
observe: function(aSubject, aTopic, aData)
{
Services.obs.removeObserver(this, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
aCallback(aSubject, aTopic, aData);
}
};
Services.obs.addObserver(observer, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
PlacesUtils.bhistory.removeAllPages();
}
function test()
{
if (!("@mozilla.org/privatebrowsing;1" in Cc)) {

View File

@ -0,0 +1,12 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Components.utils.import("resource://gre/modules/NetUtil.jsm");
function waitForClearHistory(aCallback) {
Services.obs.addObserver(function observeCH(aSubject, aTopic, aData) {
Services.obs.removeObserver(observeCH, PlacesUtils.TOPIC_EXPIRATION_FINISHED);
aCallback();
}, PlacesUtils.TOPIC_EXPIRATION_FINISHED, false);
PlacesUtils.bhistory.removeAllPages();
}

View File

@ -439,9 +439,4 @@ function run_test()
populateDB(visits);
cartProd([resultTypes, sortingModes], test_query_callback);
// Cleanup.
pages.forEach(function(aPageUrl) tagging.untagURI(uri(aPageUrl), tags));
remove_all_bookmarks();
bh.removeAllPages();
}

View File

@ -151,8 +151,6 @@ function run_test() {
Ci.nsINavHistoryService.TRANSITION_DOWNLOAD, false, 0);
do_check_eq(testDataDownload.length + 1, root.childCount);
root.containerOpen = false;
PlacesUtils.bhistory.removeAllPages();
}
/*

View File

@ -165,6 +165,4 @@ function run_test() {
// Kick off tests.
while (gTests.length)
(gTests.shift())();
hs.QueryInterface(Ci.nsIBrowserHistory).removeAllPages();
}

View File

@ -132,16 +132,11 @@ var resultObserver = {
var testURI = uri("http://mozilla.com");
// main
function run_test() {
check_history_query();
resultObserver.reset();
check_bookmarks_query();
resultObserver.reset();
check_mixed_query();
run_next_test();
}
function check_history_query() {
add_test(function check_history_query() {
var options = histsvc.getNewQueryOptions();
options.sortingMode = options.SORT_BY_DATE_DESCENDING;
options.resultType = options.RESULTS_AS_VISIT;
@ -209,9 +204,11 @@ function check_history_query() {
root.containerOpen = false;
do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer);
result.removeObserver(resultObserver);
}
resultObserver.reset();
waitForAsyncUpdates(run_next_test);
});
function check_bookmarks_query() {
add_test(function check_bookmarks_query() {
var options = histsvc.getNewQueryOptions();
var query = histsvc.getNewQuery();
query.setFolders([bmsvc.bookmarksMenuFolder], 1);
@ -276,9 +273,11 @@ function check_bookmarks_query() {
root.containerOpen = false;
do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer);
result.removeObserver(resultObserver);
}
resultObserver.reset();
waitForAsyncUpdates(run_next_test);
});
function check_mixed_query() {
add_test(function check_mixed_query() {
var options = histsvc.getNewQueryOptions();
var query = histsvc.getNewQuery();
query.onlyBookmarked = true;
@ -309,4 +308,6 @@ function check_mixed_query() {
root.containerOpen = false;
do_check_eq(resultObserver.closedContainer, resultObserver.openedContainer);
result.removeObserver(resultObserver);
}
resultObserver.reset();
waitForAsyncUpdates(run_next_test);
});