idbobjectstore-cross-realm-methods.html (5156B)
1 <!DOCTYPE html> 2 <meta charset=utf-8> 3 <title>Cross-realm IDBObjectStore methods from detached iframe work as expected</title> 4 <script src=/resources/testharness.js></script> 5 <script src=/resources/testharnessreport.js></script> 6 <script src=resources/support.js></script> 7 8 <body> 9 <script> 10 "use strict"; 11 const KEY_EXISTING_LOWER = 1000; 12 const KEY_EXISTING_UPPER = 1001; 13 const KEY_EXISTING_RANGE = IDBKeyRange.bound(KEY_EXISTING_LOWER, KEY_EXISTING_UPPER); 14 const KEY_NEWLY_ADDED = 1002; 15 16 const VALUE_EXISTING_LOWER = "VALUE_EXISTING_LOWER"; 17 const VALUE_EXISTING_UPPER = "VALUE_EXISTING_UPPER"; 18 const VALUE_NEWLY_ADDED = "VALUE_NEWLY_ADDED"; 19 20 const testCases = [ 21 { 22 methodName: "put", 23 arguments: [KEY_NEWLY_ADDED, KEY_EXISTING_LOWER], 24 validateResult: (t, e) => { 25 assert_equals(e.target.result, KEY_EXISTING_LOWER); 26 const rq = e.target.source.getAll(); 27 rq.onsuccess = t.step_func_done(e => { 28 assert_array_equals(e.target.result, [KEY_NEWLY_ADDED, VALUE_EXISTING_UPPER]); 29 }); 30 }, 31 }, 32 { 33 methodName: "add", 34 arguments: [VALUE_NEWLY_ADDED, KEY_NEWLY_ADDED], 35 validateResult: (t, e) => { 36 assert_equals(e.target.result, KEY_NEWLY_ADDED); 37 const rq = e.target.source.getAll(); 38 rq.onsuccess = t.step_func_done(e => { 39 assert_array_equals(e.target.result, [VALUE_EXISTING_LOWER, VALUE_EXISTING_UPPER, VALUE_NEWLY_ADDED]); 40 }); 41 }, 42 }, 43 { 44 methodName: "delete", 45 arguments: [KEY_EXISTING_LOWER], 46 validateResult: (t, e) => { 47 assert_equals(e.target.result, undefined); 48 const rq = e.target.source.getAllKeys(); 49 rq.onsuccess = t.step_func_done(e => { 50 assert_array_equals(e.target.result, [KEY_EXISTING_UPPER]); 51 }); 52 }, 53 }, 54 { 55 methodName: "clear", 56 arguments: [], 57 validateResult: (t, e) => { 58 assert_equals(e.target.result, undefined); 59 const rq = e.target.source.count(); 60 rq.onsuccess = t.step_func_done(e => { 61 assert_equals(e.target.result, 0); 62 }); 63 }, 64 }, 65 { 66 methodName: "get", 67 arguments: [KEY_EXISTING_UPPER], 68 validateResult: (t, e) => { 69 assert_equals(e.target.result, VALUE_EXISTING_UPPER); 70 t.done(); 71 }, 72 }, 73 { 74 methodName: "getKey", 75 arguments: [KEY_EXISTING_LOWER], 76 validateResult: (t, e) => { 77 assert_equals(e.target.result, KEY_EXISTING_LOWER); 78 t.done(); 79 }, 80 }, 81 { 82 methodName: "getAll", 83 arguments: [KEY_EXISTING_RANGE], 84 validateResult: (t, e) => { 85 assert_array_equals(e.target.result, [VALUE_EXISTING_LOWER, VALUE_EXISTING_UPPER]); 86 t.done(); 87 }, 88 }, 89 { 90 methodName: "getAllKeys", 91 arguments: [KEY_EXISTING_RANGE], 92 validateResult: (t, e) => { 93 assert_array_equals(e.target.result, [KEY_EXISTING_LOWER, KEY_EXISTING_UPPER]); 94 t.done(); 95 }, 96 }, 97 { 98 methodName: "count", 99 arguments: [], 100 validateResult: (t, e) => { 101 assert_equals(e.target.result, 2); 102 t.done(); 103 }, 104 }, 105 { 106 methodName: "openCursor", 107 arguments: [], 108 validateResult: (t, e) => { 109 const cursor = e.target.result; 110 assert_true(cursor instanceof IDBCursor); 111 assert_equals(cursor.value, VALUE_EXISTING_LOWER); 112 t.done(); 113 }, 114 }, 115 { 116 methodName: "openKeyCursor", 117 arguments: [], 118 validateResult: (t, e) => { 119 const cursor = e.target.result; 120 assert_true(cursor instanceof IDBCursor); 121 assert_equals(cursor.key, KEY_EXISTING_LOWER); 122 t.done(); 123 }, 124 }, 125 ]; 126 127 for (const testCase of testCases) { 128 async_test(t => { 129 const iframe = document.createElement("iframe"); 130 iframe.onload = t.step_func(() => { 131 const method = iframe.contentWindow.IDBObjectStore.prototype[testCase.methodName]; 132 iframe.remove(); 133 134 let db; 135 const open_rq = createdb(t); 136 open_rq.onupgradeneeded = t.step_func(e => { 137 db = e.target.result; 138 const objectStore = db.createObjectStore("store"); 139 objectStore.add(VALUE_EXISTING_LOWER, KEY_EXISTING_LOWER); 140 objectStore.add(VALUE_EXISTING_UPPER, KEY_EXISTING_UPPER); 141 }); 142 143 open_rq.onsuccess = t.step_func(() => { 144 const objectStore = db.transaction("store", "readwrite").objectStore("store"); 145 const rq = method.call(objectStore, ...testCase.arguments); 146 rq.onsuccess = t.step_func(e => { 147 testCase.validateResult(t, e); 148 }); 149 }); 150 }); 151 document.body.append(iframe); 152 }, `Cross-realm IDBObjectStore::${testCase.methodName}() method from detached <iframe> works as expected`); 153 } 154 </script>