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