Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | function test() { |
michael@0 | 5 | let tmp = {}; |
michael@0 | 6 | Cu.import("resource://gre/modules/devtools/Loader.jsm", tmp); |
michael@0 | 7 | let ObservableObject = tmp.devtools.require("devtools/shared/observable-object"); |
michael@0 | 8 | |
michael@0 | 9 | let rawObject = {}; |
michael@0 | 10 | let oe = new ObservableObject(rawObject); |
michael@0 | 11 | |
michael@0 | 12 | function str(o) { |
michael@0 | 13 | return JSON.stringify(o); |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | function areObjectsSynced() { |
michael@0 | 17 | is(str(rawObject), str(oe.object), "Objects are synced"); |
michael@0 | 18 | } |
michael@0 | 19 | |
michael@0 | 20 | areObjectsSynced(); |
michael@0 | 21 | |
michael@0 | 22 | let index = 0; |
michael@0 | 23 | let expected = [ |
michael@0 | 24 | {type: "set", path: "foo", value: 4}, |
michael@0 | 25 | {type: "get", path: "foo", value: 4}, |
michael@0 | 26 | {type: "get", path: "foo", value: 4}, |
michael@0 | 27 | {type: "get", path: "bar", value: undefined}, |
michael@0 | 28 | {type: "get", path: "bar", value: undefined}, |
michael@0 | 29 | {type: "set", path: "bar", value: {}}, |
michael@0 | 30 | {type: "get", path: "bar", value: {}}, |
michael@0 | 31 | {type: "get", path: "bar", value: {}}, |
michael@0 | 32 | {type: "set", path: "bar.a", value: [1,2,3,4]}, |
michael@0 | 33 | {type: "get", path: "bar", value: {a:[1,2,3,4]}}, |
michael@0 | 34 | {type: "set", path: "bar.mop", value: 1}, |
michael@0 | 35 | {type: "set", path: "bar", value: {}}, |
michael@0 | 36 | {type: "set", path: "foo", value: [{a:42}]}, |
michael@0 | 37 | {type: "get", path: "foo", value: [{a:42}]}, |
michael@0 | 38 | {type: "get", path: "foo.0", value: {a:42}}, |
michael@0 | 39 | {type: "get", path: "foo.0.a", value: 42}, |
michael@0 | 40 | {type: "get", path: "foo", value: [{a:42}]}, |
michael@0 | 41 | {type: "get", path: "foo.0", value: {a:42}}, |
michael@0 | 42 | {type: "set", path: "foo.0.a", value: 2}, |
michael@0 | 43 | {type: "get", path: "foo", value: [{a:2}]}, |
michael@0 | 44 | {type: "get", path: "bar", value: {}}, |
michael@0 | 45 | {type: "set", path: "foo.1", value: {}}, |
michael@0 | 46 | ]; |
michael@0 | 47 | |
michael@0 | 48 | function callback(event, path, value) { |
michael@0 | 49 | oe.off("get", callback); |
michael@0 | 50 | ok(event, "event defined"); |
michael@0 | 51 | ok(path, "path defined"); |
michael@0 | 52 | if (index >= expected.length) { |
michael@0 | 53 | return; |
michael@0 | 54 | } |
michael@0 | 55 | let e = expected[index]; |
michael@0 | 56 | is(event, e.type, "[" + index + "] Right event received"); |
michael@0 | 57 | is(path.join("."), e.path, "[" + index + "] Path valid"); |
michael@0 | 58 | is(str(value), str(e.value), "[" + index + "] Value valid"); |
michael@0 | 59 | index++; |
michael@0 | 60 | areObjectsSynced(); |
michael@0 | 61 | oe.on("get", callback); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | oe.on("set", callback); |
michael@0 | 65 | oe.on("get", callback); |
michael@0 | 66 | |
michael@0 | 67 | oe.object.foo = 4; |
michael@0 | 68 | oe.object.foo; |
michael@0 | 69 | Object.getOwnPropertyDescriptor(oe.object, "foo") |
michael@0 | 70 | oe.object["bar"]; |
michael@0 | 71 | oe.object.bar; |
michael@0 | 72 | oe.object.bar = {}; |
michael@0 | 73 | oe.object.bar; |
michael@0 | 74 | oe.object.bar.a = [1,2,3,4]; |
michael@0 | 75 | Object.defineProperty(oe.object.bar, "mop", {value:1}); |
michael@0 | 76 | oe.object.bar = {}; |
michael@0 | 77 | oe.object.foo = [{a:42}]; |
michael@0 | 78 | oe.object.foo[0].a; |
michael@0 | 79 | oe.object.foo[0].a = 2; |
michael@0 | 80 | oe.object.foo[1] = oe.object.bar; |
michael@0 | 81 | |
michael@0 | 82 | is(index, expected.length, "Event count is right"); |
michael@0 | 83 | is(oe.object.bar, oe.object.bar, "Object attributes are wrapped only once"); |
michael@0 | 84 | |
michael@0 | 85 | finish(); |
michael@0 | 86 | } |