content/test/unit/head_content.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/test/unit/head_content.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,156 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +const I                    = Components.interfaces;
    1.11 +const C                    = Components.classes;
    1.12 +
    1.13 +const nsILocalFile         = I.nsILocalFile;
    1.14 +const nsIProperties        = I.nsIProperties;
    1.15 +const nsIFileInputStream   = I.nsIFileInputStream;
    1.16 +const nsIInputStream       = I.nsIInputStream;
    1.17 +
    1.18 +const nsIDOMParser         = I.nsIDOMParser;
    1.19 +const nsIDOMSerializer     = I.nsIDOMSerializer;
    1.20 +const nsIDOMDocument       = I.nsIDOMDocument;
    1.21 +const nsIDOMElement        = I.nsIDOMElement;
    1.22 +const nsIDOMNode           = I.nsIDOMNode;
    1.23 +const nsIDOMCharacterData  = I.nsIDOMCharacterData;
    1.24 +const nsIDOMAttr           = I.nsIDOMAttr;
    1.25 +const nsIDOMNodeList       = I.nsIDOMNodeList;
    1.26 +const nsIDOMXULElement     = I.nsIDOMXULElement;
    1.27 +const nsIDOMProcessingInstruction = I.nsIDOMProcessingInstruction;
    1.28 +
    1.29 +function DOMParser() {
    1.30 +  var parser = C["@mozilla.org/xmlextras/domparser;1"].createInstance(nsIDOMParser);
    1.31 +  parser.init();
    1.32 +  return parser;
    1.33 +}
    1.34 +
    1.35 +var __testsDirectory = null;
    1.36 +
    1.37 +function ParseFile(file) {
    1.38 +  if (typeof(file) == "string") {
    1.39 +    if (!__testsDirectory) {
    1.40 +      __testsDirectory = do_get_cwd();
    1.41 +    }
    1.42 +    var fileObj = __testsDirectory.clone();
    1.43 +    fileObj.append(file);
    1.44 +    file = fileObj;
    1.45 +  }
    1.46 +
    1.47 +  do_check_eq(file instanceof nsILocalFile, true);
    1.48 +
    1.49 +  var fileStr = C["@mozilla.org/network/file-input-stream;1"]
    1.50 +                 .createInstance(nsIFileInputStream);
    1.51 +  // Init for readonly reading
    1.52 +  fileStr.init(file,  0x01, 0400, nsIFileInputStream.CLOSE_ON_EOF);
    1.53 +  return ParseXML(fileStr);
    1.54 +}
    1.55 +
    1.56 +function ParseXML(data) {
    1.57 +  if (typeof(data) == "string") {
    1.58 +    return DOMParser().parseFromString(data, "application/xml");
    1.59 +  }
    1.60 +
    1.61 +  do_check_eq(data instanceof nsIInputStream, true);
    1.62 +  
    1.63 +  return DOMParser().parseFromStream(data, "UTF-8", data.available(),
    1.64 +                                     "application/xml");
    1.65 +}
    1.66 +
    1.67 +function DOMSerializer() {
    1.68 +  return C["@mozilla.org/xmlextras/xmlserializer;1"]
    1.69 +          .createInstance(nsIDOMSerializer);
    1.70 +}
    1.71 +
    1.72 +function SerializeXML(node) {
    1.73 +  return DOMSerializer().serializeToString(node);
    1.74 +}
    1.75 +
    1.76 +function roundtrip(obj) {
    1.77 +  if (typeof(obj) == "string") {
    1.78 +    return SerializeXML(ParseXML(obj));
    1.79 +  }
    1.80 +
    1.81 +  do_check_eq(obj instanceof nsIDOMNode, true);
    1.82 +  return ParseXML(SerializeXML(obj));
    1.83 +}
    1.84 +
    1.85 +function do_compare_attrs(e1, e2) {
    1.86 +  const xmlns = "http://www.w3.org/2000/xmlns/";
    1.87 +
    1.88 +  var a1 = e1.attributes;
    1.89 +  var a2 = e2.attributes;
    1.90 +  for (var i = 0; i < a1.length; ++i) {
    1.91 +    var att = a1.item(i);
    1.92 +    // Don't test for namespace decls, since those can just sorta be
    1.93 +    // scattered about
    1.94 +    if (att.namespaceURI != xmlns) {
    1.95 +      var att2 = a2.getNamedItemNS(att.namespaceURI, att.localName);
    1.96 +      if (!att2) {
    1.97 +        do_throw("Missing attribute with namespaceURI '" + att.namespaceURI +
    1.98 +                 "' and localName '" + att.localName + "'");
    1.99 +      }
   1.100 +      do_check_eq(att.QueryInterface(nsIDOMAttr).value, 
   1.101 +                  att2.QueryInterface(nsIDOMAttr).value);
   1.102 +    }
   1.103 +  }
   1.104 +}
   1.105 +
   1.106 +function do_check_equiv(dom1, dom2) {
   1.107 +  do_check_eq(dom1.nodeType, dom2.nodeType);
   1.108 +  // There's no classinfo around, so we'll need to do some QIing to
   1.109 +  // make sure the right interfaces are flattened as needed.
   1.110 +  switch (dom1.nodeType) {
   1.111 +  case nsIDOMNode.PROCESSING_INSTRUCTION_NODE:
   1.112 +    do_check_eq(dom1.QueryInterface(nsIDOMProcessingInstruction).target, 
   1.113 +                dom2.QueryInterface(nsIDOMProcessingInstruction).target);
   1.114 +    do_check_eq(dom1.data, dom2.data);
   1.115 +  case nsIDOMNode.TEXT_NODE:
   1.116 +  case nsIDOMNode.CDATA_SECTION_NODE:
   1.117 +  case nsIDOMNode.COMMENT_NODE:
   1.118 +    do_check_eq(dom1.QueryInterface(nsIDOMCharacterData).data,
   1.119 +                dom2.QueryInterface(nsIDOMCharacterData).data);
   1.120 +    break;
   1.121 +  case nsIDOMNode.ELEMENT_NODE:
   1.122 +    do_check_eq(dom1.namespaceURI, dom2.namespaceURI);
   1.123 +    do_check_eq(dom1.localName, dom2.localName);
   1.124 +    // Compare attrs in both directions -- do_compare_attrs does a
   1.125 +    // subset check.
   1.126 +    do_compare_attrs(dom1, dom2);
   1.127 +    do_compare_attrs(dom2, dom1);
   1.128 +    // Fall through
   1.129 +  case nsIDOMNode.DOCUMENT_NODE:
   1.130 +    do_check_eq(dom1.childNodes.length, dom2.childNodes.length);
   1.131 +    for (var i = 0; i < dom1.childNodes.length; ++i) {
   1.132 +      do_check_equiv(dom1.childNodes.item(i), dom2.childNodes.item(i));
   1.133 +    }
   1.134 +    break;
   1.135 +  }
   1.136 +}
   1.137 +
   1.138 +function do_check_serialize(dom) {
   1.139 +  do_check_equiv(dom, roundtrip(dom));
   1.140 +}
   1.141 +
   1.142 +function Pipe() {
   1.143 +  var p = C["@mozilla.org/pipe;1"].createInstance(I.nsIPipe);
   1.144 +  p.init(false, false, 0, 0xffffffff, null);
   1.145 +  return p;
   1.146 +}
   1.147 +
   1.148 +function ScriptableInput(arg) {
   1.149 +  if (arg instanceof I.nsIPipe) {
   1.150 +    arg = arg.inputStream;
   1.151 +  }
   1.152 +
   1.153 +  var str = C["@mozilla.org/scriptableinputstream;1"].
   1.154 +    createInstance(I.nsIScriptableInputStream);
   1.155 +
   1.156 +  str.init(arg);
   1.157 +
   1.158 +  return str;
   1.159 +}

mercurial