OLD | NEW |
---|---|
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <title>Service Worker: Indexed DB</title> | 2 <title>Service Worker: Indexed DB</title> |
3 <script src="/resources/testharness.js"></script> | 3 <script src="/resources/testharness.js"></script> |
4 <script src="/resources/testharnessreport.js"></script> | 4 <script src="/resources/testharnessreport.js"></script> |
5 <script src="resources/test-helpers.sub.js"></script> | 5 <script src="resources/test-helpers.sub.js"></script> |
6 <script> | 6 <script> |
7 async_test(function(t) { | 7 function readDB() { |
8 var scope = 'resources/blank.html'; | 8 return new Promise(function(resolve, reject) { |
9 service_worker_unregister_and_register( | 9 var openRequest = indexedDB.open('db'); |
10 t, 'resources/indexeddb-worker.js', scope) | |
11 .then(function(registration) { | |
12 var sw = registration.installing; | |
13 var messageChannel = new MessageChannel(); | |
14 messageChannel.port1.onmessage = t.step_func(onMessage); | |
15 sw.postMessage({port: messageChannel.port2}, [messageChannel.port2]); | |
16 }) | |
17 .catch(unreached_rejection(t)); | |
18 | 10 |
19 function onMessage() { | 11 openRequest.onerror = reject; |
20 var openRequest = indexedDB.open('db'); | 12 openRequest.onsuccess = function() { |
21 openRequest.onsuccess = t.step_func(function() { | |
22 var db = openRequest.result; | 13 var db = openRequest.result; |
23 var tx = db.transaction('store'); | 14 var tx = db.transaction('store'); |
24 var store = tx.objectStore('store'); | 15 var store = tx.objectStore('store'); |
25 var getRequest = store.get('key'); | 16 var getRequest = store.get('key'); |
26 getRequest.onsuccess = t.step_func(function() { | 17 |
27 assert_equals( | 18 getRequest.onerror = function() { |
28 getRequest.result, 'value', | 19 db.close(); |
29 'The get() result should match what the worker put().'); | 20 reject(getRequest.error); |
30 service_worker_unregister_and_done(t, scope); | 21 }; |
31 }); | 22 getRequest.onsuccess = function() { |
23 db.close(); | |
24 resolve(getRequest.result); | |
25 }; | |
26 }; | |
27 }); | |
28 } | |
29 | |
30 function send(worker, action) { | |
31 return new Promise(function(resolve, reject) { | |
32 var messageChannel = new MessageChannel(); | |
33 messageChannel.port1.onmessage = function(event) { | |
34 if (event.data.type === 'error') { | |
35 reject(event.data.reason); | |
36 } | |
37 | |
38 resolve(); | |
39 }; | |
40 | |
41 worker.postMessage( | |
42 {action: action, port: messageChannel.port2}, | |
43 [messageChannel.port2]); | |
44 }); | |
45 } | |
46 | |
47 promise_test(function(t) { | |
48 var scope = 'resources/blank.html'; | |
49 var cleanup, keyValue; | |
50 | |
51 return service_worker_unregister_and_register( | |
52 t, 'resources/indexeddb-worker.js', scope) | |
53 .then(function(registration) { | |
54 var worker = registration.installing; | |
55 | |
56 promise_test(function() { | |
57 return registration.unregister(); | |
58 }, 'clean up: unregister worker'); | |
59 | |
60 return send(worker, 'create') | |
61 .then(function() { | |
62 promise_test(function() { | |
63 return send(worker, 'cleanup'); | |
Marijn Kruisselbrink
2017/05/16 18:17:44
Does this work? The registration.unregister() from
mike3
2017/05/17 22:07:40
It does indeed work, but I think the state we're d
| |
64 }, 'clean up: delete database'); | |
65 }) | |
66 .then(readDB) | |
67 .then(function(value) { | |
68 assert_equals( | |
69 value, 'value', | |
70 'The get() result should match what the worker put().'); | |
71 }); | |
32 }); | 72 }); |
33 } | |
34 }, 'Verify Indexed DB operation in a Service Worker'); | 73 }, 'Verify Indexed DB operation in a Service Worker'); |
35 </script> | 74 </script> |
OLD | NEW |