content/base/test/chrome/test_domparsing.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/chrome/test_domparsing.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,144 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
     1.7 +<window title="Test for the Mozilla extesion of the DOM Parsing and Serialization API"
     1.8 +  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
     1.9 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.10 +
    1.11 +  <!-- test results are displayed in the html:body -->
    1.12 +  <body xmlns="http://www.w3.org/1999/xhtml">
    1.13 +  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=816410"
    1.14 +     target="_blank">Mozilla Bug 816410</a>
    1.15 +  </body>
    1.16 +
    1.17 +  <!-- test code goes here -->
    1.18 +  <script type="application/javascript;version=1.7"><![CDATA[
    1.19 +"use strict";
    1.20 +/** Test for Bug 816410 **/
    1.21 +
    1.22 +const Cc = Components.classes;
    1.23 +const Ci = Components.interfaces;
    1.24 +
    1.25 +function throws(a, type, message) {
    1.26 +  for (let fn of Array.isArray(a) ? a : [a]) {
    1.27 +    try {
    1.28 +      fn();
    1.29 +      ok(false, message);
    1.30 +    } catch (e) {
    1.31 +      if (type) {
    1.32 +        is(e.name, type, message);
    1.33 +      } else {
    1.34 +        ok(true, message);
    1.35 +      }
    1.36 +    }
    1.37 +  }
    1.38 +}
    1.39 +
    1.40 +// DOMParser constructor should not throw for null arguments
    1.41 +new DOMParser(undefined);
    1.42 +new DOMParser(null);
    1.43 +
    1.44 +throws([
    1.45 +  function() { new DOMParser(false); },
    1.46 +  function() { new DOMParser(0); },
    1.47 +  function() { new DOMParser(""); },
    1.48 +  function() { new DOMParser({}); },
    1.49 +], "TypeError", "DOMParser constructor should throw for extra arguments");
    1.50 +
    1.51 +{
    1.52 +  let parser = new DOMParser();
    1.53 +  throws(function() {
    1.54 +    parser.init();
    1.55 +  }, "NS_ERROR_UNEXPECTED", "init method should throw when DOMParser is created by constructor");
    1.56 +}
    1.57 +
    1.58 +// XMLSerializer constructor should not throw for extra arguments
    1.59 +new XMLSerializer(undefined);
    1.60 +new XMLSerializer(null);
    1.61 +new XMLSerializer(false);
    1.62 +new XMLSerializer(0);
    1.63 +new XMLSerializer("");
    1.64 +new XMLSerializer({});
    1.65 +
    1.66 +runTest(new DOMParser(), new XMLSerializer());
    1.67 +
    1.68 +{
    1.69 +  let parser = Cc["@mozilla.org/xmlextras/domparser;1"]
    1.70 +               .createInstance(Ci.nsIDOMParser);
    1.71 +  parser.init();
    1.72 +  throws(function() {
    1.73 +    parser.init();
    1.74 +  }, "NS_ERROR_UNEXPECTED", "init method should throw when called twice");
    1.75 +}
    1.76 +
    1.77 +runTest(Cc["@mozilla.org/xmlextras/domparser;1"]
    1.78 +        .createInstance(Ci.nsIDOMParser),
    1.79 +        Cc["@mozilla.org/xmlextras/xmlserializer;1"]
    1.80 +        .createInstance(Ci.nsIDOMSerializer));
    1.81 +
    1.82 +function runTest(parser, serializer) {
    1.83 +  is(typeof parser.parseFromString, "function", "parseFromString should exist");
    1.84 +  is(typeof parser.parseFromBuffer, "function", "parseFromBuffer should exist");
    1.85 +  is(typeof parser.parseFromStream, "function", "parseFromStream should exist");
    1.86 +  is(typeof parser.init, "function", "init should exist");
    1.87 +
    1.88 +  is(typeof serializer.serializeToString, "function", "serializeToString should exist");
    1.89 +  is(typeof serializer.serializeToStream, "function", "serializeToStream should exist");
    1.90 +
    1.91 +  let tests = [
    1.92 +    {input: "<html></html>", type: "text/html",
    1.93 +     expected: '<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>'},
    1.94 +    {input: "<xml></xml>", type: "text/xml", expected: "<xml/>"},
    1.95 +    {input: "<xml></xml>", type: "application/xml", expected: "<xml/>"},
    1.96 +    {input: "<html></html>", type: "application/xhtml+xml", expected: "<html/>"},
    1.97 +    {input: "<svg></svg>", type: "image/svg+xml", expected: "<svg/>"},
    1.98 +  ];
    1.99 +  for (let t of tests) {
   1.100 +    is(serializer.serializeToString(parser.parseFromString(t.input, t.type)), t.expected,
   1.101 +       "parseFromString test for " + t.type);
   1.102 +
   1.103 +    let ostream = {
   1.104 +      write: function(buf, count) { this.data += buf; return count; }
   1.105 +    };
   1.106 +    for (let charset of [null, "UTF-8"]) {
   1.107 +      ostream.data = "";
   1.108 +      serializer.serializeToStream(parser.parseFromString(t.input, t.type), ostream, charset);
   1.109 +      is(ostream.data, t.expected,
   1.110 +         "serializeToStream test for " + t.type + ", charset=" + charset);
   1.111 +    }
   1.112 +
   1.113 +    if (t.type === "text/html") {
   1.114 +      // parseFromBuffer and parseFromStream don't support "text/html".
   1.115 +      continue;
   1.116 +    }
   1.117 +
   1.118 +    let array = Array.map(t.input, function(c) { return c.charCodeAt(c); });
   1.119 +    let inputs = [
   1.120 +      {array: array, name: "parseFromBuffer (array)"},
   1.121 +      {array: new Uint8Array(array), name: "parseFromBuffer (Uint8Array)"},
   1.122 +    ];
   1.123 +    for (let input of inputs) {
   1.124 +      let a = input.array;
   1.125 +      is(serializer.serializeToString(parser.parseFromBuffer(a, a.length, t.type)), t.expected,
   1.126 +         input.name + " test for " + t.type);
   1.127 +      throws(function() {
   1.128 +          parser.parseFromBuffer(a, a.length + 1, t.type);
   1.129 +        }, "NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY",
   1.130 +        input.name + " should throw if bufLen parameter is greater than actual length"
   1.131 +      );
   1.132 +    }
   1.133 +
   1.134 +    let istream = Cc["@mozilla.org/io/string-input-stream;1"].
   1.135 +                  createInstance(Ci.nsIStringInputStream);
   1.136 +    for (let charset of [null, "UTF-8"]) {
   1.137 +      istream.setData(t.input, -1);
   1.138 +      is(serializer.serializeToString(parser.parseFromStream(istream, charset, t.input.length, t.type)),
   1.139 +         t.expected, "parseFromStream test for " + t.type + ", charset=" + charset);
   1.140 +    }
   1.141 +  }
   1.142 +  throws(function() {
   1.143 +    parser.parseFromString("<xml></xml>", "foo/bar");
   1.144 +  }, "TypeError", "parseFromString should throw for the unknown type");
   1.145 +}
   1.146 +  ]]></script>
   1.147 +</window>

mercurial