content/test/unit/test_normalize.js

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 function run_test()
     7 {
     8   /*
     9    * NOTE: [i] is not allowed in this test, since it's done via classinfo and
    10    * we don't have that in xpcshell; the workaround is item(i).  Suck.
    11    */
    12   init();
    14   test_element();
    16   // more tests would be nice here (such as for documents), but the primary
    17   // uses of Node.normalize() are in test_element; stuff beyond this is either
    18   // unimplemented or is unlikely to be used all that much within a browser
    19   // DOM implementation
    20 }
    22 // TEST CODE
    24 var doc; // cache for use in all tests
    26 function init()
    27 {
    28   doc = ParseFile("empty_document.xml");
    29 }
    31 function test_element()
    32 {
    33   var x = doc.createElement("funk");
    35   // one empty Text node
    36   x.appendChild(doc.createTextNode(""));
    37   do_check_eq(x.childNodes.length, 1);
    39   x.normalize();
    40   do_check_eq(x.childNodes.length, 0);
    43   // multiple empty Text nodes
    44   x.appendChild(doc.createTextNode(""));
    45   x.appendChild(doc.createTextNode(""));
    46   do_check_eq(x.childNodes.length, 2);
    48   x.normalize();
    49   do_check_eq(x.childNodes.length, 0);
    52   // empty Text node followed by other Text node
    53   x.appendChild(doc.createTextNode(""));
    54   x.appendChild(doc.createTextNode("Guaraldi"));
    55   do_check_eq(x.childNodes.length, 2);
    57   x.normalize();
    58   do_check_eq(x.childNodes.length, 1);
    59   do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi");
    62   // Text node followed by empty Text node
    63   clearKids(x);
    64   x.appendChild(doc.createTextNode("Guaraldi"));
    65   x.appendChild(doc.createTextNode(""));
    66   do_check_eq(x.childNodes.length, 2);
    68   x.normalize();
    69   do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi");
    72   // Text node followed by empty Text node followed by other Node
    73   clearKids(x);
    74   x.appendChild(doc.createTextNode("Guaraldi"));
    75   x.appendChild(doc.createTextNode(""));
    76   x.appendChild(doc.createElement("jazzy"));
    77   do_check_eq(x.childNodes.length, 3);
    79   x.normalize();
    80   do_check_eq(x.childNodes.length, 2);
    81   do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi");
    82   do_check_eq(x.childNodes.item(1).nodeName, "jazzy");
    85   // Nodes are recursively normalized
    86   clearKids(x);
    87   var kid = doc.createElement("eit");
    88   kid.appendChild(doc.createTextNode(""));
    90   x.appendChild(doc.createTextNode("Guaraldi"));
    91   x.appendChild(doc.createTextNode(""));
    92   x.appendChild(kid);
    93   do_check_eq(x.childNodes.length, 3);
    94   do_check_eq(x.childNodes.item(2).childNodes.length, 1);
    96   x.normalize();
    97   do_check_eq(x.childNodes.length, 2);
    98   do_check_eq(x.childNodes.item(0).nodeValue, "Guaraldi");
    99   do_check_eq(x.childNodes.item(1).childNodes.length, 0);
   100 }
   103 // UTILITY FUNCTIONS
   105 function clearKids(node)
   106 {
   107   while (node.hasChildNodes())
   108     node.removeChild(node.childNodes.item(0));
   109 }

mercurial