| 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 Array.from(new Uint8Array(a)), Array.from(new Uint8Array(b)), message); |
| 19 } |
| 20 |
| 21 // Verifies that a JavaScript value round-trips through IndexedDB as a key. |
| 22 function check_key_roundtrip_and_done(t, db, key, key_buffer) { |
| 23 const tx = db.transaction('store', 'readwrite'); |
| 24 const store = tx.objectStore('store'); |
| 25 |
| 26 // Verify put with key |
| 27 const put_request = store.put('value', key); |
| 28 put_request.onerror = t.unreached_func('put should succeed'); |
| 29 |
| 30 // Verify get with key |
| 31 const get_request = store.get(key); |
| 32 get_request.onerror = t.unreached_func('get should succeed'); |
| 33 get_request.onsuccess = t.step_func(() => { |
| 34 assert_equals( |
| 35 get_request.result, 'value', |
| 36 'get should retrieve the value given to put'); |
| 37 |
| 38 // Verify iteration returning key |
| 39 const cursor_request = store.openCursor(); |
| 40 cursor_request.onerror = t.unreached_func('openCursor should succeed'); |
| 41 cursor_request.onsuccess = t.step_func(() => { |
| 42 assert_not_equals( |
| 43 cursor_request.result, null, 'cursor should be present'); |
| 44 const retrieved_key = cursor_request.result.key; |
| 45 assert_true( |
| 46 retrieved_key instanceof ArrayBuffer, |
| 47 'IndexedDB binary keys should be returned in ArrayBuffer instances'); |
| 48 assert_key_equals( |
| 49 retrieved_key, key, |
| 50 'The key returned by IndexedDB should equal the key given to put()'); |
| 51 assert_buffer_equals( |
| 52 retrieved_key, key_buffer, |
| 53 'The ArrayBuffer returned by IndexedDB should equal the buffer ' + |
| 54 'backing the key given to put()'); |
| 55 |
| 56 t.done(); |
| 57 }); |
| 58 }); |
| 59 } |
| 60 |
| 61 // Checks that IndexedDB handles the given view type for binary keys correctly. |
| 62 function view_type_test(type) { |
| 63 indexeddb_test( |
| 64 (t, db) => { db.createObjectStore('store'); }, |
| 65 (t, db) => { |
| 66 const key = new self[type](buffer); |
| 67 assert_key_valid(key, `${type} should be usable as an IndexedDB key`); |
| 68 assert_key_equals(key, buffer, |
| 69 'Binary keys with the same data but different view types should be ' + |
| 70 ' equal'); |
| 71 check_key_roundtrip_and_done(t, db, key, buffer); |
| 72 }, |
| 73 `Binary keys can be supplied using the view type ${type}`, |
| 74 ); |
| 75 } |
| 76 |
| 77 [ |
| 78 'Uint8Array', |
| 79 'Uint8ClampedArray', |
| 80 'Int8Array', |
| 81 'Uint16Array', |
| 82 'Int16Array', |
| 83 'Uint32Array', |
| 84 'Int32Array', |
| 85 'Float32Array', |
| 86 'Float64Array' |
| 87 ].forEach((type) => { view_type_test(type); }); |
| 88 |
| 89 // Checks that IndexedDB |
| 90 function value_test(value_description, value, value_buffer) { |
| 91 indexeddb_test( |
| 92 (t, db) => { db.createObjectStore('store'); }, |
| 93 (t, db) => { |
| 94 assert_key_valid( |
| 95 value, value_description + ' should be usable as an valid key'); |
| 96 check_key_roundtrip_and_done(t, db, value, value_buffer); |
| 97 }, |
| 98 `${value_description} can be used to supply a binary key` |
| 99 ); |
| 100 } |
| 101 |
| 102 value_test('ArrayBuffer', buffer, buffer); |
| 103 value_test('DataView', new DataView(buffer), buffer); |
| 104 value_test('DataView with explicit offset', new DataView(buffer, 3), |
| 105 new Uint8Array([0x11, 0xFF, 0xEE, 0xDD, 0xCC]).buffer); |
| 106 value_test('DataView with explicit offset and length', |
| 107 new DataView(buffer, 3, 4), |
| 108 new Uint8Array([0x11, 0xFF, 0xEE, 0xDD]).buffer); |
| 109 value_test('Uint8Array with explicit offset', new Uint8Array(buffer, 3), |
| 110 new Uint8Array([0x11, 0xFF, 0xEE, 0xDD, 0xCC]).buffer); |
| 111 value_test('Uint8Array with explicit offset and length', |
| 112 new Uint8Array(buffer, 3, 4), |
| 113 new Uint8Array([0x11, 0xFF, 0xEE, 0xDD]).buffer); |
| 114 |
| 115 </script> |
| OLD | NEW |