services/sync/tests/unit/test_utils_notify.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_utils_notify.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,96 @@
     1.4 +_("Make sure notify sends out the right notifications");
     1.5 +Cu.import("resource://services-sync/util.js");
     1.6 +
     1.7 +function run_test() {
     1.8 +  let ret, rightThis, didCall;
     1.9 +  let obj = {
    1.10 +    notify: Utils.notify("foo:"),
    1.11 +    _log: {
    1.12 +      trace: function() {}
    1.13 +    },
    1.14 +
    1.15 +    func: function() this.notify("bar", "baz", function() {
    1.16 +      rightThis = this == obj;
    1.17 +      didCall = true;
    1.18 +      return 5;
    1.19 +    })(),
    1.20 +
    1.21 +    throwy: function() this.notify("bad", "one", function() {
    1.22 +      rightThis = this == obj;
    1.23 +      didCall = true;
    1.24 +      throw 10;
    1.25 +    })()
    1.26 +  };
    1.27 +
    1.28 +  let state = 0;
    1.29 +  let makeObs = function(topic) {
    1.30 +    let obj = {
    1.31 +      observe: function(subject, topic, data) {
    1.32 +        this.state = ++state;
    1.33 +        this.subject = subject;
    1.34 +        this.topic = topic;
    1.35 +        this.data = data;
    1.36 +      }
    1.37 +    };
    1.38 +
    1.39 +    Svc.Obs.add(topic, obj);
    1.40 +    return obj;
    1.41 +  };
    1.42 +
    1.43 +  _("Make sure a normal call will call and return with notifications");
    1.44 +  rightThis = didCall = false;
    1.45 +  let fs = makeObs("foo:bar:start");
    1.46 +  let ff = makeObs("foo:bar:finish");
    1.47 +  let fe = makeObs("foo:bar:error");
    1.48 +  ret = obj.func();
    1.49 +  do_check_eq(ret, 5);
    1.50 +  do_check_true(rightThis);
    1.51 +  do_check_true(didCall);
    1.52 +
    1.53 +  do_check_eq(fs.state, 1);
    1.54 +  do_check_eq(fs.subject, undefined);
    1.55 +  do_check_eq(fs.topic, "foo:bar:start");
    1.56 +  do_check_eq(fs.data, "baz");
    1.57 +
    1.58 +  do_check_eq(ff.state, 2);
    1.59 +  do_check_eq(ff.subject, 5);
    1.60 +  do_check_eq(ff.topic, "foo:bar:finish");
    1.61 +  do_check_eq(ff.data, "baz");
    1.62 +
    1.63 +  do_check_eq(fe.state, undefined);
    1.64 +  do_check_eq(fe.subject, undefined);
    1.65 +  do_check_eq(fe.topic, undefined);
    1.66 +  do_check_eq(fe.data, undefined);
    1.67 +
    1.68 +  _("Make sure a throwy call will call and throw with notifications");
    1.69 +  ret = null;
    1.70 +  rightThis = didCall = false;
    1.71 +  let ts = makeObs("foo:bad:start");
    1.72 +  let tf = makeObs("foo:bad:finish");
    1.73 +  let te = makeObs("foo:bad:error");
    1.74 +  try {
    1.75 +    ret = obj.throwy();
    1.76 +    do_throw("throwy should have thrown!");
    1.77 +  }
    1.78 +  catch(ex) {
    1.79 +    do_check_eq(ex, 10);
    1.80 +  }
    1.81 +  do_check_eq(ret, null);
    1.82 +  do_check_true(rightThis);
    1.83 +  do_check_true(didCall);
    1.84 +
    1.85 +  do_check_eq(ts.state, 3);
    1.86 +  do_check_eq(ts.subject, undefined);
    1.87 +  do_check_eq(ts.topic, "foo:bad:start");
    1.88 +  do_check_eq(ts.data, "one");
    1.89 +
    1.90 +  do_check_eq(tf.state, undefined);
    1.91 +  do_check_eq(tf.subject, undefined);
    1.92 +  do_check_eq(tf.topic, undefined);
    1.93 +  do_check_eq(tf.data, undefined);
    1.94 +
    1.95 +  do_check_eq(te.state, 4);
    1.96 +  do_check_eq(te.subject, 10);
    1.97 +  do_check_eq(te.topic, "foo:bad:error");
    1.98 +  do_check_eq(te.data, "one");
    1.99 +}

mercurial