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 |
| 50 return service_worker_unregister_and_register( |
| 51 t, 'resources/indexeddb-worker.js', scope) |
| 52 .then(function(registration) { |
| 53 var worker = registration.installing; |
| 54 |
| 55 promise_test(function() { |
| 56 return registration.unregister(); |
| 57 }, 'clean up: registration'); |
| 58 |
| 59 return send(worker, 'create') |
| 60 .then(function() { |
| 61 promise_test(function() { |
| 62 return new Promise(function(resolve, reject) { |
| 63 var delete_request = indexedDB.deleteDatabase('db'); |
| 64 |
| 65 delete_request.onsuccess = resolve; |
| 66 delete_request.onerror = reject; |
| 67 }); |
| 68 }, 'clean up: database'); |
| 69 }) |
| 70 .then(readDB) |
| 71 .then(function(value) { |
| 72 assert_equals( |
| 73 value, 'value', |
| 74 'The get() result should match what the worker put().'); |
| 75 }); |
32 }); | 76 }); |
33 } | |
34 }, 'Verify Indexed DB operation in a Service Worker'); | 77 }, 'Verify Indexed DB operation in a Service Worker'); |
35 </script> | 78 </script> |
OLD | NEW |