Bug 1191931, Part 1 - Use tasks in the Push subscription tests. r=mt

--HG--
extra : commitid : 5Pl1YQzsI9W
extra : rebase_source : d7acb5a8841fe60fa3aec5618f687da74ab0ed9a
This commit is contained in:
Kit Cambridge 2016-01-28 07:57:42 -08:00
Родитель 165b9138a5
Коммит f7a0799918
6 изменённых файлов: 114 добавлений и 155 удалений

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -11,6 +11,7 @@ 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">
@ -25,25 +26,10 @@ 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 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);
});
yield setupPrefs();
yield setPushPermission(true);
var url = "worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
@ -51,15 +37,7 @@ http://creativecommons.org/licenses/publicdomain/
var controlledFrame;
add_task(function* createControlledIFrame() {
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);
});
controlledFrame = yield injectControlledFrame();
});
var pushSubscription;
@ -67,16 +45,6 @@ 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, '/');
@ -138,7 +106,7 @@ http://creativecommons.org/licenses/publicdomain/
function waitForMessage(pushSubscription, message) {
return Promise.all([
controlledFrame.contentWindow.waitOnPushMessage(pushSubscription),
controlledFrame.waitOnPushMessage(),
webpush(pushSubscription, message),
]).then(([message]) => message);
}
@ -190,11 +158,10 @@ 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.contentWindow.waitOnPushMessage(pushSubscription),
controlledFrame.waitOnPushMessage(),
fetch("http://mochi.test:8888/tests/dom/push/test/push-server.sjs", {
method: "PUT",
headers: {
@ -207,8 +174,7 @@ http://creativecommons.org/licenses/publicdomain/
});
add_task(function* unsubscribe() {
controlledFrame.parentNode.removeChild(controlledFrame);
controlledFrame = null;
controlledFrame.remove();
yield pushSubscription.unsubscribe();
});

Просмотреть файл

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

Просмотреть файл

@ -10,6 +10,8 @@ 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>
@ -27,112 +29,53 @@ http://creativecommons.org/licenses/publicdomain/
// console.log(str + "\n");
}
var controlledFrame;
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";
iframe.onload = function() {
res(swr)
}
controlledFrame = iframe;
document.body.appendChild(iframe);
});
return p;
}
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;
});
}
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");
}
var registration;
add_task(function* start() {
yield setupPrefs();
yield setPushPermission(true);
function start() {
return navigator.serviceWorker.register("worker.js" + "?" + (Math.random()), {scope: "."})
.then((swr) => registration = swr);
}
var url = "worker.js" + "?" + (Math.random());
registration = yield navigator.serviceWorker.register(url, {scope: "."});
});
function unregister() {
return registration.unregister().then(function(result) {
ok(result, "Unregister should return true.");
}, function(e) {
dump("Unregistering the SW failed with " + e + "\n");
});
}
var controlledFrame;
add_task(function* createControlledIFrame() {
controlledFrame = yield injectControlledFrame();
});
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;
}
add_task(function* checkPermissionState() {
var state = yield registration.pushManager.permissionState();
is(state, "granted", "permissionState() should resolve to granted.");
});
function unregisterPushNotification(pushSubscription) {
controlledFrame.parentNode.removeChild(controlledFrame);
controlledFrame = null;
return pushSubscription.unsubscribe();
}
var pushSubscription;
add_task(function* setupPushNotification() {
pushSubscription = yield registration.pushManager.subscribe();
});
function waitForPushNotification(pushSubscription) {
var p = controlledFrame.contentWindow.waitOnPushMessage();
sendPushToPushServer(pushSubscription.endpoint);
return p.then(function() {
return pushSubscription;
});
}
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 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);
}
add_task(function* unsubscribe() {
controlledFrame.remove();
yield pushSubscription.unsubscribe();
});
add_task(function* unregister() {
var result = yield registration.unregister();
ok(result, "Unregister should return true.");
});
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>

Просмотреть файл

@ -0,0 +1,60 @@
// 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]);
});
});
}