2016-04-27 01:26:30 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var pushNotifier = Cc['@mozilla.org/push/Notifier;1']
|
|
|
|
.getService(Ci.nsIPushNotifier);
|
|
|
|
var systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
|
|
|
|
add_task(function* test_notifyWithData() {
|
|
|
|
let textData = '{"hello":"world"}';
|
2016-05-02 16:38:47 +00:00
|
|
|
let payload = new TextEncoder('utf-8').encode(textData);
|
2016-04-27 01:26:30 +00:00
|
|
|
|
|
|
|
let notifyPromise =
|
|
|
|
promiseObserverNotification(PushServiceComponent.pushTopic);
|
|
|
|
pushNotifier.notifyPushWithData('chrome://notify-test', systemPrincipal,
|
2016-05-02 16:38:47 +00:00
|
|
|
'' /* messageId */, payload.length, payload);
|
2016-04-27 01:26:30 +00:00
|
|
|
|
2016-05-02 16:38:47 +00:00
|
|
|
let data = (yield notifyPromise).subject.QueryInterface(
|
|
|
|
Ci.nsIPushMessage).data;
|
|
|
|
deepEqual(data.json(), {
|
2016-04-27 01:26:30 +00:00
|
|
|
hello: 'world',
|
|
|
|
}, 'Should extract JSON values');
|
2016-05-02 16:38:47 +00:00
|
|
|
deepEqual(data.binary(), Array.from(payload),
|
2016-04-27 01:26:30 +00:00
|
|
|
'Should extract raw binary data');
|
2016-05-02 16:38:47 +00:00
|
|
|
equal(data.text(), textData, 'Should extract text data');
|
2016-04-27 01:26:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
add_task(function* test_empty_notifyWithData() {
|
|
|
|
let notifyPromise =
|
|
|
|
promiseObserverNotification(PushServiceComponent.pushTopic);
|
|
|
|
pushNotifier.notifyPushWithData('chrome://notify-test', systemPrincipal,
|
|
|
|
'' /* messageId */, 0, null);
|
|
|
|
|
2016-05-02 16:38:47 +00:00
|
|
|
let data = (yield notifyPromise).subject.QueryInterface(
|
|
|
|
Ci.nsIPushMessage).data;
|
|
|
|
throws(_ => data.json(),
|
2016-04-27 01:26:30 +00:00
|
|
|
'Should throw an error when parsing an empty string as JSON');
|
2016-05-02 16:38:47 +00:00
|
|
|
strictEqual(data.text(), '', 'Should return an empty string');
|
|
|
|
deepEqual(data.binary(), [], 'Should return an empty array');
|
2016-04-27 01:26:30 +00:00
|
|
|
});
|