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