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