Skip to content

Commit

Permalink
Bug 1265194 - Refactor prompt tests to use state/action objects and n…
Browse files Browse the repository at this point in the history
…ew common helpers. r=adw
  • Loading branch information
dolske committed Apr 24, 2016
1 parent 1ad0f7c commit 9c5d8cf
Show file tree
Hide file tree
Showing 8 changed files with 353 additions and 315 deletions.
39 changes: 6 additions & 33 deletions toolkit/components/prompts/test/chromeScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function getSelectState(ui) {
function getPromptState(ui) {
let state = {};
state.msg = ui.infoBody.textContent;
state.titleHidden = ui.infoTitle.getAttribute("hidden") == "true";
state.textHidden = ui.loginContainer.hidden;
state.passHidden = ui.password1Container.hidden;
state.checkHidden = ui.checkboxContainer.hidden;
Expand Down Expand Up @@ -174,6 +175,11 @@ function dismissPrompt(ui, action) {
case 2:
ui.button2.click();
break;
case "ESC":
// XXX This is assuming tab-modal.
let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
EventUtils.synthesizeKey("KEY_Escape", { code: "Escape" }, browserWin);
break;
case "pollOK":
// Buttons are disabled at the moment, poll until they're reenabled.
// Can't use setInterval here, because the window's in a modal state
Expand All @@ -191,39 +197,6 @@ function dismissPrompt(ui, action) {
}
}


addMessageListener("cancelPrompt", message => {
cancelPromptWhenItAppears();
});

function cancelPromptWhenItAppears() {
let interval = setInterval(() => {
if (cancelPrompt()) {
clearInterval(interval);
}
}, 100);
}

function cancelPrompt() {
let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
let gBrowser = browserWin.gBrowser;
let promptManager = gBrowser.getTabModalPromptBox(gBrowser.selectedBrowser);
let prompts = promptManager.listPrompts();
if (!prompts.length) {
return false;
}
sendAsyncMessage("promptCanceled", {
ui: {
infoTitle: {
hidden: prompts[0].ui.infoTitle.getAttribute("hidden") == "true",
},
},
});
EventUtils.synthesizeKey("KEY_Escape", { code: "Escape" }, browserWin);
return true;
}


function getDialogDoc() {
// Trudge through all the open windows, until we find the one
// that has either commonDialog.xul or selectDialog.xul loaded.
Expand Down
125 changes: 9 additions & 116 deletions toolkit/components/prompts/test/prompt_common.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,132 +3,21 @@ const Cc = SpecialPowers.Cc;
ok(Ci != null, "Access Ci");
ok(Cc != null, "Access Cc");

var didDialog;

var isSelectDialog = false;
var isTabModal = false;
if (SpecialPowers.getBoolPref("prompts.tab_modal.enabled")) {
isTabModal = true;
}
var usePromptService = true;

var timer; // keep in outer scope so it's not GC'd before firing
function startCallbackTimer() {
didDialog = false;

// Delay before the callback twiddles the prompt.
const dialogDelay = 10;

// Use a timer to invoke a callback to twiddle the authentication dialog
timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
timer.init(observer, dialogDelay, Ci.nsITimer.TYPE_ONE_SHOT);
}


var observer = {
QueryInterface : SpecialPowers.wrapCallback(function (iid) {
const interfaces = [Ci.nsIObserver,
Ci.nsISupports, Ci.nsISupportsWeakReference];

if (!interfaces.some( function(v) { return iid.equals(v) } ))
throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
return this;
}),

observe : SpecialPowers.wrapCallback(function (subject, topic, data) {
try {
if (isTabModal) {
var promptBox = getTabModalPromptBox(window);
ok(promptBox, "got tabmodal promptbox");
var prompts = SpecialPowers.wrap(promptBox).listPrompts();
if (prompts.length)
handleDialog(prompts[0].Dialog.ui, testNum);
else
startCallbackTimer(); // try again in a bit
} else {
var doc = getDialogDoc();
if (isSelectDialog && doc)
handleDialog(doc, testNum);
else if (doc)
handleDialog(doc.defaultView.Dialog.ui, testNum);
else
startCallbackTimer(); // try again in a bit
}
} catch (e) {
ok(false, "Exception thrown in the timer callback: " + e + " at " + (e.fileName || e.filename) + ":" + (e.lineNumber || e.linenumber));
}
})
};

function getTabModalPromptBox(domWin) {
var promptBox = null;

// Given a content DOM window, returns the chrome window it's in.
function getChromeWindow(aWindow) {
var chromeWin = SpecialPowers.wrap(aWindow).QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebNavigation)
.QueryInterface(Ci.nsIDocShell)
.chromeEventHandler.ownerDocument.defaultView;
return chromeWin;
}

try {
// Get the topmost window, in case we're in a frame.
var promptWin = domWin.top;

// Get the chrome window for the content window we're using.
var chromeWin = getChromeWindow(promptWin);

if (chromeWin.getTabModalPromptBox)
promptBox = chromeWin.getTabModalPromptBox(promptWin);
} catch (e) {
// If any errors happen, just assume no tabmodal prompter.
}

// Callers get confused by a wrapped promptBox here.
return SpecialPowers.unwrap(promptBox);
}

function getDialogDoc() {
// Trudge through all the open windows, until we find the one
// that has either commonDialog.xul or selectDialog.xul loaded.
var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
//var enumerator = wm.getEnumerator("navigator:browser");
var enumerator = wm.getXULWindowEnumerator(null);

while (enumerator.hasMoreElements()) {
var win = enumerator.getNext();
var windowDocShell = win.QueryInterface(Ci.nsIXULWindow).docShell;

var containedDocShells = windowDocShell.getDocShellEnumerator(
Ci.nsIDocShellTreeItem.typeChrome,
Ci.nsIDocShell.ENUMERATE_FORWARDS);
while (containedDocShells.hasMoreElements()) {
// Get the corresponding document for this docshell
var childDocShell = containedDocShells.getNext();
// We don't want it if it's not done loading.
if (childDocShell.busyFlags != Ci.nsIDocShell.BUSY_FLAGS_NONE)
continue;
var childDoc = childDocShell.QueryInterface(Ci.nsIDocShell)
.contentViewer
.DOMDocument;

//ok(true, "Got window: " + childDoc.location.href);
if (childDoc.location.href == "chrome://global/content/commonDialog.xul")
return childDoc;
if (childDoc.location.href == "chrome://global/content/selectDialog.xul")
return childDoc;
}
}

return null;
}

var isOSX = ("nsILocalFileMac" in SpecialPowers.Ci);
var isLinux = ("@mozilla.org/gnome-gconf-service;1" in SpecialPowers.Cc);
var isE10S = SpecialPowers.Services.appinfo.processType == 2;


var gChromeScript = SpecialPowers.loadChromeScript(SimpleTest.getTestFileURL("chromeScript.js"));
SimpleTest.registerCleanupFunction(() => gChromeScript.destroy());


function handlePrompt() {
return new Promise(resolve => {
gChromeScript.addMessageListener("promptHandled", function handled(msg) {
Expand All @@ -143,6 +32,10 @@ function handlePrompt() {
function checkPromptState(promptState, expectedState) {
// XXX check title? OS X has title in content
is(promptState.msg, expectedState.msg, "Checking expected message");
if (isOSX && !isTabModal)
ok(!promptState.titleHidden, "Checking title always visible on OS X");
else
is(promptState.titleHidden, expectedState.titleHidden, "Checking title visibility");
is(promptState.textHidden, expectedState.textHidden, "Checking textbox visibility");
is(promptState.passHidden, expectedState.passHidden, "Checking passbox visibility");
is(promptState.checkHidden, expectedState.checkHidden, "Checking checkbox visibility");
Expand Down
61 changes: 39 additions & 22 deletions toolkit/components/prompts/test/test_bug619644.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,67 @@
<script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<script type="text/javascript" src="prompt_common.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=619644">Mozilla Bug 619644</a>
<pre id="test">
<script class="testbody" type="text/javascript">
function hasTabModalPrompts() {
var prefName = "prompts.tab_modal.enabled";
Services = SpecialPowers.Services;
return Services.prefs.getPrefType(prefName) == Services.prefs.PREF_BOOL &&
Services.prefs.getBoolPref(prefName);
}

if (!hasTabModalPrompts()) {
todo(false, "Test disabled when tab modal prompts are not enabled.");
} else {
SimpleTest.waitForExplicitFinish();

// This is a little yucky, but it works
// The contents of bug619644_inner.html
const expectedFinalDoc =
"<head><\/head><body><p>Original content<\/p>\n<script>\n window.opener.postMessage(\"\", \"*\");\n confirm (\"Message\");\n document.write (\"Extra content\");\n window.opener.postMessage(document.documentElement.innerHTML, \"*\");\n<\/script>Extra content<\/body>";

let gChromeMessageManager;
if (!isTabModal) {
todo(false, "Test disabled when tab modal prompts are not enabled.");
} else {
inittest();
}

function runtest(e)
{
var promptDone;

function inittest() {
window.addEventListener("message", runtest, false);
window.open("bug619644_inner.html", "619644");

SimpleTest.waitForExplicitFinish();
}

function runtest(e) {
window.removeEventListener("message", runtest, false);
window.addEventListener("message", checktest, false);

let url = SimpleTest.getTestFileURL("chromeScript.js");
gChromeMessageManager = SpecialPowers.loadChromeScript(url);
gChromeMessageManager.sendAsyncMessage("cancelPrompt");
state = {
msg : "Message",
iconClass : "question-icon",
titleHidden : true,
textHidden : true,
passHidden : true,
checkHidden : true,
textValue : "",
passValue : "",
checkMsg : "",
checked : false,
focused : "button0",
defButton : "button0",
};
action = {
buttonClick: "ESC",
};

promptDone = handlePrompt(action);
}

function checktest(e) {
is(e.data, expectedFinalDoc, "ESC press should not abort document load");
e.source.close();
gChromeMessageManager.destroy();
SimpleTest.finish();
promptDone.then(endtest);
}

window.addEventListener("message", runtest, false);
window.open("bug619644_inner.html", "619644");
function endtest() {
info("Ending test");
SimpleTest.finish();
}
</script>
</pre>
Expand Down
Loading

0 comments on commit 9c5d8cf

Please sign in to comment.