addon-sdk/source/test/test-namespace.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 const { ns } = require("sdk/core/namespace");
michael@0 8 const { Cc, Ci, Cu } = require("chrome");
michael@0 9 const { setTimeout } = require("sdk/timers")
michael@0 10
michael@0 11 exports["test post GC references"] = function (assert, done) {
michael@0 12 var target = {}, local = ns()
michael@0 13 local(target).there = true
michael@0 14
michael@0 15 assert.equal(local(target).there, true, "namespaced preserved");
michael@0 16
michael@0 17 Cu.schedulePreciseGC(function() {
michael@0 18 assert.equal(local(target).there, true, "namespace is preserved post GC");
michael@0 19 done();
michael@0 20 });
michael@0 21 };
michael@0 22
michael@0 23 exports["test namsepace basics"] = function(assert) {
michael@0 24 var privates = ns();
michael@0 25 var object = { foo: function foo() { return "hello foo"; } };
michael@0 26
michael@0 27 assert.notEqual(privates(object), object,
michael@0 28 "namespaced object is not the same");
michael@0 29 assert.ok(!('foo' in privates(object)),
michael@0 30 "public properties are not in the namespace");
michael@0 31
michael@0 32 assert.equal(privates(object), privates(object),
michael@0 33 "same namespaced object is returned on each call");
michael@0 34 };
michael@0 35
michael@0 36 exports["test namespace overlays"] = function(assert) {
michael@0 37 var _ = ns();
michael@0 38 var object = { foo: 'foo' };
michael@0 39
michael@0 40 _(object).foo = 'bar';
michael@0 41
michael@0 42 assert.equal(_(object).foo, "bar",
michael@0 43 "namespaced property `foo` changed value");
michael@0 44
michael@0 45 assert.equal(object.foo, "foo",
michael@0 46 "public property `foo` has original value");
michael@0 47
michael@0 48 object.foo = "baz";
michael@0 49 assert.equal(_(object).foo, "bar",
michael@0 50 "property changes do not affect namespaced properties");
michael@0 51
michael@0 52 object.bar = "foo";
michael@0 53 assert.ok(!("bar" in _(object)),
michael@0 54 "new public properties are not reflected in namespace");
michael@0 55 };
michael@0 56
michael@0 57 exports["test shared namespaces"] = function(assert) {
michael@0 58 var _ = ns();
michael@0 59
michael@0 60 var f1 = { hello: 1 };
michael@0 61 var f2 = { foo: 'foo', hello: 2 };
michael@0 62 _(f1).foo = _(f2).foo = 'bar';
michael@0 63
michael@0 64 assert.equal(_(f1).hello, _(f2).hello, "namespace can be shared");
michael@0 65 assert.notEqual(f1.hello, _(f1).hello, "shared namespace can overlay");
michael@0 66 assert.notEqual(f2.hello, _(f2).hello, "target is not affected");
michael@0 67
michael@0 68 _(f1).hello = 3;
michael@0 69
michael@0 70 assert.notEqual(_(f1).hello, _(f2).hello,
michael@0 71 "namespaced property can be overided");
michael@0 72 assert.equal(_(f2).hello, _({}).hello, "namespace does not change");
michael@0 73 };
michael@0 74
michael@0 75 exports["test multi namespace"] = function(assert) {
michael@0 76 var n1 = ns();
michael@0 77 var n2 = ns();
michael@0 78 var object = { baz: 1 };
michael@0 79 n1(object).foo = 1;
michael@0 80 n2(object).foo = 2;
michael@0 81 n1(object).bar = n2(object).bar = 3;
michael@0 82
michael@0 83 assert.notEqual(n1(object).foo, n2(object).foo,
michael@0 84 "object can have multiple namespaces");
michael@0 85 assert.equal(n1(object).bar, n2(object).bar,
michael@0 86 "object can have matching props in diff namespaces");
michael@0 87 };
michael@0 88
michael@0 89 exports["test ns alias"] = function(assert) {
michael@0 90 assert.strictEqual(ns, require('sdk/core/namespace').Namespace,
michael@0 91 "ns is an alias of Namespace");
michael@0 92 };
michael@0 93
michael@0 94 exports["test ns inheritance"] = function(assert) {
michael@0 95 let _ = ns();
michael@0 96
michael@0 97 let prototype = { level: 1 };
michael@0 98 let object = Object.create(prototype);
michael@0 99 let delegee = Object.create(object);
michael@0 100
michael@0 101 _(prototype).foo = {};
michael@0 102
michael@0 103 assert.ok(!Object.prototype.hasOwnProperty.call(_(delegee), "foo"),
michael@0 104 "namespaced property is not copied to descendants");
michael@0 105 assert.equal(_(delegee).foo, _(prototype).foo,
michael@0 106 "namespaced properties are inherited by descendants");
michael@0 107
michael@0 108 _(object).foo = {};
michael@0 109 assert.notEqual(_(object).foo, _(prototype).foo,
michael@0 110 "namespaced properties may be shadowed");
michael@0 111 assert.equal(_(object).foo, _(delegee).foo,
michael@0 112 "shadwed properties are inherited by descendants");
michael@0 113
michael@0 114 _(object).bar = {};
michael@0 115 assert.ok(!("bar" in _(prototype)),
michael@0 116 "descendants properties are not copied to ancestors");
michael@0 117 assert.ok(_(object).bar, _(delegee).bar,
michael@0 118 "descendants properties are inherited");
michael@0 119 };
michael@0 120
michael@0 121 require("test").run(exports);

mercurial