|
1 _("Make sure notify sends out the right notifications"); |
|
2 Cu.import("resource://services-sync/util.js"); |
|
3 |
|
4 function run_test() { |
|
5 let ret, rightThis, didCall; |
|
6 let obj = { |
|
7 notify: Utils.notify("foo:"), |
|
8 _log: { |
|
9 trace: function() {} |
|
10 }, |
|
11 |
|
12 func: function() this.notify("bar", "baz", function() { |
|
13 rightThis = this == obj; |
|
14 didCall = true; |
|
15 return 5; |
|
16 })(), |
|
17 |
|
18 throwy: function() this.notify("bad", "one", function() { |
|
19 rightThis = this == obj; |
|
20 didCall = true; |
|
21 throw 10; |
|
22 })() |
|
23 }; |
|
24 |
|
25 let state = 0; |
|
26 let makeObs = function(topic) { |
|
27 let obj = { |
|
28 observe: function(subject, topic, data) { |
|
29 this.state = ++state; |
|
30 this.subject = subject; |
|
31 this.topic = topic; |
|
32 this.data = data; |
|
33 } |
|
34 }; |
|
35 |
|
36 Svc.Obs.add(topic, obj); |
|
37 return obj; |
|
38 }; |
|
39 |
|
40 _("Make sure a normal call will call and return with notifications"); |
|
41 rightThis = didCall = false; |
|
42 let fs = makeObs("foo:bar:start"); |
|
43 let ff = makeObs("foo:bar:finish"); |
|
44 let fe = makeObs("foo:bar:error"); |
|
45 ret = obj.func(); |
|
46 do_check_eq(ret, 5); |
|
47 do_check_true(rightThis); |
|
48 do_check_true(didCall); |
|
49 |
|
50 do_check_eq(fs.state, 1); |
|
51 do_check_eq(fs.subject, undefined); |
|
52 do_check_eq(fs.topic, "foo:bar:start"); |
|
53 do_check_eq(fs.data, "baz"); |
|
54 |
|
55 do_check_eq(ff.state, 2); |
|
56 do_check_eq(ff.subject, 5); |
|
57 do_check_eq(ff.topic, "foo:bar:finish"); |
|
58 do_check_eq(ff.data, "baz"); |
|
59 |
|
60 do_check_eq(fe.state, undefined); |
|
61 do_check_eq(fe.subject, undefined); |
|
62 do_check_eq(fe.topic, undefined); |
|
63 do_check_eq(fe.data, undefined); |
|
64 |
|
65 _("Make sure a throwy call will call and throw with notifications"); |
|
66 ret = null; |
|
67 rightThis = didCall = false; |
|
68 let ts = makeObs("foo:bad:start"); |
|
69 let tf = makeObs("foo:bad:finish"); |
|
70 let te = makeObs("foo:bad:error"); |
|
71 try { |
|
72 ret = obj.throwy(); |
|
73 do_throw("throwy should have thrown!"); |
|
74 } |
|
75 catch(ex) { |
|
76 do_check_eq(ex, 10); |
|
77 } |
|
78 do_check_eq(ret, null); |
|
79 do_check_true(rightThis); |
|
80 do_check_true(didCall); |
|
81 |
|
82 do_check_eq(ts.state, 3); |
|
83 do_check_eq(ts.subject, undefined); |
|
84 do_check_eq(ts.topic, "foo:bad:start"); |
|
85 do_check_eq(ts.data, "one"); |
|
86 |
|
87 do_check_eq(tf.state, undefined); |
|
88 do_check_eq(tf.subject, undefined); |
|
89 do_check_eq(tf.topic, undefined); |
|
90 do_check_eq(tf.data, undefined); |
|
91 |
|
92 do_check_eq(te.state, 4); |
|
93 do_check_eq(te.subject, 10); |
|
94 do_check_eq(te.topic, "foo:bad:error"); |
|
95 do_check_eq(te.data, "one"); |
|
96 } |