michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const { ns } = require("sdk/core/namespace"); michael@0: const { Cc, Ci, Cu } = require("chrome"); michael@0: const { setTimeout } = require("sdk/timers") michael@0: michael@0: exports["test post GC references"] = function (assert, done) { michael@0: var target = {}, local = ns() michael@0: local(target).there = true michael@0: michael@0: assert.equal(local(target).there, true, "namespaced preserved"); michael@0: michael@0: Cu.schedulePreciseGC(function() { michael@0: assert.equal(local(target).there, true, "namespace is preserved post GC"); michael@0: done(); michael@0: }); michael@0: }; michael@0: michael@0: exports["test namsepace basics"] = function(assert) { michael@0: var privates = ns(); michael@0: var object = { foo: function foo() { return "hello foo"; } }; michael@0: michael@0: assert.notEqual(privates(object), object, michael@0: "namespaced object is not the same"); michael@0: assert.ok(!('foo' in privates(object)), michael@0: "public properties are not in the namespace"); michael@0: michael@0: assert.equal(privates(object), privates(object), michael@0: "same namespaced object is returned on each call"); michael@0: }; michael@0: michael@0: exports["test namespace overlays"] = function(assert) { michael@0: var _ = ns(); michael@0: var object = { foo: 'foo' }; michael@0: michael@0: _(object).foo = 'bar'; michael@0: michael@0: assert.equal(_(object).foo, "bar", michael@0: "namespaced property `foo` changed value"); michael@0: michael@0: assert.equal(object.foo, "foo", michael@0: "public property `foo` has original value"); michael@0: michael@0: object.foo = "baz"; michael@0: assert.equal(_(object).foo, "bar", michael@0: "property changes do not affect namespaced properties"); michael@0: michael@0: object.bar = "foo"; michael@0: assert.ok(!("bar" in _(object)), michael@0: "new public properties are not reflected in namespace"); michael@0: }; michael@0: michael@0: exports["test shared namespaces"] = function(assert) { michael@0: var _ = ns(); michael@0: michael@0: var f1 = { hello: 1 }; michael@0: var f2 = { foo: 'foo', hello: 2 }; michael@0: _(f1).foo = _(f2).foo = 'bar'; michael@0: michael@0: assert.equal(_(f1).hello, _(f2).hello, "namespace can be shared"); michael@0: assert.notEqual(f1.hello, _(f1).hello, "shared namespace can overlay"); michael@0: assert.notEqual(f2.hello, _(f2).hello, "target is not affected"); michael@0: michael@0: _(f1).hello = 3; michael@0: michael@0: assert.notEqual(_(f1).hello, _(f2).hello, michael@0: "namespaced property can be overided"); michael@0: assert.equal(_(f2).hello, _({}).hello, "namespace does not change"); michael@0: }; michael@0: michael@0: exports["test multi namespace"] = function(assert) { michael@0: var n1 = ns(); michael@0: var n2 = ns(); michael@0: var object = { baz: 1 }; michael@0: n1(object).foo = 1; michael@0: n2(object).foo = 2; michael@0: n1(object).bar = n2(object).bar = 3; michael@0: michael@0: assert.notEqual(n1(object).foo, n2(object).foo, michael@0: "object can have multiple namespaces"); michael@0: assert.equal(n1(object).bar, n2(object).bar, michael@0: "object can have matching props in diff namespaces"); michael@0: }; michael@0: michael@0: exports["test ns alias"] = function(assert) { michael@0: assert.strictEqual(ns, require('sdk/core/namespace').Namespace, michael@0: "ns is an alias of Namespace"); michael@0: }; michael@0: michael@0: exports["test ns inheritance"] = function(assert) { michael@0: let _ = ns(); michael@0: michael@0: let prototype = { level: 1 }; michael@0: let object = Object.create(prototype); michael@0: let delegee = Object.create(object); michael@0: michael@0: _(prototype).foo = {}; michael@0: michael@0: assert.ok(!Object.prototype.hasOwnProperty.call(_(delegee), "foo"), michael@0: "namespaced property is not copied to descendants"); michael@0: assert.equal(_(delegee).foo, _(prototype).foo, michael@0: "namespaced properties are inherited by descendants"); michael@0: michael@0: _(object).foo = {}; michael@0: assert.notEqual(_(object).foo, _(prototype).foo, michael@0: "namespaced properties may be shadowed"); michael@0: assert.equal(_(object).foo, _(delegee).foo, michael@0: "shadwed properties are inherited by descendants"); michael@0: michael@0: _(object).bar = {}; michael@0: assert.ok(!("bar" in _(prototype)), michael@0: "descendants properties are not copied to ancestors"); michael@0: assert.ok(_(object).bar, _(delegee).bar, michael@0: "descendants properties are inherited"); michael@0: }; michael@0: michael@0: require("test").run(exports);