michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: function test() { michael@0: let tmp = {}; michael@0: Cu.import("resource://gre/modules/devtools/Loader.jsm", tmp); michael@0: let ObservableObject = tmp.devtools.require("devtools/shared/observable-object"); michael@0: michael@0: let rawObject = {}; michael@0: let oe = new ObservableObject(rawObject); michael@0: michael@0: function str(o) { michael@0: return JSON.stringify(o); michael@0: } michael@0: michael@0: function areObjectsSynced() { michael@0: is(str(rawObject), str(oe.object), "Objects are synced"); michael@0: } michael@0: michael@0: areObjectsSynced(); michael@0: michael@0: let index = 0; michael@0: let expected = [ michael@0: {type: "set", path: "foo", value: 4}, michael@0: {type: "get", path: "foo", value: 4}, michael@0: {type: "get", path: "foo", value: 4}, michael@0: {type: "get", path: "bar", value: undefined}, michael@0: {type: "get", path: "bar", value: undefined}, michael@0: {type: "set", path: "bar", value: {}}, michael@0: {type: "get", path: "bar", value: {}}, michael@0: {type: "get", path: "bar", value: {}}, michael@0: {type: "set", path: "bar.a", value: [1,2,3,4]}, michael@0: {type: "get", path: "bar", value: {a:[1,2,3,4]}}, michael@0: {type: "set", path: "bar.mop", value: 1}, michael@0: {type: "set", path: "bar", value: {}}, michael@0: {type: "set", path: "foo", value: [{a:42}]}, michael@0: {type: "get", path: "foo", value: [{a:42}]}, michael@0: {type: "get", path: "foo.0", value: {a:42}}, michael@0: {type: "get", path: "foo.0.a", value: 42}, michael@0: {type: "get", path: "foo", value: [{a:42}]}, michael@0: {type: "get", path: "foo.0", value: {a:42}}, michael@0: {type: "set", path: "foo.0.a", value: 2}, michael@0: {type: "get", path: "foo", value: [{a:2}]}, michael@0: {type: "get", path: "bar", value: {}}, michael@0: {type: "set", path: "foo.1", value: {}}, michael@0: ]; michael@0: michael@0: function callback(event, path, value) { michael@0: oe.off("get", callback); michael@0: ok(event, "event defined"); michael@0: ok(path, "path defined"); michael@0: if (index >= expected.length) { michael@0: return; michael@0: } michael@0: let e = expected[index]; michael@0: is(event, e.type, "[" + index + "] Right event received"); michael@0: is(path.join("."), e.path, "[" + index + "] Path valid"); michael@0: is(str(value), str(e.value), "[" + index + "] Value valid"); michael@0: index++; michael@0: areObjectsSynced(); michael@0: oe.on("get", callback); michael@0: } michael@0: michael@0: oe.on("set", callback); michael@0: oe.on("get", callback); michael@0: michael@0: oe.object.foo = 4; michael@0: oe.object.foo; michael@0: Object.getOwnPropertyDescriptor(oe.object, "foo") michael@0: oe.object["bar"]; michael@0: oe.object.bar; michael@0: oe.object.bar = {}; michael@0: oe.object.bar; michael@0: oe.object.bar.a = [1,2,3,4]; michael@0: Object.defineProperty(oe.object.bar, "mop", {value:1}); michael@0: oe.object.bar = {}; michael@0: oe.object.foo = [{a:42}]; michael@0: oe.object.foo[0].a; michael@0: oe.object.foo[0].a = 2; michael@0: oe.object.foo[1] = oe.object.bar; michael@0: michael@0: is(index, expected.length, "Event count is right"); michael@0: is(oe.object.bar, oe.object.bar, "Object attributes are wrapped only once"); michael@0: michael@0: finish(); michael@0: }