tor-browser

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

test_observer_data.js (1512B)


      1 "use strict";
      2 
      3 var pushNotifier = Cc["@mozilla.org/push/Notifier;1"].getService(
      4  Ci.nsIPushNotifier
      5 );
      6 var systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
      7 
      8 add_task(async function test_notifyWithData() {
      9  let textData = '{"hello":"world"}';
     10  let payload = new TextEncoder().encode(textData);
     11 
     12  let notifyPromise = promiseObserverNotification(
     13    PushServiceComponent.pushTopic
     14  );
     15  pushNotifier.notifyPushWithData(
     16    "chrome://notify-test",
     17    systemPrincipal,
     18    "" /* messageId */,
     19    payload
     20  );
     21 
     22  let data = (await notifyPromise).subject.QueryInterface(
     23    Ci.nsIPushMessage
     24  ).data;
     25  deepEqual(
     26    data.json(),
     27    {
     28      hello: "world",
     29    },
     30    "Should extract JSON values"
     31  );
     32  deepEqual(
     33    data.binary(),
     34    Array.from(payload),
     35    "Should extract raw binary data"
     36  );
     37  equal(data.text(), textData, "Should extract text data");
     38 });
     39 
     40 add_task(async function test_empty_notifyWithData() {
     41  let notifyPromise = promiseObserverNotification(
     42    PushServiceComponent.pushTopic
     43  );
     44  pushNotifier.notifyPushWithData(
     45    "chrome://notify-test",
     46    systemPrincipal,
     47    "" /* messageId */,
     48    []
     49  );
     50 
     51  let data = (await notifyPromise).subject.QueryInterface(
     52    Ci.nsIPushMessage
     53  ).data;
     54  throws(
     55    _ => data.json(),
     56    /InvalidStateError/,
     57    "Should throw an error when parsing an empty string as JSON"
     58  );
     59  strictEqual(data.text(), "", "Should return an empty string");
     60  deepEqual(data.binary(), [], "Should return an empty array");
     61 });