1 <html> 2 <head> 3 <link rel="stylesheet" href="../../fast/js/resources/js-test-style.css"> 4 <script src="../../fast/js/resources/js-test-pre.js"></script> 5 <script src="../../fast/js/resources/js-test-post-function.js"></script> 6 <script src="resources/shared.js"></script> 7 </head> 8 <body> 9 <p id="description"></p> 10 <div id="console"></div> 11 <script> 12 13 description("Test IndexedDB's openCursor."); 14 if (window.layoutTestController) 15 layoutTestController.waitUntilDone(); 16 17 test(); 18 19 function test() 20 { 21 request = evalAndLog("webkitIndexedDB.open('cursor-index-delete')"); 22 request.onsuccess = openSuccess; 23 request.onerror = unexpectedErrorCallback; 24 } 25 26 function openSuccess() 27 { 28 var db = evalAndLog("db = event.target.result"); 29 30 request = evalAndLog("db.setVersion('new version')"); 31 request.onsuccess = setVersionSuccess; 32 request.onerror = unexpectedErrorCallback; 33 } 34 35 function setVersionSuccess() 36 { 37 debug("setVersionSuccess():"); 38 window.trans = evalAndLog("trans = event.target.result"); 39 shouldBeTrue("trans !== null"); 40 trans.onabort = unexpectedAbortCallback; 41 trans.oncomplete = openCursor; 42 43 deleteAllObjectStores(db); 44 45 objectStore = evalAndLog("objectStore = db.createObjectStore('test')"); 46 evalAndLog("objectStore.createIndex('testIndex', 'x')"); 47 48 evalAndLog("objectStore.add({x: 1}, 'myKey1')"); 49 evalAndLog("objectStore.add({x: 2}, 'myKey2')"); 50 evalAndLog("objectStore.add({x: 3}, 'myKey3')"); 51 evalAndLog("objectStore.add({x: 4}, 'myKey4')"); 52 } 53 54 function openCursor() 55 { 56 debug("openCursor1"); 57 evalAndLog("trans = db.transaction(['test'], webkitIDBTransaction.READ_WRITE)"); 58 keyRange = webkitIDBKeyRange.lowerBound(1); 59 request = evalAndLog("trans.objectStore('test').index('testIndex').openCursor(keyRange)"); 60 request.onsuccess = cursorSuccess; 61 request.onerror = unexpectedErrorCallback; 62 counter = 1; 63 } 64 65 function cursorSuccess() 66 { 67 if (event.target.result == null) { 68 shouldBe("counter", "5"); 69 request = evalAndLog("trans.objectStore('test').index('testIndex').openCursor(keyRange)"); 70 request.onsuccess = cursorEmpty; 71 request.onerror = unexpectedErrorCallback; 72 return; 73 } 74 var deleteRequest = evalAndLog("event.target.result.delete()"); 75 deleteRequest.onerror = unexpectedErrorCallback; 76 shouldBe("event.target.result.key", "counter++"); 77 evalAndLog("event.target.result.continue()"); 78 } 79 80 function cursorEmpty() 81 { 82 shouldBeNull("event.target.result"); 83 trans.oncomplete = addObject; 84 } 85 86 function addObject() 87 { 88 evalAndLog("trans = db.transaction(['test'], webkitIDBTransaction.READ_WRITE)"); 89 objectStore = evalAndLog("objectStore = trans.objectStore('test')"); 90 request = evalAndLog("objectStore.add({x: 1}, 'myKey1')"); 91 request.onsuccess = openCursor2; 92 request.onerror = unexpectedErrorCallback; 93 } 94 95 function openCursor2() 96 { 97 debug("openCursor2"); 98 evalAndLog("index = event.target.source.index('testIndex')"); 99 request = evalAndLog("index.openCursor(keyRange)"); 100 request.onsuccess = deleteObject; 101 request.onerror = unexpectedErrorCallback; 102 } 103 104 function deleteObject() 105 { 106 shouldBeNonNull(event.target.result); 107 evalAndLog("event.target.result.delete()"); 108 request = evalAndLog("index.get(1)"); 109 request.onsuccess = unexpectedSuccessCallback; 110 request.onerror = verifyObjectDeleted; 111 } 112 113 function verifyObjectDeleted() 114 { 115 evalAndLog("event.preventDefault()"); 116 done(); 117 } 118 119 var successfullyParsed = true; 120 121 </script> 122 </body> 123 </html> 124