Home | History | Annotate | Download | only in resources
      1 if (window.layoutTestController)
      2     layoutTestController.dumpAsText();
      3 
      4 function description(msg)
      5 {
      6     // For MSIE 6 compatibility
      7     var span = document.createElement("span");
      8     span.innerHTML = '<p>' + msg + '</p><p>On success, you will see a series of "<span class="pass">PASS</span>" messages, followed by "<span class="pass">TEST COMPLETE</span>".</p>';
      9     var description = document.getElementById("description");
     10     if (description.firstChild)
     11         description.replaceChild(span, description.firstChild);
     12     else
     13         description.appendChild(span);
     14 }
     15 
     16 function debug(msg)
     17 {
     18     var span = document.createElement("span");
     19     document.getElementById("console").appendChild(span); // insert it first so XHTML knows the namespace
     20     span.innerHTML = msg + '<br />';
     21 }
     22 
     23 function escapeHTML(text)
     24 {
     25     return text.replace(/&/g, "&amp;").replace(/</g, "&lt;");
     26 }
     27 
     28 function testPassed(msg)
     29 {
     30     debug('<span><span class="pass">PASS</span> ' + escapeHTML(msg) + '</span>');
     31 }
     32 
     33 function testFailed(msg)
     34 {
     35     debug('<span><span class="fail">FAIL</span> ' + escapeHTML(msg) + '</span>');
     36 }
     37 
     38 function areArraysEqual(_a, _b)
     39 {
     40     if (_a.length !== _b.length)
     41         return false;
     42     for (var i = 0; i < _a.length; i++)
     43         if (_a[i] !== _b[i])
     44             return false;
     45     return true;
     46 }
     47 
     48 function isMinusZero(n)
     49 {
     50     // the only way to tell 0 from -0 in JS is the fact that 1/-0 is
     51     // -Infinity instead of Infinity
     52     return n === 0 && 1/n < 0;
     53 }
     54 
     55 function isResultCorrect(_actual, _expected)
     56 {
     57     if (_expected === 0)
     58         return _actual === _expected && (1/_actual) === (1/_expected);
     59     if (_actual === _expected)
     60         return true;
     61     if (typeof(_expected) == "number" && isNaN(_expected))
     62         return typeof(_actual) == "number" && isNaN(_actual);
     63     if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([]))
     64         return areArraysEqual(_actual, _expected);
     65     return false;
     66 }
     67 
     68 function stringify(v)
     69 {
     70     if (v === 0 && 1/v < 0)
     71         return "-0";
     72     else return "" + v;
     73 }
     74 
     75 function evalAndLog(_a)
     76 {
     77   if (typeof _a != "string")
     78     debug("WARN: tryAndLog() expects a string argument");
     79 
     80   // Log first in case things go horribly wrong or this causes a sync event.
     81   debug(_a);
     82 
     83   var _av;
     84   try {
     85      _av = eval(_a);
     86   } catch (e) {
     87     testFailed(_a + " threw exception " + e);
     88   }
     89 }
     90 
     91 function shouldBe(_a, _b)
     92 {
     93   if (typeof _a != "string" || typeof _b != "string")
     94     debug("WARN: shouldBe() expects string arguments");
     95   var exception;
     96   var _av;
     97   try {
     98      _av = eval(_a);
     99   } catch (e) {
    100      exception = e;
    101   }
    102   var _bv = eval(_b);
    103 
    104   if (exception)
    105     testFailed(_a + " should be " + _bv + ". Threw exception " + exception);
    106   else if (isResultCorrect(_av, _bv))
    107     testPassed(_a + " is " + _b);
    108   else if (typeof(_av) == typeof(_bv))
    109     testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
    110   else
    111     testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ").");
    112 }
    113 
    114 function shouldBeTrue(_a) { shouldBe(_a, "true"); }
    115 function shouldBeFalse(_a) { shouldBe(_a, "false"); }
    116 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); }
    117 function shouldBeNull(_a) { shouldBe(_a, "null"); }
    118 
    119 function shouldBeEqualToString(a, b)
    120 {
    121   var unevaledString = '"' + b.replace(/"/g, "\"") + '"';
    122   shouldBe(a, unevaledString);
    123 }
    124 
    125 function shouldEvaluateTo(actual, expected) {
    126   // A general-purpose comparator.  'actual' should be a string to be
    127   // evaluated, as for shouldBe(). 'expected' may be any type and will be
    128   // used without being eval'ed.
    129   if (expected == null) {
    130     // Do this before the object test, since null is of type 'object'.
    131     shouldBeNull(actual);
    132   } else if (typeof expected == "undefined") {
    133     shouldBeUndefined(actual);
    134   } else if (typeof expected == "function") {
    135     // All this fuss is to avoid the string-arg warning from shouldBe().
    136     try {
    137       actualValue = eval(actual);
    138     } catch (e) {
    139       testFailed("Evaluating " + actual + ": Threw exception " + e);
    140       return;
    141     }
    142     shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'",
    143              "'" + expected.toString().replace(/\n/g, "") + "'");
    144   } else if (typeof expected == "object") {
    145     shouldBeTrue(actual + " == '" + expected + "'");
    146   } else if (typeof expected == "string") {
    147     shouldBe(actual, expected);
    148   } else if (typeof expected == "boolean") {
    149     shouldBe("typeof " + actual, "'boolean'");
    150     if (expected)
    151       shouldBeTrue(actual);
    152     else
    153       shouldBeFalse(actual);
    154   } else if (typeof expected == "number") {
    155     shouldBe(actual, stringify(expected));
    156   } else {
    157     debug(expected + " is unknown type " + typeof expected);
    158     shouldBeTrue(actual, "'"  +expected.toString() + "'");
    159   }
    160 }
    161 
    162 function shouldBeNonZero(_a)
    163 {
    164   var exception;
    165   var _av;
    166   try {
    167      _av = eval(_a);
    168   } catch (e) {
    169      exception = e;
    170   }
    171 
    172   if (exception)
    173     testFailed(_a + " should be non-zero. Threw exception " + exception);
    174   else if (_av != 0)
    175     testPassed(_a + " is non-zero.");
    176   else
    177     testFailed(_a + " should be non-zero. Was " + _av);
    178 }
    179 
    180 function shouldBeNonNull(_a)
    181 {
    182   var exception;
    183   var _av;
    184   try {
    185      _av = eval(_a);
    186   } catch (e) {
    187      exception = e;
    188   }
    189 
    190   if (exception)
    191     testFailed(_a + " should be non-null. Threw exception " + exception);
    192   else if (_av != null)
    193     testPassed(_a + " is non-null.");
    194   else
    195     testFailed(_a + " should be non-null. Was " + _av);
    196 }
    197 
    198 function shouldBeUndefined(_a)
    199 {
    200   var exception;
    201   var _av;
    202   try {
    203      _av = eval(_a);
    204   } catch (e) {
    205      exception = e;
    206   }
    207 
    208   if (exception)
    209     testFailed(_a + " should be undefined. Threw exception " + exception);
    210   else if (typeof _av == "undefined")
    211     testPassed(_a + " is undefined.");
    212   else
    213     testFailed(_a + " should be undefined. Was " + _av);
    214 }
    215 
    216 
    217 function shouldThrow(_a, _e)
    218 {
    219   var exception;
    220   var _av;
    221   try {
    222      _av = eval(_a);
    223   } catch (e) {
    224      exception = e;
    225   }
    226 
    227   var _ev;
    228   if (_e)
    229       _ev =  eval(_e);
    230 
    231   if (exception) {
    232     if (typeof _e == "undefined" || exception == _ev)
    233       testPassed(_a + " threw exception " + exception + ".");
    234     else
    235       testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + ".");
    236   } else if (typeof _av == "undefined")
    237     testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined.");
    238   else
    239     testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + ".");
    240 }
    241 
    242 function gc() {
    243     if (typeof GCController !== "undefined")
    244         GCController.collect();
    245     else {
    246         function gcRec(n) {
    247             if (n < 1)
    248                 return {};
    249             var temp = {i: "ab" + i + (i / 100000)};
    250             temp += "foo";
    251             gcRec(n-1);
    252         }
    253         for (var i = 0; i < 1000; i++)
    254             gcRec(10)
    255     }
    256 }
    257