| OLD | NEW |
| 1 // Copyright (c) 2011 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 function test() |
| 6 window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction; | |
| 7 | |
| 8 window.testResult = ''; | |
| 9 | |
| 10 function unexpectedErrorCallback() | |
| 11 { | 6 { |
| 12 document.title = 'fail - unexpected error callback'; | |
| 13 } | |
| 14 function unexpectedAbortCallback() | |
| 15 { | |
| 16 document.title = 'fail - unexpected abort callback'; | |
| 17 } | |
| 18 function unexpectedCompleteCallback() | |
| 19 { | |
| 20 document.title = 'fail - unexpected complete callback'; | |
| 21 } | |
| 22 | |
| 23 function test() { | |
| 24 if (document.location.hash === '#part1') { | 7 if (document.location.hash === '#part1') { |
| 25 testPart1(); | 8 testPart1(); |
| 26 } else if (document.location.hash === '#part2') { | 9 } else if (document.location.hash === '#part2') { |
| 27 testPart2(); | 10 testPart2(); |
| 28 } else if (document.location.hash === '#part3') { | 11 } else if (document.location.hash === '#part3') { |
| 29 testPart3(); | 12 testPart3(); |
| 30 } else { | 13 } else { |
| 31 document.title = 'fail'; | 14 result('fail - unexpected hash'); |
| 32 } | 15 } |
| 33 } | 16 } |
| 34 | 17 |
| 35 function testPart1() { | 18 function testPart1() |
| 19 { |
| 36 // Prepare the database, then exit normally | 20 // Prepare the database, then exit normally |
| 37 | 21 |
| 38 // Set version 1, create store1 | 22 // Set version 1, create store1 |
| 39 var delreq = window.indexedDB.deleteDatabase('version-change-crash'); | 23 var delreq = window.indexedDB.deleteDatabase('version-change-crash'); |
| 40 delreq.onerror = unexpectedErrorCallback; | 24 delreq.onerror = unexpectedErrorCallback; |
| 41 delreq.onsuccess = function() { | 25 delreq.onsuccess = function() { |
| 42 var openreq = window.indexedDB.open('version-change-crash'); | 26 var openreq = window.indexedDB.open('version-change-crash'); |
| 43 openreq.onerror = unexpectedErrorCallback; | 27 openreq.onerror = unexpectedErrorCallback; |
| 44 openreq.onsuccess = function(e) { | 28 openreq.onsuccess = function(e) { |
| 45 var db = openreq.result; | 29 var db = openreq.result; |
| 46 var setverreq = db.setVersion('1'); | 30 var setverreq = db.setVersion('1'); |
| 47 setverreq.onerror = unexpectedErrorCallback; | 31 setverreq.onerror = unexpectedErrorCallback; |
| 48 setverreq.onsuccess = function(e) { | 32 setverreq.onsuccess = function(e) { |
| 49 var transaction = setverreq.result; | 33 var transaction = setverreq.result; |
| 50 db.createObjectStore('store1'); | 34 db.createObjectStore('store1'); |
| 51 transaction.onabort = unexpectedAbortCallback; | 35 transaction.onabort = unexpectedAbortCallback; |
| 52 transaction.oncomplete = function (e) { | 36 transaction.oncomplete = function (e) { |
| 53 document.title = 'pass - part1 - complete'; | 37 result('pass - part1 - complete'); |
| 54 }; | 38 }; |
| 55 }; | 39 }; |
| 56 }; | 40 }; |
| 57 }; | 41 }; |
| 58 } | 42 } |
| 59 | 43 |
| 60 function testPart2() { | 44 function testPart2() |
| 45 { |
| 61 // Start a VERSION_CHANGE then crash | 46 // Start a VERSION_CHANGE then crash |
| 62 | 47 |
| 63 // Set version 2, twiddle stores and crash | 48 // Set version 2, twiddle stores and crash |
| 64 var openreq = window.indexedDB.open('version-change-crash'); | 49 var openreq = window.indexedDB.open('version-change-crash'); |
| 65 openreq.onerror = unexpectedErrorCallback; | 50 openreq.onerror = unexpectedErrorCallback; |
| 66 openreq.onsuccess = function(e) { | 51 openreq.onsuccess = function(e) { |
| 67 var db = openreq.result; | 52 var db = openreq.result; |
| 68 var setverreq = db.setVersion('2'); | 53 var setverreq = db.setVersion('2'); |
| 69 setverreq.onerror = unexpectedErrorCallback; | 54 setverreq.onerror = unexpectedErrorCallback; |
| 70 setverreq.onsuccess = function(e) { | 55 setverreq.onsuccess = function(e) { |
| 71 var transaction = setverreq.result; | 56 var transaction = setverreq.result; |
| 72 transaction.onabort = unexpectedAbortCallback; | 57 transaction.onabort = unexpectedAbortCallback; |
| 73 transaction.oncomplete = unexpectedCompleteCallback; | 58 transaction.oncomplete = unexpectedCompleteCallback; |
| 74 | 59 |
| 75 var store = db.createObjectStore('store2'); | 60 var store = db.createObjectStore('store2'); |
| 76 document.title = 'pass - part2 - crash me'; | 61 result('pass - part2 - crash me'); |
| 77 | 62 |
| 78 // Keep adding to the transaction so it can't commit | 63 // Keep adding to the transaction so it can't commit |
| 79 (function loop() { store.put(0, 0).onsuccess = loop; }()); | 64 (function loop() { store.put(0, 0).onsuccess = loop; }()); |
| 80 }; | 65 }; |
| 81 }; | 66 }; |
| 82 } | 67 } |
| 83 | 68 |
| 84 function testPart3() { | 69 function testPart3() |
| 70 { |
| 85 // Validate that Part 2 never committed | 71 // Validate that Part 2 never committed |
| 86 | 72 |
| 87 // Check version | 73 // Check version |
| 88 var openreq = window.indexedDB.open('version-change-crash'); | 74 var openreq = window.indexedDB.open('version-change-crash'); |
| 89 openreq.onerror = unexpectedErrorCallback; | 75 openreq.onerror = unexpectedErrorCallback; |
| 90 openreq.onsuccess = function(e) { | 76 openreq.onsuccess = function(e) { |
| 91 var db = openreq.result; | 77 var db = openreq.result; |
| 92 if (db.version !== '1') { | 78 if (db.version !== '1') { |
| 93 document.title = 'fail - version incorrect'; | 79 result('fail - version incorrect'); |
| 94 return; | 80 return; |
| 95 } | 81 } |
| 96 | 82 |
| 97 if (!db.objectStoreNames.contains('store1')) { | 83 if (!db.objectStoreNames.contains('store1')) { |
| 98 document.title = 'fail - store1 does not exist'; | 84 result('fail - store1 does not exist'); |
| 99 return; | 85 return; |
| 100 } | 86 } |
| 101 | 87 |
| 102 if (db.objectStoreNames.contains('store2')) { | 88 if (db.objectStoreNames.contains('store2')) { |
| 103 document.title = 'fail - store2 exists'; | 89 result('fail - store2 exists'); |
| 104 return; | 90 return; |
| 105 } | 91 } |
| 106 | 92 |
| 107 document.title = 'pass - part3 - rolled back'; | 93 result('pass - part3 - rolled back'); |
| 108 }; | 94 }; |
| 109 } | 95 } |
| OLD | NEW |