addon-sdk/source/test/test-namespace.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 "use strict";
     7 const { ns } = require("sdk/core/namespace");
     8 const { Cc, Ci, Cu } = require("chrome");
     9 const { setTimeout } = require("sdk/timers")
    11 exports["test post GC references"] = function (assert, done) {
    12   var target = {}, local = ns()
    13   local(target).there = true
    15   assert.equal(local(target).there, true, "namespaced preserved");
    17   Cu.schedulePreciseGC(function() {
    18     assert.equal(local(target).there, true, "namespace is preserved post GC");
    19     done();
    20   });
    21 };
    23 exports["test namsepace basics"] = function(assert) {
    24   var privates = ns();
    25   var object = { foo: function foo() { return "hello foo"; } };
    27   assert.notEqual(privates(object), object,
    28                   "namespaced object is not the same");
    29   assert.ok(!('foo' in privates(object)),
    30             "public properties are not in the namespace");
    32   assert.equal(privates(object), privates(object),
    33                "same namespaced object is returned on each call");
    34 };
    36 exports["test namespace overlays"] = function(assert) {
    37   var _ = ns();
    38   var object = { foo: 'foo' };
    40   _(object).foo = 'bar';
    42   assert.equal(_(object).foo, "bar",
    43                "namespaced property `foo` changed value");
    45   assert.equal(object.foo, "foo",
    46                "public property `foo` has original value");
    48   object.foo = "baz";
    49   assert.equal(_(object).foo, "bar",
    50                "property changes do not affect namespaced properties");
    52   object.bar = "foo";
    53   assert.ok(!("bar" in _(object)),
    54               "new public properties are not reflected in namespace");
    55 };
    57 exports["test shared namespaces"] = function(assert) {
    58   var _ = ns();
    60   var f1 = { hello: 1 };
    61   var f2 = { foo: 'foo', hello: 2 };
    62   _(f1).foo = _(f2).foo = 'bar';
    64   assert.equal(_(f1).hello, _(f2).hello, "namespace can be shared");
    65   assert.notEqual(f1.hello, _(f1).hello, "shared namespace can overlay");
    66   assert.notEqual(f2.hello, _(f2).hello, "target is not affected");
    68   _(f1).hello = 3;
    70   assert.notEqual(_(f1).hello, _(f2).hello,
    71                   "namespaced property can be overided");
    72   assert.equal(_(f2).hello, _({}).hello, "namespace does not change");
    73 };
    75 exports["test multi namespace"] = function(assert) {
    76   var n1 = ns();
    77   var n2 = ns();
    78   var object = { baz: 1 };
    79   n1(object).foo = 1;
    80   n2(object).foo = 2;
    81   n1(object).bar = n2(object).bar = 3;
    83   assert.notEqual(n1(object).foo, n2(object).foo,
    84                   "object can have multiple namespaces");
    85   assert.equal(n1(object).bar, n2(object).bar,
    86                "object can have matching props in diff namespaces");
    87 };
    89 exports["test ns alias"] = function(assert) {
    90   assert.strictEqual(ns, require('sdk/core/namespace').Namespace,
    91                       "ns is an alias of Namespace");
    92 };
    94 exports["test ns inheritance"] = function(assert) {
    95   let _ = ns();
    97   let prototype = { level: 1 };
    98   let object = Object.create(prototype);
    99   let delegee = Object.create(object);
   101   _(prototype).foo = {};
   103   assert.ok(!Object.prototype.hasOwnProperty.call(_(delegee), "foo"),
   104             "namespaced property is not copied to descendants");
   105   assert.equal(_(delegee).foo, _(prototype).foo,
   106                "namespaced properties are inherited by descendants");
   108   _(object).foo = {};
   109   assert.notEqual(_(object).foo, _(prototype).foo,
   110                   "namespaced properties may be shadowed");
   111   assert.equal(_(object).foo, _(delegee).foo,
   112                "shadwed properties are inherited by descendants");
   114   _(object).bar = {};
   115   assert.ok(!("bar" in _(prototype)),
   116             "descendants properties are not copied to ancestors");
   117   assert.ok(_(object).bar, _(delegee).bar,
   118             "descendants properties are inherited");
   119 };
   121 require("test").run(exports);

mercurial