tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_utils_notify.js (2507B)


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