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