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
= 5;
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");
90 function testXhr(url
, onSuccess
) {
91 var xmlhttp
= new window
.XMLHttpRequest();
92 xmlhttp
.open("GET", url
, /*async=*/false);
93 xmlhttp
.onreadystatechange = function() {
94 if (xmlhttp
.readyState
=== 4) {
95 if (xmlhttp
.status
=== 200) {
104 // Common setup tasks for the other tests in this object
105 testCommon: function(mode
) {
106 transaction
= db
.transaction('storeName', mode
);
107 db
.onclose
= databaseClosed
;
108 transaction
.onabort
= transactionAbort
;
109 transaction
.onerror
= transactionError
;
110 objectStore
= transaction
.objectStore('storeName');
113 testXhr("/corrupt/test/fail?class=LevelDBTransaction&method=Get&instNum=1",
115 tests
.testCommon('readonly');
116 request
= objectStore
.get('key-0');
117 request
.onsuccess
= unexpectedSuccessCallback
;
118 request
.onerror
= requestError
;
121 failTransactionCommit: function() {
122 tests
.testCommon('readwrite');
123 db
.onclose = function(event
) {
124 shouldBe("numTransactionErrors", "0");
125 shouldBe("numTransactionAborts", "1");
127 done("Closed as expected");
129 testXhr("/corrupt/test/fail?class=LevelDBTransaction&method=Commit", function() {
130 request
= objectStore
.put("Any Value", 'key-0');
131 request
.onerror
= requestError
;
134 failWebkitGetDatabaseNames: function() {
135 tests
.testCommon('readonly');
137 db
.onclose = function(event
) {
138 shouldBe("numTransactionErrors", "0");
139 shouldBe("numTransactionAborts", "1");
140 shouldBe("gotRequestError", "1");
142 done("Closed as expected");
144 testXhr("/corrupt/test/fail?class=LevelDBIterator&method=Seek", function() {
145 request
= window
.indexedDB
.webkitGetDatabaseNames();
146 request
.onsuccess
= unexpectedSuccessCallback
;
147 request
.onerror = function(evt
) {
148 gotRequestError
+= 1;
152 iterate: function() {
153 testXhr("/corrupt/test/corruptdb?storeName", function() {
154 tests
.testCommon('readonly');
155 request
= objectStore
.openCursor();
156 request
.onerror
= requestError
;
157 request
.onsuccess = function (event
){
158 var cursor
= request
.result
;
160 // Get an object. Probably shouldn't get this far, but won't call this an error.
163 // Got the last object. We shouldn't get this far.
164 fail("Should *not* have been able to iterate over database.");
169 failGetBlobJournal: function() {
170 // Get() #1 the actual get, #2 is for the journal.
171 testXhr("/corrupt/test/fail?class=LevelDBTransaction&method=Get&instNum=2", function() {
172 tests
.testCommon('readwrite');
173 request
.onsuccess
= unexpectedSuccessCallback
;
174 request
.onerror
= requestError
;
175 db
.onclose
= function databaseClosed(event
) {
176 shouldBe("numTransactionErrors", "0");
177 shouldBe("numTransactionAborts", "1");
179 done("Closed as expected");
181 request
= objectStore
.get('key-0');
184 clearObjectStore: function() {
185 testXhr("/corrupt/test/corruptdb?storeName", function() {
186 tests
.testCommon('readwrite');
187 request
= objectStore
.clear();
188 request
.onerror
= requestError
;
189 request
.onsuccess
= unexpectedSuccessCallback
194 function openCallback() {
195 if (testType
in tests
)
198 fail('Unknown test: "' + testType
+ '"');
203 <body onLoad=
"test()">
204 <div id=
"status">Starting...
</div>