1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 function debug(message)
7 var span = document.createElement("span");
8 span.appendChild(document.createTextNode(message));
9 span.appendChild(document.createElement("br"));
10 document.getElementById('status').appendChild(span);
13 function done(message)
15 if (document.location.hash == '#fail')
18 debug('PASS: ' + message);
21 document.location.hash = '#pass';
24 function fail(message)
26 debug('FAILED: ' + message);
27 document.location.hash = '#fail';
32 return "" + document.getElementById('status').innerHTML;
35 function unexpectedUpgradeNeededCallback()
37 fail('unexpectedUpgradeNeededCallback');
40 function unexpectedAbortCallback()
42 fail('unexpectedAbortCallback');
45 function unexpectedSuccessCallback()
47 fail('unexpectedSuccessCallback');
50 function unexpectedCompleteCallback()
52 fail('unexpectedCompleteCallback');
55 function unexpectedErrorCallback()
57 fail('unexpectedErrorCallback');
60 function unexpectedBlockedCallback()
62 fail('unexpectedBlockedCallback');
65 function unexpectedUpgradeNeededCallback()
67 fail('unexpectedUpgradeNeededCallback');
70 function deleteAllObjectStores(db)
72 objectStoreNames = db.objectStoreNames;
73 for (var i = 0; i < objectStoreNames.length; ++i)
74 db.deleteObjectStore(objectStoreNames[i]);
77 // The following functions are based on
78 // WebKit/LayoutTests/fast/js/resources/js-test-pre.js
79 // so that the tests will look similar to the existing layout tests.
82 if (v === 0 && 1/v < 0)
87 function isResultCorrect(_actual, _expected)
90 return _actual === _expected && (1/_actual) === (1/_expected);
91 if (_actual === _expected)
93 if (typeof(_expected) == "number" && isNaN(_expected))
94 return typeof(_actual) == "number" && isNaN(_actual);
95 if (Object.prototype.toString.call(_expected) ==
96 Object.prototype.toString.call([]))
97 return areArraysEqual(_actual, _expected);
101 function shouldBe(_a, _b)
103 if (typeof _a != "string" || typeof _b != "string")
104 debug("WARN: shouldBe() expects string arguments");
115 fail(_a + " should be " + _bv + ". Threw exception " + exception);
116 else if (isResultCorrect(_av, _bv))
117 debug(_a + " is " + _b);
118 else if (typeof(_av) == typeof(_bv))
119 fail(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
121 fail(_a + " should be " + _bv + " (of type " + typeof _bv + "). " +
122 "Was " + _av + " (of type " + typeof _av + ").");
125 function shouldBeTrue(_a) { shouldBe(_a, "true"); }
126 function shouldBeFalse(_a) { shouldBe(_a, "false"); }
127 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); }
128 function shouldBeNull(_a) { shouldBe(_a, "null"); }
129 function shouldBeEqualToString(a, b)
131 var unevaledString = '"' + b.replace(/\\/g, "\\\\").replace(/"/g, "\"") + '"';
132 shouldBe(a, unevaledString);
135 function indexedDBTest(upgradeCallback, optionalOpenCallback) {
136 dbname = self.location.pathname.substring(
137 1 + self.location.pathname.lastIndexOf("/"));
138 var deleteRequest = indexedDB.deleteDatabase(dbname);
139 deleteRequest.onerror = unexpectedErrorCallback;
140 deleteRequest.onblocked = unexpectedBlockedCallback;
141 deleteRequest.onsuccess = function() {
142 var openRequest = indexedDB.open(dbname);
143 openRequest.onerror = unexpectedErrorCallback;
144 openRequest.onupgradeneeded = upgradeCallback;
145 openRequest.onblocked = unexpectedBlockedCallback;
146 if (optionalOpenCallback)
147 openRequest.onsuccess = optionalOpenCallback;
151 if (typeof String.prototype.startsWith !== 'function') {
152 String.prototype.startsWith = function (str) {
153 return this.indexOf(str) === 0;