Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!doctype html> | |
| 2 <meta charset=utf-8> | |
| 3 <title>IndexedDB: Binary keys written to a database and read back</title> | |
| 4 <script src="/resources/testharness.js"></script> | |
| 5 <script src="/resources/testharnessreport.js"></script> | |
| 6 <script src="support.js"></script> | |
| 7 <script> | |
| 8 | |
| 9 const sample = [0x44, 0x33, 0x22, 0x11, 0xFF, 0xEE, 0xDD, 0xCC]; | |
| 10 const buffer = new Uint8Array(sample).buffer; | |
| 11 | |
| 12 function assert_key_valid(a, message) { | |
| 13 assert_equals(indexedDB.cmp(a, a), 0, message); | |
| 14 } | |
| 15 | |
| 16 function assert_buffer_equals(a, b, message) { | |
| 17 assert_array_equals( | |
| 18 [].slice.call(new Uint8Array(a)), | |
|
jsbell
2017/02/07 21:50:17
Change this to Array.from()
pwnall
2017/02/08 00:33:34
Done.
| |
| 19 [].slice.call(new Uint8Array(b)), | |
| 20 message); | |
| 21 } | |
| 22 | |
| 23 // Verifies that a JavaScript value round-trips through IndexedDB as a key. | |
| 24 function check_key_roundtrip_and_done(t, db, key, key_buffer) { | |
| 25 const tx = db.transaction('store', 'readwrite'); | |
| 26 const store = tx.objectStore('store'); | |
| 27 | |
| 28 // Verify put with key | |
| 29 const put_request = store.put('value', key); | |
| 30 put_request.onerror = t.unreached_func('put should succeed'); | |
| 31 | |
| 32 // Verify get with key | |
| 33 const get_request = store.get(key); | |
| 34 get_request.onerror = t.unreached_func('get should succeed'); | |
| 35 get_request.onsuccess = t.step_func(() => { | |
| 36 assert_equals( | |
| 37 get_request.result, 'value', | |
| 38 'get should retrieve the value given to put'); | |
| 39 | |
| 40 // Verify iteration returning key | |
| 41 const cursor_request = store.openCursor(); | |
| 42 cursor_request.onerror = t.unreached_func('openCursor should succeed'); | |
| 43 cursor_request.onsuccess = t.step_func(() => { | |
| 44 assert_not_equals( | |
| 45 cursor_request.result, null, 'cursor should be present'); | |
| 46 const retrieved_key = cursor_request.result.key; | |
| 47 assert_true( | |
| 48 retrieved_key instanceof ArrayBuffer, | |
| 49 'IndexedDB binary keys should be returned in ArrayBuffer instances'); | |
| 50 assert_key_equals( | |
| 51 retrieved_key, key, | |
| 52 'The key returned by IndexedDB should equal the key given to put()'); | |
| 53 assert_buffer_equals( | |
| 54 retrieved_key, key_buffer, | |
| 55 'The ArrayBuffer returned by IndexedDB should equal the buffer ' + | |
| 56 'backing the key given to put()'); | |
| 57 | |
| 58 t.done(); | |
| 59 }); | |
| 60 }); | |
| 61 } | |
| 62 | |
| 63 // Checks that IndexedDB handles the given view type for binary keys correctly. | |
| 64 function view_type_test(type) { | |
| 65 indexeddb_test( | |
| 66 (t, db) => { db.createObjectStore('store'); }, | |
| 67 (t, db) => { | |
| 68 const key = new self[type](buffer); | |
| 69 assert_key_valid(key, `${type} should be usable as an IndexedDB key`); | |
| 70 assert_key_equals(key, buffer, | |
| 71 'Binary keys with the same data but different view types should be ' + | |
| 72 ' equal'); | |
| 73 check_key_roundtrip_and_done(t, db, key, buffer); | |
| 74 }, | |
| 75 `Binary keys can be supplied using the view type ${type}`, | |
| 76 ); | |
| 77 } | |
| 78 | |
| 79 [ | |
| 80 'Uint8Array', | |
| 81 'Uint8ClampedArray', | |
| 82 'Int8Array', | |
| 83 'Uint16Array', | |
| 84 'Int16Array', | |
| 85 'Uint32Array', | |
| 86 'Int32Array', | |
| 87 'Float32Array', | |
| 88 'Float64Array' | |
| 89 ].forEach((type) => { view_type_test(type); }); | |
| 90 | |
| 91 // Checks that IndexedDB | |
| 92 function value_test(value_description, value, value_buffer) { | |
| 93 indexeddb_test( | |
| 94 (t, db) => { db.createObjectStore('store'); }, | |
| 95 (t, db) => { | |
| 96 assert_key_valid( | |
| 97 value, value_description + ' should be usable as an valid key'); | |
| 98 check_key_roundtrip_and_done(t, db, value, value_buffer); | |
| 99 }, | |
| 100 `${value_description} can be used to supply a binary key` | |
| 101 ); | |
| 102 } | |
| 103 | |
| 104 value_test('ArrayBuffer', buffer, buffer); | |
| 105 value_test('DataView', new DataView(buffer), buffer); | |
| 106 value_test('DataView with explicit offset', new DataView(buffer, 3), | |
| 107 new Uint8Array([0x11, 0xFF, 0xEE, 0xDD, 0xCC]).buffer); | |
| 108 value_test('DataView with explicit offset and length', | |
| 109 new DataView(buffer, 3, 4), | |
| 110 new Uint8Array([0x11, 0xFF, 0xEE, 0xDD]).buffer); | |
| 111 value_test('Uint8Array with explicit offset', new Uint8Array(buffer, 3), | |
| 112 new Uint8Array([0x11, 0xFF, 0xEE, 0xDD, 0xCC]).buffer); | |
| 113 value_test('Uint8Array with explicit offset and length', | |
| 114 new Uint8Array(buffer, 3, 4), | |
| 115 new Uint8Array([0x11, 0xFF, 0xEE, 0xDD]).buffer); | |
| 116 | |
| 117 </script> | |
| OLD | NEW |