fune/dom/push/test/xpcshell/test_observer_data.js
Victor Porof 0a8ff0ad85 Bug 1561435 - Format dom/, a=automatic-formatting
# ignore-this-changeset

Differential Revision: https://phabricator.services.mozilla.com/D35951

--HG--
extra : source : 62f3501af4bc1c0bd1ee1977a28aee04706a6663
2019-07-05 10:44:55 +02:00

59 lines
1.5 KiB
JavaScript

"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("utf-8").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");
});