From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- dom/push/test/xpcshell/test_observer_data.js | 61 ++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 dom/push/test/xpcshell/test_observer_data.js (limited to 'dom/push/test/xpcshell/test_observer_data.js') diff --git a/dom/push/test/xpcshell/test_observer_data.js b/dom/push/test/xpcshell/test_observer_data.js new file mode 100644 index 0000000000..01c331237c --- /dev/null +++ b/dom/push/test/xpcshell/test_observer_data.js @@ -0,0 +1,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"); +}); -- cgit v1.2.3