idbindex-rename-errors.any.js (6042B)
1 // META: title=IndexedDB: index renaming error handling 2 // META: global=window,worker 3 // META: script=resources/support-promises.js 4 5 // Spec: "https://w3c.github.io/IndexedDB/#dom-idbindex-name" 6 7 'use strict'; 8 9 promise_test(testCase => { 10 return createDatabase( 11 testCase, 12 (database, transaction) => { 13 createBooksStore(testCase, database); 14 }) 15 .then(database => { 16 database.close(); 17 }) 18 .then( 19 () => migrateDatabase( 20 testCase, 2, 21 (database, transaction) => { 22 const store = transaction.objectStore('books'); 23 const index = store.index('by_author'); 24 store.deleteIndex('by_author'); 25 assert_throws_dom( 26 'InvalidStateError', 27 () => index.name = 'renamed_by_author'); 28 })) 29 .then(database => database.close()); 30 }, 'IndexedDB deleted index rename throws'); 31 32 promise_test(testCase => { 33 return createDatabase(testCase, (database, transaction) => { 34 createBooksStore(testCase, database); 35 }).then(database => { 36 const transaction = database.transaction('books', 'readonly'); 37 const store = transaction.objectStore('books'); 38 const index = store.index('by_author'); 39 40 assert_throws_dom( 41 'InvalidStateError', () => index.name = 'renamed_by_author'); 42 database.close(); 43 }); 44 }, 'IndexedDB index rename throws in a readonly transaction'); 45 46 promise_test(testCase => { 47 return createDatabase(testCase, (database, transaction) => { 48 createBooksStore(testCase, database); 49 }).then(database => { 50 const transaction = database.transaction('books', 'readwrite'); 51 const store = transaction.objectStore('books'); 52 const index = store.index('by_author'); 53 54 assert_throws_dom( 55 'InvalidStateError', () => index.name = 'renamed_by_author'); 56 database.close(); 57 }); 58 }, 'IndexedDB index rename throws in a readwrite transaction'); 59 60 promise_test(testCase => { 61 let authorIndex = null; 62 return createDatabase(testCase, (database, transaction) => { 63 const store = createBooksStore(testCase, database); 64 authorIndex = store.index('by_author'); 65 }).then(database => { 66 assert_throws_dom( 67 'TransactionInactiveError', 68 () => authorIndex.name = 'renamed_by_author'); 69 database.close(); 70 }); 71 }, 'IndexedDB index rename throws in an inactive transaction'); 72 73 promise_test(testCase => { 74 return createDatabase( 75 testCase, 76 (database, transaction) => { 77 createBooksStore(testCase, database); 78 }) 79 .then(database => { 80 database.close(); 81 }) 82 .then( 83 () => migrateDatabase( 84 testCase, 2, 85 (database, transaction) => { 86 const store = transaction.objectStore('books'); 87 const index = store.index('by_author'); 88 89 assert_throws_dom( 90 'ConstraintError', () => index.name = 'by_title'); 91 assert_array_equals( 92 store.indexNames, ['by_author', 'by_title'], 93 'An index rename that throws an exception should not change the ' + 94 'index\'s IDBObjectStore.indexNames'); 95 })) 96 .then(database => { 97 const transaction = database.transaction('books', 'readonly'); 98 const store = transaction.objectStore('books'); 99 assert_array_equals( 100 store.indexNames, ['by_author', 'by_title'], 101 'Committing a transaction with a failed store rename attempt ' + 102 'should not change the index\'s IDBObjectStore.indexNames'); 103 const index = store.index('by_author'); 104 return checkAuthorIndexContents( 105 testCase, index, 106 'Committing a transaction with a failed rename attempt should ' + 107 'not change the index\'s contents') 108 .then(() => database.close()); 109 }); 110 }, 'IndexedDB index rename to the name of another index throws'); 111 112 promise_test(testCase => { 113 return createDatabase( 114 testCase, 115 (database, transaction) => { 116 createBooksStore(testCase, database); 117 }) 118 .then(database => { 119 database.close(); 120 }) 121 .then( 122 () => migrateDatabase( 123 testCase, 2, 124 (database, transaction) => { 125 const store = transaction.objectStore('books'); 126 const index = store.index('by_author'); 127 const exception = {name: 'Custom stringifying error'}; 128 assert_throws_exactly(exception, () => { 129 index.name = { 130 toString: () => { 131 throw exception; 132 } 133 }; 134 }, 'IDBObjectStore rename should re-raise toString() exception'); 135 assert_array_equals( 136 store.indexNames, ['by_author', 'by_title'], 137 'An index rename that throws an exception should not change the ' + 138 'index\'s IDBObjectStore.indexNames'); 139 })) 140 .then(database => { 141 const transaction = database.transaction('books', 'readonly'); 142 const store = transaction.objectStore('books'); 143 assert_array_equals( 144 store.indexNames, ['by_author', 'by_title'], 145 'Committing a transaction with a failed store rename attempt ' + 146 'should not change the index\'s IDBObjectStore.indexNames'); 147 const index = store.index('by_author'); 148 return checkAuthorIndexContents( 149 testCase, index, 150 'Committing a transaction with a failed rename attempt should ' + 151 'not change the index\'s contents') 152 .then(() => database.close()); 153 }); 154 }, 'IndexedDB index rename handles exceptions when stringifying names');