Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / storage / indexeddb / resources / shared.js
blob94010bb6b5005e845042f3ec8171320ac6e9953b
1 var jsTestIsAsync = true;
2 if (self.importScripts && !self.postMessage) {
3     // Shared worker.  Make postMessage send to the newest client, which in
4     // our tests is the only client.
6     // Store messages for sending until we have somewhere to send them.
7     self.postMessage = function(message)
8     {
9         if (typeof self.pendingMessages === "undefined")
10             self.pendingMessages = [];
11         self.pendingMessages.push(message);
12     };
13     self.onconnect = function(event)
14     {
15         self.postMessage = function(message)
16         {
17             event.ports[0].postMessage(message);
18         };
19         // Offload any stored messages now that someone has connected to us.
20         if (typeof self.pendingMessages === "undefined")
21             return;
22         while (self.pendingMessages.length)
23             event.ports[0].postMessage(self.pendingMessages.shift());
24     };
27 function unexpectedSuccessCallback()
29     testFailed("Success function called unexpectedly.");
30     finishJSTest();
33 function unexpectedErrorCallback(event)
35     testFailed("Error function called unexpectedly: (" + event.target.error.name + ") " + event.target.error.message);
36     finishJSTest();
39 function unexpectedAbortCallback(e)
41     testFailed("Abort function called unexpectedly! Message: [" + e.target.error.message + "]");
42     finishJSTest();
45 function unexpectedCompleteCallback()
47     testFailed("oncomplete function called unexpectedly!");
48     finishJSTest();
51 function unexpectedBlockedCallback(e)
53     testFailed("onblocked called unexpectedly. oldVersion = " + e.oldVersion + ", newVersion = " + e.newVersion);
54     finishJSTest();
57 function unexpectedUpgradeNeededCallback()
59     testFailed("onupgradeneeded called unexpectedly");
60     finishJSTest();
63 function unexpectedVersionChangeCallback(e)
65     testFailed("onversionchange called unexpectedly. oldVersion = " + e.oldVersion + ". newVersion = " + e.newVersion);
66     finishJSTest();
69 function evalAndExpectException(cmd, exceptionCode, exceptionName, _quiet)
71     if (!_quiet)
72         debug("Expecting exception from " + cmd);
73     try {
74         eval(cmd);
75         testFailed("No exception thrown! Should have been " + exceptionCode);
76     } catch (e) {
77         code = e.code;
78         if (!_quiet)
79             testPassed("Exception was thrown.");
80         shouldBe("code", exceptionCode, _quiet);
81         if (exceptionName) {
82             ename = e.name;
83             shouldBe("ename", exceptionName, _quiet);
84         }
85         if (!_quiet)
86             debug("Exception message: " + e.message);
87     }
90 function evalAndExpectExceptionClass(cmd, expected)
92     debug("Expecting " + expected + " exception from " + cmd);
93     try {
94         eval(cmd);
95         testFailed("No exception thrown!" );
96     } catch (e) {
97         testPassed("Exception was thrown.");
98         if (eval("e instanceof " + expected))
99             testPassed(cmd + " threw " + e);
100         else
101             testFailed("Expected " + expected + " but saw " + e);
102     }
105 function evalAndLogCallback(cmd) {
106   function callback() {
107     evalAndLog(cmd);
108   }
109   return callback;
112 // If this function is deleted, a standalone layout test exercising its
113 // functionality should be added.
114 function deleteAllObjectStores(db)
116     while (db.objectStoreNames.length)
117         db.deleteObjectStore(db.objectStoreNames.item(0));
118     debug("Deleted all object stores.");
121 function setDBNameFromPath(suffix) {
122     var name = self.location.pathname.substring(1 + self.location.pathname.lastIndexOf("/"));
123     if (suffix)
124         name += suffix;
125     evalAndLog('dbname = "' + name + '"');
128 function preamble(evt)
130     if (evt)
131         event = evt;
132     debug("");
133     debug(preamble.caller.name + "():");
136 // For Workers
137 if (!self.DOMException) {
138     self.DOMException = {
139         INDEX_SIZE_ERR: 1,
140         DOMSTRING_SIZE_ERR: 2,
141         HIERARCHY_REQUEST_ERR: 3,
142         WRONG_DOCUMENT_ERR: 4,
143         INVALID_CHARACTER_ERR: 5,
144         NO_DATA_ALLOWED_ERR: 6,
145         NO_MODIFICATION_ALLOWED_ERR: 7,
146         NOT_FOUND_ERR: 8,
147         NOT_SUPPORTED_ERR: 9,
148         INUSE_ATTRIBUTE_ERR: 10,
149         INVALID_STATE_ERR: 11,
150         SYNTAX_ERR: 12,
151         INVALID_MODIFICATION_ERR: 13,
152         NAMESPACE_ERR: 14,
153         INVALID_ACCESS_ERR: 15,
154         VALIDATION_ERR: 16,
155         TYPE_MISMATCH_ERR: 17,
156         SECURITY_ERR: 18,
157         NETWORK_ERR: 19,
158         ABORT_ERR: 20,
159         URL_MISMATCH_ERR: 21,
160         QUOTA_EXCEEDED_ERR: 22,
161         TIMEOUT_ERR: 23,
162         INVALID_NODE_TYPE_ERR: 24,
163         DATA_CLONE_ERR: 25
164     };
167 function indexedDBTest(upgradeCallback, optionalOpenCallback, optionalParameters) {
168     if (optionalParameters && 'suffix' in optionalParameters) {
169         setDBNameFromPath(optionalParameters['suffix']);
170     } else {
171         setDBNameFromPath();
172     }
173     var deleteRequest = evalAndLog("indexedDB.deleteDatabase(dbname)");
174     deleteRequest.onerror = unexpectedErrorCallback;
175     deleteRequest.onblocked = unexpectedBlockedCallback;
176     deleteRequest.onsuccess = function() {
177         self.openRequest = null;
178         if (optionalParameters && 'version' in optionalParameters)
179             openRequest = evalAndLog("indexedDB.open(dbname, " + optionalParameters['version'] + ")");
180         else
181             openRequest = evalAndLog("indexedDB.open(dbname)");
182         shouldBe("openRequest.readyState", "'pending'", true/*quiet*/);
183         openRequest.onerror = unexpectedErrorCallback;
184         openRequest.onupgradeneeded = upgradeCallback;
185         openRequest.onblocked = unexpectedBlockedCallback;
186         if (optionalOpenCallback)
187             openRequest.onsuccess = optionalOpenCallback;
188         delete self.openRequest;
189         if (optionalParameters && 'runAfterOpen' in optionalParameters)
190             (optionalParameters['runAfterOpen'])();
191     };
194 function waitForRequests(requests, callback) {
195     var count = requests.length;
197     if (!count) {
198         callback(requests);
199         return;
200     }
202     requests.forEach(function(req) {
203         req.onsuccess = function() {
204             --count;
205             if (!count)
206                 callback(requests);
207         };
208         req.onerror = unexpectedErrorCallback;
209     });