diff --git a/lib/sdk/test/runner.js b/lib/sdk/test/runner.js index b1bc355dc..4db9acae3 100644 --- a/lib/sdk/test/runner.js +++ b/lib/sdk/test/runner.js @@ -10,6 +10,18 @@ var cfxArgs = require("@test/options"); var { Cc, Ci} = require("chrome"); function runTests(findAndRunTests) { + var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"] + .getService(Ci.nsIWindowWatcher); + + let ns = 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'; + let msg = 'Running tests...'; + let markup = ''; + let url = "data:application/vnd.mozilla.xul+xml;charset=utf-8," + escape(markup); + + + var window = ww.openWindow(null, url, "harness", "centerscreen", null); + var harness = require("./harness"); function onDone(tests) { @@ -17,6 +29,7 @@ function runTests(findAndRunTests) { var total = tests.passed + tests.failed; stdout.write(tests.passed + " of " + total + " tests passed.\n"); + window.close(); if (tests.failed == 0) { if (tests.passed === 0) stdout.write("No tests were run\n"); @@ -27,22 +40,34 @@ function runTests(findAndRunTests) { } }; - // We may have to run test on next cycle, otherwise XPCOM components - // are not correctly updated. - // For ex: nsIFocusManager.getFocusedElementForWindow may throw - // NS_ERROR_ILLEGAL_VALUE exception. - require("../timers").setTimeout(function () { - harness.runTests({ - findAndRunTests: findAndRunTests, - iterations: cfxArgs.iterations || 1, - filter: cfxArgs.filter, - profileMemory: cfxArgs.profileMemory, - stopOnError: cfxArgs.stopOnError, - verbose: cfxArgs.verbose, - print: stdout.write, - onDone: onDone - }); - }, 0); + // We have to wait for this window to be fully loaded *and* focused + // in order to avoid it to mess with our various window/focus tests. + // We are first waiting for our window to be fully loaded before ensuring + // that it will take the focus, and then we wait for it to be focused. + window.addEventListener("load", function onload() { + window.removeEventListener("load", onload, true); + + window.addEventListener("focus", function onfocus() { + window.removeEventListener("focus", onfocus, true); + // Finally, we have to run test on next cycle, otherwise XPCOM components + // are not correctly updated. + // For ex: nsIFocusManager.getFocusedElementForWindow may throw + // NS_ERROR_ILLEGAL_VALUE exception. + require("../timers").setTimeout(function () { + harness.runTests({ + findAndRunTests: findAndRunTests, + iterations: cfxArgs.iterations || 1, + filter: cfxArgs.filter, + profileMemory: cfxArgs.profileMemory, + stopOnError: cfxArgs.stopOnError, + verbose: cfxArgs.verbose, + print: stdout.write, + onDone: onDone + }); + }, 0); + }, true); + window.focus(); + }, true); } function printFailedTests(tests, verbose, print) { diff --git a/test/test-window-utils.js b/test/test-window-utils.js index cac94bea3..c93b0d46e 100644 --- a/test/test-window-utils.js +++ b/test/test-window-utils.js @@ -236,6 +236,9 @@ exports['test window watcher without untracker'] = function(assert, done) { }; exports['test active window'] = function(assert, done) { + let testRunnerWindow = Cc["@mozilla.org/appshell/window-mediator;1"] + .getService(Ci.nsIWindowMediator) + .getMostRecentWindow("test:runner"); let browserWindow = Cc["@mozilla.org/appshell/window-mediator;1"] .getService(Ci.nsIWindowMediator) .getMostRecentWindow("navigator:browser"); @@ -252,21 +255,26 @@ exports['test active window'] = function(assert, done) { function() { assert.equal(windowUtils.activeWindow, browserWindow, "Correct active window [1]"); - nextTest(); + continueAfterFocus(windowUtils.activeWindow = testRunnerWindow); }, function() { + assert.equal(windowUtils.activeWindow, testRunnerWindow, + "Correct active window [2]"); assert.equal(windowUtils.activeBrowserWindow, browserWindow, - "Correct active browser window [2]"); + "Correct active browser window [3]"); continueAfterFocus(windowUtils.activeWindow = browserWindow); }, function() { assert.equal(windowUtils.activeWindow, browserWindow, - "Correct active window [3]"); - nextTest(); + "Correct active window [4]"); + continueAfterFocus(windowUtils.activeWindow = testRunnerWindow); }, function() { + assert.equal(windowUtils.activeWindow, testRunnerWindow, + "Correct active window [5]"); assert.equal(windowUtils.activeBrowserWindow, browserWindow, - "Correct active browser window [4]"); + "Correct active browser window [6]"); + testRunnerWindow = null; browserWindow = null; done(); } @@ -280,6 +288,7 @@ exports['test active window'] = function(assert, done) { } function continueAfterFocus(targetWindow) { + // Based on SimpleTest.waitForFocus var fm = Cc["@mozilla.org/focus-manager;1"]. getService(Ci.nsIFocusManager); diff --git a/test/windows/test-firefox-windows.js b/test/windows/test-firefox-windows.js index 2b5688b5d..a0cd4f94a 100644 --- a/test/windows/test-firefox-windows.js +++ b/test/windows/test-firefox-windows.js @@ -10,6 +10,8 @@ const wm = Cc['@mozilla.org/appshell/window-mediator;1']. getService(Ci.nsIWindowMediator); let browserWindows; +function getTestRunnerWindow() wm.getMostRecentWindow("test:runner"); + // TEST: open & close window exports.testOpenAndCloseWindow = function(test) { test.waitUntilDone(); @@ -178,7 +180,7 @@ exports.testActiveWindow = function(test) { let window2, window3; // Raw window objects - let rawWindow2, rawWindow3; + let nonBrowserWindow = getTestRunnerWindow(), rawWindow2, rawWindow3; test.waitUntilDone(); @@ -194,7 +196,8 @@ exports.testActiveWindow = function(test) { continueAfterFocus(rawWindow2); }, function() { - nextStep(); + nonBrowserWindow.focus(); + continueAfterFocus(nonBrowserWindow); }, function() { /** @@ -220,6 +223,7 @@ exports.testActiveWindow = function(test) { }, function() { test.assertEqual(windows.activeWindow.title, window3.title, "Correct active window - 3"); + nonBrowserWindow.focus(); finishTest(); } ]; @@ -249,6 +253,7 @@ exports.testActiveWindow = function(test) { } function continueAfterFocus(targetWindow) { + // Based on SimpleTest.waitForFocus var fm = Cc["@mozilla.org/focus-manager;1"]. getService(Ci.nsIFocusManager); @@ -331,12 +336,13 @@ exports.testTrackWindows = function(test) { browserWindows.on("activate", function (window) { let index = windows.indexOf(window); + actions.push("global activate " + index) }) browserWindows.on("deactivate", function (window) { let index = windows.indexOf(window); - if (index < 0) return; + actions.push("global deactivate " + index) })