diff --git a/browser/base/content/test/newtab/browser_newtab_bug722273.js b/browser/base/content/test/newtab/browser_newtab_bug722273.js index bc561b321e11..82d86063cb0c 100644 --- a/browser/base/content/test/newtab/browser_newtab_bug722273.js +++ b/browser/base/content/test/newtab/browser_newtab_bug722273.js @@ -11,20 +11,27 @@ Cc["@mozilla.org/moz/jssubscript-loader;1"] let {Sanitizer} = tmp; -function runTests() { - sanitizeHistory(); - yield addFakeVisits(); - yield addNewTabPageTab(); - +add_task(function*() { + yield promiseSanitizeHistory(); + yield promiseAddFakeVisits(); + yield addNewTabPageTabPromise(); is(getCell(0).site.url, URL, "first site is our fake site"); - whenPagesUpdated(); - yield sanitizeHistory(); + whenPagesUpdated(() => {}); + yield promiseSanitizeHistory(); - ok(!getCell(0).site, "the fake site is gone"); -} + // Now wait until the grid is updated + while (true) { + if (!getCell(0).site) { + break; + } + info("the fake site is still present"); + yield new Promise(resolve => setTimeout(resolve, 1000)); + } + ok(!getCell(0).site, "fake site is gone"); +}); -function addFakeVisits() { +function promiseAddFakeVisits() { let visits = []; for (let i = 59; i > 0; i--) { visits.push({ @@ -37,19 +44,21 @@ function addFakeVisits() { title: "fake site", visits: visits }; - PlacesUtils.asyncHistory.updatePlaces(place, { - handleError: function () ok(false, "couldn't add visit"), - handleResult: function () {}, - handleCompletion: function () { - NewTabUtils.links.populateCache(function () { - NewTabUtils.allPages.update(); - TestRunner.next(); - }, true); - } + return new Promise((resolve, reject) => { + PlacesUtils.asyncHistory.updatePlaces(place, { + handleError: function () reject(new Error("Couldn't add visit")), + handleResult: function () {}, + handleCompletion: function () { + NewTabUtils.links.populateCache(function () { + NewTabUtils.allPages.update(); + resolve(); + }, true); + } + }); }); } -function sanitizeHistory() { +function promiseSanitizeHistory() { let s = new Sanitizer(); s.prefDomain = "privacy.cpd."; @@ -64,5 +73,5 @@ function sanitizeHistory() { prefs.setBoolPref("sessions", false); prefs.setBoolPref("siteSettings", false); - s.sanitize(); + return s.sanitize(); } diff --git a/browser/base/content/test/newtab/head.js b/browser/base/content/test/newtab/head.js index ab88fc9f6431..a6935070c748 100644 --- a/browser/base/content/test/newtab/head.js +++ b/browser/base/content/test/newtab/head.js @@ -116,18 +116,58 @@ function watchLinksChangeOnce() { /** * Provide the default test function to start our test runner. + * + * We need different code paths for tests that are still wired for + * `TestRunner` and tests that have been ported to `add_task` as + * we cannot have both in the same file. */ -function test() { - waitForExplicitFinish(); - // start TestRunner.run() after directory links is downloaded and written to disk - watchLinksChangeOnce().then(() => { - // Wait for hidden page to update with the desired links - whenPagesUpdated(() => TestRunner.run(), true); - }); +function isTestPortedToAddTask() { + return gTestPath.endsWith("browser_newtab_bug722273.js"); +} +if (!isTestPortedToAddTask()) { + this.test = function() { + waitForExplicitFinish(); + // start TestRunner.run() after directory links is downloaded and written to disk + watchLinksChangeOnce().then(() => { + // Wait for hidden page to update with the desired links + whenPagesUpdated(() => TestRunner.run(), true); + }); - // Save the original directory source (which is set globally for tests) - gOrigDirectorySource = Services.prefs.getCharPref(PREF_NEWTAB_DIRECTORYSOURCE); - Services.prefs.setCharPref(PREF_NEWTAB_DIRECTORYSOURCE, gDirectorySource); + // Save the original directory source (which is set globally for tests) + gOrigDirectorySource = Services.prefs.getCharPref(PREF_NEWTAB_DIRECTORYSOURCE); + Services.prefs.setCharPref(PREF_NEWTAB_DIRECTORYSOURCE, gDirectorySource); + } +} else { + add_task(function* setup() { + registerCleanupFunction(function() { + return new Promise(resolve => { + function cleanupAndFinish() { + PlacesTestUtils.clearHistory().then(() => { + whenPagesUpdated(resolve); + NewTabUtils.restore(); + }); + } + + let callbacks = NewTabUtils.links._populateCallbacks; + let numCallbacks = callbacks.length; + + if (numCallbacks) + callbacks.splice(0, numCallbacks, cleanupAndFinish); + else + cleanupAndFinish(); + }); + }); + + let promiseReady = Task.spawn(function*() { + yield watchLinksChangeOnce(); + yield new Promise(resolve => whenPagesUpdated(resolve, true)); + }); + + // Save the original directory source (which is set globally for tests) + gOrigDirectorySource = Services.prefs.getCharPref(PREF_NEWTAB_DIRECTORYSOURCE); + Services.prefs.setCharPref(PREF_NEWTAB_DIRECTORYSOURCE, gDirectorySource); + yield promiseReady; + }); } /**