content/base/test/chrome/test_domparsing.xul

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <?xml version="1.0"?>
     2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
     4 <window title="Test for the Mozilla extesion of the DOM Parsing and Serialization API"
     5   xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
     6   <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
     8   <!-- test results are displayed in the html:body -->
     9   <body xmlns="http://www.w3.org/1999/xhtml">
    10   <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=816410"
    11      target="_blank">Mozilla Bug 816410</a>
    12   </body>
    14   <!-- test code goes here -->
    15   <script type="application/javascript;version=1.7"><![CDATA[
    16 "use strict";
    17 /** Test for Bug 816410 **/
    19 const Cc = Components.classes;
    20 const Ci = Components.interfaces;
    22 function throws(a, type, message) {
    23   for (let fn of Array.isArray(a) ? a : [a]) {
    24     try {
    25       fn();
    26       ok(false, message);
    27     } catch (e) {
    28       if (type) {
    29         is(e.name, type, message);
    30       } else {
    31         ok(true, message);
    32       }
    33     }
    34   }
    35 }
    37 // DOMParser constructor should not throw for null arguments
    38 new DOMParser(undefined);
    39 new DOMParser(null);
    41 throws([
    42   function() { new DOMParser(false); },
    43   function() { new DOMParser(0); },
    44   function() { new DOMParser(""); },
    45   function() { new DOMParser({}); },
    46 ], "TypeError", "DOMParser constructor should throw for extra arguments");
    48 {
    49   let parser = new DOMParser();
    50   throws(function() {
    51     parser.init();
    52   }, "NS_ERROR_UNEXPECTED", "init method should throw when DOMParser is created by constructor");
    53 }
    55 // XMLSerializer constructor should not throw for extra arguments
    56 new XMLSerializer(undefined);
    57 new XMLSerializer(null);
    58 new XMLSerializer(false);
    59 new XMLSerializer(0);
    60 new XMLSerializer("");
    61 new XMLSerializer({});
    63 runTest(new DOMParser(), new XMLSerializer());
    65 {
    66   let parser = Cc["@mozilla.org/xmlextras/domparser;1"]
    67                .createInstance(Ci.nsIDOMParser);
    68   parser.init();
    69   throws(function() {
    70     parser.init();
    71   }, "NS_ERROR_UNEXPECTED", "init method should throw when called twice");
    72 }
    74 runTest(Cc["@mozilla.org/xmlextras/domparser;1"]
    75         .createInstance(Ci.nsIDOMParser),
    76         Cc["@mozilla.org/xmlextras/xmlserializer;1"]
    77         .createInstance(Ci.nsIDOMSerializer));
    79 function runTest(parser, serializer) {
    80   is(typeof parser.parseFromString, "function", "parseFromString should exist");
    81   is(typeof parser.parseFromBuffer, "function", "parseFromBuffer should exist");
    82   is(typeof parser.parseFromStream, "function", "parseFromStream should exist");
    83   is(typeof parser.init, "function", "init should exist");
    85   is(typeof serializer.serializeToString, "function", "serializeToString should exist");
    86   is(typeof serializer.serializeToStream, "function", "serializeToStream should exist");
    88   let tests = [
    89     {input: "<html></html>", type: "text/html",
    90      expected: '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>'},
    91     {input: "<xml></xml>", type: "text/xml", expected: "<xml/>"},
    92     {input: "<xml></xml>", type: "application/xml", expected: "<xml/>"},
    93     {input: "<html></html>", type: "application/xhtml+xml", expected: "<html/>"},
    94     {input: "<svg></svg>", type: "image/svg+xml", expected: "<svg/>"},
    95   ];
    96   for (let t of tests) {
    97     is(serializer.serializeToString(parser.parseFromString(t.input, t.type)), t.expected,
    98        "parseFromString test for " + t.type);
   100     let ostream = {
   101       write: function(buf, count) { this.data += buf; return count; }
   102     };
   103     for (let charset of [null, "UTF-8"]) {
   104       ostream.data = "";
   105       serializer.serializeToStream(parser.parseFromString(t.input, t.type), ostream, charset);
   106       is(ostream.data, t.expected,
   107          "serializeToStream test for " + t.type + ", charset=" + charset);
   108     }
   110     if (t.type === "text/html") {
   111       // parseFromBuffer and parseFromStream don't support "text/html".
   112       continue;
   113     }
   115     let array = Array.map(t.input, function(c) { return c.charCodeAt(c); });
   116     let inputs = [
   117       {array: array, name: "parseFromBuffer (array)"},
   118       {array: new Uint8Array(array), name: "parseFromBuffer (Uint8Array)"},
   119     ];
   120     for (let input of inputs) {
   121       let a = input.array;
   122       is(serializer.serializeToString(parser.parseFromBuffer(a, a.length, t.type)), t.expected,
   123          input.name + " test for " + t.type);
   124       throws(function() {
   125           parser.parseFromBuffer(a, a.length + 1, t.type);
   126         }, "NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY",
   127         input.name + " should throw if bufLen parameter is greater than actual length"
   128       );
   129     }
   131     let istream = Cc["@mozilla.org/io/string-input-stream;1"].
   132                   createInstance(Ci.nsIStringInputStream);
   133     for (let charset of [null, "UTF-8"]) {
   134       istream.setData(t.input, -1);
   135       is(serializer.serializeToString(parser.parseFromStream(istream, charset, t.input.length, t.type)),
   136          t.expected, "parseFromStream test for " + t.type + ", charset=" + charset);
   137     }
   138   }
   139   throws(function() {
   140     parser.parseFromString("<xml></xml>", "foo/bar");
   141   }, "TypeError", "parseFromString should throw for the unknown type");
   142 }
   143   ]]></script>
   144 </window>

mercurial