1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/shared/test/browser_observableobject.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,86 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +function test() { 1.8 + let tmp = {}; 1.9 + Cu.import("resource://gre/modules/devtools/Loader.jsm", tmp); 1.10 + let ObservableObject = tmp.devtools.require("devtools/shared/observable-object"); 1.11 + 1.12 + let rawObject = {}; 1.13 + let oe = new ObservableObject(rawObject); 1.14 + 1.15 + function str(o) { 1.16 + return JSON.stringify(o); 1.17 + } 1.18 + 1.19 + function areObjectsSynced() { 1.20 + is(str(rawObject), str(oe.object), "Objects are synced"); 1.21 + } 1.22 + 1.23 + areObjectsSynced(); 1.24 + 1.25 + let index = 0; 1.26 + let expected = [ 1.27 + {type: "set", path: "foo", value: 4}, 1.28 + {type: "get", path: "foo", value: 4}, 1.29 + {type: "get", path: "foo", value: 4}, 1.30 + {type: "get", path: "bar", value: undefined}, 1.31 + {type: "get", path: "bar", value: undefined}, 1.32 + {type: "set", path: "bar", value: {}}, 1.33 + {type: "get", path: "bar", value: {}}, 1.34 + {type: "get", path: "bar", value: {}}, 1.35 + {type: "set", path: "bar.a", value: [1,2,3,4]}, 1.36 + {type: "get", path: "bar", value: {a:[1,2,3,4]}}, 1.37 + {type: "set", path: "bar.mop", value: 1}, 1.38 + {type: "set", path: "bar", value: {}}, 1.39 + {type: "set", path: "foo", value: [{a:42}]}, 1.40 + {type: "get", path: "foo", value: [{a:42}]}, 1.41 + {type: "get", path: "foo.0", value: {a:42}}, 1.42 + {type: "get", path: "foo.0.a", value: 42}, 1.43 + {type: "get", path: "foo", value: [{a:42}]}, 1.44 + {type: "get", path: "foo.0", value: {a:42}}, 1.45 + {type: "set", path: "foo.0.a", value: 2}, 1.46 + {type: "get", path: "foo", value: [{a:2}]}, 1.47 + {type: "get", path: "bar", value: {}}, 1.48 + {type: "set", path: "foo.1", value: {}}, 1.49 + ]; 1.50 + 1.51 + function callback(event, path, value) { 1.52 + oe.off("get", callback); 1.53 + ok(event, "event defined"); 1.54 + ok(path, "path defined"); 1.55 + if (index >= expected.length) { 1.56 + return; 1.57 + } 1.58 + let e = expected[index]; 1.59 + is(event, e.type, "[" + index + "] Right event received"); 1.60 + is(path.join("."), e.path, "[" + index + "] Path valid"); 1.61 + is(str(value), str(e.value), "[" + index + "] Value valid"); 1.62 + index++; 1.63 + areObjectsSynced(); 1.64 + oe.on("get", callback); 1.65 + } 1.66 + 1.67 + oe.on("set", callback); 1.68 + oe.on("get", callback); 1.69 + 1.70 + oe.object.foo = 4; 1.71 + oe.object.foo; 1.72 + Object.getOwnPropertyDescriptor(oe.object, "foo") 1.73 + oe.object["bar"]; 1.74 + oe.object.bar; 1.75 + oe.object.bar = {}; 1.76 + oe.object.bar; 1.77 + oe.object.bar.a = [1,2,3,4]; 1.78 + Object.defineProperty(oe.object.bar, "mop", {value:1}); 1.79 + oe.object.bar = {}; 1.80 + oe.object.foo = [{a:42}]; 1.81 + oe.object.foo[0].a; 1.82 + oe.object.foo[0].a = 2; 1.83 + oe.object.foo[1] = oe.object.bar; 1.84 + 1.85 + is(index, expected.length, "Event count is right"); 1.86 + is(oe.object.bar, oe.object.bar, "Object attributes are wrapped only once"); 1.87 + 1.88 + finish(); 1.89 +}