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