OLD | NEW |
---|---|
1 // Supports test-runner control messages being send over |messagePort|, which en able | 1 // Supports test-runner control messages being send over |messagePort|, which en able |
2 // workers to have limited access to TestRunner methods. | 2 // workers to have limited access to TestRunner methods. |
3 function supportTestRunnerMessagesOnPort(messagePort) | 3 function supportTestRunnerMessagesOnPort(messagePort) |
4 { | 4 { |
5 if (!window.testRunner) | 5 if (!window.testRunner) |
6 return; | 6 return; |
7 | 7 |
8 messagePort.addEventListener('message', function(message) { | 8 messagePort.addEventListener('message', function(message) { |
9 if (message.data.type == 'simulateWebNotificationClick') | 9 if (message.data.type == 'simulateWebNotificationClick') |
10 testRunner.simulateWebNotificationClick(message.data.title, -1 /* ac tion_index */); | 10 testRunner.simulateWebNotificationClick(message.data.title, -1 /* ac tion_index */); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 messageChannel.port1.addEventListener('message', function(event) { | 59 messageChannel.port1.addEventListener('message', function(event) { |
60 if (event.data == 'ready') | 60 if (event.data == 'ready') |
61 resolve({ registration: registration, port: messageChannel.p ort1 }); | 61 resolve({ registration: registration, port: messageChannel.p ort1 }); |
62 }); | 62 }); |
63 | 63 |
64 registration.active.postMessage(messageChannel.port2, [messageChanne l.port2]); | 64 registration.active.postMessage(messageChannel.port2, [messageChanne l.port2]); |
65 messageChannel.port1.start(); | 65 messageChannel.port1.start(); |
66 }); | 66 }); |
67 }); | 67 }); |
68 } | 68 } |
69 | |
70 // Sends a message with |data| over |port|. Returns a promise that either reject s when a bad | |
71 // response message is received, or otherwise resolves with the response data. | |
72 function sendCommand(port, data) | |
73 { | |
74 return new Promise((resolve, reject) => { | |
75 port.postMessage(data); | |
76 port.addEventListener('message', function listener(event) { | |
77 port.removeEventListener('message', listener); | |
78 if (typeof event.data != 'object' || !event.data.command) { | |
79 reject('Invalid message from the Service Worker.'); | |
Peter Beverloo
2016/04/21 17:20:56
nit: Promises should be rejected with errors.
new
Michael van Ouwerkerk
2016/04/22 09:44:22
Done.
| |
80 } else { | |
81 resolve(event.data); | |
82 } | |
83 }); | |
84 }); | |
85 } | |
86 | |
87 // Simulates a click on the notification whose title equals |title|. The |action Index| specifies | |
88 // which action button to activate, where -1 means the notification itself is cl icked, not an action | |
89 // button. | |
90 function simulateNotificationClick(title, actionIndex, port) | |
91 { | |
92 return new Promise((resolve, reject) => { | |
93 testRunner.simulateWebNotificationClick(title, actionIndex); | |
94 port.addEventListener('message', function listener(event) { | |
95 port.removeEventListener('message', listener); | |
96 if (typeof event.data != 'object' || event.data.command != 'click') { | |
97 reject('Invalid message from the Service Worker.'); | |
98 } else { | |
99 resolve(event.data); | |
100 } | |
101 }); | |
102 }); | |
103 } | |
OLD | NEW |