Get foreground tab on Android
[chromium-blink-merge.git] / content / test / data / indexeddb / common.js
blob2e12d4447e2aec11c2584d2edeb00d7114199a01
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')
16 return;
17 if (message)
18 debug('PASS: ' + message);
19 else
20 debug('PASS');
21 document.location.hash = '#pass';
24 function fail(message)
26 debug('FAILED: ' + message);
27 document.location.hash = '#fail';
30 function getLog()
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.
80 function stringify(v)
82 if (v === 0 && 1/v < 0)
83 return "-0";
84 else return "" + v;
87 function isResultCorrect(_actual, _expected)
89 if (_expected === 0)
90 return _actual === _expected && (1/_actual) === (1/_expected);
91 if (_actual === _expected)
92 return true;
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);
98 return false;
101 function shouldBe(_a, _b)
103 if (typeof _a != "string" || typeof _b != "string")
104 debug("WARN: shouldBe() expects string arguments");
105 var exception;
106 var _av;
107 try {
108 _av = eval(_a);
109 } catch (e) {
110 exception = e;
112 var _bv = eval(_b);
114 if (exception)
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) + ".");
120 else
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;