domstringlist.html (2393B)
1 <!doctype html> 2 <title>DOMStringList</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <script> 6 7 // Returns a promise that resolves to a DOMStringList with 8 // the requested entries. Relies on Indexed DB. 9 function createDOMStringList(entries) { 10 return new Promise((resolve, reject) => { 11 const dbname = String(self.location + Math.random()); 12 const request = indexedDB.open(dbname); 13 request.onerror = () => reject(request.error); 14 request.onupgradeneeded = () => { 15 const db = request.result; 16 entries.forEach(entry => db.createObjectStore(entry)); 17 const dsl = db.objectStoreNames; 18 resolve(dsl); 19 request.transaction.abort(); 20 } 21 }); 22 } 23 24 function dsl_test(entries, func, description) { 25 promise_test(t => createDOMStringList(entries).then(dsl => func(t, dsl)), 26 description); 27 } 28 29 dsl_test(['a', 'b', 'c'], (t, dsl) => { 30 assert_equals(dsl.length, 3, 'length attribute'); 31 }, 'DOMStringList: length attribute'); 32 33 dsl_test(['a', 'b', 'c'], (t, dsl) => { 34 assert_equals(dsl.item(0), 'a', 'item method'); 35 assert_equals(dsl.item(1), 'b', 'item method'); 36 assert_equals(dsl.item(2), 'c', 'item method'); 37 assert_equals(dsl.item(3), null, 'item method out of range'); 38 assert_equals(dsl.item(-1), null, 'item method out of range'); 39 assert_throws_js(TypeError, () => dsl.item(), 40 'item method should throw if called without enough args'); 41 }, 'DOMStringList: item() method'); 42 43 dsl_test(['a', 'b', 'c'], (t, dsl) => { 44 assert_equals(dsl[0], 'a', 'indexed getter'); 45 assert_equals(dsl[1], 'b', 'indexed getter'); 46 assert_equals(dsl[2], 'c', 'indexed getter'); 47 assert_equals(dsl[3], undefined, 'indexed getter out of range'); 48 assert_equals(dsl[-1], undefined, 'indexed getter out of range'); 49 }, 'DOMStringList: indexed getter'); 50 51 dsl_test(['a', 'b', 'c'], (t, dsl) => { 52 assert_true(dsl.contains('a'), 'contains method matched'); 53 assert_true(dsl.contains('b'), 'contains method matched'); 54 assert_true(dsl.contains('c'), 'contains method matched'); 55 assert_false(dsl.contains(''), 'contains method unmatched'); 56 assert_false(dsl.contains('d'), 'contains method unmatched'); 57 assert_throws_js(TypeError, () => dsl.contains(), 58 'contains method should throw if called without enough args'); 59 }, 'DOMStringList: contains() method'); 60 61 </script>