js/xpconnect/tests/unit/test_tearoffs.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 const Cc = Components.classes;
     6 const Ci = Components.interfaces;
     8 function run_test() {
    10   // Load the component manifest containing our test interface implementations.
    11   Components.manager.autoRegister(do_get_file('../components/js/xpctest.manifest'));
    13   // Shortcut the interfaces we're using.
    14   var ifs = {
    15     a: Ci['nsIXPCTestInterfaceA'],
    16     b: Ci['nsIXPCTestInterfaceB'],
    17     c: Ci['nsIXPCTestInterfaceC']
    18   };
    20   // Shortcut the class we're instantiating. This implements all three interfaces.
    21   var cls = Cc["@mozilla.org/js/xpc/test/js/TestInterfaceAll;1"];
    23   // Run through the logic a few times.
    24   for (i = 0; i < 2; ++i)
    25     play_with_tearoffs(ifs, cls);
    26 }
    28 function play_with_tearoffs(ifs, cls) {
    30   // Allocate a bunch of objects, QI-ed to B.
    31   var instances = [];
    32   for (var i = 0; i < 300; ++i)
    33     instances.push(cls.createInstance(ifs.b));
    35   // Nothing to collect.
    36   gc();
    38   // QI them to A.
    39   instances.forEach(function(v, i, a) { v.QueryInterface(ifs.a); });
    41   // QI them to C.
    42   instances.forEach(function(v, i, a) { v.QueryInterface(ifs.c); });
    44   // Check
    45   do_check_true('name' in instances[10], 'Have the prop from A/B');
    46   do_check_true('someInteger' in instances[10], 'Have the prop from C');
    48   // Grab tearoff reflections for a and b.
    49   var aTearOffs = instances.map(function(v, i, a) { return v.nsIXPCTestInterfaceA; } );
    50   var bTearOffs = instances.map(function(v, i, a) { return v.nsIXPCTestInterfaceB; } );
    52   // Check
    53   do_check_true('name' in aTearOffs[1], 'Have the prop from A');
    54   do_check_true(!('someInteger' in aTearOffs[1]), 'Dont have the prop from C');
    56   // Nothing to collect.
    57   gc();
    59   // Null out every even instance pointer.
    60   for (var i = 0; i < instances.length; ++i)
    61     if (i % 2 == 0)
    62         instances[i] = null;
    64   // Nothing to collect, since we still have the A and B tearoff reflections.
    65   gc();
    67   // Null out A tearoff reflections that are a multiple of 3.
    68   for (var i = 0; i < aTearOffs.length; ++i)
    69     if (i % 3 == 0)
    70         aTearOffs[i] = null;
    72   // Nothing to collect, since we still have the B tearoff reflections.
    73   gc();
    75   // Null out B tearoff reflections that are a multiple of 5.
    76   for (var i = 0; i < bTearOffs.length; ++i)
    77     if (i % 5 == 0)
    78         bTearOffs[i] = null;
    80   // This should collect every 30th object (indices that are multiples of 2, 3, and 5).
    81   gc();
    83   // Kill the b tearoffs entirely.
    84   bTearOffs = 0;
    86   // Collect more.
    87   gc();
    89   // Get C tearoffs.
    90   var cTearOffs = instances.map(function(v, i, a) { return v ? v.nsIXPCTestInterfaceC : null; } );
    92   // Check.
    93   do_check_true(!('name' in cTearOffs[1]), 'Dont have the prop from A');
    94   do_check_true('someInteger' in cTearOffs[1], 'have the prop from C');
    96   // Null out the a tearoffs.
    97   aTearOffs = null;
    99   // Collect all even indices.
   100   gc();
   102   // Collect all indices.
   103   instances = null;
   104   gc();
   106   // Give ourselves a pat on the back. :-)
   107   do_check_true(true, "Got all the way through without crashing!");
   108 }

mercurial