Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(790)

Unified Diff: LayoutTests/http/tests/serviceworker/chromium/resources/sw-test-helpers.js

Issue 879403005: [ServiceWorker] Tests for Clients.openWindow(). (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@window_interaction_tests
Patch Set: cleanup and add test helper Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « LayoutTests/http/tests/serviceworker/chromium/resources/notificationclick-can-openwindow.js ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: LayoutTests/http/tests/serviceworker/chromium/resources/sw-test-helpers.js
diff --git a/LayoutTests/http/tests/serviceworker/chromium/resources/sw-test-helpers.js b/LayoutTests/http/tests/serviceworker/chromium/resources/sw-test-helpers.js
new file mode 100644
index 0000000000000000000000000000000000000000..b30e7ddb733baf76c5cd61f22867fdbc6afeb88e
--- /dev/null
+++ b/LayoutTests/http/tests/serviceworker/chromium/resources/sw-test-helpers.js
@@ -0,0 +1,72 @@
+// Test helper that is meant as a mini test framework to be used from a service
+// worker that runs some tests and send results back to its client.
+//
+// A simple usage of this framework would consist of calling initialize() to
+// setup then runNextTestOrQuit() in order to start running the methods defined
+// by TESTS. Then, tests can start sending messages back to the client using
+// postMessage().
+//
+// Example:
+// var TESTS = [
+// function simpleTest() {
+// self.postMessage('you will receive this first');
+// },
+// function secondTest() {
+// self.postMessage('secondTest done!');
+// runNextTestOrQuit();
+// }
+// ];
+//
+// initialize().runNextTestOrQuit();
+//
+// In addition, there is a helper method meant to synthesized notificationclick
+// events sent to a service worker, see synthesizeNotificationClick.
+
+var client = null;
+var currentTest = -1;
+
+self.initialize = function() {
+ return self.clients.getAll().then(function(clients) {
+ client = clients[0];
+ });
+}
+
+self.postMessage = function(msg) {
+ client.postMessage(msg);
+}
+
+// Run the next test in TESTS if any. Otherwise sends a 'quit' message. and
+// stops.
+// In order for that call to succeed, the script MUST have a TESTS array
+// defined.
+self.runNextTestOrQuit = function() {
+ ++currentTest;
+ if (currentTest >= TESTS.length) {
+ client.postMessage('quit');
+ return;
+ }
+ TESTS[currentTest]();
+}
+
+// This method will use the |client| in order to synthesize a notificationclick
+// event. The client will then use the testRunner.
+// The returned promise will be resolved with the notificationclick event
+// object.
+self.synthesizeNotificationClick = function() {
+ var promise = new Promise(function(resolve) {
+ var title = "fake notification";
+ registration.showNotification(title).then(function() {
+ client.postMessage({type: 'click', title: title});
+ });
+
+ var handler = function(e) {
+ resolve(e);
+ e.notification.close();
+ self.removeEventListener('notificationclick', handler);
+ };
+
+ self.addEventListener('notificationclick', handler);
+ });
+
+ return promise;
+}
« no previous file with comments | « LayoutTests/http/tests/serviceworker/chromium/resources/notificationclick-can-openwindow.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698