Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1012)

Side by Side Diff: third_party/WebKit/LayoutTests/http/tests/serviceworker/navigation-preload/get-state.html

Issue 2521793004: service worker: Persist NavigationPreloadState (Closed)
Patch Set: expect_ name Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 <!DOCTYPE html> 1 <!DOCTYPE html>
2 <meta charset="utf-8"> 2 <meta charset="utf-8">
3 <title>NavigationPreloadManager.getState</title> 3 <title>NavigationPreloadManager.getState</title>
4 <script src="../../resources/testharness.js"></script> 4 <script src="../../resources/testharness.js"></script>
5 <script src="../../resources/testharnessreport.js"></script> 5 <script src="../../resources/testharnessreport.js"></script>
6 <script src="../resources/test-helpers.js"></script> 6 <script src="../resources/test-helpers.js"></script>
7 <script src="resources/helpers.js"></script>
7 <body> 8 <body>
8 <script> 9 <script>
9 function post_and_wait_for_reply(worker, message) { 10 function post_and_wait_for_reply(worker, message) {
10 return new Promise(resolve => { 11 return new Promise(resolve => {
11 navigator.serviceWorker.onmessage = e => { resolve(e.data); }; 12 navigator.serviceWorker.onmessage = e => { resolve(e.data); };
12 worker.postMessage(message); 13 worker.postMessage(message);
13 }); 14 });
14 } 15 }
15 16
16 function expect_state(state, enabled, header, desc) {
17 assert_equals(Object.keys(state).length, 2, desc + ': # of keys');
18 assert_equals(state.enabled, enabled, desc + ': enabled');
19 assert_equals(state.headerValue, header, desc + ': header');
20 }
21
22 promise_test(t => { 17 promise_test(t => {
23 const scope = '../resources/get-state'; 18 const scope = '../resources/get-state';
24 const script = '../resources/empty-worker.js'; 19 const script = '../resources/empty-worker.js';
25 var np; 20 var np;
26 21
27 return service_worker_unregister_and_register(t, script, scope) 22 return service_worker_unregister_and_register(t, script, scope)
28 .then(r => { 23 .then(r => {
29 np = r.navigationPreload; 24 np = r.navigationPreload;
30 add_completion_callback(() => r.unregister()); 25 add_completion_callback(() => r.unregister());
31 return np.getState(); 26 return wait_for_state(t, r.installing, 'activated');
32 }) 27 })
28 .then(() => np.getState())
33 .then(state => { 29 .then(state => {
34 expect_state(state, false, 'true', 'default state'); 30 expect_navigation_preload_state(state, false, 'true', 'default state');
35 return np.enable(); 31 return np.enable();
36 }) 32 })
37 .then(result => { 33 .then(result => {
38 assert_equals(result, undefined, 34 assert_equals(result, undefined,
39 'enable() should resolve to undefined'); 35 'enable() should resolve to undefined');
40 return np.getState(); 36 return np.getState();
41 }) 37 })
42 .then(state => { 38 .then(state => {
43 expect_state(state, true, 'true', 'state after enable()'); 39 expect_navigation_preload_state(state, true, 'true',
40 'state after enable()');
44 return np.disable(); 41 return np.disable();
45 }) 42 })
46 .then(result => { 43 .then(result => {
47 assert_equals(result, undefined, 44 assert_equals(result, undefined,
48 'disable() should resolve to undefined'); 45 'disable() should resolve to undefined');
49 return np.getState(); 46 return np.getState();
50 }) 47 })
51 .then(state => { 48 .then(state => {
52 expect_state(state, false, 'true', 'state after disable()'); 49 expect_navigation_preload_state(state, false, 'true',
50 'state after disable()');
53 return np.setHeaderValue('dreams that cannot be'); 51 return np.setHeaderValue('dreams that cannot be');
54 }) 52 })
55 .then(result => { 53 .then(result => {
56 assert_equals(result, undefined, 54 assert_equals(result, undefined,
57 'setHeaderValue() should resolve to undefined'); 55 'setHeaderValue() should resolve to undefined');
58 return np.getState(); 56 return np.getState();
59 }) 57 })
60 .then(state => { 58 .then(state => {
61 expect_state(state, false, 'dreams that cannot be', 59 expect_navigation_preload_state(state, false, 'dreams that cannot be',
62 'state after setHeaderValue()'); 60 'state after setHeaderValue()');
63 return np.setHeaderValue('').then(() => np.getState()); 61 return np.setHeaderValue('').then(() => np.getState());
64 }) 62 })
65 .then(state => { 63 .then(state => {
66 expect_state(state, false, '', 'after setHeaderValue to empty string'); 64 expect_navigation_preload_state(state, false, '',
65 'after setHeaderValue to empty string');
67 return np.setHeaderValue(null).then(() => np.getState()); 66 return np.setHeaderValue(null).then(() => np.getState());
68 }) 67 })
69 .then(state => { 68 .then(state => {
70 expect_state(state, false, 'null', 'after setHeaderValue to null'); 69 expect_navigation_preload_state(state, false, 'null',
70 'after setHeaderValue to null');
71 return promise_rejects(t, 71 return promise_rejects(t,
72 new TypeError, 72 new TypeError,
73 np.setHeaderValue('what\uDC00\uD800this'), 73 np.setHeaderValue('what\uDC00\uD800this'),
74 'setHeaderValue() should throw if passed surrogates'); 74 'setHeaderValue() should throw if passed surrogates');
75 }) 75 })
76 .then(() => { 76 .then(() => {
77 return promise_rejects(t, 77 return promise_rejects(t,
78 new TypeError, 78 new TypeError,
79 np.setHeaderValue('zer\0o'), 79 np.setHeaderValue('zer\0o'),
80 'setHeaderValue() should throw if passed \\0'); 80 'setHeaderValue() should throw if passed \\0');
81 }) 81 })
82 .then(() => { 82 .then(() => {
83 return promise_rejects(t, 83 return promise_rejects(t,
84 new TypeError, 84 new TypeError,
85 np.setHeaderValue('\rcarriage'), 85 np.setHeaderValue('\rcarriage'),
86 'setHeaderValue() should throw if passed \\r'); 86 'setHeaderValue() should throw if passed \\r');
87 }) 87 })
88 .then(() => { 88 .then(() => {
89 return promise_rejects(t, 89 return promise_rejects(t,
90 new TypeError, 90 new TypeError,
91 np.setHeaderValue('newline\n'), 91 np.setHeaderValue('newline\n'),
92 'setHeaderValue() should throw if passed \\n'); 92 'setHeaderValue() should throw if passed \\n');
93 }) 93 })
94 .then(() => {
95 return promise_rejects(t,
96 new TypeError,
97 np.setHeaderValue(),
98 'setHeaderValue() should throw if passed undefined');
99 })
94 .then(() => np.enable().then(() => np.getState())) 100 .then(() => np.enable().then(() => np.getState()))
95 .then(state => { 101 .then(state => {
96 expect_state(state, true, 'null', 'enable() should not change header'); 102 expect_navigation_preload_state(state, true, 'null',
103 'enable() should not change header');
97 }); 104 });
98 }, 'getState'); 105 }, 'getState');
99 106
100 // This test sends commands to a worker to call enable()/disable()/getState(). 107 // This test sends commands to a worker to call enable()/disable()/getState().
101 // It checks the results from the worker and verifies that they match the 108 // It checks the results from the worker and verifies that they match the
102 // navigation preload state accessible from the page. 109 // navigation preload state accessible from the page.
103 promise_test(t => { 110 promise_test(t => {
104 const scope = 'resources/get-state-worker'; 111 const scope = 'resources/get-state-worker';
105 const script = 'resources/get-state-worker.js'; 112 const script = 'resources/get-state-worker.js';
106 var worker; 113 var worker;
107 var registration; 114 var registration;
108 115
109 return service_worker_unregister_and_register(t, script, scope) 116 return service_worker_unregister_and_register(t, script, scope)
110 .then(r => { 117 .then(r => {
111 registration = r; 118 registration = r;
112 add_completion_callback(() => registration.unregister()); 119 add_completion_callback(() => registration.unregister());
113 worker = registration.installing; 120 worker = registration.installing;
121 return wait_for_state(t, worker, 'activated');
122 })
123 .then(() => {
114 // Call getState(). 124 // Call getState().
115 return post_and_wait_for_reply(worker, 'getState'); 125 return post_and_wait_for_reply(worker, 'getState');
116 }) 126 })
117 .then(data => { 127 .then(data => {
118 return Promise.all([data, registration.navigationPreload.getState()]); 128 return Promise.all([data, registration.navigationPreload.getState()]);
119 }) 129 })
120 .then(states => { 130 .then(states => {
121 expect_state(states[0], false, 'true', 131 expect_navigation_preload_state(states[0], false, 'true',
122 'default state (from worker)'); 132 'default state (from worker)');
123 expect_state(states[1], false, 'true', 133 expect_navigation_preload_state(states[1], false, 'true',
124 'default state (from page)'); 134 'default state (from page)');
125 // Call enable() and then getState(). 135 // Call enable() and then getState().
126 return post_and_wait_for_reply(worker, 'enable'); 136 return post_and_wait_for_reply(worker, 'enable');
127 }) 137 })
128 .then(data => { 138 .then(data => {
129 assert_equals(data, undefined, 'enable() should resolve to undefined'); 139 assert_equals(data, undefined, 'enable() should resolve to undefined');
130 return Promise.all([ 140 return Promise.all([
131 post_and_wait_for_reply(worker, 'getState'), 141 post_and_wait_for_reply(worker, 'getState'),
132 registration.navigationPreload.getState() 142 registration.navigationPreload.getState()
133 ]); 143 ]);
134 }) 144 })
135 .then(states => { 145 .then(states => {
136 expect_state(states[0], true, 'true', 146 expect_navigation_preload_state(states[0], true, 'true',
137 'state after enable() (from worker)'); 147 'state after enable() (from worker)');
138 expect_state(states[1], true, 'true', 148 expect_navigation_preload_state(states[1], true, 'true',
139 'state after enable() (from page)'); 149 'state after enable() (from page)');
140 // Call disable() and then getState(). 150 // Call disable() and then getState().
141 return post_and_wait_for_reply(worker, 'disable'); 151 return post_and_wait_for_reply(worker, 'disable');
142 }) 152 })
143 .then(data => { 153 .then(data => {
144 assert_equals(data, undefined, 154 assert_equals(data, undefined,
145 '.disable() should resolve to undefined'); 155 '.disable() should resolve to undefined');
146 return Promise.all([ 156 return Promise.all([
147 post_and_wait_for_reply(worker, 'getState'), 157 post_and_wait_for_reply(worker, 'getState'),
148 registration.navigationPreload.getState() 158 registration.navigationPreload.getState()
149 ]); 159 ]);
150 }) 160 })
151 .then(states => { 161 .then(states => {
152 expect_state(states[0], false, 'true', 162 expect_navigation_preload_state(states[0], false, 'true',
153 'state after disable() (from worker)'); 163 'state after disable() (from worker)');
154 expect_state(states[1], false, 'true', 164 expect_navigation_preload_state(states[1], false, 'true',
155 'state after disable() (from page)'); 165 'state after disable() (from page)');
156 return post_and_wait_for_reply(worker, 'setHeaderValue'); 166 return post_and_wait_for_reply(worker, 'setHeaderValue');
157 }) 167 })
158 .then(data => { 168 .then(data => {
159 assert_equals(data, undefined, 169 assert_equals(data, undefined,
160 '.setHeaderValue() should resolve to undefined'); 170 '.setHeaderValue() should resolve to undefined');
161 return Promise.all([ 171 return Promise.all([
162 post_and_wait_for_reply(worker, 'getState'), 172 post_and_wait_for_reply(worker, 'getState'),
163 registration.navigationPreload.getState()]); 173 registration.navigationPreload.getState()]);
164 }) 174 })
165 .then(states => { 175 .then(states => {
166 expect_state(states[0], false, 'insightful', 176 expect_navigation_preload_state(
167 'state after setHeaderValue() (from worker)'); 177 states[0], false, 'insightful',
168 expect_state(states[1], false, 'insightful', 178 'state after setHeaderValue() (from worker)');
169 'state after setHeaderValue() (from page)'); 179 expect_navigation_preload_state(
180 states[1], false, 'insightful',
181 'state after setHeaderValue() (from page)');
170 }); 182 });
171 }, 'getState from a worker'); 183 }, 'getState from a worker');
184
185 // This tests navigation preload API when there is no active worker. It calls
186 // the API from the main page and then from the worker itself.
187 promise_test(t => {
188 const scope = 'resources/wait-for-activate-worker';
189 const script = 'resources/wait-for-activate-worker.js';
190 var registration;
191 var np;
192 return service_worker_unregister_and_register(t, script, scope)
193 .then(r => {
194 registration = r;
195 np = registration.navigationPreload;
196 add_completion_callback(() => registration.unregister());
197 return Promise.all([
198 promise_rejects(
199 t, 'InvalidStateError', np.enable(),
200 'enable should reject if there is no active worker'),
201 promise_rejects(
202 t, 'InvalidStateError', np.disable(),
203 'disable should reject if there is no active worker'),
204 promise_rejects(
205 t, 'InvalidStateError', np.setHeaderValue('umm'),
206 'setHeaderValue should reject if there is no active worker')]);
207 })
208 .then(() => np.getState())
209 .then(state => {
210 expect_navigation_preload_state(state, false, 'true',
211 'state before activation');
212 return post_and_wait_for_reply(registration.installing, 'ping');
213 })
214 .then(result => assert_equals(result, 'PASS'));
215 }, 'no active worker');
172 </script> 216 </script>
173 </body> 217 </body>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698