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

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 var Cortex = require("sdk/deprecated/cortex").Cortex;
michael@0 8
michael@0 9 exports["test property changes propagate"] = function (assert) {
michael@0 10 var source = {
michael@0 11 _foo: "secret",
michael@0 12 get foo() {
michael@0 13 return this._foo;
michael@0 14 },
michael@0 15 set foo(value) {
michael@0 16 this._foo = value;
michael@0 17 },
michael@0 18 get getOnly() {
michael@0 19 return this._foo;
michael@0 20 },
michael@0 21 set setOnly(value) {
michael@0 22 this._setOnly = value;
michael@0 23 },
michael@0 24 bar: "public",
michael@0 25 method: function method(a, b) {
michael@0 26 return this._foo + a + b
michael@0 27 }
michael@0 28 };
michael@0 29 var fixture = Cortex(source);
michael@0 30
michael@0 31 assert.ok(!('_foo' in fixture),
michael@0 32 "properties that start with `_` are omitted");
michael@0 33 assert.equal(fixture.foo, "secret", "get accessor alias works");
michael@0 34 fixture.foo = "new secret";
michael@0 35 assert.equal(fixture.foo, "new secret", "set accessor alias works");
michael@0 36 assert.equal(source.foo, "new secret", "accessor delegates to the source");
michael@0 37 assert.equal(fixture.bar, "public", "data property alias works");
michael@0 38 fixture.bar = "bar";
michael@0 39 assert.equal(source.bar, "bar", "data property change propagates");
michael@0 40 source.bar = "foo"
michael@0 41 assert.equal(fixture.bar, "foo", "data property change probagets back");
michael@0 42 assert.equal(fixture.method("a", "b"), "new secretab",
michael@0 43 "public methods are callable");
michael@0 44 assert.equal(fixture.method.call({ _foo: "test" }, " a,", "b"),
michael@0 45 "new secret a,b",
michael@0 46 "`this` pseudo-variable can not be passed through call.");
michael@0 47 assert.equal(fixture.method.apply({ _foo: "test" }, [" a,", "b"]),
michael@0 48 "new secret a,b",
michael@0 49 "`this` pseudo-variable can not be passed through apply.");
michael@0 50 assert.equal(fixture.getOnly, source._foo,
michael@0 51 "getter returned property of wrapped object");
michael@0 52 fixture.setOnly = 'bar'
michael@0 53 assert.equal(source._setOnly, 'bar', "setter modified wrapped object")
michael@0 54 };
michael@0 55
michael@0 56
michael@0 57 exports["test immunity of inheritance"] = function(assert) {
michael@0 58 function Type() {}
michael@0 59 Type.prototype = {
michael@0 60 constructor: Type,
michael@0 61 _bar: 2,
michael@0 62 bar: 3,
michael@0 63 get_Foo: function getFoo() {
michael@0 64 return this._foo;
michael@0 65 }
michael@0 66 }
michael@0 67 var source = Object.create(Type.prototype, {
michael@0 68 _foo: { value: 'secret' },
michael@0 69 getBar: { value: function get_Bar() {
michael@0 70 return this.bar
michael@0 71 }},
michael@0 72 get_Bar: { value: function getBar() {
michael@0 73 return this._bar
michael@0 74 }}
michael@0 75 });
michael@0 76
michael@0 77 var fixture = Cortex(source);
michael@0 78
michael@0 79 assert.ok(Cortex({}, null, Type.prototype) instanceof Type,
michael@0 80 "if custom prototype is providede cortex will inherit from it");
michael@0 81 assert.ok(fixture instanceof Type,
michael@0 82 "if no prototype is given cortex inherits from object's prototype");
michael@0 83
michael@0 84 source.bar += 1;
michael@0 85 assert.notEqual(fixture.bar, source.bar,
michael@0 86 "chages of properties don't propagate to non-aliases");
michael@0 87 assert.equal(fixture.getBar(), source.bar,
michael@0 88 "methods accessing public properties are bound to the source");
michael@0 89
michael@0 90 fixture._bar += 1;
michael@0 91 assert.notEqual(fixture._bar, source._bar,
michael@0 92 "changes of non aliased properties don't propagate");
michael@0 93 assert.equal(fixture.get_Bar(), source._bar,
michael@0 94 "methods accessing privates are bound to the source");
michael@0 95 assert.notEqual(fixture.get_Foo(), source._foo,
michael@0 96 "prototoype methods are not bound to the source");
michael@0 97 }
michael@0 98
michael@0 99 exports["test customized public properties"] = function(assert) {
michael@0 100 var source = {
michael@0 101 _a: 'a',
michael@0 102 b: 'b',
michael@0 103 get: function get(name) {
michael@0 104 return this[name];
michael@0 105 }
michael@0 106 };
michael@0 107
michael@0 108 var fixture = Cortex(source, ['_a', 'get']);
michael@0 109 fixture._a += "#change";
michael@0 110
michael@0 111
michael@0 112 assert.ok(!("b" in fixture), "non-public own property is not defined");
michael@0 113 assert.equal(fixture.get("b"), source.b,
michael@0 114 "public methods preserve access to the private properties");
michael@0 115 assert.equal(fixture._a, source._a,
michael@0 116 "custom public property changes propagate");
michael@0 117 }
michael@0 118
michael@0 119 //if (require.main == module)
michael@0 120 require("test").run(exports);

mercurial