OLD | NEW |
(Empty) | |
| 1 var client = null; |
| 2 |
| 3 function initialize() { |
| 4 return self.clients.getAll().then(function(clients) { |
| 5 client = clients[0]; |
| 6 }); |
| 7 } |
| 8 |
| 9 function synthesizeNotificationClick() { |
| 10 var promise = new Promise(function(resolve) { |
| 11 var title = "fake notification"; |
| 12 |
| 13 registration.showNotification(title).then(function() { |
| 14 client.postMessage({type: 'click', title: title}); |
| 15 }); |
| 16 |
| 17 var handler = function(e) { |
| 18 self.removeEventListener('notificationclick', handler); |
| 19 e.notification.close(); |
| 20 |
| 21 resolve(e); |
| 22 }; |
| 23 |
| 24 self.addEventListener('notificationclick', handler); |
| 25 }); |
| 26 |
| 27 return promise; |
| 28 } |
| 29 |
| 30 var currentTest = -1; |
| 31 var TESTS = [ |
| 32 function testWithNoNotificationClick() { |
| 33 client.focus().catch(function() { |
| 34 client.postMessage('focus() outside of a notificationclick event fai
led'); |
| 35 }).then(runNextTestOrQuit); |
| 36 }, |
| 37 |
| 38 function testInStackOutOfWaitUntil() { |
| 39 synthesizeNotificationClick().then(function() { |
| 40 client.focus().then(function() { |
| 41 client.postMessage('focus() in notificationclick outside of wait
Until but in stack succeeded'); |
| 42 }).then(runNextTestOrQuit); |
| 43 }); |
| 44 }, |
| 45 |
| 46 function testOutOfStackOutOfWaitUntil() { |
| 47 synthesizeNotificationClick().then(function() { |
| 48 self.clients.getAll().then(function() { |
| 49 client.focus().catch(function() { |
| 50 client.postMessage('focus() in notificationclick outside of
waitUntil not in stack failed'); |
| 51 }).then(runNextTestOrQuit); |
| 52 }); |
| 53 }); |
| 54 }, |
| 55 |
| 56 function testInWaitUntilAsync() { |
| 57 synthesizeNotificationClick().then(function(e) { |
| 58 e.waitUntil(self.clients.getAll().then(function() { |
| 59 return client.focus().then(function() { |
| 60 client.postMessage('focus() in notificationclick\'s waitUnti
l suceeded'); |
| 61 }).then(runNextTestOrQuit); |
| 62 })); |
| 63 }); |
| 64 }, |
| 65 |
| 66 function testDoubleCallInWaitUntilAsync() { |
| 67 synthesizeNotificationClick().then(function(e) { |
| 68 e.waitUntil(self.clients.getAll().then(function() { |
| 69 return client.focus().then(function() { |
| 70 return client.focus(); |
| 71 }).catch(function() { |
| 72 client.postMessage('focus() called twice failed'); |
| 73 }).then(runNextTestOrQuit); |
| 74 })); |
| 75 }); |
| 76 }, |
| 77 |
| 78 function testWaitUntilTimeout() { |
| 79 var p = new Promise(function(resolve) { |
| 80 setTimeout(function() { |
| 81 resolve(); |
| 82 }, 2000); |
| 83 }); |
| 84 |
| 85 synthesizeNotificationClick().then(function(e) { |
| 86 e.waitUntil(p.then(function() { |
| 87 return client.focus().catch(function() { |
| 88 client.postMessage('focus() failed after timeout'); |
| 89 }).then(runNextTestOrQuit); |
| 90 })); |
| 91 }); |
| 92 }, |
| 93 |
| 94 function testFocusWindowOpenWindowCombo() { |
| 95 synthesizeNotificationClick().then(function(e) { |
| 96 e.waitUntil(clients.openWindow('/foo.html').then(function() { |
| 97 client.focus().catch(function() { |
| 98 client.postMessage('focus() failed because a window was open
ed before'); |
| 99 }).then(runNextTestOrQuit); |
| 100 })); |
| 101 }); |
| 102 }, |
| 103 ]; |
| 104 |
| 105 function runNextTestOrQuit() { |
| 106 ++currentTest; |
| 107 if (currentTest >= TESTS.length) { |
| 108 client.postMessage('quit'); |
| 109 return; |
| 110 } |
| 111 TESTS[currentTest](); |
| 112 } |
| 113 |
| 114 self.onmessage = function(e) { |
| 115 if (e.data == "start") { |
| 116 initialize().then(runNextTestOrQuit); |
| 117 } else { |
| 118 initialize().then(function() { |
| 119 client.postMessage('received unexpected message'); |
| 120 client.postMessage('quit'); |
| 121 }); |
| 122 } |
| 123 } |
OLD | NEW |