| 1 |
// Copyright 2012 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.dbTest'); |
| 16 |
goog.setTestOnly('goog.dbTest'); |
| 17 |
|
| 18 |
goog.require('goog.Disposable'); |
| 19 |
goog.require('goog.Promise'); |
| 20 |
goog.require('goog.array'); |
| 21 |
goog.require('goog.db'); |
| 22 |
goog.require('goog.db.Cursor'); |
| 23 |
goog.require('goog.db.Error'); |
| 24 |
goog.require('goog.db.IndexedDb'); |
| 25 |
goog.require('goog.db.KeyRange'); |
| 26 |
goog.require('goog.db.Transaction'); |
| 27 |
goog.require('goog.events'); |
| 28 |
goog.require('goog.object'); |
| 29 |
goog.require('goog.testing.PropertyReplacer'); |
| 30 |
goog.require('goog.testing.asserts'); |
| 31 |
goog.require('goog.testing.jsunit'); |
| 32 |
goog.require('goog.userAgent.product'); |
| 33 |
|
| 34 |
var idbSupported = goog.userAgent.product.CHROME; |
| 35 |
var dbName; |
| 36 |
var dbBaseName = 'testDb'; |
| 37 |
var globalDb = null; |
| 38 |
var dbsToClose = []; |
| 39 |
var propertyReplacer; |
| 40 |
|
| 41 |
var baseVersion = 1; |
| 42 |
|
| 43 |
var dbVersion = 1; |
| 44 |
|
| 45 |
var TransactionMode = goog.db.Transaction.TransactionMode; |
| 46 |
var EventTypes = goog.db.Transaction.EventTypes; |
| 47 |
|
| 48 |
function openDatabase() { |
| 49 |
return goog.db.openDatabase(dbName).addCallback(function(db) { |
| 50 |
dbsToClose.push(db); |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
function incrementVersion(db, onUpgradeNeeded) { |
| 55 |
db.close(); |
| 56 |
|
| 57 |
var onBlocked = function(ev) { |
| 58 |
fail('Upgrade to version ' + dbVersion + ' is blocked.'); |
| 59 |
}; |
| 60 |
|
| 61 |
return goog.db.openDatabase(dbName, ++dbVersion, onUpgradeNeeded, onBlocked) |
| 62 |
.addCallback(function(db) { |
| 63 |
dbsToClose.push(db); |
| 64 |
assertEquals(dbVersion, db.getVersion()); |
| 65 |
}); |
| 66 |
} |
| 67 |
|
| 68 |
function addStore(db) { |
| 69 |
return incrementVersion( |
| 70 |
db, function(ev, db, tx) { db.createObjectStore('store'); }); |
| 71 |
} |
| 72 |
|
| 73 |
function addStoreWithIndex(db) { |
| 74 |
return incrementVersion(db, function(ev, db, tx) { |
| 75 |
var store = db.createObjectStore('store', {keyPath: 'key'}); |
| 76 |
store.createIndex('index', 'value'); |
| 77 |
}); |
| 78 |
} |
| 79 |
|
| 80 |
function populateStore(values, keys, db) { |
| 81 |
var putTx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 82 |
var store = putTx.objectStore('store'); |
| 83 |
for (var i = 0; i < values.length; ++i) { |
| 84 |
store.put(values[i], keys[i]); |
| 85 |
} |
| 86 |
return putTx.wait(); |
| 87 |
} |
| 88 |
|
| 89 |
function populateStoreWithObjects(values, keys, db) { |
| 90 |
var putTx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 91 |
var store = putTx.objectStore('store'); |
| 92 |
goog.array.forEach(values, function(value, index) { |
| 93 |
store.put({'key': keys[index], 'value': value}); |
| 94 |
}); |
| 95 |
return putTx.wait(); |
| 96 |
} |
| 97 |
|
| 98 |
function assertStoreValues(values, db) { |
| 99 |
var assertStoreTx = db.createTransaction(['store']); |
| 100 |
assertStoreTx.objectStore('store').getAll().addCallback(function(results) { |
| 101 |
assertSameElements(values, results); |
| 102 |
}); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Assert the keys are as expected. |
| 107 |
* @param {!Array<string>} keys - The expected keys. |
| 108 |
* @param {!goog.db.IndexedDb} db - The indexed db. |
| 109 |
*/ |
| 110 |
function assertStoreKeyValues(keys, db) { |
| 111 |
var assertStoreTx = db.createTransaction(['store']); |
| 112 |
assertStoreTx.objectStore('store').getAllKeys().addCallback(function( |
| 113 |
results) { |
| 114 |
assertSameElements(keys, results); |
| 115 |
}); |
| 116 |
} |
| 117 |
|
| 118 |
function assertStoreObjectValues(values, db) { |
| 119 |
var assertStoreTx = db.createTransaction(['store']); |
| 120 |
assertStoreTx.objectStore('store').getAll().addCallback(function(results) { |
| 121 |
var retrievedValues = |
| 122 |
goog.array.map(results, function(result) { return result['value']; }); |
| 123 |
assertSameElements(values, retrievedValues); |
| 124 |
}); |
| 125 |
} |
| 126 |
|
| 127 |
function assertStoreValuesAndCursorsDisposed(values, cursors, db) { |
| 128 |
var assertStoreTx = db.createTransaction(['store']); |
| 129 |
assertStoreTx.objectStore('store').getAll().addCallback(function(results) { |
| 130 |
assertSameElements(values, results); |
| 131 |
assertTrue(cursors.length > 0); |
| 132 |
goog.array.forEach(cursors, function(elem, index, array) { |
| 133 |
console.log(elem); |
| 134 |
assertTrue( |
| 135 |
'array[' + index + '] (' + elem + ') is not disposed', |
| 136 |
goog.Disposable.isDisposed(elem)); |
| 137 |
}); |
| 138 |
}); |
| 139 |
} |
| 140 |
|
| 141 |
function assertStoreDoesntExist(db) { |
| 142 |
try { |
| 143 |
db.createTransaction(['store']); |
| 144 |
fail('Create transaction with a non-existent store should have failed.'); |
| 145 |
} catch (e) { |
| 146 |
// expected |
| 147 |
assertEquals(e.getName(), goog.db.Error.ErrorName.NOT_FOUND_ERR); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
function transactionToPromise(db, trx) { |
| 152 |
return new goog.Promise(function(resolve, reject) { |
| 153 |
goog.events.listen(trx, EventTypes.ERROR, reject); |
| 154 |
goog.events.listen(trx, EventTypes.COMPLETE, function() { resolve(db); }); |
| 155 |
}); |
| 156 |
} |
| 157 |
|
| 158 |
// Calls onRecordReady each time that a new record can be read by the |
| 159 |
// cursor with cursor.next(). Returns a promise that resolves or rejects |
| 160 |
// based on when the cursor is complete or errors out. If onRecordReady |
| 161 |
// returns a promise that promises is also waited on before the returned |
| 162 |
// promise resolves. |
| 163 |
function forEachRecord(cursor, onRecordReady) { |
| 164 |
var promises = []; |
| 165 |
return new goog |
| 166 |
.Promise(function(resolve, reject) { |
| 167 |
var key = goog.events.listen( |
| 168 |
cursor, goog.db.Cursor.EventType.NEW_DATA, function() { |
| 169 |
var result = onRecordReady(); |
| 170 |
if (result && ('then' in result)) { |
| 171 |
promises.push(result); |
| 172 |
} |
| 173 |
}); |
| 174 |
|
| 175 |
goog.events.listenOnce( |
| 176 |
cursor, |
| 177 |
[goog.db.Cursor.EventType.COMPLETE, goog.db.Cursor.EventType.ERROR], |
| 178 |
function(evt) { |
| 179 |
goog.events.unlistenByKey(key); |
| 180 |
if (evt.type == goog.db.Cursor.EventType.COMPLETE) { |
| 181 |
resolve(); |
| 182 |
} else { |
| 183 |
reject(evt); |
| 184 |
} |
| 185 |
}); |
| 186 |
}) |
| 187 |
.then(function() { return goog.Promise.all(promises); }); |
| 188 |
} |
| 189 |
|
| 190 |
function failOnErrorEvent(ev) { |
| 191 |
fail(ev.target.message); |
| 192 |
} |
| 193 |
|
| 194 |
function setUpPage() { |
| 195 |
propertyReplacer = new goog.testing.PropertyReplacer(); |
| 196 |
} |
| 197 |
|
| 198 |
function setUp() { |
| 199 |
if (!idbSupported) { |
| 200 |
return; |
| 201 |
} |
| 202 |
|
| 203 |
// Always use a clean database by generating a new database name. |
| 204 |
dbName = dbBaseName + Date.now().toString(); |
| 205 |
globalDb = openDatabase(); |
| 206 |
} |
| 207 |
|
| 208 |
function tearDown() { |
| 209 |
for (var i = 0; i < dbsToClose.length; i++) { |
| 210 |
dbsToClose[i].close(); |
| 211 |
} |
| 212 |
dbsToClose = []; |
| 213 |
propertyReplacer.reset(); |
| 214 |
} |
| 215 |
|
| 216 |
function testDatabaseOpened() { |
| 217 |
if (!idbSupported) { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
assertNotNull(globalDb); |
| 222 |
return globalDb.branch().addCallback(function(db) { |
| 223 |
assertTrue(db.isOpen()); |
| 224 |
}); |
| 225 |
} |
| 226 |
|
| 227 |
function testOpenWithNewVersion() { |
| 228 |
if (!idbSupported) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
var upgradeNeeded = false; |
| 233 |
return globalDb.branch() |
| 234 |
.addCallback(function(db) { |
| 235 |
assertEquals(baseVersion, db.getVersion()); |
| 236 |
return incrementVersion( |
| 237 |
db, function(ev, db, tx) { upgradeNeeded = true; }); |
| 238 |
}) |
| 239 |
.addCallback(function(db) { assertTrue(upgradeNeeded); }); |
| 240 |
} |
| 241 |
|
| 242 |
function testManipulateObjectStores() { |
| 243 |
if (!idbSupported) { |
| 244 |
return; |
| 245 |
} |
| 246 |
|
| 247 |
return globalDb.branch() |
| 248 |
.addCallback(function(db) { |
| 249 |
assertEquals(baseVersion, db.getVersion()); |
| 250 |
return incrementVersion(db, function(ev, db, tx) { |
| 251 |
db.createObjectStore('basicStore'); |
| 252 |
db.createObjectStore('keyPathStore', {keyPath: 'keyGoesHere'}); |
| 253 |
db.createObjectStore('autoIncrementStore', {autoIncrement: true}); |
| 254 |
}); |
| 255 |
}) |
| 256 |
.addCallback(function(db) { |
| 257 |
var storeNames = db.getObjectStoreNames(); |
| 258 |
assertEquals(3, storeNames.length); |
| 259 |
assertTrue(storeNames.contains('basicStore')); |
| 260 |
assertTrue(storeNames.contains('keyPathStore')); |
| 261 |
assertTrue(storeNames.contains('autoIncrementStore')); |
| 262 |
return incrementVersion( |
| 263 |
db, function(ev, db, tx) { db.deleteObjectStore('basicStore'); }); |
| 264 |
}) |
| 265 |
.addCallback(function(db) { |
| 266 |
var storeNames = db.getObjectStoreNames(); |
| 267 |
assertEquals(2, storeNames.length); |
| 268 |
assertFalse(storeNames.contains('basicStore')); |
| 269 |
assertTrue(storeNames.contains('keyPathStore')); |
| 270 |
assertTrue(storeNames.contains('autoIncrementStore')); |
| 271 |
}); |
| 272 |
} |
| 273 |
|
| 274 |
function testBadObjectStoreManipulation() { |
| 275 |
if (!idbSupported) { |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
var expectedCode = goog.db.Error.ErrorName.INVALID_STATE_ERR; |
| 280 |
return globalDb.branch() |
| 281 |
.addCallback(function(db) { |
| 282 |
try { |
| 283 |
db.createObjectStore('diediedie'); |
| 284 |
fail('Create object store outside transaction should have failed.'); |
| 285 |
} catch (err) { |
| 286 |
// expected |
| 287 |
assertEquals(expectedCode, err.getName()); |
| 288 |
} |
| 289 |
}) |
| 290 |
.addCallback(addStore) |
| 291 |
.addCallback(function(db) { |
| 292 |
try { |
| 293 |
db.deleteObjectStore('store'); |
| 294 |
fail('Delete object store outside transaction should have failed.'); |
| 295 |
} catch (err) { |
| 296 |
// expected |
| 297 |
assertEquals(expectedCode, err.getName()); |
| 298 |
} |
| 299 |
}) |
| 300 |
.addCallback(function(db) { |
| 301 |
return incrementVersion(db, function(ev, db, tx) { |
| 302 |
try { |
| 303 |
db.deleteObjectStore('diediedie'); |
| 304 |
fail('Delete non-existent store should have failed.'); |
| 305 |
} catch (err) { |
| 306 |
// expected |
| 307 |
assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName()); |
| 308 |
} |
| 309 |
}); |
| 310 |
}); |
| 311 |
} |
| 312 |
|
| 313 |
function testGetNonExistentObjectStore() { |
| 314 |
if (!idbSupported) { |
| 315 |
return; |
| 316 |
} |
| 317 |
|
| 318 |
return globalDb.branch().addCallback(addStore).addCallback(function(db) { |
| 319 |
var tx = db.createTransaction(['store']); |
| 320 |
try { |
| 321 |
tx.objectStore('diediedie'); |
| 322 |
fail('getting non-existent object store should have failed'); |
| 323 |
} catch (err) { |
| 324 |
assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName()); |
| 325 |
} |
| 326 |
}); |
| 327 |
} |
| 328 |
|
| 329 |
function testCreateTransaction() { |
| 330 |
if (!idbSupported) { |
| 331 |
return; |
| 332 |
} |
| 333 |
|
| 334 |
return globalDb.branch().addCallback(addStore).addCallback(function(db) { |
| 335 |
var tx = db.createTransaction(['store']); |
| 336 |
assertEquals('mode not READ_ONLY', TransactionMode.READ_ONLY, tx.getMode()); |
| 337 |
tx = db.createTransaction( |
| 338 |
['store'], goog.db.Transaction.TransactionMode.READ_WRITE); |
| 339 |
assertEquals( |
| 340 |
'mode not READ_WRITE', TransactionMode.READ_WRITE, tx.getMode()); |
| 341 |
}); |
| 342 |
} |
| 343 |
|
| 344 |
function testPutRecord() { |
| 345 |
if (!idbSupported) { |
| 346 |
return; |
| 347 |
} |
| 348 |
|
| 349 |
return globalDb.branch() |
| 350 |
.addCallback(addStore) |
| 351 |
.addCallback(function(db) { |
| 352 |
var initialPutTx = |
| 353 |
db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 354 |
var putOperation = initialPutTx.objectStore('store').put( |
| 355 |
{key: 'initial', value: 'value1'}, 'putKey'); |
| 356 |
putOperation.addCallback(function(key) { |
| 357 |
assertEquals('putKey', key); |
| 358 |
}); |
| 359 |
return transactionToPromise(db, initialPutTx); |
| 360 |
}) |
| 361 |
.addCallback(function(db) { |
| 362 |
var checkResultsTx = db.createTransaction(['store']); |
| 363 |
var getOperation = checkResultsTx.objectStore('store').get('putKey'); |
| 364 |
getOperation.addCallback(function(result) { |
| 365 |
assertEquals('initial', result.key); |
| 366 |
assertEquals('value1', result.value); |
| 367 |
}); |
| 368 |
return transactionToPromise(db, checkResultsTx); |
| 369 |
}) |
| 370 |
.addCallback(function(db) { |
| 371 |
var overwriteTx = |
| 372 |
db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 373 |
var putOperation = overwriteTx.objectStore('store').put( |
| 374 |
{key: 'overwritten', value: 'value2'}, 'putKey'); |
| 375 |
putOperation.addCallback(function(key) { |
| 376 |
assertEquals('putKey', key); |
| 377 |
}); |
| 378 |
return transactionToPromise(db, overwriteTx); |
| 379 |
}) |
| 380 |
.addCallback(function(db) { |
| 381 |
var checkOverwriteTx = db.createTransaction(['store']); |
| 382 |
checkOverwriteTx.objectStore('store').get('putKey').addCallback( |
| 383 |
function(result) { |
| 384 |
// this is guaranteed to run before the COMPLETE event fires on |
| 385 |
// the transaction |
| 386 |
assertEquals('overwritten', result.key); |
| 387 |
assertEquals('value2', result.value); |
| 388 |
}); |
| 389 |
|
| 390 |
return transactionToPromise(db, checkOverwriteTx); |
| 391 |
}); |
| 392 |
} |
| 393 |
|
| 394 |
function testAddRecord() { |
| 395 |
if (!idbSupported) { |
| 396 |
return; |
| 397 |
} |
| 398 |
|
| 399 |
return globalDb.branch() |
| 400 |
.addCallback(addStore) |
| 401 |
.addCallback(function(db) { |
| 402 |
var initialAddTx = |
| 403 |
db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 404 |
var addOperation = initialAddTx.objectStore('store').add( |
| 405 |
{key: 'hi', value: 'something'}, 'stuff'); |
| 406 |
addOperation.addCallback(function(key) { |
| 407 |
assertEquals('stuff', key); |
| 408 |
}); |
| 409 |
return transactionToPromise(db, initialAddTx); |
| 410 |
}) |
| 411 |
.addCallback(function(db) { |
| 412 |
var successfulAddTx = db.createTransaction(['store']); |
| 413 |
var getOperation = successfulAddTx.objectStore('store').get('stuff'); |
| 414 |
getOperation.addCallback(function(result) { |
| 415 |
assertEquals('hi', result.key); |
| 416 |
assertEquals('something', result.value); |
| 417 |
}); |
| 418 |
return transactionToPromise(db, successfulAddTx); |
| 419 |
}) |
| 420 |
.addCallback(function(db) { |
| 421 |
var addOverwriteTx = |
| 422 |
db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 423 |
addOverwriteTx.objectStore('store') |
| 424 |
.add({key: 'bye', value: 'nothing'}, 'stuff') |
| 425 |
.addErrback(function(err) { |
| 426 |
// expected |
| 427 |
assertEquals( |
| 428 |
goog.db.Error.ErrorName.CONSTRAINT_ERR, err.getName()); |
| 429 |
}); |
| 430 |
return transactionToPromise(db, addOverwriteTx) |
| 431 |
.then( |
| 432 |
function() { |
| 433 |
fail('adding existing record should not have succeeded'); |
| 434 |
}, |
| 435 |
function(ev) { |
| 436 |
// expected |
| 437 |
assertEquals( |
| 438 |
goog.db.Error.ErrorName.CONSTRAINT_ERR, |
| 439 |
ev.target.getName()); |
| 440 |
}); |
| 441 |
}); |
| 442 |
} |
| 443 |
|
| 444 |
function testPutRecordKeyPathStore() { |
| 445 |
if (!idbSupported) { |
| 446 |
return; |
| 447 |
} |
| 448 |
|
| 449 |
return globalDb.branch() |
| 450 |
.addCallback(function(db) { |
| 451 |
return incrementVersion(db, function(ev, db, tx) { |
| 452 |
db.createObjectStore('keyStore', {keyPath: 'key'}); |
| 453 |
}); |
| 454 |
}) |
| 455 |
.addCallback(function(db) { |
| 456 |
var putTx = |
| 457 |
db.createTransaction(['keyStore'], TransactionMode.READ_WRITE); |
| 458 |
var putOperation = |
| 459 |
putTx.objectStore('keyStore').put({key: 'hi', value: 'something'}); |
| 460 |
putOperation.addCallback(function(key) { |
| 461 |
assertEquals('hi', key); |
| 462 |
}); |
| 463 |
return transactionToPromise(db, putTx); |
| 464 |
}) |
| 465 |
.addCallback(function(db) { |
| 466 |
var checkResultsTx = db.createTransaction(['keyStore']); |
| 467 |
checkResultsTx.objectStore('keyStore') |
| 468 |
.get('hi') |
| 469 |
.addCallback(function(result) { |
| 470 |
assertNotUndefined(result); |
| 471 |
assertEquals('hi', result.key); |
| 472 |
assertEquals('something', result.value); |
| 473 |
}); |
| 474 |
return transactionToPromise(db, checkResultsTx); |
| 475 |
}); |
| 476 |
} |
| 477 |
|
| 478 |
function testPutBadRecordKeyPathStore() { |
| 479 |
if (!idbSupported) { |
| 480 |
return; |
| 481 |
} |
| 482 |
|
| 483 |
return globalDb.branch() |
| 484 |
.addCallback(function(db) { |
| 485 |
return incrementVersion(db, function(ev, db, tx) { |
| 486 |
db.createObjectStore('keyStore', {keyPath: 'key'}); |
| 487 |
}); |
| 488 |
}) |
| 489 |
.addCallback(function(db) { |
| 490 |
var badTx = |
| 491 |
db.createTransaction(['keyStore'], TransactionMode.READ_WRITE); |
| 492 |
return badTx.objectStore('keyStore') |
| 493 |
.put({key: 'diedie', value: 'anything'}, 'badKey') |
| 494 |
.then( |
| 495 |
function() { |
| 496 |
fail('inserting with explicit key should have failed'); |
| 497 |
}, |
| 498 |
function(err) { |
| 499 |
// expected |
| 500 |
assertEquals(goog.db.Error.ErrorName.DATA_ERR, err.getName()); |
| 501 |
}); |
| 502 |
}); |
| 503 |
} |
| 504 |
|
| 505 |
function testPutRecordAutoIncrementStore() { |
| 506 |
if (!idbSupported) { |
| 507 |
return; |
| 508 |
} |
| 509 |
|
| 510 |
return globalDb.branch() |
| 511 |
.addCallback(function(db) { |
| 512 |
return incrementVersion(db, function(ev, db, tx) { |
| 513 |
db.createObjectStore('aiStore', {autoIncrement: true}); |
| 514 |
}); |
| 515 |
}) |
| 516 |
.addCallback(function(db) { |
| 517 |
var tx = db.createTransaction(['aiStore'], TransactionMode.READ_WRITE); |
| 518 |
var putOperation1 = tx.objectStore('aiStore').put('1'); |
| 519 |
var putOperation2 = tx.objectStore('aiStore').put('2'); |
| 520 |
var putOperation3 = tx.objectStore('aiStore').put('3'); |
| 521 |
putOperation1.addCallback(function(key) { |
| 522 |
assertNotUndefined(key); |
| 523 |
}); |
| 524 |
putOperation2.addCallback(function(key) { |
| 525 |
assertNotUndefined(key); |
| 526 |
}); |
| 527 |
putOperation3.addCallback(function(key) { |
| 528 |
assertNotUndefined(key); |
| 529 |
}); |
| 530 |
return transactionToPromise(db, tx); |
| 531 |
}) |
| 532 |
.addCallback(function(db) { |
| 533 |
var tx = db.createTransaction(['aiStore']); |
| 534 |
var getAllOperation = tx.objectStore('aiStore').getAll(); |
| 535 |
return getAllOperation.addCallback(function(results) { |
| 536 |
assertEquals(3, results.length); |
| 537 |
// only checking to see if the results are included because the keys |
| 538 |
// are not specified |
| 539 |
assertNotEquals(-1, results.indexOf('1')); |
| 540 |
assertNotEquals(-1, results.indexOf('2')); |
| 541 |
assertNotEquals(-1, results.indexOf('3')); |
| 542 |
}); |
| 543 |
}); |
| 544 |
} |
| 545 |
|
| 546 |
function testPutRecordKeyPathAndAutoIncrementStore() { |
| 547 |
if (!idbSupported) { |
| 548 |
return; |
| 549 |
} |
| 550 |
|
| 551 |
return globalDb.branch() |
| 552 |
.addCallback(function(db) { |
| 553 |
return incrementVersion(db, function(ev, db, tx) { |
| 554 |
db.createObjectStore( |
| 555 |
'hybridStore', {keyPath: 'key', autoIncrement: true}); |
| 556 |
}); |
| 557 |
}) |
| 558 |
.addCallback(function(db) { |
| 559 |
var tx = |
| 560 |
db.createTransaction(['hybridStore'], TransactionMode.READ_WRITE); |
| 561 |
var putOperation = |
| 562 |
tx.objectStore('hybridStore').put({value: 'whatever'}); |
| 563 |
putOperation.addCallback(function(key) { |
| 564 |
assertNotUndefined(key); |
| 565 |
}); |
| 566 |
return putOperation.addCallback(function() { |
| 567 |
return db; |
| 568 |
}); |
| 569 |
}) |
| 570 |
.addCallback(function(db) { |
| 571 |
var tx = db.createTransaction(['hybridStore']); |
| 572 |
return tx.objectStore('hybridStore').getAll().then(function(results) { |
| 573 |
assertEquals(1, results.length); |
| 574 |
assertEquals('whatever', results[0].value); |
| 575 |
assertNotUndefined(results[0].key); |
| 576 |
}); |
| 577 |
}); |
| 578 |
} |
| 579 |
|
| 580 |
function testPutIllegalRecords() { |
| 581 |
if (!idbSupported) { |
| 582 |
return; |
| 583 |
} |
| 584 |
|
| 585 |
return globalDb.branch().addCallback(addStore).addCallback(function(db) { |
| 586 |
var tx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 587 |
|
| 588 |
var promises = []; |
| 589 |
var badKeyFail = function(keyKind) { |
| 590 |
return function() { |
| 591 |
return fail('putting with ' + keyKind + ' key should have failed'); |
| 592 |
} |
| 593 |
}; |
| 594 |
var assertExpectedError = function(err) { |
| 595 |
assertEquals(goog.db.Error.ErrorName.DATA_ERR, err.getName()); |
| 596 |
}; |
| 597 |
|
| 598 |
promises.push( |
| 599 |
tx.objectStore('store') |
| 600 |
.put('death', null) |
| 601 |
.then(badKeyFail('null'), assertExpectedError)); |
| 602 |
|
| 603 |
promises.push( |
| 604 |
tx.objectStore('store') |
| 605 |
.put('death', NaN) |
| 606 |
.then(badKeyFail('NaN'), assertExpectedError)); |
| 607 |
|
| 608 |
promises.push( |
| 609 |
tx.objectStore('store') |
| 610 |
.put('death', undefined) |
| 611 |
.then(badKeyFail('undefined'), assertExpectedError)); |
| 612 |
|
| 613 |
return goog.Promise.all(promises); |
| 614 |
}); |
| 615 |
} |
| 616 |
|
| 617 |
function testPutIllegalRecordsWithIndex() { |
| 618 |
if (!idbSupported) { |
| 619 |
return; |
| 620 |
} |
| 621 |
|
| 622 |
return globalDb.branch() |
| 623 |
.addCallback(addStoreWithIndex) |
| 624 |
.addCallback(function(db) { |
| 625 |
var tx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 626 |
var promises = []; |
| 627 |
var badKeyFail = function(keyKind) { |
| 628 |
return function() { |
| 629 |
fail('putting with ' + keyKind + ' key should have failed'); |
| 630 |
} |
| 631 |
}; |
| 632 |
var assertExpectedError = function(err) { |
| 633 |
// expected |
| 634 |
assertEquals(goog.db.Error.ErrorName.DATA_ERR, err.getName()); |
| 635 |
}; |
| 636 |
|
| 637 |
promises.push( |
| 638 |
tx.objectStore('store') |
| 639 |
.put({value: 'diediedie', key: null}) |
| 640 |
.then(badKeyFail('null'), assertExpectedError)); |
| 641 |
|
| 642 |
promises.push( |
| 643 |
tx.objectStore('store') |
| 644 |
.put({value: 'dietodeath', key: NaN}) |
| 645 |
.then(badKeyFail('NaN'), assertExpectedError)); |
| 646 |
promises.push( |
| 647 |
tx.objectStore('store') |
| 648 |
.put({value: 'dietodeath', key: undefined}) |
| 649 |
.then(badKeyFail('undefined'), assertExpectedError)); |
| 650 |
|
| 651 |
return goog.Promise.all(promises); |
| 652 |
}); |
| 653 |
} |
| 654 |
|
| 655 |
function testDeleteRecord() { |
| 656 |
if (!idbSupported) { |
| 657 |
return; |
| 658 |
} |
| 659 |
|
| 660 |
var db; |
| 661 |
return globalDb.branch() |
| 662 |
.addCallback(addStore) |
| 663 |
.addCallback(function(openedDb) { |
| 664 |
db = openedDb; |
| 665 |
return db.createTransaction(['store'], TransactionMode.READ_WRITE) |
| 666 |
.objectStore('store') |
| 667 |
.put({key: 'hi', value: 'something'}, 'stuff'); |
| 668 |
}) |
| 669 |
.addCallback(function() { |
| 670 |
return db.createTransaction(['store'], TransactionMode.READ_WRITE) |
| 671 |
.objectStore('store') |
| 672 |
.remove('stuff'); |
| 673 |
}) |
| 674 |
.addCallback(function() { |
| 675 |
return db.createTransaction(['store']).objectStore('store').get( |
| 676 |
'stuff'); |
| 677 |
}) |
| 678 |
.addCallback(function(result) { assertUndefined(result); }); |
| 679 |
} |
| 680 |
|
| 681 |
function testDeleteRange() { |
| 682 |
if (!idbSupported) { |
| 683 |
return; |
| 684 |
} |
| 685 |
|
| 686 |
var values = ['1', '2', '3']; |
| 687 |
var keys = ['a', 'b', 'c']; |
| 688 |
|
| 689 |
var addData = goog.partial(populateStore, values, keys); |
| 690 |
var checkStore = goog.partial(assertStoreValues, ['1']); |
| 691 |
|
| 692 |
return globalDb.branch() |
| 693 |
.addCallback(addStore) |
| 694 |
.addCallback(addData) |
| 695 |
.addCallback(function(db) { |
| 696 |
return db.createTransaction(['store'], TransactionMode.READ_WRITE) |
| 697 |
.objectStore('store') |
| 698 |
.remove(goog.db.KeyRange.bound('b', 'c')) |
| 699 |
.then(function() { |
| 700 |
return db; |
| 701 |
}); |
| 702 |
}) |
| 703 |
.addCallback(checkStore); |
| 704 |
} |
| 705 |
|
| 706 |
function testGetAll() { |
| 707 |
if (!idbSupported) { |
| 708 |
return; |
| 709 |
} |
| 710 |
|
| 711 |
var values = ['1', '2', '3']; |
| 712 |
var keys = ['a', 'b', 'c']; |
| 713 |
|
| 714 |
var addData = goog.partial(populateStore, values, keys); |
| 715 |
var checkStore = goog.partial(assertStoreValues, values); |
| 716 |
|
| 717 |
return globalDb.branch() |
| 718 |
.addCallback(addStore) |
| 719 |
.addCallback(addData) |
| 720 |
.addCallback(checkStore); |
| 721 |
} |
| 722 |
|
| 723 |
function testGetAllKeys() { |
| 724 |
if (!idbSupported) { |
| 725 |
return; |
| 726 |
} |
| 727 |
|
| 728 |
var values = ['1', '2', '3']; |
| 729 |
var keys = ['a', 'b', 'c']; |
| 730 |
|
| 731 |
var addData = goog.partial(populateStore, values, keys); |
| 732 |
var checkStore = goog.partial(assertStoreKeyValues, keys); |
| 733 |
|
| 734 |
return globalDb.branch() |
| 735 |
.addCallback(addStore) |
| 736 |
.addCallback(addData) |
| 737 |
.addCallback(checkStore); |
| 738 |
} |
| 739 |
|
| 740 |
function testGetAllFreesCursor() { |
| 741 |
if (!idbSupported) { |
| 742 |
return; |
| 743 |
} |
| 744 |
var values = ['1', '2', '3']; |
| 745 |
var keys = ['a', 'b', 'c']; |
| 746 |
|
| 747 |
var addData = goog.partial(populateStore, values, keys); |
| 748 |
var origCursor = goog.db.Cursor; |
| 749 |
var cursors = []; |
| 750 |
/** @constructor */ |
| 751 |
var testCursor = function() { |
| 752 |
origCursor.call(this); |
| 753 |
cursors.push(this); |
| 754 |
}; |
| 755 |
goog.object.extend(testCursor, origCursor); |
| 756 |
// We don't use goog.inherits here because we are going to be overwriting |
| 757 |
// goog.db.Cursor and we don't want a new "base" method as |
| 758 |
// goog.db.Cursor.base(this, 'constructor') would be a call to |
| 759 |
// testCursor.base(this, 'constructor') which would be goog.db.Cursor and be |
| 760 |
// an infinite loop. |
| 761 |
testCursor.prototype = origCursor.prototype; |
| 762 |
propertyReplacer.replace(goog.db, 'Cursor', testCursor); |
| 763 |
var checkStoreAndCursorDisposed = |
| 764 |
goog.partial(assertStoreValuesAndCursorsDisposed, values, cursors); |
| 765 |
|
| 766 |
return globalDb.branch() |
| 767 |
.addCallback(addStore) |
| 768 |
.addCallback(addData) |
| 769 |
.addCallback(checkStoreAndCursorDisposed); |
| 770 |
} |
| 771 |
|
| 772 |
function testObjectStoreCursorGet() { |
| 773 |
if (!idbSupported) { |
| 774 |
return; |
| 775 |
} |
| 776 |
|
| 777 |
var values = ['1', '2', '3', '4']; |
| 778 |
var keys = ['a', 'b', 'c', 'd']; |
| 779 |
|
| 780 |
var addData = goog.partial(populateStore, values, keys); |
| 781 |
var db; |
| 782 |
|
| 783 |
var resultValues = []; |
| 784 |
var resultKeys = []; |
| 785 |
// Open the cursor over range ['b', 'c'], move in backwards direction. |
| 786 |
return globalDb.branch() |
| 787 |
.addCallback(addStore) |
| 788 |
.addCallback(addData) |
| 789 |
.addCallback(function(theDb) { |
| 790 |
db = theDb; |
| 791 |
var cursorTx = db.createTransaction(['store']); |
| 792 |
var store = cursorTx.objectStore('store'); |
| 793 |
|
| 794 |
var cursor = store.openCursor( |
| 795 |
goog.db.KeyRange.bound('b', 'c'), goog.db.Cursor.Direction.PREV); |
| 796 |
|
| 797 |
var whenCursorComplete = forEachRecord(cursor, function() { |
| 798 |
resultValues.push(cursor.getValue()); |
| 799 |
resultKeys.push(cursor.getKey()); |
| 800 |
cursor.next(); |
| 801 |
}); |
| 802 |
|
| 803 |
return goog.Promise.all([cursorTx.wait(), whenCursorComplete]); |
| 804 |
}) |
| 805 |
.addCallback(function() { |
| 806 |
assertArrayEquals(['3', '2'], resultValues); |
| 807 |
assertArrayEquals(['c', 'b'], resultKeys); |
| 808 |
}); |
| 809 |
} |
| 810 |
|
| 811 |
function testObjectStoreCursorReplace() { |
| 812 |
if (!idbSupported) { |
| 813 |
return; |
| 814 |
} |
| 815 |
|
| 816 |
var values = ['1', '2', '3', '4']; |
| 817 |
var keys = ['a', 'b', 'c', 'd']; |
| 818 |
|
| 819 |
var addData = goog.partial(populateStore, values, keys); |
| 820 |
|
| 821 |
// Store should contain ['1', '2', '5', '4'] after replacement. |
| 822 |
var checkStore = goog.partial(assertStoreValues, ['1', '2', '5', '4']); |
| 823 |
|
| 824 |
// Use a bounded cursor for ('b', 'c'] to update value '3' -> '5'. |
| 825 |
var openCursorAndReplace = function(db) { |
| 826 |
var cursorTx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 827 |
var store = cursorTx.objectStore('store'); |
| 828 |
|
| 829 |
var cursor = store.openCursor(goog.db.KeyRange.bound('b', 'c', true)); |
| 830 |
var whenCursorComplete = forEachRecord(cursor, function() { |
| 831 |
assertEquals('3', cursor.getValue()); |
| 832 |
return cursor.update('5').addCallback(function() { cursor.next(); }); |
| 833 |
}); |
| 834 |
|
| 835 |
return goog.Promise.all([cursorTx.wait(), whenCursorComplete]) |
| 836 |
.then(function() { return db; }); |
| 837 |
}; |
| 838 |
|
| 839 |
return globalDb.branch() |
| 840 |
.addCallback(addStore) |
| 841 |
.addCallback(addData) |
| 842 |
.addCallback(openCursorAndReplace) |
| 843 |
.addCallback(checkStore); |
| 844 |
} |
| 845 |
|
| 846 |
function testObjectStoreCursorRemove() { |
| 847 |
if (!idbSupported) { |
| 848 |
return; |
| 849 |
} |
| 850 |
|
| 851 |
var values = ['1', '2', '3', '4']; |
| 852 |
var keys = ['a', 'b', 'c', 'd']; |
| 853 |
|
| 854 |
var addData = goog.partial(populateStore, values, keys); |
| 855 |
|
| 856 |
// Store should contain ['1', '2'] after removing elements. |
| 857 |
var checkStore = goog.partial(assertStoreValues, ['1', '2']); |
| 858 |
|
| 859 |
// Use a bounded cursor for ('b', ...) to remove '3', '4'. |
| 860 |
var openCursorAndRemove = function(db) { |
| 861 |
var cursorTx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 862 |
|
| 863 |
var store = cursorTx.objectStore('store'); |
| 864 |
var cursor = store.openCursor(goog.db.KeyRange.lowerBound('b', true)); |
| 865 |
var whenCursorComplete = forEachRecord(cursor, function() { |
| 866 |
return cursor.remove('5').addCallback(function() { cursor.next(); }); |
| 867 |
}); |
| 868 |
return goog.Promise.all([cursorTx.wait(), whenCursorComplete]) |
| 869 |
.then(function(results) { return db; }); |
| 870 |
}; |
| 871 |
|
| 872 |
// Setup and execute test case. |
| 873 |
return globalDb.branch() |
| 874 |
.addCallback(addStore) |
| 875 |
.addCallback(addData) |
| 876 |
.addCallback(openCursorAndRemove) |
| 877 |
.addCallback(checkStore); |
| 878 |
} |
| 879 |
|
| 880 |
function testClear() { |
| 881 |
if (!idbSupported) { |
| 882 |
return; |
| 883 |
} |
| 884 |
|
| 885 |
var db; |
| 886 |
return globalDb.branch() |
| 887 |
.addCallback(addStore) |
| 888 |
.addCallback(function(theDb) { |
| 889 |
db = theDb; |
| 890 |
|
| 891 |
var putTx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 892 |
putTx.objectStore('store').put('1', 'a'); |
| 893 |
putTx.objectStore('store').put('2', 'b'); |
| 894 |
putTx.objectStore('store').put('3', 'c'); |
| 895 |
return putTx.wait(); |
| 896 |
}) |
| 897 |
.addCallback(function() { |
| 898 |
return db.createTransaction(['store']).objectStore('store').getAll(); |
| 899 |
}) |
| 900 |
.addCallback(function(results) { |
| 901 |
assertEquals(3, results.length); |
| 902 |
return db.createTransaction(['store'], TransactionMode.READ_WRITE) |
| 903 |
.objectStore('store') |
| 904 |
.clear(); |
| 905 |
}) |
| 906 |
.addCallback(function() { |
| 907 |
return db.createTransaction(['store']).objectStore('store').getAll(); |
| 908 |
}) |
| 909 |
.addCallback(function(results) { assertEquals(0, results.length); }); |
| 910 |
} |
| 911 |
|
| 912 |
function testAbortTransaction() { |
| 913 |
if (!idbSupported) { |
| 914 |
return; |
| 915 |
} |
| 916 |
|
| 917 |
var db; |
| 918 |
return globalDb.branch() |
| 919 |
.addCallback(addStore) |
| 920 |
.addCallback(function(theDb) { |
| 921 |
db = theDb; |
| 922 |
return new Promise(function(resolve, reject) { |
| 923 |
var abortTx = |
| 924 |
db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 925 |
abortTx.objectStore('store') |
| 926 |
.put('data', 'stuff') |
| 927 |
.addCallback(function() { abortTx.abort(); }); |
| 928 |
goog.events.listen(abortTx, EventTypes.ERROR, reject); |
| 929 |
|
| 930 |
goog.events.listen(abortTx, EventTypes.COMPLETE, function() { |
| 931 |
fail( |
| 932 |
'transaction shouldn\'t have' + |
| 933 |
' completed after being aborted'); |
| 934 |
}); |
| 935 |
|
| 936 |
goog.events.listen(abortTx, EventTypes.ABORT, resolve); |
| 937 |
}); |
| 938 |
}) |
| 939 |
.addCallback(function() { |
| 940 |
var checkResultsTx = db.createTransaction(['store']); |
| 941 |
return checkResultsTx.objectStore('store').get('stuff'); |
| 942 |
}) |
| 943 |
.addCallback(function(result) { assertUndefined(result); }); |
| 944 |
} |
| 945 |
|
| 946 |
function testInactiveTransaction() { |
| 947 |
if (!idbSupported) { |
| 948 |
return; |
| 949 |
} |
| 950 |
|
| 951 |
var db; |
| 952 |
var store; |
| 953 |
var index; |
| 954 |
var createAndFinishTransaction = function(theDb) { |
| 955 |
db = theDb; |
| 956 |
var tx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 957 |
store = tx.objectStore('store'); |
| 958 |
index = store.getIndex('index'); |
| 959 |
store.put({key: 'something', value: 'anything'}); |
| 960 |
return tx.wait(); |
| 961 |
}; |
| 962 |
|
| 963 |
var assertCantUseInactiveTransaction = function() { |
| 964 |
var expectedCode = goog.db.Error.ErrorName.TRANSACTION_INACTIVE_ERR; |
| 965 |
var promises = []; |
| 966 |
|
| 967 |
var failOp = function(op) { |
| 968 |
return function() { |
| 969 |
fail(op + ' with inactive transaction should have failed'); |
| 970 |
}; |
| 971 |
}; |
| 972 |
var assertCorrectError = function(err) { |
| 973 |
assertEquals(expectedCode, err.getName()); |
| 974 |
}; |
| 975 |
promises.push( |
| 976 |
store.put({key: 'another', value: 'thing'}) |
| 977 |
.then(failOp('putting'), assertCorrectError)); |
| 978 |
promises.push( |
| 979 |
store.add({key: 'another', value: 'thing'}) |
| 980 |
.then(failOp('adding'), assertCorrectError)); |
| 981 |
promises.push( |
| 982 |
store.remove('something').then(failOp('deleting'), assertCorrectError)); |
| 983 |
promises.push( |
| 984 |
store.get('something').then(failOp('getting'), assertCorrectError)); |
| 985 |
promises.push( |
| 986 |
store.getAll().then(failOp('getting all'), assertCorrectError)); |
| 987 |
promises.push( |
| 988 |
store.clear().then(failOp('clearing all'), assertCorrectError)); |
| 989 |
|
| 990 |
promises.push( |
| 991 |
index.get('anything') |
| 992 |
.then(failOp('getting from index'), assertCorrectError)); |
| 993 |
promises.push( |
| 994 |
index.getKey('anything') |
| 995 |
.then(failOp('getting key from index'), assertCorrectError)); |
| 996 |
promises.push( |
| 997 |
index.getAll('anything') |
| 998 |
.then(failOp('getting all from index'), assertCorrectError)); |
| 999 |
promises.push( |
| 1000 |
index.getAllKeys('anything') |
| 1001 |
.then(failOp('getting all keys from index'), assertCorrectError)); |
| 1002 |
return goog.Promise.all(promises); |
| 1003 |
}; |
| 1004 |
|
| 1005 |
return globalDb.branch() |
| 1006 |
.addCallback(addStoreWithIndex) |
| 1007 |
.addCallback(createAndFinishTransaction) |
| 1008 |
.addCallback(assertCantUseInactiveTransaction); |
| 1009 |
} |
| 1010 |
|
| 1011 |
function testWrongTransactionMode() { |
| 1012 |
if (!idbSupported) { |
| 1013 |
return; |
| 1014 |
} |
| 1015 |
|
| 1016 |
return globalDb.branch().addCallback(addStore).addCallback(function(db) { |
| 1017 |
var tx = db.createTransaction(['store']); |
| 1018 |
assertEquals(goog.db.Transaction.TransactionMode.READ_ONLY, tx.getMode()); |
| 1019 |
var promises = []; |
| 1020 |
promises.push( |
| 1021 |
tx.objectStore('store') |
| 1022 |
.put('KABOOM!', 'anything') |
| 1023 |
.then( |
| 1024 |
function() { fail('putting should have failed'); }, |
| 1025 |
function(err) { |
| 1026 |
assertEquals( |
| 1027 |
goog.db.Error.ErrorName.READ_ONLY_ERR, err.getName()); |
| 1028 |
})); |
| 1029 |
promises.push( |
| 1030 |
tx.objectStore('store') |
| 1031 |
.add('EXPLODE!', 'die') |
| 1032 |
.then( |
| 1033 |
function() { fail('adding should have failed'); }, |
| 1034 |
function(err) { |
| 1035 |
assertEquals( |
| 1036 |
goog.db.Error.ErrorName.READ_ONLY_ERR, err.getName()); |
| 1037 |
})); |
| 1038 |
promises.push( |
| 1039 |
tx.objectStore('store') |
| 1040 |
.remove('no key', 'nothing') |
| 1041 |
.then( |
| 1042 |
function() { fail('deleting should have failed'); }, |
| 1043 |
function(err) { |
| 1044 |
assertEquals( |
| 1045 |
goog.db.Error.ErrorName.READ_ONLY_ERR, err.getName()); |
| 1046 |
})); |
| 1047 |
return goog.Promise.all(promises); |
| 1048 |
}); |
| 1049 |
} |
| 1050 |
|
| 1051 |
function testManipulateIndexes() { |
| 1052 |
if (!idbSupported) { |
| 1053 |
return; |
| 1054 |
} |
| 1055 |
|
| 1056 |
return globalDb.branch() |
| 1057 |
.addCallback(function(db) { |
| 1058 |
return incrementVersion(db, function(ev, db, tx) { |
| 1059 |
var store = db.createObjectStore('store'); |
| 1060 |
store.createIndex('index', 'attr1'); |
| 1061 |
store.createIndex('uniqueIndex', 'attr2', {unique: true}); |
| 1062 |
store.createIndex('multirowIndex', 'attr3', {multirow: true}); |
| 1063 |
}); |
| 1064 |
}) |
| 1065 |
.addCallback(function(db) { |
| 1066 |
var tx = db.createTransaction(['store']); |
| 1067 |
var store = tx.objectStore('store'); |
| 1068 |
var index = store.getIndex('index'); |
| 1069 |
var uniqueIndex = store.getIndex('uniqueIndex'); |
| 1070 |
var multirowIndex = store.getIndex('multirowIndex'); |
| 1071 |
try { |
| 1072 |
var dies = store.getIndex('diediedie'); |
| 1073 |
fail('getting non-existent index should have failed'); |
| 1074 |
} catch (err) { |
| 1075 |
// expected |
| 1076 |
assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName()); |
| 1077 |
} |
| 1078 |
|
| 1079 |
return tx.wait(); |
| 1080 |
}) |
| 1081 |
.addCallback(function(db) { |
| 1082 |
return incrementVersion(db, function(ev, db, tx) { |
| 1083 |
var store = tx.objectStore('store'); |
| 1084 |
store.deleteIndex('index'); |
| 1085 |
try { |
| 1086 |
store.deleteIndex('diediedie'); |
| 1087 |
fail('deleting non-existent index should have failed'); |
| 1088 |
} catch (err) { |
| 1089 |
// expected |
| 1090 |
assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName()); |
| 1091 |
} |
| 1092 |
}); |
| 1093 |
}) |
| 1094 |
.addCallback(function(db) { |
| 1095 |
var tx = db.createTransaction(['store']); |
| 1096 |
var store = tx.objectStore('store'); |
| 1097 |
try { |
| 1098 |
var index = store.getIndex('index'); |
| 1099 |
fail('getting deleted index should have failed'); |
| 1100 |
} catch (err) { |
| 1101 |
// expected |
| 1102 |
assertEquals(goog.db.Error.ErrorName.NOT_FOUND_ERR, err.getName()); |
| 1103 |
} |
| 1104 |
var uniqueIndex = store.getIndex('uniqueIndex'); |
| 1105 |
var multirowIndex = store.getIndex('multirowIndex'); |
| 1106 |
}); |
| 1107 |
} |
| 1108 |
|
| 1109 |
function testAddRecordWithIndex() { |
| 1110 |
if (!idbSupported) { |
| 1111 |
return; |
| 1112 |
} |
| 1113 |
|
| 1114 |
var addData = function(db) { |
| 1115 |
var store = db.createTransaction(['store'], TransactionMode.READ_WRITE) |
| 1116 |
.objectStore('store'); |
| 1117 |
assertFalse(store.getIndex('index').isUnique()); |
| 1118 |
assertEquals('value', store.getIndex('index').getKeyPath()); |
| 1119 |
return store.add({key: 'someKey', value: 'lookUpThis'}) |
| 1120 |
.addCallback(function() { return db; }); |
| 1121 |
}; |
| 1122 |
var readAndAssertAboutData = function(db) { |
| 1123 |
var index = |
| 1124 |
db.createTransaction(['store']).objectStore('store').getIndex('index'); |
| 1125 |
var promises = [ |
| 1126 |
index.get('lookUpThis').addCallback(function(result) { |
| 1127 |
assertNotUndefined(result); |
| 1128 |
assertEquals('someKey', result.key); |
| 1129 |
assertEquals('lookUpThis', result.value); |
| 1130 |
}), |
| 1131 |
index.getKey('lookUpThis').addCallback(function(result) { |
| 1132 |
assertNotUndefined(result); |
| 1133 |
assertEquals('someKey', result); |
| 1134 |
}) |
| 1135 |
]; |
| 1136 |
return goog.Promise.all(promises).then(function() { return db; }); |
| 1137 |
}; |
| 1138 |
return globalDb.branch() |
| 1139 |
.addCallback(addStoreWithIndex) |
| 1140 |
.addCallback(addData) |
| 1141 |
.addCallback(readAndAssertAboutData); |
| 1142 |
} |
| 1143 |
|
| 1144 |
function testGetMultipleRecordsFromIndex() { |
| 1145 |
if (!idbSupported) { |
| 1146 |
return; |
| 1147 |
} |
| 1148 |
|
| 1149 |
var addData = function(db) { |
| 1150 |
var addTx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 1151 |
addTx.objectStore('store').add({key: '1', value: 'a'}); |
| 1152 |
addTx.objectStore('store').add({key: '2', value: 'a'}); |
| 1153 |
addTx.objectStore('store').add({key: '3', value: 'b'}); |
| 1154 |
|
| 1155 |
return addTx.wait(); |
| 1156 |
}; |
| 1157 |
var readData = function(db) { |
| 1158 |
var index = |
| 1159 |
db.createTransaction(['store']).objectStore('store').getIndex('index'); |
| 1160 |
var promises = []; |
| 1161 |
promises.push(index.getAll().addCallback(function(results) { |
| 1162 |
assertNotUndefined(results); |
| 1163 |
assertEquals(3, results.length); |
| 1164 |
})); |
| 1165 |
promises.push(index.getAll('a').addCallback(function(results) { |
| 1166 |
assertNotUndefined(results); |
| 1167 |
assertEquals(2, results.length); |
| 1168 |
})); |
| 1169 |
promises.push(index.getAllKeys().addCallback(function(results) { |
| 1170 |
assertNotUndefined(results); |
| 1171 |
assertEquals(3, results.length); |
| 1172 |
assertArrayEquals(['1', '2', '3'], results); |
| 1173 |
})); |
| 1174 |
promises.push(index.getAllKeys('b').addCallback(function(results) { |
| 1175 |
assertNotUndefined(results); |
| 1176 |
assertEquals(1, results.length); |
| 1177 |
assertArrayEquals(['3'], results); |
| 1178 |
})); |
| 1179 |
|
| 1180 |
return goog.Promise.all(promises).then(function() { return db; }); |
| 1181 |
}; |
| 1182 |
return globalDb.branch() |
| 1183 |
.addCallback(addStoreWithIndex) |
| 1184 |
.addCallback(addData) |
| 1185 |
.addCallback(readData); |
| 1186 |
} |
| 1187 |
|
| 1188 |
function testUniqueIndex() { |
| 1189 |
if (!idbSupported) { |
| 1190 |
return; |
| 1191 |
} |
| 1192 |
|
| 1193 |
var storeDuplicatesToUniqueIndex = function(db) { |
| 1194 |
var tx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 1195 |
assertTrue(tx.objectStore('store').getIndex('index').isUnique()); |
| 1196 |
tx.objectStore('store').add({key: '1', value: 'a'}); |
| 1197 |
tx.objectStore('store').add({key: '2', value: 'a'}); |
| 1198 |
return transactionToPromise(db, tx).then( |
| 1199 |
function() { |
| 1200 |
fail('Expected transaction violating unique constraint to fail'); |
| 1201 |
}, |
| 1202 |
function(ev) { |
| 1203 |
// expected |
| 1204 |
assertEquals( |
| 1205 |
goog.db.Error.ErrorName.CONSTRAINT_ERR, ev.target.getName()); |
| 1206 |
}); |
| 1207 |
}; |
| 1208 |
|
| 1209 |
return globalDb.branch() |
| 1210 |
.addCallback(function(db) { |
| 1211 |
return incrementVersion(db, function(ev, db, tx) { |
| 1212 |
var store = db.createObjectStore('store', {keyPath: 'key'}); |
| 1213 |
store.createIndex('index', 'value', {unique: true}); |
| 1214 |
}); |
| 1215 |
}) |
| 1216 |
.addCallback(storeDuplicatesToUniqueIndex); |
| 1217 |
} |
| 1218 |
|
| 1219 |
function testDeleteDatabase() { |
| 1220 |
if (!idbSupported) { |
| 1221 |
return; |
| 1222 |
} |
| 1223 |
|
| 1224 |
return globalDb.branch() |
| 1225 |
.addCallback(addStore) |
| 1226 |
.addCallback(function(db) { |
| 1227 |
db.close(); |
| 1228 |
return goog.db.deleteDatabase(dbName, function() { |
| 1229 |
fail('didn\'t expect deleteDatabase to be blocked'); |
| 1230 |
}); |
| 1231 |
}) |
| 1232 |
.addCallback(openDatabase) |
| 1233 |
.addCallback(assertStoreDoesntExist); |
| 1234 |
} |
| 1235 |
|
| 1236 |
function testDeleteDatabaseIsBlocked() { |
| 1237 |
if (!idbSupported) { |
| 1238 |
return; |
| 1239 |
} |
| 1240 |
|
| 1241 |
var wasBlocked = false; |
| 1242 |
return globalDb.branch() |
| 1243 |
.addCallback(addStore) |
| 1244 |
.addCallback(function(db) { |
| 1245 |
db.close(); |
| 1246 |
// Get a fresh connection, without any events registered on globalDb. |
| 1247 |
return goog.db.openDatabase(dbName); |
| 1248 |
}) |
| 1249 |
.addCallback(function(db) { |
| 1250 |
dbsToClose.push(db); |
| 1251 |
return goog.db.deleteDatabase(dbName, function(ev) { |
| 1252 |
wasBlocked = true; |
| 1253 |
db.close(); |
| 1254 |
}); |
| 1255 |
}) |
| 1256 |
.addCallback(function() { |
| 1257 |
assertTrue(wasBlocked); |
| 1258 |
return openDatabase(); |
| 1259 |
}) |
| 1260 |
.addCallback(assertStoreDoesntExist); |
| 1261 |
} |
| 1262 |
|
| 1263 |
function testBlockedDeleteDatabaseWithVersionChangeEvent() { |
| 1264 |
if (!idbSupported) { |
| 1265 |
return; |
| 1266 |
} |
| 1267 |
|
| 1268 |
var gotVersionChange = false; |
| 1269 |
return globalDb.branch() |
| 1270 |
.addCallback(addStore) |
| 1271 |
.addCallback(function(db) { |
| 1272 |
db.close(); |
| 1273 |
// Get a fresh connection, without any events registered on globalDb. |
| 1274 |
return goog.db.openDatabase(dbName); |
| 1275 |
}) |
| 1276 |
.addCallback(function(db) { |
| 1277 |
dbsToClose.push(db); |
| 1278 |
goog.events.listen( |
| 1279 |
db, goog.db.IndexedDb.EventType.VERSION_CHANGE, function(ev) { |
| 1280 |
gotVersionChange = true; |
| 1281 |
db.close(); |
| 1282 |
}); |
| 1283 |
return goog.db.deleteDatabase(dbName); |
| 1284 |
}) |
| 1285 |
.addCallback(function() { |
| 1286 |
assertTrue(gotVersionChange); |
| 1287 |
return openDatabase(); |
| 1288 |
}) |
| 1289 |
.addCallback(assertStoreDoesntExist); |
| 1290 |
} |
| 1291 |
|
| 1292 |
function testDeleteNonExistentDatabase() { |
| 1293 |
if (!idbSupported) { |
| 1294 |
return; |
| 1295 |
} |
| 1296 |
|
| 1297 |
// Deleting non-existent db is a no-op. Shall not throw anything. |
| 1298 |
return globalDb.branch().addCallback(function(db) { |
| 1299 |
db.close(); |
| 1300 |
return goog.db.deleteDatabase('non-existent-db'); |
| 1301 |
}); |
| 1302 |
} |
| 1303 |
|
| 1304 |
function testObjectStoreCountAll() { |
| 1305 |
if (!idbSupported) { |
| 1306 |
return; |
| 1307 |
} |
| 1308 |
|
| 1309 |
var values = ['1', '2', '3', '4']; |
| 1310 |
var keys = ['a', 'b', 'c', 'd']; |
| 1311 |
|
| 1312 |
var addData = goog.partial(populateStore, values, keys); |
| 1313 |
|
| 1314 |
return globalDb.branch() |
| 1315 |
.addCallback(addStore) |
| 1316 |
.addCallback(addData) |
| 1317 |
.addCallback(function(db) { |
| 1318 |
var tx = db.createTransaction(['store']); |
| 1319 |
return tx.objectStore('store').count().addCallback(function(count) { |
| 1320 |
assertEquals(values.length, count); |
| 1321 |
}); |
| 1322 |
}); |
| 1323 |
} |
| 1324 |
|
| 1325 |
|
| 1326 |
function testObjectStoreCountSome() { |
| 1327 |
if (!idbSupported) { |
| 1328 |
return; |
| 1329 |
} |
| 1330 |
|
| 1331 |
var values = ['1', '2', '3', '4']; |
| 1332 |
var keys = ['a', 'b', 'c', 'd']; |
| 1333 |
|
| 1334 |
var addData = goog.partial(populateStore, values, keys); |
| 1335 |
var countData = function(db) { |
| 1336 |
var tx = db.createTransaction(['store']); |
| 1337 |
return tx.objectStore('store') |
| 1338 |
.count(goog.db.KeyRange.bound('b', 'c')) |
| 1339 |
.addCallback(function(count) { assertEquals(2, count); }); |
| 1340 |
}; |
| 1341 |
|
| 1342 |
return globalDb.branch() |
| 1343 |
.addCallback(addStore) |
| 1344 |
.addCallback(addData) |
| 1345 |
.addCallback(countData); |
| 1346 |
} |
| 1347 |
|
| 1348 |
function testIndexCursorGet() { |
| 1349 |
if (!idbSupported) { |
| 1350 |
return; |
| 1351 |
} |
| 1352 |
|
| 1353 |
var values = ['1', '2', '3', '4']; |
| 1354 |
var keys = ['a', 'b', 'c', 'd']; |
| 1355 |
var addData = goog.partial(populateStoreWithObjects, values, keys); |
| 1356 |
|
| 1357 |
var valuesResult = []; |
| 1358 |
var keysResult = []; |
| 1359 |
|
| 1360 |
// Open the cursor over range ['b', 'c'], move in backwards direction. |
| 1361 |
var walkBackwardsOverCursor = function(db) { |
| 1362 |
var cursorTx = db.createTransaction(['store']); |
| 1363 |
var index = cursorTx.objectStore('store').getIndex('index'); |
| 1364 |
var values = []; |
| 1365 |
var keys = []; |
| 1366 |
|
| 1367 |
var cursor = index.openCursor( |
| 1368 |
goog.db.KeyRange.bound('2', '3'), goog.db.Cursor.Direction.PREV); |
| 1369 |
var cursorFinished = forEachRecord(cursor, function() { |
| 1370 |
valuesResult.push(cursor.getValue()['value']); |
| 1371 |
keysResult.push(cursor.getValue()['key']); |
| 1372 |
cursor.next(); |
| 1373 |
}); |
| 1374 |
|
| 1375 |
return goog.Promise.all([cursorFinished, cursorTx.wait()]).then(function() { |
| 1376 |
return db |
| 1377 |
}); |
| 1378 |
}; |
| 1379 |
|
| 1380 |
return globalDb.branch() |
| 1381 |
.addCallbacks(addStoreWithIndex) |
| 1382 |
.addCallback(addData) |
| 1383 |
.addCallback(walkBackwardsOverCursor) |
| 1384 |
.addCallback(function(db) { |
| 1385 |
assertArrayEquals(['3', '2'], valuesResult); |
| 1386 |
assertArrayEquals(['c', 'b'], keysResult); |
| 1387 |
}); |
| 1388 |
} |
| 1389 |
|
| 1390 |
function testIndexCursorReplace() { |
| 1391 |
if (!idbSupported) { |
| 1392 |
return; |
| 1393 |
} |
| 1394 |
|
| 1395 |
var values = ['1', '2', '3', '4']; |
| 1396 |
var keys = ['a', 'b', 'c', 'd']; |
| 1397 |
|
| 1398 |
var addData = goog.partial(populateStoreWithObjects, values, keys); |
| 1399 |
var valuesResult = []; |
| 1400 |
var keysResult = []; |
| 1401 |
|
| 1402 |
// Store should contain ['1', '2', '5', '4'] after replacement. |
| 1403 |
var checkStore = goog.partial(assertStoreObjectValues, ['1', '2', '5', '4']); |
| 1404 |
|
| 1405 |
// Use a bounded cursor for ['3', '4') to update value '3' -> '5'. |
| 1406 |
var openCursorAndReplace = function(db) { |
| 1407 |
var cursorTx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 1408 |
var index = cursorTx.objectStore('store').getIndex('index'); |
| 1409 |
var cursor = |
| 1410 |
index.openCursor(goog.db.KeyRange.bound('3', '4', false, true)); |
| 1411 |
|
| 1412 |
var cursorFinished = forEachRecord(cursor, function() { |
| 1413 |
assertEquals('3', cursor.getValue()['value']); |
| 1414 |
return cursor.update({'key': cursor.getValue()['key'], 'value': '5'}) |
| 1415 |
.addCallback(function() { cursor.next(); }); |
| 1416 |
}); |
| 1417 |
|
| 1418 |
return goog.Promise.all([cursorFinished, cursorTx.wait()]) |
| 1419 |
.then(function(results) { return db; }); |
| 1420 |
}; |
| 1421 |
|
| 1422 |
// Setup and execute test case. |
| 1423 |
return globalDb.branch() |
| 1424 |
.addCallback(addStoreWithIndex) |
| 1425 |
.addCallback(addData) |
| 1426 |
.addCallback(openCursorAndReplace) |
| 1427 |
.addCallback(checkStore); |
| 1428 |
} |
| 1429 |
|
| 1430 |
function testIndexCursorRemove() { |
| 1431 |
if (!idbSupported) { |
| 1432 |
return; |
| 1433 |
} |
| 1434 |
|
| 1435 |
var values = ['1', '2', '3', '4']; |
| 1436 |
var keys = ['a', 'b', 'c', 'd']; |
| 1437 |
|
| 1438 |
var addData = goog.partial(populateStoreWithObjects, values, keys); |
| 1439 |
|
| 1440 |
// Store should contain ['1', '2'] after removing elements. |
| 1441 |
var checkStore = goog.partial(assertStoreObjectValues, ['1', '2']); |
| 1442 |
|
| 1443 |
// Use a bounded cursor for ('2', ...) to remove '3', '4'. |
| 1444 |
var openCursorAndRemove = function(db) { |
| 1445 |
var cursorTx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 1446 |
|
| 1447 |
var store = cursorTx.objectStore('store'); |
| 1448 |
var index = store.getIndex('index'); |
| 1449 |
var cursor = index.openCursor(goog.db.KeyRange.lowerBound('2', true)); |
| 1450 |
var cursorFinished = forEachRecord(cursor, function() { |
| 1451 |
return cursor.remove('5').addCallback(function() { cursor.next(); }); |
| 1452 |
}); |
| 1453 |
|
| 1454 |
return goog.Promise.all([cursorFinished, cursorTx.wait()]) |
| 1455 |
.then(function(results) { return db; }); |
| 1456 |
}; |
| 1457 |
|
| 1458 |
// Setup and execute test case. |
| 1459 |
return globalDb.branch() |
| 1460 |
.addCallback(addStoreWithIndex) |
| 1461 |
.addCallback(addData) |
| 1462 |
.addCallback(openCursorAndRemove) |
| 1463 |
.addCallback(checkStore); |
| 1464 |
} |
| 1465 |
|
| 1466 |
function testCanWaitForTransactionToComplete() { |
| 1467 |
if (!idbSupported) { |
| 1468 |
return; |
| 1469 |
} |
| 1470 |
return globalDb.branch().addCallback(addStore).addCallback(function(db) { |
| 1471 |
var tx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 1472 |
tx.objectStore('store').add({key: 'hi', value: 'something'}, 'stuff'); |
| 1473 |
return tx.wait(); |
| 1474 |
}); |
| 1475 |
} |
| 1476 |
|
| 1477 |
function testWaitingOnTransactionThatHasAnError() { |
| 1478 |
if (!idbSupported) { |
| 1479 |
return; |
| 1480 |
} |
| 1481 |
return globalDb.branch() |
| 1482 |
.addCallback(function(db) { |
| 1483 |
return incrementVersion(db, function(ev, db, tx) { |
| 1484 |
var store = db.createObjectStore('store', {keyPath: 'key'}); |
| 1485 |
store.createIndex('index', 'value', {unique: true}); |
| 1486 |
}); |
| 1487 |
}) |
| 1488 |
.addCallback(function(db) { |
| 1489 |
var tx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 1490 |
assertTrue(tx.objectStore('store').getIndex('index').isUnique()); |
| 1491 |
tx.objectStore('store').add({key: '1', value: 'a'}); |
| 1492 |
tx.objectStore('store').add({key: '2', value: 'a'}); |
| 1493 |
return transactionToPromise(db, tx).then( |
| 1494 |
function() { fail('expected transaction to fail'); }, |
| 1495 |
function(ev) { |
| 1496 |
// expected |
| 1497 |
assertEquals( |
| 1498 |
goog.db.Error.ErrorName.CONSTRAINT_ERR, ev.target.getName()); |
| 1499 |
}); |
| 1500 |
}); |
| 1501 |
} |
| 1502 |
|
| 1503 |
function testWaitingOnAnAbortedTransaction() { |
| 1504 |
if (!idbSupported) { |
| 1505 |
return; |
| 1506 |
} |
| 1507 |
return globalDb.addCallback(addStore).addCallback(function(db) { |
| 1508 |
var tx = db.createTransaction(['store'], TransactionMode.READ_WRITE); |
| 1509 |
var waiting = tx.wait().then( |
| 1510 |
function() { fail('Wait result should have failed'); }, |
| 1511 |
function(e) { |
| 1512 |
assertEquals(goog.db.Error.ErrorName.ABORT_ERR, e.getName()); |
| 1513 |
}); |
| 1514 |
tx.abort(); |
| 1515 |
return waiting; |
| 1516 |
}); |
| 1517 |
} |