| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 window.indexedDB = window.indexedDB || window.webkitIndexedDB || | 5 window.indexedDB = window.indexedDB || window.webkitIndexedDB || |
| 6 window.mozIndexedDB || window.msIndexedDB; | 6 window.mozIndexedDB || window.msIndexedDB; |
| 7 | 7 |
| 8 var automation = { | 8 var automation = { |
| 9 results: {} | 9 results: {} |
| 10 }; | 10 }; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 function onError(e) { | 42 function onError(e) { |
| 43 var s = "Caught error."; | 43 var s = "Caught error."; |
| 44 if (e.target && e.target.webkitErrorMessage) | 44 if (e.target && e.target.webkitErrorMessage) |
| 45 s += "\n" + e.target.webkitErrorMessage; | 45 s += "\n" + e.target.webkitErrorMessage; |
| 46 console.log(s); | 46 console.log(s); |
| 47 automation.setStatus(s); | 47 automation.setStatus(s); |
| 48 e.stopPropagation(); | 48 e.stopPropagation(); |
| 49 throw new Error(e); | 49 throw new Error(e); |
| 50 } | 50 } |
| 51 | 51 |
| 52 var version = 2; // The version with our object stores. | 52 var baseVersion = 2; // The version with our object stores. |
| 53 var curVersion; |
| 53 | 54 |
| 54 // Valid options fields: | 55 // Valid options fields: |
| 55 // indexName: the name of an index to create on each object store | 56 // indexName: the name of an index to create on each object store |
| 56 // indexKeyPath: likewise | 57 // indexKeyPath: the key path for that index |
| 57 // indexIsUnique: the "unique" option for IDBIndexParameters | 58 // indexIsUnique: the "unique" option for IDBIndexParameters |
| 58 // indexIsMultiEntry: the "multiEntry" option for IDBIndexParameters | 59 // indexIsMultiEntry: the "multiEntry" option for IDBIndexParameters |
| 59 // | 60 // |
| 60 function createDatabase( | 61 function createDatabase( |
| 61 name, objectStoreNames, handler, errorHandler, options) { | 62 name, objectStoreNames, handler, errorHandler, options) { |
| 62 var openRequest = indexedDB.open(name, version); | 63 var openRequest = indexedDB.open(name, baseVersion); |
| 63 openRequest.onblocked = errorHandler; | 64 openRequest.onblocked = errorHandler; |
| 64 function createObjectStores(db) { | 65 function createObjectStores(db) { |
| 65 for (var store in objectStoreNames) { | 66 for (var store in objectStoreNames) { |
| 66 var name = objectStoreNames[store]; | 67 var name = objectStoreNames[store]; |
| 67 assert(!db.objectStoreNames.contains(name)); | 68 assert(!db.objectStoreNames.contains(name)); |
| 68 var os = db.createObjectStore(name); | 69 var os = db.createObjectStore(name); |
| 69 if (options && options.indexName) { | 70 if (options && options.indexName) { |
| 70 assert('indexKeyPath' in options); | 71 assert('indexKeyPath' in options); |
| 71 os.createIndex(options.indexName, options.indexKeyPath, | 72 os.createIndex(options.indexName, options.indexKeyPath, |
| 72 { unique: options.indexIsUnique, | 73 { unique: options.indexIsUnique, |
| 73 multiEntry: options.indexIsMultiEntry }); | 74 multiEntry: options.indexIsMultiEntry }); |
| 74 } | 75 } |
| 75 } | 76 } |
| 76 } | 77 } |
| 77 openRequest.onupgradeneeded = function(ev) { | 78 openRequest.onupgradeneeded = function(ev) { |
| 78 // TODO: This is the spec-compliant path, which doesn't yet work in Chrome, | 79 // TODO: This is the spec-compliant path, which doesn't yet work in Chrome, |
| 79 // and isn't yet tested, as this function won't currently be called. | 80 // and isn't yet tested, as this function won't currently be called. |
| 80 assert(openRequest == ev.target); | 81 assert(openRequest == ev.target); |
| 81 createObjectStores(openRequest.result); | 82 createObjectStores(openRequest.result); |
| 82 // onsuccess will get called after this exits. | 83 // onsuccess will get called after this exits. |
| 83 }; | 84 }; |
| 84 openRequest.onsuccess = function(ev) { | 85 openRequest.onsuccess = function(ev) { |
| 85 assert(openRequest == ev.target); | 86 assert(openRequest == ev.target); |
| 86 var db = openRequest.result; | 87 var db = openRequest.result; |
| 87 db.onerror = function(ev) { | 88 db.onerror = function(ev) { |
| 88 console.log("db error", arguments, openRequest.webkitErrorMessage); | 89 console.log("db error", arguments, openRequest.webkitErrorMessage); |
| 89 errorHandler(); | 90 errorHandler(); |
| 90 }; | 91 } |
| 91 if (db.version != version) { | 92 if (db.version != baseVersion) { |
| 92 // This is the current Chrome path. | 93 // This is the current Chrome path. |
| 93 var setVersionRequest = db.setVersion(version); | 94 var setVersionRequest = db.setVersion(baseVersion); |
| 94 setVersionRequest.onfailure = errorHandler; | 95 setVersionRequest.onerror = errorHandler; |
| 95 setVersionRequest.onsuccess = function(e) { | 96 setVersionRequest.onsuccess = function(e) { |
| 97 curVersion = db.version; |
| 96 assert(setVersionRequest == e.target); | 98 assert(setVersionRequest == e.target); |
| 97 createObjectStores(db); | 99 createObjectStores(db); |
| 98 var versionTransaction = setVersionRequest.result; | 100 var versionTransaction = setVersionRequest.result; |
| 99 versionTransaction.oncomplete = function() {handler(db); }; | 101 versionTransaction.oncomplete = function() { handler(db); }; |
| 100 versionTransaction.onerror = onError; | 102 versionTransaction.onerror = onError; |
| 101 }; | 103 } |
| 102 } else { | 104 } else { |
| 103 handler(db); | 105 handler(db); |
| 104 } | 106 } |
| 105 } | 107 } |
| 106 } | 108 } |
| 107 | 109 |
| 110 // You must close all database connections before calling this. |
| 111 function alterObjectStores( |
| 112 name, objectStoreNames, func, handler, errorHandler) { |
| 113 var version = curVersion + 1; |
| 114 var openRequest = indexedDB.open(name, version); |
| 115 openRequest.onblocked = errorHandler; |
| 116 // TODO: This won't work in Firefox yet; see above in createDatabase. |
| 117 openRequest.onsuccess = function(ev) { |
| 118 assert(openRequest == ev.target); |
| 119 var db = openRequest.result; |
| 120 db.onerror = function(ev) { |
| 121 console.log("error altering db", arguments, |
| 122 openRequest.webkitErrorMessage); |
| 123 errorHandler(); |
| 124 } |
| 125 if (db.version != version) { |
| 126 var setVersionRequest = db.setVersion(version); |
| 127 setVersionRequest.onerror = errorHandler; |
| 128 setVersionRequest.onsuccess = |
| 129 function(e) { |
| 130 curVersion = db.version; |
| 131 assert(setVersionRequest == e.target); |
| 132 var versionTransaction = setVersionRequest.result; |
| 133 versionTransaction.oncomplete = function() { handler(db); }; |
| 134 versionTransaction.onerror = onError; |
| 135 for (var store in objectStoreNames) { |
| 136 func(versionTransaction.objectStore(objectStoreNames[store])); |
| 137 } |
| 138 } |
| 139 } else { |
| 140 errorHandler(); |
| 141 } |
| 142 } |
| 143 } |
| 144 |
| 108 function getTransaction(db, objectStoreNames, mode, opt_handler) { | 145 function getTransaction(db, objectStoreNames, mode, opt_handler) { |
| 109 var transaction = db.transaction(objectStoreNames, mode); | 146 var transaction = db.transaction(objectStoreNames, mode); |
| 110 transaction.onerror = onError; | 147 transaction.onerror = onError; |
| 111 transaction.onabort = onError; | 148 transaction.onabort = onError; |
| 112 if (opt_handler) { | 149 if (opt_handler) { |
| 113 transaction.oncomplete = opt_handler; | 150 transaction.oncomplete = opt_handler; |
| 114 } | 151 } |
| 115 return transaction; | 152 return transaction; |
| 116 } | 153 } |
| 117 | 154 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 132 var duration = Date.now() - startTime; | 169 var duration = Date.now() - startTime; |
| 133 // Ignore the cleanup time for this test. | 170 // Ignore the cleanup time for this test. |
| 134 automation.addResult(testName, duration); | 171 automation.addResult(testName, duration); |
| 135 automation.setStatus("Deleting database."); | 172 automation.setStatus("Deleting database."); |
| 136 // TODO: Turn on actual deletion; for now it's way too slow. | 173 // TODO: Turn on actual deletion; for now it's way too slow. |
| 137 // deleteDatabase(testName, onDeleted); | 174 // deleteDatabase(testName, onDeleted); |
| 138 onTestComplete(); | 175 onTestComplete(); |
| 139 } | 176 } |
| 140 } | 177 } |
| 141 | 178 |
| 179 function getDisplayName(args) { |
| 180 // The last arg is the completion callback the test runner tacks on. |
| 181 return getDisplayName.caller.name + "_" + |
| 182 Array.prototype.slice.call(args, 0, args.length - 1).join("_"); |
| 183 } |
| 184 |
| 185 function getSimpleKey(i) { |
| 186 return "key " + i; |
| 187 } |
| 188 |
| 189 function getSimpleValue(i) { |
| 190 return "value " + i; |
| 191 } |
| 192 |
| 193 function putLinearValues( |
| 194 transaction, objectStoreNames, numKeys, getKey, getValue) { |
| 195 if (!getKey) |
| 196 getKey = getSimpleKey; |
| 197 if (!getValue) |
| 198 getValue = getSimpleValue; |
| 199 for (var i in objectStoreNames) { |
| 200 var os = transaction.objectStore(objectStoreNames[i]); |
| 201 for (var j = 0; j < numKeys; ++j) { |
| 202 var request = os.put(getValue(j), getKey(j)); |
| 203 request.onerror = onError; |
| 204 } |
| 205 } |
| 206 } |
| 207 |
| 208 function getRandomValues( |
| 209 transaction, objectStoreNames, numReads, numKeys, indexName, getKey) { |
| 210 if (!getKey) |
| 211 getKey = getSimpleKey; |
| 212 for (var i in objectStoreNames) { |
| 213 var os = transaction.objectStore(objectStoreNames[i]); |
| 214 var source = os; |
| 215 if (indexName) |
| 216 source = source.index(indexName); |
| 217 for (var j = 0; j < numReads; ++j) { |
| 218 var rand = Math.floor(Math.random() * numKeys); |
| 219 var request = source.get(getKey(rand)); |
| 220 request.onerror = onError; |
| 221 } |
| 222 } |
| 223 } |
| 224 |
| 225 function putRandomValues( |
| 226 transaction, objectStoreNames, numPuts, numKeys, getKey, getValue) { |
| 227 if (!getKey) |
| 228 getKey = getSimpleKey; |
| 229 if (!getValue) |
| 230 getValue = getSimpleValue; |
| 231 for (var i in objectStoreNames) { |
| 232 var os = transaction.objectStore(objectStoreNames[i]); |
| 233 for (var j = 0; j < numPuts; ++j) { |
| 234 var rand = Math.floor(Math.random() * numKeys); |
| 235 var request = os.put(getValue(rand), getKey(rand)); |
| 236 request.onerror = onError; |
| 237 } |
| 238 } |
| 239 } |
| 240 |
| 142 function stringOfLength(n) { | 241 function stringOfLength(n) { |
| 143 assert(n > 0); | 242 assert(n > 0); |
| 144 assert(n == Math.floor(n)); | 243 assert(n == Math.floor(n)); |
| 145 return new Array(n + 1).join('0'); | 244 return new Array(n + 1).join('0'); |
| 146 } | 245 } |
| OLD | NEW |