Backed out changeset 02bcedf966fd (bug 1191931) for still failing push perma failure

This commit is contained in:
Carsten "Tomcat" Book 2016-02-01 12:41:03 +01:00
parent 6f89c9107b
commit 9a95326890
6 changed files with 156 additions and 115 deletions

View File

@ -5,7 +5,7 @@
<script>
function waitOnPushMessage()
function waitOnPushMessage(pushSubscription)
{
var p = new Promise(function(res, rej) {
navigator.serviceWorker.onmessage = function(e) {

View File

@ -6,7 +6,6 @@ support-files =
frame.html
webpush.js
lifetime_worker.js
test_utils.js
[test_has_permissions.html]
skip-if = os == "android" || toolkit == "gonk"

View File

@ -11,7 +11,6 @@ http://creativecommons.org/licenses/publicdomain/
<title>Test for Bug 1185544</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>
<script type="text/javascript" src="/tests/dom/push/test/webpush.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
@ -26,10 +25,25 @@ http://creativecommons.org/licenses/publicdomain/
<script class="testbody" type="text/javascript">
SimpleTest.registerCleanupFunction(() =>
new Promise(resolve => SpecialPowers.popPermissions(resolve))
);
var registration;
add_task(function* start() {
yield setupPrefs();
yield setPushPermission(true);
yield new Promise(resolve => {
SpecialPowers.pushPermissions([
{ type: "desktop-notification", allow: true, context: document },
], resolve);
});
yield new Promise(resolve => {
SpecialPowers.pushPrefEnv({"set": [
["dom.push.enabled", true],
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, resolve);
});
var url = "worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
@ -37,7 +51,15 @@ http://creativecommons.org/licenses/publicdomain/
var controlledFrame;
add_task(function* createControlledIFrame() {
controlledFrame = yield injectControlledFrame();
yield new Promise(function(res, rej) {
var iframe = document.createElement('iframe');
iframe.id = "controlledFrame";
iframe.src = "http://mochi.test:8888/tests/dom/push/test/frame.html";
iframe.onload = () => res();
controlledFrame = iframe;
document.body.appendChild(iframe);
});
});
var pushSubscription;
@ -45,6 +67,16 @@ http://creativecommons.org/licenses/publicdomain/
pushSubscription = yield registration.pushManager.subscribe();
});
function sendRequestToWorker(request) {
return new Promise((resolve, reject) => {
var channel = new MessageChannel();
channel.port1.onmessage = e => {
(e.data.error ? reject : resolve)(e.data);
};
registration.active.postMessage(request, [channel.port2]);
});
}
function base64UrlDecode(s) {
s = s.replace(/-/g, '+').replace(/_/g, '/');
@ -106,7 +138,7 @@ http://creativecommons.org/licenses/publicdomain/
function waitForMessage(pushSubscription, message) {
return Promise.all([
controlledFrame.waitOnPushMessage(),
controlledFrame.contentWindow.waitOnPushMessage(pushSubscription),
webpush(pushSubscription, message),
]).then(([message]) => message);
}
@ -158,10 +190,11 @@ http://creativecommons.org/licenses/publicdomain/
reader.readAsText(message.data.blob);
});
is(text, "Hi! \ud83d\udc40", "Wrong blob data for message with emoji");
is(text, "Hi! \ud83d\udc40", "Wrong blob data for message with emoji");
// Send a blank message.
var [message] = yield Promise.all([
controlledFrame.waitOnPushMessage(),
controlledFrame.contentWindow.waitOnPushMessage(pushSubscription),
fetch("http://mochi.test:8888/tests/dom/push/test/push-server.sjs", {
method: "PUT",
headers: {
@ -174,7 +207,8 @@ http://creativecommons.org/licenses/publicdomain/
});
add_task(function* unsubscribe() {
controlledFrame.remove();
controlledFrame.parentNode.removeChild(controlledFrame);
controlledFrame = null;
yield pushSubscription.unsubscribe();
});

View File

@ -11,7 +11,6 @@ http://creativecommons.org/licenses/publicdomain/
<title>Test for Bug 1038811</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>
@ -31,8 +30,15 @@ http://creativecommons.org/licenses/publicdomain/
var registration;
add_task(function* start() {
yield setupPrefs();
yield setPushPermission(false);
SpecialPowers.addPermission("desktop-notification", false, document);
yield new Promise(resolve => {
SpecialPowers.pushPrefEnv({"set": [
["dom.push.enabled", true],
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, resolve);
});
var url = "worker.js" + "?" + Math.random();
registration = yield navigator.serviceWorker.register(url, {scope: "."});
@ -69,7 +75,12 @@ http://creativecommons.org/licenses/publicdomain/
state: "prompt",
}];
for (var test of tests) {
yield setPushPermission(test.action);
if (test.action == permissionManager.UNKNOWN_ACTION) {
SpecialPowers.removePermission("desktop-notification", document);
} else {
SpecialPowers.addPermission("desktop-notification",
test.action, document);
}
var state = yield registration.pushManager.permissionState();
is(state, test.state, JSON.stringify(test));
}

View File

@ -10,8 +10,6 @@ http://creativecommons.org/licenses/publicdomain/
<head>
<title>Test for Bug 1038811</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>
@ -29,53 +27,112 @@ http://creativecommons.org/licenses/publicdomain/
// console.log(str + "\n");
}
var registration;
add_task(function* start() {
yield setupPrefs();
yield setPushPermission(true);
var url = "worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
});
var controlledFrame;
add_task(function* createControlledIFrame() {
controlledFrame = yield injectControlledFrame();
});
function createControlledIFrame(swr) {
var p = new Promise(function(res, rej) {
var iframe = document.createElement('iframe');
iframe.id = "controlledFrame";
iframe.src = "http://mochi.test:8888/tests/dom/push/test/frame.html";
add_task(function* checkPermissionState() {
var state = yield registration.pushManager.permissionState();
is(state, "granted", "permissionState() should resolve to granted.");
});
iframe.onload = function() {
res(swr)
}
controlledFrame = iframe;
document.body.appendChild(iframe);
});
return p;
}
var pushSubscription;
add_task(function* setupPushNotification() {
pushSubscription = yield registration.pushManager.subscribe();
});
function checkPermissionState(swr) {
return swr.pushManager.permissionState().then(function(state) {
ok(state === "granted", "permissionState() should resolve to granted.");
return swr;
}).catch(function(e) {
ok(false, "permissionState() should resolve to granted.");
return swr;
});
}
add_task(function* waitForPushNotification() {
yield Promise.all([
controlledFrame.waitOnPushMessage(),
fetch("http://mochi.test:8888/tests/dom/push/test/push-server.sjs", {
method: "PUT",
headers: {
"X-Push-Method": "POST",
"X-Push-Server": pushSubscription.endpoint,
},
}),
]);
});
function sendPushToPushServer(pushEndpoint) {
// Work around CORS for now.
var xhr = new XMLHttpRequest();
xhr.open('GET', "http://mochi.test:8888/tests/dom/push/test/push-server.sjs", true);
xhr.setRequestHeader("X-Push-Method", "PUT");
xhr.setRequestHeader("X-Push-Server", pushEndpoint);
xhr.onload = function(e) {
debug("xhr : " + this.status);
}
xhr.onerror = function(e) {
debug("xhr error: " + e);
}
xhr.send("version=24601");
}
add_task(function* unsubscribe() {
controlledFrame.remove();
yield pushSubscription.unsubscribe();
});
var registration;
add_task(function* unregister() {
var result = yield registration.unregister();
ok(result, "Unregister should return true.");
});
function start() {
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: "."})
.then((swr) => registration = swr);
}
function unregister() {
return registration.unregister().then(function(result) {
ok(result, "Unregister should return true.");
}, function(e) {
dump("Unregistering the SW failed with " + e + "\n");
});
}
function setupPushNotification(swr) {
var p = new Promise(function(res, rej) {
swr.pushManager.subscribe().then(
function(pushSubscription) {
ok(true, "successful registered for push notification");
res(pushSubscription);
}, function(error) {
ok(false, "could not register for push notification");
res(null);
}
);
});
return p;
}
function unregisterPushNotification(pushSubscription) {
controlledFrame.parentNode.removeChild(controlledFrame);
controlledFrame = null;
return pushSubscription.unsubscribe();
}
function waitForPushNotification(pushSubscription) {
var p = controlledFrame.contentWindow.waitOnPushMessage();
sendPushToPushServer(pushSubscription.endpoint);
return p.then(function() {
return pushSubscription;
});
}
function runTest() {
start()
.then(createControlledIFrame)
.then(checkPermissionState)
.then(setupPushNotification)
.then(waitForPushNotification)
.then(unregisterPushNotification)
.then(unregister)
.catch(function(e) {
ok(false, "Some test failed with error " + e);
}).then(SimpleTest.finish);
}
SpecialPowers.pushPrefEnv({"set": [
["dom.push.enabled", true],
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, runTest);
SpecialPowers.addPermission("desktop-notification", true, document);
SimpleTest.waitForExplicitFinish();
</script>
</body>
</html>

View File

@ -1,60 +0,0 @@
// Remove permissions and prefs when the test finishes.
SimpleTest.registerCleanupFunction(() =>
new Promise(resolve => {
SpecialPowers.flushPermissions(_ => {
SpecialPowers.flushPrefEnv(resolve);
});
})
);
function setPushPermission(allow) {
return new Promise(resolve => {
SpecialPowers.pushPermissions([
{ type: "desktop-notification", allow, context: document },
], resolve);
});
}
function setupPrefs() {
return new Promise(resolve => {
SpecialPowers.pushPrefEnv({"set": [
["dom.push.enabled", true],
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true]
]}, resolve);
});
}
function injectControlledFrame(target = document.body) {
return new Promise(function(res, rej) {
var iframe = document.createElement("iframe");
iframe.src = "http://mochi.test:8888/tests/dom/push/test/frame.html";
var controlledFrame = {
remove() {
target.removeChild(iframe);
iframe = null;
},
waitOnPushMessage() {
return iframe ? iframe.contentWindow.waitOnPushMessage() :
Promise.reject(new Error("Frame removed from document"));
},
};
iframe.onload = () => res(controlledFrame);
target.appendChild(iframe);
});
}
function sendRequestToWorker(request) {
return navigator.serviceWorker.ready.then(registration => {
return new Promise((resolve, reject) => {
var channel = new MessageChannel();
channel.port1.onmessage = e => {
(e.data.error ? reject : resolve)(e.data);
};
registration.active.postMessage(request, [channel.port2]);
});
});
}