| OLD | NEW |
| 1 var client = null; | 1 // This helper will setup a small test framework that will use TESTS and run |
| 2 // them iteratively and call self.postMessage('quit') when done. |
| 3 // This helper also exposes |client|, |postMessage()|, |runNextTestOrQuit()|, |
| 4 // |synthesizeNotificationClick()| and |initialize()|. |
| 5 importScripts('sw-test-helpers.js'); |
| 2 | 6 |
| 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 = [ | 7 var TESTS = [ |
| 30 function testWithNoNotificationClick() { | 8 function testWithNoNotificationClick() { |
| 31 clients.openWindow('/foo.html').catch(function() { | 9 clients.openWindow('/foo.html').catch(function() { |
| 32 client.postMessage('openWindow() outside of a notificationclick even
t failed'); | 10 self.postMessage('openWindow() outside of a notificationclick event
failed'); |
| 33 }).then(runNextTestOrQuit); | 11 }).then(runNextTestOrQuit); |
| 34 }, | 12 }, |
| 35 | 13 |
| 36 function testInStackOutOfWaitUntil() { | 14 function testInStackOutOfWaitUntil() { |
| 37 synthesizeNotificationClick().then(function() { | 15 synthesizeNotificationClick().then(function() { |
| 38 clients.openWindow('/foo.html').then(function() { | 16 clients.openWindow('/foo.html').then(function() { |
| 39 client.postMessage('openWindow() in notificationclick outside of
waitUntil but in stack succeeded'); | 17 self.postMessage('openWindow() in notificationclick outside of w
aitUntil but in stack succeeded'); |
| 40 }).then(runNextTestOrQuit); | 18 }).then(runNextTestOrQuit); |
| 41 }); | 19 }); |
| 42 }, | 20 }, |
| 43 | 21 |
| 44 function testOutOfStackOutOfWaitUntil() { | 22 function testOutOfStackOutOfWaitUntil() { |
| 45 synthesizeNotificationClick().then(function() { | 23 synthesizeNotificationClick().then(function() { |
| 46 self.clients.getAll().then(function() { | 24 self.clients.getAll().then(function() { |
| 47 clients.openWindow('/foo.html').catch(function() { | 25 clients.openWindow('/foo.html').catch(function() { |
| 48 client.postMessage('openWindow() in notificationclick outsid
e of waitUntil not in stack failed'); | 26 self.postMessage('openWindow() in notificationclick outside
of waitUntil not in stack failed'); |
| 49 }).then(runNextTestOrQuit); | 27 }).then(runNextTestOrQuit); |
| 50 }); | 28 }); |
| 51 }); | 29 }); |
| 52 }, | 30 }, |
| 53 | 31 |
| 54 function testInWaitUntilAsyncAndDoubleCall() { | 32 function testInWaitUntilAsyncAndDoubleCall() { |
| 55 synthesizeNotificationClick().then(function(e) { | 33 synthesizeNotificationClick().then(function(e) { |
| 56 e.waitUntil(self.clients.getAll().then(function() { | 34 e.waitUntil(self.clients.getAll().then(function() { |
| 57 return clients.openWindow('/foo.html').then(function() { | 35 return clients.openWindow('/foo.html').then(function() { |
| 58 client.postMessage('openWindow() in notificationclick\'s wai
tUntil suceeded'); | 36 self.postMessage('openWindow() in notificationclick\'s waitU
ntil suceeded'); |
| 59 }).then(runNextTestOrQuit); | 37 }).then(runNextTestOrQuit); |
| 60 })); | 38 })); |
| 61 }); | 39 }); |
| 62 }, | 40 }, |
| 63 | 41 |
| 64 function testDoubleCallInWaitUntilAsync() { | 42 function testDoubleCallInWaitUntilAsync() { |
| 65 synthesizeNotificationClick().then(function(e) { | 43 synthesizeNotificationClick().then(function(e) { |
| 66 e.waitUntil(self.clients.getAll().then(function() { | 44 e.waitUntil(self.clients.getAll().then(function() { |
| 67 return clients.openWindow('/foo.html').then(function() { | 45 return clients.openWindow('/foo.html').then(function() { |
| 68 return clients.openWindow('/foo.html'); | 46 return clients.openWindow('/foo.html'); |
| 69 }).catch(function() { | 47 }).catch(function() { |
| 70 client.postMessage('openWindow() called twice failed'); | 48 self.postMessage('openWindow() called twice failed'); |
| 71 }).then(runNextTestOrQuit); | 49 }).then(runNextTestOrQuit); |
| 72 })); | 50 })); |
| 73 }); | 51 }); |
| 74 }, | 52 }, |
| 75 | 53 |
| 76 | 54 |
| 77 function testWaitUntilTimeout() { | 55 function testWaitUntilTimeout() { |
| 78 var p = new Promise(function(resolve) { | 56 var p = new Promise(function(resolve) { |
| 79 setTimeout(function() { | 57 setTimeout(function() { |
| 80 resolve(); | 58 resolve(); |
| 81 }, 2000); | 59 }, 2000); |
| 82 }); | 60 }); |
| 83 | 61 |
| 84 synthesizeNotificationClick().then(function(e) { | 62 synthesizeNotificationClick().then(function(e) { |
| 85 e.waitUntil(p.then(function() { | 63 e.waitUntil(p.then(function() { |
| 86 return clients.openWindow('/foo.html').catch(function() { | 64 return clients.openWindow('/foo.html').catch(function() { |
| 87 client.postMessage('openWindow() failed after timeout'); | 65 self.postMessage('openWindow() failed after timeout'); |
| 88 }).then(runNextTestOrQuit); | 66 }).then(runNextTestOrQuit); |
| 89 })); | 67 })); |
| 90 }); | 68 }); |
| 91 }, | 69 }, |
| 92 | 70 |
| 93 function testFocusWindowOpenWindowCombo() { | 71 function testFocusWindowOpenWindowCombo() { |
| 94 synthesizeNotificationClick().then(function(e) { | 72 synthesizeNotificationClick().then(function(e) { |
| 95 e.waitUntil(client.focus().then(function() { | 73 e.waitUntil(client.focus().then(function() { |
| 96 clients.openWindow().catch(function() { | 74 clients.openWindow().catch(function() { |
| 97 client.postMessage('openWindow() failed because a window was
focused before'); | 75 self.postMessage('openWindow() failed because a window was f
ocused before'); |
| 98 }).then(runNextTestOrQuit); | 76 }).then(runNextTestOrQuit); |
| 99 })); | 77 })); |
| 100 }); | 78 }); |
| 101 }, | 79 }, |
| 102 ]; | 80 ]; |
| 103 | 81 |
| 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) { | 82 self.onmessage = function(e) { |
| 114 if (e.data == "start") { | 83 if (e.data == "start") { |
| 115 initialize().then(runNextTestOrQuit); | 84 initialize().then(runNextTestOrQuit); |
| 116 } else { | 85 } else { |
| 117 initialize().then(function() { | 86 initialize().then(function() { |
| 118 client.postMessage('received unexpected message'); | 87 self.postMessage('received unexpected message'); |
| 119 client.postMessage('quit'); | 88 self.postMessage('quit'); |
| 120 }); | 89 }); |
| 121 } | 90 } |
| 122 } | 91 } |
| OLD | NEW |