4 Copyright 2014 The Chromium Authors. All rights reserved.
5 Use of this source code is governed by a BSD-style license that can be
6 found in the LICENSE file.
9 <title>IDB test that db's corrupted while open are properly handled Part 1 / 2</title>
10 <script type="text/javascript" src="common.js"></script>
16 testType = location.hash.substring(1);
17 if (testType == 'testCommon') {
18 fail('"testCommon" is a reserved test name');
21 indexedDBTest(upgradeCallback, openCallback);
24 var numObjectsWrittenToDb = 500;
25 var numTransactions = 0;
26 var numTransactionErrors = 0;
27 var numTransactionAborts = 0;
34 function upgradeCallback() {
35 db = event.target.result;
36 deleteAllObjectStores(db);
37 objectStore = db.createObjectStore('storeName', { autoIncrement : true });
41 var data = Array(len);
42 for (i = 0; i < len; ++i) {
46 for (i = 0; i < numObjectsWrittenToDb; ++i) {
48 request = objectStore.add(data, key);
49 request.onerror = unexpectedErrorCallback;
50 request.onsuccess = upgradeTransactionComplete;
54 function upgradeTransactionComplete() {
56 if (numTransactions === numObjectsWrittenToDb) {
57 debug("All transactions written");
61 function transactionError(event) {
62 if (event.target.error) {
63 numTransactionErrors += 1;
65 fail("Transaction onerror had no error");
69 function transactionAbort(event) {
70 if (event.target.error) {
71 numTransactionAborts += 1;
73 fail("Transaction onabort had no error");
77 function requestError(event) {
78 if (!event.target.error) {
79 fail("get request had no/invalid error");
83 function databaseClosed(event) {
84 shouldBe("numTransactionErrors", "1");
85 shouldBe("numTransactionAborts", "1");
87 done("Closed as expected");
91 // Common setup tasks for the other tests in this object
92 testCommon: function(mode) {
93 transaction = db.transaction('storeName', mode);
94 db.onclose = databaseClosed;
95 transaction.onabort = transactionAbort;
96 transaction.onerror = transactionError;
97 objectStore = transaction.objectStore('storeName');
100 tests.testCommon('readonly');
101 request = objectStore.get('key-0');
102 request.onsuccess = unexpectedSuccessCallback;
103 request.onerror = requestError;
105 iterate: function() {
106 tests.testCommon('readonly');
107 request = objectStore.openCursor();
108 request.onerror = requestError;
109 request.onsuccess = function (event){
110 var cursor = request.result;
112 // Get an object. Probably shouldn't get this far, but won't call this an error.
115 // Got the last object. We shouldn't get this far.
116 fail("Should *not* have been able to iterate over database.");
120 clearObjectStore: function() {
121 tests.testCommon('readwrite');
122 request = objectStore.clear();
123 request.onerror = requestError;
124 request.onsuccess = unexpectedSuccessCallback
128 function openCallback() {
129 var xmlhttp = new window.XMLHttpRequest();
130 xmlhttp.open("GET", "/corrupt/test/corruptdb?storeName", false /*sync*/);
131 xmlhttp.onreadystatechange = function() {
132 if (xmlhttp.readyState === 4) {
133 if (xmlhttp.status === 200) {
134 // The database is now corrupt.
135 if (testType in tests) {
138 fail('Unknown test: "' + testType + '"');
148 <body onLoad="test()">
149 <div id="status">Starting...</div>