Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / inspector / indexeddb / indexeddb-test.js
1 var initialize_IndexedDBTest = function() {
2
3 InspectorTest.dumpIndexedDBTree = function()
4 {
5     InspectorTest.addResult("Dumping IndexedDB tree:");
6     var indexedDBTreeElement = WebInspector.panels.resources.indexedDBListTreeElement;
7     if (!indexedDBTreeElement.children.length) {
8         InspectorTest.addResult("    (empty)");
9         return;
10     }
11     for (var i = 0; i < indexedDBTreeElement.children.length; ++i) {
12         var databaseTreeElement = indexedDBTreeElement.children[i];
13         InspectorTest.addResult("    database: " + databaseTreeElement.titleText);
14         if (!databaseTreeElement.children.length) {
15             InspectorTest.addResult("        (no object stores)");
16             continue;
17         }
18         for (var j = 0; j < databaseTreeElement.children.length; ++j) {
19             var objectStoreTreeElement = databaseTreeElement.children[j];
20             InspectorTest.addResult("        Object store: " + objectStoreTreeElement.titleText);
21             if (!objectStoreTreeElement.children.length) {
22                 InspectorTest.addResult("            (no indexes)");
23                 continue;
24             }
25             for (var j = 0; j < objectStoreTreeElement.children.length; ++j) {
26                 var indexTreeElement = objectStoreTreeElement.children[j];
27                 InspectorTest.addResult("            Index: " + indexTreeElement.titleText);
28             }
29         }
30     }
31 }
32
33 var lastCallbackId = 0;
34 var callbacks = {};
35 var callbackIdPrefix = "InspectorTest.IndexedDB_callback";
36 InspectorTest.evaluateWithCallback = function(frameId, methodName, parameters, callback)
37 {
38     InspectorTest._installIndexedDBSniffer();
39     var callbackId = ++lastCallbackId;
40     callbacks[callbackId] = callback;
41     var parametersString = "dispatchCallback.bind(this, \"" + callbackIdPrefix + callbackId + "\")";
42     for (var i = 0; i < parameters.length; ++i)
43         parametersString += ", " + JSON.stringify(parameters[i]);
44
45     var requestString = methodName + "(" + parametersString + ")";
46     InspectorTest.evaluateInPage(requestString);
47 };
48
49 InspectorTest._installIndexedDBSniffer = function()
50 {
51     InspectorTest.addConsoleSniffer(consoleMessageOverride, false);
52
53     function consoleMessageOverride(msg)
54     {
55         var text = msg.consoleMessage().messageText;
56         if (!text.startsWith(callbackIdPrefix)) {
57             InspectorTest.addConsoleSniffer(consoleMessageOverride, false);
58             return;
59         }
60         var callbackId = text.substring(callbackIdPrefix.length);
61         callbacks[callbackId].call();
62         delete callbacks[callbackId];
63     }
64 };
65
66 InspectorTest.createDatabase = function(frameId, databaseName, callback)
67 {
68     InspectorTest.evaluateWithCallback(frameId, "createDatabase", [databaseName], callback)
69 };
70
71 InspectorTest.deleteDatabase = function(frameId, databaseName, callback)
72 {
73     InspectorTest.evaluateWithCallback(frameId, "deleteDatabase", [databaseName], callback)
74 };
75
76 InspectorTest.createObjectStore = function(frameId, databaseName, objectStoreName, keyPath, autoIncrement, callback)
77 {
78     InspectorTest.evaluateWithCallback(frameId, "createObjectStore", [databaseName, objectStoreName, keyPath, autoIncrement], callback)
79 };
80
81 InspectorTest.deleteObjectStore = function(frameId, databaseName, objectStoreName, callback)
82 {
83     InspectorTest.evaluateWithCallback(frameId, "deleteObjectStore", [databaseName, objectStoreName], callback)
84 };
85
86 InspectorTest.createObjectStoreIndex = function(frameId, databaseName, objectStoreName, objectStoreIndexName, keyPath, unique, multiEntry, callback)
87 {
88     InspectorTest.evaluateWithCallback(frameId, "createObjectStoreIndex", [databaseName, objectStoreName, objectStoreIndexName, keyPath, unique, multiEntry], callback)
89 };
90
91 InspectorTest.deleteObjectStoreIndex = function(frameId, databaseName, objectStoreName, objectStoreIndexName, callback)
92 {
93     InspectorTest.evaluateWithCallback(frameId, "deleteObjectStoreIndex", [databaseName, objectStoreName, objectStoreIndexName], callback)
94 };
95
96 InspectorTest.addIDBValue = function(frameId, databaseName, objectStoreName, value, key, callback)
97 {
98     InspectorTest.evaluateWithCallback(frameId, "addIDBValue", [databaseName, objectStoreName, value, key], callback)
99 };
100
101 };
102
103 function dispatchCallback(callbackId)
104 {
105     console.log(callbackId);
106 }
107
108 function onIndexedDBError(e)
109 {
110     console.error("IndexedDB error: " + e);
111 }
112
113 function onIndexedDBBlocked(e)
114 {
115     console.error("IndexedDB blocked: " + e);
116 }
117
118 function doWithDatabase(databaseName, callback)
119 {
120     function innerCallback()
121     {
122         var db = request.result;
123         callback(db);
124     }
125
126     var request = indexedDB.open(databaseName);
127     request.onblocked = onIndexedDBBlocked;
128     request.onerror = onIndexedDBError;
129     request.onsuccess = innerCallback;
130 }
131
132 function doWithVersionTransaction(databaseName, callback, commitCallback)
133 {
134     doWithDatabase(databaseName, step2);
135
136     function step2(db)
137     {
138         var version = db.version;
139         db.close();
140         request = indexedDB.open(databaseName, version + 1);
141         request.onerror = onIndexedDBError;
142         request.onupgradeneeded = onUpgradeNeeded;
143         request.onsuccess = onOpened;
144
145         function onUpgradeNeeded(e)
146         {
147             var db = e.target.result;
148             var trans = e.target.transaction;
149             callback(db, trans);
150         }
151
152         function onOpened(e)
153         {
154             var db = e.target.result;
155             db.close();
156             commitCallback();
157         }
158     }
159 }
160
161 function doWithReadWriteTransaction(databaseName, objectStoreName, callback, commitCallback)
162 {
163     doWithDatabase(databaseName, step2);
164
165     function step2(db)
166     {
167         var transaction = db.transaction([objectStoreName], 'readwrite');
168         var objectStore = transaction.objectStore(objectStoreName);
169         callback(objectStore, innerCommitCallback);
170
171         function innerCommitCallback()
172         {
173             db.close();
174             commitCallback();
175         }
176     }
177 }
178
179 function createDatabase(callback, databaseName)
180 {
181     var request = indexedDB.open(databaseName);
182     request.onerror = onIndexedDBError;
183     request.onsuccess = closeDatabase;
184
185     function closeDatabase()
186     {
187         request.result.close();
188         callback();
189     }
190 }
191
192 function deleteDatabase(callback, databaseName)
193 {
194     var request = indexedDB.deleteDatabase(databaseName);
195     request.onerror = onIndexedDBError;
196     request.onsuccess = callback;
197 }
198
199 function createObjectStore(callback, databaseName, objectStoreName, keyPath, autoIncrement)
200 {
201     doWithVersionTransaction(databaseName, withTransactionCallback, callback);
202
203     function withTransactionCallback(db, transaction)
204     {
205         var store = db.createObjectStore(objectStoreName, { keyPath: keyPath, autoIncrement: autoIncrement });
206     }
207 }
208
209 function deleteObjectStore(callback, databaseName, objectStoreName)
210 {
211     doWithVersionTransaction(databaseName, withTransactionCallback, callback);
212
213     function withTransactionCallback(db, transaction)
214     {
215         var store = db.deleteObjectStore(objectStoreName);
216     }
217 }
218
219 function createObjectStoreIndex(callback, databaseName, objectStoreName, objectStoreIndexName, keyPath, unique, multiEntry)
220 {
221     doWithVersionTransaction(databaseName, withTransactionCallback, callback);
222
223     function withTransactionCallback(db, transaction)
224     {
225         var objectStore = transaction.objectStore(objectStoreName);
226         objectStore.createIndex(objectStoreIndexName, keyPath, { unique: unique, multiEntry: multiEntry });
227     }
228 }
229
230 function deleteObjectStoreIndex(callback, databaseName, objectStoreName, objectStoreIndexName)
231 {
232     doWithVersionTransaction(databaseName, withTransactionCallback, callback);
233
234     function withTransactionCallback(db, transaction)
235     {
236         var objectStore = transaction.objectStore(objectStoreName);
237         objectStore.deleteIndex(objectStoreIndexName);
238     }
239 }
240
241 function addIDBValue(callback, databaseName, objectStoreName, value, key)
242 {
243     doWithReadWriteTransaction(databaseName, objectStoreName, withTransactionCallback, callback)
244
245     function withTransactionCallback(objectStore, commitCallback)
246     {
247         var request;
248         if (key)
249             request = objectStore.add(value, key);
250         else
251             request = objectStore.add(value);
252         request.onerror = onIndexedDBError;
253         request.onsuccess = commitCallback;
254     }
255 }