Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / test / data / indexeddb / corrupted_open_db_detection.html
blobce4569034cf6bb07e79ae5937f1c7bb884887592
1 <!DOCTYPE html>
2 <html>
3 <!--
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.
7 -->
8 <head>
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>
11 <script>
13 var testType = 'get';
15 function test() {
16 testType = location.hash.substring(1);
17 if (testType == 'testCommon') {
18 fail('"testCommon" is a reserved test name');
19 return;
21 indexedDBTest(upgradeCallback, openCallback);
24 var numObjectsWrittenToDb = 5;
25 var numTransactions = 0;
26 var numTransactionErrors = 0;
27 var numTransactionAborts = 0;
28 var numKeys = 0;
29 var transaction;
30 var request;
31 var db;
32 var objectStore;
34 function upgradeCallback() {
35 db = event.target.result;
36 deleteAllObjectStores(db);
37 objectStore = db.createObjectStore('storeName', { autoIncrement : true });
39 var i;
40 var len = 80;
41 var data = Array(len);
42 for (i = 0; i < len; ++i) {
43 data[i] = i;
46 for (i = 0; i < numObjectsWrittenToDb; ++i) {
47 var key = 'key-' + i;
48 request = objectStore.add(data, key);
49 request.onerror = unexpectedErrorCallback;
50 request.onsuccess = upgradeTransactionComplete;
54 function upgradeTransactionComplete() {
55 ++numTransactions;
56 if (numTransactions === numObjectsWrittenToDb) {
57 debug("All transactions written");
61 function transactionError(event) {
62 if (event.target.error) {
63 numTransactionErrors += 1;
64 } else {
65 fail("Transaction onerror had no error");
69 function transactionAbort(event) {
70 if (event.target.error) {
71 numTransactionAborts += 1;
72 } else {
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) {
96 onSuccess()
100 xmlhttp.send();
103 var tests = {
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');
112 get: function() {
113 testXhr("/corrupt/test/fail?class=LevelDBTransaction&method=Get&instNum=1",
114 function() {
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');
136 gotRequestError = 0;
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;
159 if (cursor) {
160 // Get an object. Probably shouldn't get this far, but won't call this an error.
161 cursor.continue();
162 } else {
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)
196 tests[testType]();
197 else
198 fail('Unknown test: "' + testType + '"');
201 </script>
202 </head>
203 <body onLoad="test()">
204 <div id="status">Starting...</div>
205 </body>
206 </html>