Upstream version 7.36.149.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 var indexedDB = window.indexeddb || window.webkitIndexedDB;
104 window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
105
106 function dispatchCallback(callbackId)
107 {
108     console.log(callbackId);
109 }
110
111 function onIndexedDBError(e)
112 {
113     console.error("IndexedDB error: " + e);
114 }
115
116 function onIndexedDBBlocked(e)
117 {
118     console.error("IndexedDB blocked: " + e);
119 }
120
121 function doWithDatabase(databaseName, callback)
122 {
123     function innerCallback()
124     {
125         var db = request.result;
126         callback(db);
127     }
128
129     var request = indexedDB.open(databaseName);
130     request.onblocked = onIndexedDBBlocked;
131     request.onerror = onIndexedDBError;
132     request.onsuccess = innerCallback;
133 }
134
135 function doWithVersionTransaction(databaseName, callback, commitCallback)
136 {
137     doWithDatabase(databaseName, step2);
138
139     function step2(db)
140     {
141         var version = db.version;
142         db.close();
143         request = indexedDB.open(databaseName, version + 1);
144         request.onerror = onIndexedDBError;
145         request.onupgradeneeded = onUpgradeNeeded;
146         request.onsuccess = onOpened;
147
148         function onUpgradeNeeded(e)
149         {
150             var db = e.target.result;
151             var trans = e.target.transaction;
152             callback(db, trans);
153         }
154
155         function onOpened(e)
156         {
157             var db = e.target.result;
158             db.close();
159             commitCallback();
160         }
161     }
162 }
163
164 function doWithReadWriteTransaction(databaseName, objectStoreName, callback, commitCallback)
165 {
166     doWithDatabase(databaseName, step2);
167
168     function step2(db)
169     {
170         var transaction = db.transaction([objectStoreName], 'readwrite');
171         var objectStore = transaction.objectStore(objectStoreName);
172         callback(objectStore, innerCommitCallback);
173
174         function innerCommitCallback()
175         {
176             db.close();
177             commitCallback();
178         }
179     }
180 }
181
182 function createDatabase(callback, databaseName)
183 {
184     var request = indexedDB.open(databaseName);
185     request.onerror = onIndexedDBError;
186     request.onsuccess = closeDatabase;
187
188     function closeDatabase()
189     {
190         request.result.close();
191         callback();
192     }
193 }
194
195 function deleteDatabase(callback, databaseName)
196 {
197     var request = indexedDB.deleteDatabase(databaseName);
198     request.onerror = onIndexedDBError;
199     request.onsuccess = callback;
200 }
201
202 function createObjectStore(callback, databaseName, objectStoreName, keyPath, autoIncrement)
203 {
204     doWithVersionTransaction(databaseName, withTransactionCallback, callback);
205
206     function withTransactionCallback(db, transaction)
207     {
208         var store = db.createObjectStore(objectStoreName, { keyPath: keyPath, autoIncrement: autoIncrement });
209     }
210 }
211
212 function deleteObjectStore(callback, databaseName, objectStoreName)
213 {
214     doWithVersionTransaction(databaseName, withTransactionCallback, callback);
215
216     function withTransactionCallback(db, transaction)
217     {
218         var store = db.deleteObjectStore(objectStoreName);
219     }
220 }
221
222 function createObjectStoreIndex(callback, databaseName, objectStoreName, objectStoreIndexName, keyPath, unique, multiEntry)
223 {
224     doWithVersionTransaction(databaseName, withTransactionCallback, callback);
225
226     function withTransactionCallback(db, transaction)
227     {
228         var objectStore = transaction.objectStore(objectStoreName);
229         objectStore.createIndex(objectStoreIndexName, keyPath, { unique: unique, multiEntry: multiEntry });
230     }
231 }
232
233 function deleteObjectStoreIndex(callback, databaseName, objectStoreName, objectStoreIndexName)
234 {
235     doWithVersionTransaction(databaseName, withTransactionCallback, callback);
236
237     function withTransactionCallback(db, transaction)
238     {
239         var objectStore = transaction.objectStore(objectStoreName);
240         objectStore.deleteIndex(objectStoreIndexName);
241     }
242 }
243
244 function addIDBValue(callback, databaseName, objectStoreName, value, key)
245 {
246     doWithReadWriteTransaction(databaseName, objectStoreName, withTransactionCallback, callback)
247
248     function withTransactionCallback(objectStore, commitCallback)
249     {
250         var request;
251         if (key)
252             request = objectStore.add(value, key);
253         else
254             request = objectStore.add(value);
255         request.onerror = onIndexedDBError;
256         request.onsuccess = commitCallback;
257     }
258 }
259