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(e
)
37 fail('unexpectedUpgradeNeededCallback' +
38 ' (oldVersion: ' + e
.oldVersion
+ ' newVersion: ' + e
.newVersion
+ ')');
41 function unexpectedAbortCallback(e
)
43 fail('unexpectedAbortCallback' +
44 ' (' + e
.target
.error
.name
+ ': ' + e
.target
.error
.message
+ ')');
47 function unexpectedSuccessCallback()
49 fail('unexpectedSuccessCallback');
52 function unexpectedCompleteCallback()
54 fail('unexpectedCompleteCallback');
57 function unexpectedErrorCallback(e
)
59 fail('unexpectedErrorCallback' +
60 ' (' + e
.target
.error
.name
+ ': ' + e
.target
.error
.message
+ ')');
63 function unexpectedBlockedCallback(e
)
65 fail('unexpectedBlockedCallback' +
66 ' (oldVersion: ' + e
.oldVersion
+ ' newVersion: ' + e
.newVersion
+ ')');
69 function deleteAllObjectStores(db
)
71 objectStoreNames
= db
.objectStoreNames
;
72 for (var i
= 0; i
< objectStoreNames
.length
; ++i
)
73 db
.deleteObjectStore(objectStoreNames
[i
]);
76 // The following functions are based on
77 // WebKit/LayoutTests/fast/js/resources/js-test-pre.js
78 // so that the tests will look similar to the existing layout tests.
81 if (v
=== 0 && 1/v
< 0)
86 function isResultCorrect(_actual
, _expected
)
89 return _actual
=== _expected
&& (1/_actual) === (1/_expected
);
90 if (_actual
=== _expected
)
92 if (typeof(_expected
) == "number" && isNaN(_expected
))
93 return typeof(_actual
) == "number" && isNaN(_actual
);
94 if (Object
.prototype.toString
.call(_expected
) ==
95 Object
.prototype.toString
.call([]))
96 return areArraysEqual(_actual
, _expected
);
100 function shouldBe(_a
, _b
)
102 if (typeof _a
!= "string" || typeof _b
!= "string")
103 debug("WARN: shouldBe() expects string arguments");
114 fail(_a
+ " should be " + _bv
+ ". Threw exception " + exception
);
115 else if (isResultCorrect(_av
, _bv
))
116 debug(_a
+ " is " + _b
);
117 else if (typeof(_av
) == typeof(_bv
))
118 fail(_a
+ " should be " + _bv
+ ". Was " + stringify(_av
) + ".");
120 fail(_a
+ " should be " + _bv
+ " (of type " + typeof _bv
+ "). " +
121 "Was " + _av
+ " (of type " + typeof _av
+ ").");
124 function shouldBeTrue(_a
) { shouldBe(_a
, "true"); }
125 function shouldBeFalse(_a
) { shouldBe(_a
, "false"); }
126 function shouldBeNaN(_a
) { shouldBe(_a
, "NaN"); }
127 function shouldBeNull(_a
) { shouldBe(_a
, "null"); }
128 function shouldBeEqualToString(a
, b
)
130 var unevaledString
= '"' + b
.replace(/\\/g, "\\\\").replace(/"/g, "\"") + '"';
131 shouldBe(a, unevaledString);
134 function indexedDBTest(upgradeCallback, optionalOpenCallback) {
135 dbname = self.location.pathname.substring(
136 1 + self.location.pathname.lastIndexOf("/"));
137 var deleteRequest = indexedDB.deleteDatabase(dbname);
138 deleteRequest.onerror = unexpectedErrorCallback;
139 deleteRequest.onblocked = unexpectedBlockedCallback;
140 deleteRequest.onsuccess = function() {
141 var openRequest = indexedDB.open(dbname);
142 openRequest.onerror = unexpectedErrorCallback;
143 openRequest.onupgradeneeded = upgradeCallback;
144 openRequest.onblocked = unexpectedBlockedCallback;
145 if (optionalOpenCallback)
146 openRequest.onsuccess = optionalOpenCallback;
150 if (typeof String.prototype.startsWith !== 'function') {
151 String.prototype.startsWith = function (str) {
152 return this.indexOf(str) === 0;