Test IndexedDB's cursor mutation
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
dbname = "cursor-mutation.html"
indexedDB.deleteDatabase(dbname)
indexedDB.open(dbname)
setupObjectStoreAndCreateIndex():
objectStore = db.createObjectStore('foo', { keyPath: 'ss' })
index = objectStore.createIndex('name', 'name', { unique: true })
objectStoreData = [
{ ss: '237-23-7732', name: 'Bob' },
{ ss: '237-23-7733', name: 'Ann' },
{ ss: '237-23-7734', name: 'Ron' },
{ ss: '237-23-7735', name: 'Sue' },
{ ss: '237-23-7736', name: 'Joe' },
{ ss: '237-23-7737', name: 'Pat' }
]
objectStore.add(objectStoreData[0])
objectStore.add(objectStoreData[1])
objectStore.add(objectStoreData[2])
objectStore.add(objectStoreData[3])
objectStore.add(objectStoreData[4])
setupCursor():
count = 0
sawAdded = false
sawRemoved = false
request = objectStore.openCursor()
iterateCursor():
cursor = event.target.result
PASS cursor.value.name is "Bob"
sawRemoved = true
count++
cursor.continue()
iterateCursor():
cursor = event.target.result
PASS cursor.value.name is "Ann"
count++
cursor.continue()
iterateCursor():
cursor = event.target.result
PASS cursor.value.name is "Ron"
count++
cursor.continue()
iterateCursor():
cursor = event.target.result
PASS cursor.value.name is "Sue"
count++
cursor.continue()
iterateCursor():
cursor = event.target.result
PASS cursor.value.name is "Joe"
count++
cursor.continue()
iterateCursor():
cursor = event.target.result
checkCursorResults():
PASS count is objectStoreData.length - 1
PASS sawAdded is false
PASS sawRemoved is true
setupMutatingCursor():
count = 0
sawAdded = false
sawRemoved = false
[objectStoreDataNameSort is an array of indexes into objectStoreData in alphabetical order by name]
objectStoreDataNameSort = [ 1, 4, 5, 2, 3 ]
trans = db.transaction('foo', 'readwrite', {durability: 'relaxed'})
objectStore = trans.objectStore('foo')
request = objectStore.index('name').openCursor()
trans.oncomplete = checkMutatingCursorResults
iterateMutatingCursor():
cursor = event.target.result
PASS cursor.value.name is "Ann"
count++
Mutating the object store:
Removing Bob
request = objectStore.delete(objectStoreData[0].ss)
addFinalData():
Adding Pat
request = objectStore.add(objectStoreData[objectStoreData.length - 1])
iterateMutatingCursor():
cursor = event.target.result
PASS cursor.value.name is "Joe"
count++
iterateMutatingCursor():
cursor = event.target.result
PASS cursor.value.name is "Pat"
sawAdded = true
count++
iterateMutatingCursor():
cursor = event.target.result
PASS cursor.value.name is "Ron"
count++
iterateMutatingCursor():
cursor = event.target.result
PASS cursor.value.name is "Sue"
count++
iterateMutatingCursor():
cursor = event.target.result
checkMutatingCursorResults():
PASS count is objectStoreData.length - 1
PASS sawAdded is true
PASS sawRemoved is false
PASS successfullyParsed is true
TEST COMPLETE