Skip to content
This repository has been archived by the owner on Jan 12, 2022. It is now read-only.

Commit

Permalink
Bug 680550 - Handle removeAllPages more sanely in tests.
Browse files Browse the repository at this point in the history
r=dietrich
  • Loading branch information
mak77 committed Aug 23, 2011
1 parent 871aba9 commit 9a853d9
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 171 deletions.
55 changes: 44 additions & 11 deletions browser/base/content/test/browser_sanitizeDialog.js
Expand Up @@ -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);

Expand Down Expand Up @@ -583,7 +581,7 @@ WindowHelper.prototype = {
try {
if (wh.onunload)
wh.onunload();
doNextTest();
waitForAsyncUpdates(doNextTest);
}
catch (exc) {
win.close();
Expand Down Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -758,7 +791,7 @@ function downloadExists(aID)
function doNextTest() {
if (gAllTests.length <= gCurrTest) {
blankSlate();
finish();
waitForAsyncUpdates(finish);
}
else {
let ct = gCurrTest;
Expand Down Expand Up @@ -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");
});
}
Expand All @@ -836,5 +869,5 @@ function test() {
blankSlate();
waitForExplicitFinish();
// Kick off all the tests in the gAllTests array.
doNextTest();
waitForAsyncUpdates(doNextTest);
}
55 changes: 44 additions & 11 deletions browser/base/content/test/browser_sanitizeDialog_treeView.js
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -548,7 +581,7 @@ function downloadExists(aID)
function doNextTest() {
if (gAllTests.length <= gCurrTest) {
blankSlate();
finish();
waitForAsyncUpdates(finish);
}
else {
let ct = gCurrTest;
Expand Down Expand Up @@ -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");
});
}
Expand All @@ -625,7 +658,7 @@ function openWindow(aOnloadCallback) {
// ok()/is() do...
try {
aOnloadCallback(win);
doNextTest();
waitForAsyncUpdates(doNextTest);
}
catch (exc) {
win.close();
Expand All @@ -649,5 +682,5 @@ function test() {
blankSlate();
waitForExplicitFinish();
// Kick off all the tests in the gAllTests array.
doNextTest();
waitForAsyncUpdates(doNextTest);
}
Expand Up @@ -536,7 +536,7 @@ function runNextTest() {
gCurrentTest.cleanup();
info("End of test: " + gCurrentTest.desc);
gCurrentTest = null;
executeSoon(runNextTest);
waitForAsyncUpdates(runNextTest);
return;
}

Expand Down
Expand Up @@ -146,8 +146,7 @@ gTests.push({

menuNode.containerOpen = false;

bhist.removeAllPages();
nextTest();
waitForClearHistory(nextTest);
}
});

Expand Down
41 changes: 41 additions & 0 deletions browser/components/places/tests/browser/head.js
Expand Up @@ -29,10 +29,51 @@ 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);
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);
}
});
}
Expand Up @@ -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;
Expand All @@ -80,46 +67,43 @@ 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");
// enter private browsing mode
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;
}

onBeginUpdateBatch: function () {},
onEndUpdateBatch: function () {},
onVisit: function () {},
onBeforeDeleteURI: function () {},
onDeleteURI: function () {},
onClearHistory: function () {},
onPageChanged: function () {},
onDeleteVisits: function() {},

QueryInterface: XPCOMUtils.generateQI([Ci.nsINavHistoryObserver])
};
histsvc.addObserver(observer, false);
PlacesUtils.history.addObserver(observer, false);

gBrowser.selectedTab = gBrowser.addTab(TEST_URL);
waitForCleanup(function () {
gBrowser.selectedTab = gBrowser.addTab(TEST_URL);
});
}

0 comments on commit 9a853d9

Please sign in to comment.