Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / test / data / indexeddb / corrupted_open_db_detection.html
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>
12
13 var testType = 'get';
14
15 function test() {
16   testType = location.hash.substring(1);
17   if (testType == 'testCommon') {
18     fail('"testCommon" is a reserved test name');
19     return;
20   }
21   indexedDBTest(upgradeCallback, openCallback);
22 }
23
24 var numObjectsWrittenToDb = 500;
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;
33
34 function upgradeCallback() {
35   db = event.target.result;
36   deleteAllObjectStores(db);
37   objectStore = db.createObjectStore('storeName', { autoIncrement : true });
38
39   var i;
40   var len = 80;
41   var data = Array(len);
42   for (i = 0; i < len; ++i) {
43     data[i] = i;
44   }
45
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;
51   }
52 }
53
54 function upgradeTransactionComplete() {
55   ++numTransactions;
56   if (numTransactions === numObjectsWrittenToDb) {
57     debug("All transactions written");
58   }
59 }
60
61 function transactionError(event) {
62   if (event.target.error) {
63     numTransactionErrors += 1;
64   } else {
65     fail("Transaction onerror had no error");
66   }
67 }
68
69 function transactionAbort(event) {
70   if (event.target.error) {
71     numTransactionAborts += 1;
72   } else {
73     fail("Transaction onabort had no error");
74   }
75 }
76
77 function requestError(event) {
78   if (!event.target.error) {
79     fail("get request had no/invalid error");
80   }
81 }
82
83 function databaseClosed(event) {
84   shouldBe("numTransactionErrors", "1");
85   shouldBe("numTransactionAborts", "1");
86
87   done("Closed as expected");
88 }
89
90 var tests = {
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');
98   },
99   get: function() {
100     tests.testCommon('readonly');
101     request = objectStore.get('key-0');
102     request.onsuccess = unexpectedSuccessCallback;
103     request.onerror = requestError;
104   },
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;
111       if (cursor) {
112         // Get an object. Probably shouldn't get this far, but won't call this an error.
113         cursor.continue();
114       } else {
115         // Got the last object. We shouldn't get this far.
116         fail("Should *not* have been able to iterate over database.");
117       }
118     };
119   },
120   clearObjectStore: function() {
121     tests.testCommon('readwrite');
122     request = objectStore.clear();
123     request.onerror = requestError;
124     request.onsuccess = unexpectedSuccessCallback
125   }
126 };
127
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) {
136             tests[testType]();
137           } else {
138             fail('Unknown test: "' + testType + '"');
139           }
140         }
141       }
142     };
143   xmlhttp.send();
144 }
145
146 </script>
147 </head>
148 <body onLoad="test()">
149 <div id="status">Starting...</div>
150 </body>
151 </html>