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