idbobjectstore-rename-errors.any.js (5703B)
1 // META: global=window,worker 2 // META: title=IndexedDB: object store renaming error handling 3 // META: script=resources/support-promises.js 4 5 // Spec: https://w3c.github.io/IndexedDB/#dom-idbobjectstore-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 database.deleteObjectStore('books'); 24 assert_throws_dom( 25 'InvalidStateError', () => store.name = 'renamed_books'); 26 })) 27 .then(database => { 28 database.close(); 29 }); 30 }, 'IndexedDB deleted object store 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 assert_throws_dom('InvalidStateError', () => store.name = 'renamed_books'); 39 database.close(); 40 }); 41 }, 'IndexedDB object store rename throws in a readonly transaction'); 42 43 promise_test(testCase => { 44 return createDatabase(testCase, (database, transaction) => { 45 createBooksStore(testCase, database); 46 }).then(database => { 47 const transaction = database.transaction('books', 'readwrite'); 48 const store = transaction.objectStore('books'); 49 50 assert_throws_dom('InvalidStateError', () => store.name = 'renamed_books'); 51 database.close(); 52 }); 53 }, 'IndexedDB object store rename throws in a readwrite transaction'); 54 55 promise_test(testCase => { 56 let bookStore = null; 57 return createDatabase(testCase, (database, transaction) => { 58 bookStore = createBooksStore(testCase, database); 59 }).then(database => { 60 assert_throws_dom('TransactionInactiveError', () => { 61 bookStore.name = 'renamed_books'; 62 }); 63 database.close(); 64 }); 65 }, 'IndexedDB object store rename throws in an inactive transaction'); 66 67 promise_test(testCase => { 68 return createDatabase( 69 testCase, 70 (database, transaction) => { 71 createBooksStore(testCase, database); 72 createNotBooksStore(testCase, database); 73 }) 74 .then(database => { 75 database.close(); 76 }) 77 .then( 78 () => migrateDatabase( 79 testCase, 2, 80 (database, transaction) => { 81 const store = transaction.objectStore('books'); 82 assert_throws_dom( 83 'ConstraintError', () => store.name = 'not_books'); 84 assert_array_equals( 85 database.objectStoreNames, ['books', 'not_books'], 86 'A store rename that throws an exception should not change the ' + 87 'store\'s IDBDatabase.objectStoreNames'); 88 })) 89 .then(database => { 90 assert_array_equals( 91 database.objectStoreNames, ['books', 'not_books'], 92 'Committing a transaction with a failed store rename attempt ' + 93 'should not change the store\'s IDBDatabase.objectStoreNames'); 94 const transaction = database.transaction('books', 'readonly'); 95 const store = transaction.objectStore('books'); 96 return checkStoreContents( 97 testCase, store, 98 'Committing a transaction with a failed rename attempt should ' + 99 'not change the store\'s contents') 100 .then(() => database.close()); 101 }); 102 }, 'IndexedDB object store rename to the name of another store throws'); 103 104 promise_test(testCase => { 105 return createDatabase( 106 testCase, 107 (database, transaction) => { 108 createBooksStore(testCase, database); 109 }) 110 .then(database => { 111 database.close(); 112 }) 113 .then( 114 () => migrateDatabase( 115 testCase, 2, 116 (database, transaction) => { 117 const store = transaction.objectStore('books'); 118 let exception = {name: 'Custom stringifying error'}; 119 assert_throws_exactly(exception, () => { 120 store.name = { 121 toString: () => { 122 throw exception; 123 } 124 }; 125 }, 'IDBObjectStore rename should re-raise toString() exception'); 126 assert_array_equals( 127 database.objectStoreNames, ['books'], 128 'A store rename that throws an exception should not change the ' + 129 'store\'s IDBDatabase.objectStoreNames'); 130 })) 131 .then(database => { 132 assert_array_equals( 133 database.objectStoreNames, ['books'], 134 'Committing a transaction with a failed store rename attempt ' + 135 'should not change the store\'s IDBDatabase.objectStoreNames'); 136 const transaction = database.transaction('books', 'readonly'); 137 const store = transaction.objectStore('books'); 138 return checkStoreContents( 139 testCase, store, 140 'Committing a transaction with a failed rename attempt should ' + 141 'not change the store\'s contents') 142 .then(() => database.close()); 143 }); 144 }, 'IndexedDB object store rename handles exceptions when stringifying names');