Bug 1246341 - Add a test for push event error reporting. r=dragana

MozReview-Commit-ID: LABOJnYtpD5

--HG--
extra : rebase_source : 86c27b8a2deb388cdb34a82aebd12b042477acb5
This commit is contained in:
Kit Cambridge 2016-03-28 11:40:58 -07:00
parent d97d284c24
commit 18c5c296ef
7 changed files with 113 additions and 9 deletions

View File

@ -65,9 +65,6 @@ NS_IMETHODIMP
PushNotifier::NotifySubscriptionChange(const nsACString& aScope,
nsIPrincipal* aPrincipal)
{
if (XRE_IsContentProcess()) {
return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult rv;
if (ShouldNotifyObservers(aPrincipal)) {
rv = NotifySubscriptionChangeObservers(aScope);
@ -89,9 +86,6 @@ PushNotifier::NotifyPush(const nsACString& aScope, nsIPrincipal* aPrincipal,
const nsAString& aMessageId,
const Maybe<nsTArray<uint8_t>>& aData)
{
if (XRE_IsContentProcess()) {
return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult rv;
if (ShouldNotifyObservers(aPrincipal)) {
rv = NotifyPushObservers(aScope, aData);

View File

@ -29,9 +29,8 @@ namespace dom {
* forwards incoming push messages to service workers running in the content
* process, and emits XPCOM observer notifications for system subscriptions.
*
* The XPCOM service can only be used from the main process. Callers running
* in the content process should use
* `ServiceWorkerManager::SendPush{SubscriptionChange}Event` directly.
* This service exists solely to support `PushService.jsm`. Other callers
* should use `ServiceWorkerManager` directly.
*/
class PushNotifier final : public nsIPushNotifier
{

View File

@ -0,0 +1,10 @@
this.onpush = function(event) {
var request = event.data.json();
if (request.type == "exception") {
throw new Error("Uncaught exception");
}
if (request.type == "rejection") {
event.waitUntil(Promise.reject(
new Error("Unhandled rejection")));
}
};

View File

@ -7,6 +7,7 @@ support-files =
lifetime_worker.js
test_utils.js
mockpushserviceparent.js
error_worker.js
[test_has_permissions.html]
[test_permissions.html]
@ -19,3 +20,4 @@ support-files =
[test_data.html]
[test_try_registering_offline_disabled.html]
[test_serviceworker_lifetime.html]
[test_error_reporting.html]

View File

@ -157,6 +157,13 @@ var MockService = {
unregister(pageRecord) {
return this.sendRequest("unregister", pageRecord);
},
reportDeliveryError(messageId, reason) {
sendAsyncMessage("service-delivery-error", {
messageId: messageId,
reason: reason,
});
},
};
addMessageListener("service-replace", function () {

View File

@ -0,0 +1,89 @@
<!DOCTYPE HTML>
<html>
<!--
Bug 1246341: Report message delivery failures to the Push server.
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/licenses/publicdomain/
-->
<head>
<title>Test for Bug 1246341</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
<script type="text/javascript" src="/tests/dom/push/test/test_utils.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1246341">Mozilla Bug 1246341</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
</pre>
<script class="testbody" type="text/javascript">
var pushNotifier = SpecialPowers.Cc["@mozilla.org/push/Notifier;1"]
.getService(SpecialPowers.Ci.nsIPushNotifier);
var reporters = new Map();
var registration;
add_task(function* start() {
yield setupPrefsAndReplaceService({
reportDeliveryError(messageId, reason) {
ok(reporters.has(messageId),
'Unexpected error reported for message ' + messageId);
var resolve = reporters.get(messageId);
reporters.delete(messageId);
resolve(reason);
},
});
yield setPushPermission(true);
var url = "error_worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
});
var controlledFrame;
add_task(function* createControlledIFrame() {
controlledFrame = yield injectControlledFrame();
});
var idCounter = 1;
function waitForDeliveryError(request) {
return new Promise(resolve => {
var data = new TextEncoder("utf-8").encode(JSON.stringify(request));
var principal = SpecialPowers.wrap(document).nodePrincipal;
let messageId = "message-" + (idCounter++);
reporters.set(messageId, resolve);
pushNotifier.notifyPushWithData(registration.scope, principal, messageId,
data.length, data);
});
}
add_task(function* reportErrors() {
var reason = yield waitForDeliveryError({ type: "exception" });
is(reason, SpecialPowers.Ci.nsIPushErrorReporter.DELIVERY_UNCAUGHT_EXCEPTION,
"Should report uncaught exceptions");
reason = yield waitForDeliveryError({ type: "rejection" });
is(reason, SpecialPowers.Ci.nsIPushErrorReporter.DELIVERY_UNHANDLED_REJECTION,
"Should report unhandled rejections");
});
add_task(function* unsubscribe() {
controlledFrame.remove();
});
add_task(function* unregister() {
yield registration.unregister();
});
</script>
</body>
</html>

View File

@ -11,6 +11,9 @@
*/
function replacePushService(mockService) {
chromeScript.sendSyncMessage("service-replace");
chromeScript.addMessageListener("service-delivery-error", function(msg) {
mockService.reportDeliveryError(msg.messageId, msg.reason);
});
chromeScript.addMessageListener("service-request", function(msg) {
let promise;
try {