Bug 840124 - implement postMessage API for remote report (tests) r=jaws, f=gps+gavin

This commit is contained in:
Mike Connor 2013-03-14 21:25:04 -04:00
parent 9361e306d3
commit 4c315e1971
4 changed files with 216 additions and 0 deletions

View File

@ -315,6 +315,9 @@ _BROWSER_FILES = \
browser_pluginCrashCommentAndURL.js \ browser_pluginCrashCommentAndURL.js \
pluginCrashCommentAndURL.html \ pluginCrashCommentAndURL.html \
browser_private_no_prompt.js \ browser_private_no_prompt.js \
browser_aboutHealthReport.js \
healthreport_testPostMessage.html \
healthreport_testRemoteCommands.html \
$(NULL) $(NULL)
# Disable test on Windows due to frequent failures (bug 841341) # Disable test on Windows due to frequent failures (bug 841341)

View File

@ -0,0 +1,153 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/
*/
XPCOMUtils.defineLazyModuleGetter(this, "Promise",
"resource://gre/modules/commonjs/sdk/core/promise.js");
XPCOMUtils.defineLazyModuleGetter(this, "Task",
"resource://gre/modules/Task.jsm");
registerCleanupFunction(function() {
// Ensure we don't pollute prefs for next tests.
try {
Services.prefs.clearUserPref("datareporting.healthreport.about.reportUrl");
let policy = Cc["@mozilla.org/datareporting/service;1"]
.getService(Ci.nsISupports)
.wrappedJSObject
.policy;
policy.recordHealthReportUploadEnabled(true,
"Resetting after tests.");
} catch (ex) {}
});
let gTests = [
{
desc: "Check that about:healthreport loads correctly",
setup: function ()
{
},
run: function ()
{
is(gBrowser.selectedTab.linkedBrowser.currentURI.spec, "about:healthreport", "Page loaded correctly");
}
},
{
desc: "Test the postMessage API",
setup: function ()
{
Services.prefs.setCharPref("datareporting.healthreport.about.reportUrl",
"https://example.com/browser/browser/base/content/test/healthreport_testPostMessage.html");
},
run: function ()
{
let deferred = Promise.defer();
let acks = {}
try {
let win = gBrowser.contentWindow;
win.addEventListener("message", function process(e) {
acks[e.data.type] = true;
if (Object.keys(acks).length == 2) {
ok(acks["prefs"], "Prefs sent ok");
ok(acks["payload"], "Payload sent ok");
win.removeEventListener("message", process, false, true);
deferred.resolve();
}
}, false, true);
} catch(e) {
deferred.resolve()
}
return deferred.promise;
}
},
{
desc: "Test the remote commands",
setup: function ()
{
Services.prefs.setCharPref("datareporting.healthreport.about.reportUrl",
"https://example.com/browser/browser/base/content/test/healthreport_testRemoteCommands.html");
},
run: function ()
{
let deferred = Promise.defer();
let policy = Cc["@mozilla.org/datareporting/service;1"]
.getService(Ci.nsISupports)
.wrappedJSObject
.policy;
let acks = {}
try {
let win = gBrowser.contentWindow;
win.addEventListener("message", function testLoad(e) {
if (!e.data.command)
return;
acks[e.data.command] = true;
switch (e.data.command) {
case "DisableDataSubmission":
is(policy.healthReportUploadEnabled, false, "FHR successfully disabled");
break;
case "EnableDataSubmission":
is(policy.healthReportUploadEnabled, true, "FHR successfully enabled");
break;
case "RequestCurrentPrefs":
case "RequestCurrentPayload":
ok(true, e.data.command + " successfully processed");
}
if (Object.keys(acks).length == 4) {
deferred.resolve();
}
}, false, true);
} catch(e) {
ok(false, "Failed to get all commands");
deferred.reject();
}
return deferred.promise;
}
},
]; // gTests
function test()
{
waitForExplicitFinish();
Task.spawn(function () {
for (let test of gTests) {
info(test.desc);
test.setup();
yield promiseNewTabLoadEvent("about:healthreport");
yield test.run();
gBrowser.removeCurrentTab();
}
finish();
});
}
function promiseNewTabLoadEvent(aUrl, aEventType="load")
{
let deferred = Promise.defer();
let tab = gBrowser.selectedTab = gBrowser.addTab(aUrl);
tab.linkedBrowser.addEventListener(aEventType, function load(event) {
tab.linkedBrowser.removeEventListener(aEventType, load, true);
let iframe = tab.linkedBrowser.contentDocument.getElementById("remote-report");
iframe.addEventListener("load", function frameLoad(e) {
iframe.removeEventListener("load", frameLoad, false);
deferred.resolve();
}, false);
}, true);
return deferred.promise;
}

View File

@ -0,0 +1,24 @@
<html>
<head>
<meta charset="utf-8">
<script>
function init() {
window.addEventListener("message", receiveMessage, false);
}
function receiveMessage(event) {
acknowledgeMessage(event.data.type);
}
function acknowledgeMessage(type) {
var data = {type: type};
window.parent.postMessage(data, "*");
}
</script>
</head>
<body onload="init()">
</body>
</html>

View File

@ -0,0 +1,36 @@
<html>
<head>
<meta charset="utf-8">
<script>
function init() {
window.addEventListener("message", function process(e) {runNextTest(e)}, false);
}
var i = 0;
function runNextTest(e) {
if (e.data.type != "prefs" && e.data.type != "payload")
return;
var tests = ["DisableDataSubmission","RequestCurrentPrefs","RequestCurrentPayload","EnableDataSubmission"];
sendToBrowser(tests[i]);
dispatchMessage(tests[i]);
i++;
}
function dispatchMessage(cmd) {
var data = {command: cmd};
window.parent.postMessage(data, "*");
}
function sendToBrowser(type) {
var event = new CustomEvent("RemoteHealthReportCommand", {detail: {command: type}, bubbles: true});
document.dispatchEvent(event);
}
</script>
</head>
<body onload="init()">
</body>
</html>