summaryrefslogtreecommitdiffstats
path: root/dom/push/test/xpcshell/test_observer_data.js
blob: 01c331237c1c1bfb7e30e8cc5c97fc2737ca2e85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"use strict";

var pushNotifier = Cc["@mozilla.org/push/Notifier;1"].getService(
  Ci.nsIPushNotifier
);
var systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();

add_task(async function test_notifyWithData() {
  let textData = '{"hello":"world"}';
  let payload = new TextEncoder().encode(textData);

  let notifyPromise = promiseObserverNotification(
    PushServiceComponent.pushTopic
  );
  pushNotifier.notifyPushWithData(
    "chrome://notify-test",
    systemPrincipal,
    "" /* messageId */,
    payload
  );

  let data = (await notifyPromise).subject.QueryInterface(
    Ci.nsIPushMessage
  ).data;
  deepEqual(
    data.json(),
    {
      hello: "world",
    },
    "Should extract JSON values"
  );
  deepEqual(
    data.binary(),
    Array.from(payload),
    "Should extract raw binary data"
  );
  equal(data.text(), textData, "Should extract text data");
});

add_task(async function test_empty_notifyWithData() {
  let notifyPromise = promiseObserverNotification(
    PushServiceComponent.pushTopic
  );
  pushNotifier.notifyPushWithData(
    "chrome://notify-test",
    systemPrincipal,
    "" /* messageId */,
    []
  );

  let data = (await notifyPromise).subject.QueryInterface(
    Ci.nsIPushMessage
  ).data;
  throws(
    _ => data.json(),
    /InvalidStateError/,
    "Should throw an error when parsing an empty string as JSON"
  );
  strictEqual(data.text(), "", "Should return an empty string");
  deepEqual(data.binary(), [], "Should return an empty array");
});