| OLD | NEW |
| 1 'use strict'; | 1 'use strict'; |
| 2 | 2 |
| 3 // Returns an IndexedDB database name that is unique to the test case. | 3 // Returns an IndexedDB database name that is unique to the test case. |
| 4 function databaseName(testCase) { | 4 function databaseName(testCase) { |
| 5 return 'db' + self.location.pathname + '-' + testCase.name; | 5 return 'db' + self.location.pathname + '-' + testCase.name; |
| 6 } | 6 } |
| 7 | 7 |
| 8 // Creates an EventWatcher covering all the events that can be issued by | 8 // Creates an EventWatcher covering all the events that can be issued by |
| 9 // IndexedDB requests and transactions. | 9 // IndexedDB requests and transactions. |
| 10 function requestWatcher(testCase, request) { | 10 function requestWatcher(testCase, request) { |
| 11 return new EventWatcher(testCase, request, | 11 return new EventWatcher(testCase, request, |
| 12 ['abort', 'blocked', 'complete', 'error', 'success', 'upgradeneeded']); | 12 ['abort', 'blocked', 'complete', 'error', 'success', 'upgradeneeded']); |
| 13 } | 13 } |
| 14 | 14 |
| 15 // Migrates an IndexedDB database whose name is unique for the test case. | 15 // Migrates an IndexedDB database whose name is unique for the test case. |
| 16 // | 16 // |
| 17 // newVersion must be greater than the database's current version. | 17 // newVersion must be greater than the database's current version. |
| 18 // | 18 // |
| 19 // migrationCallback will be called during a versionchange transaction and will | 19 // migrationCallback will be called during a versionchange transaction and will |
| 20 // given the created database, the versionchange transaction, and the database | 20 // given the created database, the versionchange transaction, and the database |
| 21 // open request. | 21 // open request. |
| 22 // | 22 // |
| 23 // Returns a promise. If the versionchange transaction goes through, the promise | 23 // Returns a promise. If the versionchange transaction goes through, the promise |
| 24 // resolves to an IndexedDB database that must be closed by the caller. If the | 24 // resolves to an IndexedDB database that should be closed by the caller. If the |
| 25 // versionchange transaction is aborted, the promise resolves to an error. | 25 // versionchange transaction is aborted, the promise resolves to an error. |
| 26 function migrateDatabase(testCase, newVersion, migrationCallback) { | 26 function migrateDatabase(testCase, newVersion, migrationCallback) { |
| 27 return migrateNamedDatabase( | 27 return migrateNamedDatabase( |
| 28 testCase, databaseName(testCase), newVersion, migrationCallback); | 28 testCase, databaseName(testCase), newVersion, migrationCallback); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Migrates an IndexedDB database. | 31 // Migrates an IndexedDB database. |
| 32 // | 32 // |
| 33 // newVersion must be greater than the database's current version. | 33 // newVersion must be greater than the database's current version. |
| 34 // | 34 // |
| 35 // migrationCallback will be called during a versionchange transaction and will | 35 // migrationCallback will be called during a versionchange transaction and will |
| 36 // given the created database, the versionchange transaction, and the database | 36 // given the created database, the versionchange transaction, and the database |
| 37 // open request. | 37 // open request. |
| 38 // | 38 // |
| 39 // Returns a promise. If the versionchange transaction goes through, the promise | 39 // Returns a promise. If the versionchange transaction goes through, the promise |
| 40 // resolves to an IndexedDB database that must be closed by the caller. If the | 40 // resolves to an IndexedDB database that should be closed by the caller. If the |
| 41 // versionchange transaction is aborted, the promise resolves to an error. | 41 // versionchange transaction is aborted, the promise resolves to an error. |
| 42 function migrateNamedDatabase( | 42 function migrateNamedDatabase( |
| 43 testCase, databaseName, newVersion, migrationCallback) { | 43 testCase, databaseName, newVersion, migrationCallback) { |
| 44 // We cannot use eventWatcher.wait_for('upgradeneeded') here, because | 44 // We cannot use eventWatcher.wait_for('upgradeneeded') here, because |
| 45 // the versionchange transaction auto-commits before the Promise's then | 45 // the versionchange transaction auto-commits before the Promise's then |
| 46 // callback gets called. | 46 // callback gets called. |
| 47 return new Promise((resolve, reject) => { | 47 return new Promise((resolve, reject) => { |
| 48 const request = indexedDB.open(databaseName, newVersion); | 48 const request = indexedDB.open(databaseName, newVersion); |
| 49 request.onupgradeneeded = testCase.step_func(event => { | 49 request.onupgradeneeded = testCase.step_func(event => { |
| 50 const database = event.target.result; | 50 const database = event.target.result; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 81 request.onsuccess = null; | 81 request.onsuccess = null; |
| 82 requestEventPromise = | 82 requestEventPromise = |
| 83 requestWatcher(testCase, request).wait_for('success'); | 83 requestWatcher(testCase, request).wait_for('success'); |
| 84 } | 84 } |
| 85 | 85 |
| 86 // requestEventPromise needs to be the last promise in the chain, because | 86 // requestEventPromise needs to be the last promise in the chain, because |
| 87 // we want the event that it resolves to. | 87 // we want the event that it resolves to. |
| 88 resolve(Promise.resolve(callbackResult).then(() => requestEventPromise)); | 88 resolve(Promise.resolve(callbackResult).then(() => requestEventPromise)); |
| 89 }); | 89 }); |
| 90 request.onerror = event => reject(event.target.error); | 90 request.onerror = event => reject(event.target.error); |
| 91 request.onsuccess = () => reject(new Error( | 91 request.onsuccess = () => { |
| 92 'indexedDB.open should not succeed without creating a ' + | 92 const database = request.result; |
| 93 'versionchange transaction')); | 93 testCase.add_cleanup(() => { database.close(); }); |
| 94 }).then(event => event.target.result || event.target.error); | 94 reject(new Error( |
| 95 'indexedDB.open should not succeed without creating a ' + |
| 96 'versionchange transaction')); |
| 97 }; |
| 98 }).then(event => { |
| 99 const database = event.target.result; |
| 100 if (database) { |
| 101 testCase.add_cleanup(() => { database.close(); }); |
| 102 } |
| 103 return database || event.target.error; |
| 104 }); |
| 95 } | 105 } |
| 96 | 106 |
| 97 // Creates an IndexedDB database whose name is unique for the test case. | 107 // Creates an IndexedDB database whose name is unique for the test case. |
| 98 // | 108 // |
| 99 // setupCallback will be called during a versionchange transaction, and will be | 109 // setupCallback will be called during a versionchange transaction, and will be |
| 100 // given the created database, the versionchange transaction, and the database | 110 // given the created database, the versionchange transaction, and the database |
| 101 // open request. | 111 // open request. |
| 102 // | 112 // |
| 103 // Returns a promise that resolves to an IndexedDB database. The caller must | 113 // Returns a promise that resolves to an IndexedDB database. The caller should |
| 104 // close the database. | 114 // close the database. |
| 105 function createDatabase(testCase, setupCallback) { | 115 function createDatabase(testCase, setupCallback) { |
| 106 return createNamedDatabase(testCase, databaseName(testCase), setupCallback); | 116 return createNamedDatabase(testCase, databaseName(testCase), setupCallback); |
| 107 } | 117 } |
| 108 | 118 |
| 109 // Creates an IndexedDB database. | 119 // Creates an IndexedDB database. |
| 110 // | 120 // |
| 111 // setupCallback will be called during a versionchange transaction, and will be | 121 // setupCallback will be called during a versionchange transaction, and will be |
| 112 // given the created database, the versionchange transaction, and the database | 122 // given the created database, the versionchange transaction, and the database |
| 113 // open request. | 123 // open request. |
| 114 // | 124 // |
| 115 // Returns a promise that resolves to an IndexedDB database. The caller must | 125 // Returns a promise that resolves to an IndexedDB database. The caller should |
| 116 // close the database. | 126 // close the database. |
| 117 function createNamedDatabase(testCase, databaseName, setupCallback) { | 127 function createNamedDatabase(testCase, databaseName, setupCallback) { |
| 118 const request = indexedDB.deleteDatabase(databaseName); | 128 const request = indexedDB.deleteDatabase(databaseName); |
| 119 const eventWatcher = requestWatcher(testCase, request); | 129 const eventWatcher = requestWatcher(testCase, request); |
| 120 | 130 |
| 121 return eventWatcher.wait_for('success').then(event => | 131 return eventWatcher.wait_for('success').then(event => { |
| 122 migrateNamedDatabase(testCase, databaseName, 1, setupCallback)); | 132 testCase.add_cleanup(() => { indexedDB.deleteDatabase(databaseName); }); |
| 133 return migrateNamedDatabase(testCase, databaseName, 1, setupCallback) |
| 134 }); |
| 123 } | 135 } |
| 124 | 136 |
| 125 // Opens an IndexedDB database without performing schema changes. | 137 // Opens an IndexedDB database without performing schema changes. |
| 126 // | 138 // |
| 127 // The given version number must match the database's current version. | 139 // The given version number must match the database's current version. |
| 128 // | 140 // |
| 129 // Returns a promise that resolves to an IndexedDB database. The caller must | 141 // Returns a promise that resolves to an IndexedDB database. The caller should |
| 130 // close the database. | 142 // close the database. |
| 131 function openDatabase(testCase, version) { | 143 function openDatabase(testCase, version) { |
| 132 return openNamedDatabase(testCase, databaseName(testCase), version); | 144 return openNamedDatabase(testCase, databaseName(testCase), version); |
| 133 } | 145 } |
| 134 | 146 |
| 135 // Opens an IndexedDB database without performing schema changes. | 147 // Opens an IndexedDB database without performing schema changes. |
| 136 // | 148 // |
| 137 // The given version number must match the database's current version. | 149 // The given version number must match the database's current version. |
| 138 // | 150 // |
| 139 // Returns a promise that resolves to an IndexedDB database. The caller must | 151 // Returns a promise that resolves to an IndexedDB database. The caller should |
| 140 // close the database. | 152 // close the database. |
| 141 function openNamedDatabase(testCase, databaseName, version) { | 153 function openNamedDatabase(testCase, databaseName, version) { |
| 142 const request = indexedDB.open(databaseName, version); | 154 const request = indexedDB.open(databaseName, version); |
| 143 const eventWatcher = requestWatcher(testCase, request); | 155 const eventWatcher = requestWatcher(testCase, request); |
| 144 return eventWatcher.wait_for('success').then(event => event.target.result); | 156 return eventWatcher.wait_for('success').then(() => { |
| 157 const database = request.result; |
| 158 testCase.add_cleanup(() => { database.close(); }); |
| 159 return database; |
| 160 }); |
| 145 } | 161 } |
| 146 | 162 |
| 147 // The data in the 'books' object store records in the first example of the | 163 // The data in the 'books' object store records in the first example of the |
| 148 // IndexedDB specification. | 164 // IndexedDB specification. |
| 149 const BOOKS_RECORD_DATA = [ | 165 const BOOKS_RECORD_DATA = [ |
| 150 { title: 'Quarry Memories', author: 'Fred', isbn: 123456 }, | 166 { title: 'Quarry Memories', author: 'Fred', isbn: 123456 }, |
| 151 { title: 'Water Buffaloes', author: 'Fred', isbn: 234567 }, | 167 { title: 'Water Buffaloes', author: 'Fred', isbn: 234567 }, |
| 152 { title: 'Bedrock Nights', author: 'Barney', isbn: 345678 }, | 168 { title: 'Bedrock Nights', author: 'Barney', isbn: 345678 }, |
| 153 ]; | 169 ]; |
| 154 | 170 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 // is using it incorrectly. | 263 // is using it incorrectly. |
| 248 function checkTitleIndexContents(testCase, index, errorMessage) { | 264 function checkTitleIndexContents(testCase, index, errorMessage) { |
| 249 const request = index.get(BOOKS_RECORD_DATA[2].title); | 265 const request = index.get(BOOKS_RECORD_DATA[2].title); |
| 250 const eventWatcher = requestWatcher(testCase, request); | 266 const eventWatcher = requestWatcher(testCase, request); |
| 251 return eventWatcher.wait_for('success').then(() => { | 267 return eventWatcher.wait_for('success').then(() => { |
| 252 const result = request.result; | 268 const result = request.result; |
| 253 assert_equals(result.isbn, BOOKS_RECORD_DATA[2].isbn, errorMessage); | 269 assert_equals(result.isbn, BOOKS_RECORD_DATA[2].isbn, errorMessage); |
| 254 assert_equals(result.author, BOOKS_RECORD_DATA[2].author, errorMessage); | 270 assert_equals(result.author, BOOKS_RECORD_DATA[2].author, errorMessage); |
| 255 }); | 271 }); |
| 256 } | 272 } |
| OLD | NEW |