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. 4 5function debug(message) 6{ 7 var span = document.createElement("span"); 8 span.appendChild(document.createTextNode(message)); 9 span.appendChild(document.createElement("br")); 10 document.getElementById('status').appendChild(span); 11} 12 13function done(message) 14{ 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'; 22} 23 24function fail(message) 25{ 26 debug('FAILED: ' + message); 27 document.location.hash = '#fail'; 28} 29 30function getLog() 31{ 32 return "" + document.getElementById('status').innerHTML; 33} 34 35function unexpectedUpgradeNeededCallback() 36{ 37 fail('unexpectedUpgradeNeededCallback'); 38} 39 40function unexpectedAbortCallback() 41{ 42 fail('unexpectedAbortCallback'); 43} 44 45function unexpectedSuccessCallback() 46{ 47 fail('unexpectedSuccessCallback'); 48} 49 50function unexpectedCompleteCallback() 51{ 52 fail('unexpectedCompleteCallback'); 53} 54 55function unexpectedErrorCallback() 56{ 57 fail('unexpectedErrorCallback'); 58} 59 60function unexpectedBlockedCallback() 61{ 62 fail('unexpectedBlockedCallback'); 63} 64 65function unexpectedUpgradeNeededCallback() 66{ 67 fail('unexpectedUpgradeNeededCallback'); 68} 69 70function deleteAllObjectStores(db) 71{ 72 objectStoreNames = db.objectStoreNames; 73 for (var i = 0; i < objectStoreNames.length; ++i) 74 db.deleteObjectStore(objectStoreNames[i]); 75} 76 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. 80function stringify(v) 81{ 82 if (v === 0 && 1/v < 0) 83 return "-0"; 84 else return "" + v; 85} 86 87function isResultCorrect(_actual, _expected) 88{ 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; 99} 100 101function shouldBe(_a, _b) 102{ 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; 111 } 112 var _bv = eval(_b); 113 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 + ")."); 123} 124 125function shouldBeTrue(_a) { shouldBe(_a, "true"); } 126function shouldBeFalse(_a) { shouldBe(_a, "false"); } 127function shouldBeNaN(_a) { shouldBe(_a, "NaN"); } 128function shouldBeNull(_a) { shouldBe(_a, "null"); } 129function shouldBeEqualToString(a, b) 130{ 131 var unevaledString = '"' + b.replace(/\\/g, "\\\\").replace(/"/g, "\"") + '"'; 132 shouldBe(a, unevaledString); 133} 134 135function 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; 148 }; 149} 150 151if (typeof String.prototype.startsWith !== 'function') { 152 String.prototype.startsWith = function (str) { 153 return this.indexOf(str) === 0; 154 }; 155} 156