From 43a97878ce14b72f0981164f87f2e35e14151312 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:22:09 +0200 Subject: Adding upstream version 110.0.1. Signed-off-by: Daniel Baumann --- .../messagehandler/test/xpcshell/test_Errors.js | 99 +++++++ .../test/xpcshell/test_SessionData.js | 296 +++++++++++++++++++++ .../messagehandler/test/xpcshell/xpcshell.ini | 6 + 3 files changed, 401 insertions(+) create mode 100644 remote/shared/messagehandler/test/xpcshell/test_Errors.js create mode 100644 remote/shared/messagehandler/test/xpcshell/test_SessionData.js create mode 100644 remote/shared/messagehandler/test/xpcshell/xpcshell.ini (limited to 'remote/shared/messagehandler/test/xpcshell') diff --git a/remote/shared/messagehandler/test/xpcshell/test_Errors.js b/remote/shared/messagehandler/test/xpcshell/test_Errors.js new file mode 100644 index 0000000000..33cfb5cfd7 --- /dev/null +++ b/remote/shared/messagehandler/test/xpcshell/test_Errors.js @@ -0,0 +1,99 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +const { error } = ChromeUtils.importESModule( + "chrome://remote/content/shared/messagehandler/Errors.sys.mjs" +); + +// Note: this test file is similar to remote/shared/webdriver/test/xpcshell/test_Errors.js +// because shared/webdriver/Errors.jsm and shared/messagehandler/Errors.jsm share +// similar helpers. + +add_test(function test_toJSON() { + let e0 = new error.MessageHandlerError(); + let e0s = e0.toJSON(); + equal(e0s.error, "message handler error"); + equal(e0s.message, ""); + + let e1 = new error.MessageHandlerError("a"); + let e1s = e1.toJSON(); + equal(e1s.message, e1.message); + + let e2 = new error.UnsupportedCommandError("foo"); + let e2s = e2.toJSON(); + equal(e2.status, e2s.error); + equal(e2.message, e2s.message); + + run_next_test(); +}); + +add_test(function test_fromJSON() { + Assert.throws( + () => error.MessageHandlerError.fromJSON({ error: "foo" }), + /Not of MessageHandlerError descent/ + ); + Assert.throws( + () => error.MessageHandlerError.fromJSON({ error: "Error" }), + /Not of MessageHandlerError descent/ + ); + Assert.throws( + () => error.MessageHandlerError.fromJSON({}), + /Undeserialisable error type/ + ); + Assert.throws( + () => error.MessageHandlerError.fromJSON(undefined), + /TypeError/ + ); + + let e1 = new error.MessageHandlerError("1"); + let e1r = error.MessageHandlerError.fromJSON({ + error: "message handler error", + message: "1", + }); + ok(e1r instanceof error.MessageHandlerError); + equal(e1r.name, e1.name); + equal(e1r.status, e1.status); + equal(e1r.message, e1.message); + + let e2 = new error.UnsupportedCommandError("foo"); + let e2r = error.MessageHandlerError.fromJSON({ + error: "unsupported message handler command", + message: "foo", + }); + ok(e2r instanceof error.MessageHandlerError); + ok(e2r instanceof error.UnsupportedCommandError); + equal(e2r.name, e2.name); + equal(e2r.status, e2.status); + equal(e2r.message, e2.message); + + // parity with toJSON + let e3 = new error.UnsupportedCommandError("foo"); + let e3toJSON = e3.toJSON(); + let e3fromJSON = error.MessageHandlerError.fromJSON(e3toJSON); + equal(e3toJSON.error, e3fromJSON.status); + equal(e3toJSON.message, e3fromJSON.message); + equal(e3toJSON.stacktrace, e3fromJSON.stack); + + run_next_test(); +}); + +add_test(function test_MessageHandlerError() { + let err = new error.MessageHandlerError("foo"); + equal("MessageHandlerError", err.name); + equal("foo", err.message); + equal("message handler error", err.status); + ok(err instanceof error.MessageHandlerError); + + run_next_test(); +}); + +add_test(function test_UnsupportedCommandError() { + let e = new error.UnsupportedCommandError("foo"); + equal("UnsupportedCommandError", e.name); + equal("foo", e.message); + equal("unsupported message handler command", e.status); + ok(e instanceof error.MessageHandlerError); + + run_next_test(); +}); diff --git a/remote/shared/messagehandler/test/xpcshell/test_SessionData.js b/remote/shared/messagehandler/test/xpcshell/test_SessionData.js new file mode 100644 index 0000000000..ef61ce27d4 --- /dev/null +++ b/remote/shared/messagehandler/test/xpcshell/test_SessionData.js @@ -0,0 +1,296 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +const { ContextDescriptorType } = ChromeUtils.importESModule( + "chrome://remote/content/shared/messagehandler/MessageHandler.sys.mjs" +); +const { RootMessageHandler } = ChromeUtils.importESModule( + "chrome://remote/content/shared/messagehandler/RootMessageHandler.sys.mjs" +); +const { SessionData, SessionDataMethod } = ChromeUtils.importESModule( + "chrome://remote/content/shared/messagehandler/sessiondata/SessionData.sys.mjs" +); + +add_task(async function test_sessionData() { + const sessionData = new SessionData(new RootMessageHandler("session-id-1")); + equal(sessionData.getSessionData("mod", "event").length, 0); + + const globalContext = { + type: ContextDescriptorType.All, + }; + const otherContext = { type: "other-type", id: "some-id" }; + + info("Add a first event for the global context"); + let updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Add, globalContext, ["first.event"]), + ]); + equal(updatedItems.length, 1, "One item updated"); + equal(updatedItems[0].method, SessionDataMethod.Add, "One item added"); + let updatedValues = updatedItems[0].values; + equal(updatedValues.length, 1, "One value added"); + equal(updatedValues[0], "first.event", "Expected value was added"); + checkEvents(sessionData.getSessionData("mod", "event"), [ + { + value: "first.event", + contextDescriptor: globalContext, + }, + ]); + + info("Add the exact same data (same module, type, context, value)"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Add, globalContext, ["first.event"]), + ]); + equal(updatedItems.length, 0, "No new item updated"); + checkEvents(sessionData.getSessionData("mod", "event"), [ + { + value: "first.event", + contextDescriptor: globalContext, + }, + ]); + + info("Add another context for the same event"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Add, otherContext, ["first.event"]), + ]); + equal(updatedItems.length, 1, "One item updated"); + equal(updatedItems[0].method, SessionDataMethod.Add, "One item added"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 1, "One value added"); + equal(updatedValues[0], "first.event", "Expected value was added"); + checkEvents(sessionData.getSessionData("mod", "event"), [ + { + value: "first.event", + contextDescriptor: globalContext, + }, + { + value: "first.event", + contextDescriptor: otherContext, + }, + ]); + + info("Add a second event for the global context"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Add, globalContext, ["second.event"]), + ]); + equal(updatedItems.length, 1, "One item updated"); + equal(updatedItems[0].method, SessionDataMethod.Add, "One item added"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 1, "One value added"); + equal(updatedValues[0], "second.event", "Expected value was added"); + checkEvents(sessionData.getSessionData("mod", "event"), [ + { + value: "first.event", + contextDescriptor: globalContext, + }, + { + value: "first.event", + contextDescriptor: otherContext, + }, + { + value: "second.event", + contextDescriptor: globalContext, + }, + ]); + + info("Add two events for the global context"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Add, globalContext, [ + "third.event", + "fourth.event", + ]), + ]); + equal(updatedItems.length, 1, "One item updated"); + equal(updatedItems[0].method, SessionDataMethod.Add, "One item added"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 2, "Two values added"); + equal(updatedValues[0], "third.event", "Expected value was added"); + equal(updatedValues[1], "fourth.event", "Expected value was added"); + checkEvents(sessionData.getSessionData("mod", "event"), [ + { + value: "first.event", + contextDescriptor: globalContext, + }, + { + value: "first.event", + contextDescriptor: otherContext, + }, + { + value: "second.event", + contextDescriptor: globalContext, + }, + { + value: "third.event", + contextDescriptor: globalContext, + }, + { + value: "fourth.event", + contextDescriptor: globalContext, + }, + ]); + + info("Remove the second, third and fourth events"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Remove, globalContext, [ + "second.event", + "third.event", + "fourth.event", + ]), + ]); + equal(updatedItems.length, 1, "One item updated"); + equal(updatedItems[0].method, SessionDataMethod.Remove, "One item removed"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 3, "Three values removed"); + equal(updatedValues[0], "second.event", "Expected value was removed"); + equal(updatedValues[1], "third.event", "Expected value was removed"); + equal(updatedValues[2], "fourth.event", "Expected value was removed"); + checkEvents(sessionData.getSessionData("mod", "event"), [ + { + value: "first.event", + contextDescriptor: globalContext, + }, + { + value: "first.event", + contextDescriptor: otherContext, + }, + ]); + + info("Remove the global context from the first event"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Remove, globalContext, ["first.event"]), + ]); + equal(updatedItems.length, 1, "One item updated"); + equal(updatedItems[0].method, SessionDataMethod.Remove, "One item removed"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 1, "One value removed"); + equal(updatedValues[0], "first.event", "Expected value was removed"); + checkEvents(sessionData.getSessionData("mod", "event"), [ + { + value: "first.event", + contextDescriptor: otherContext, + }, + ]); + + info("Remove the other context from the first event"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Remove, otherContext, ["first.event"]), + ]); + equal(updatedItems.length, 1, "One item updated"); + equal(updatedItems[0].method, SessionDataMethod.Remove, "One item removed"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 1, "One value removed"); + equal(updatedValues[0], "first.event", "Expected value was removed"); + checkEvents(sessionData.getSessionData("mod", "event"), []); + + info("Add two events for different contexts"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Add, otherContext, ["first.event"]), + createUpdate(SessionDataMethod.Add, globalContext, ["second.event"]), + ]); + equal(updatedItems.length, 2, "Two items updated"); + equal(updatedItems[0].method, SessionDataMethod.Add, "First item added"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 1, "One value for first item added"); + equal(updatedValues[0], "first.event", "Expected value first item was added"); + equal(updatedItems[1].method, SessionDataMethod.Add, "Second item added"); + updatedValues = updatedItems[1].values; + equal(updatedValues.length, 1, "One value for second item added"); + equal( + updatedValues[0], + "second.event", + "Expected value second item was added" + ); + checkEvents(sessionData.getSessionData("mod", "event"), [ + { + value: "first.event", + contextDescriptor: otherContext, + }, + { + value: "second.event", + contextDescriptor: globalContext, + }, + ]); + + info("Remove two events for different contexts"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Remove, otherContext, ["first.event"]), + createUpdate(SessionDataMethod.Remove, globalContext, ["second.event"]), + ]); + equal(updatedItems.length, 2, "Two items updated"); + equal(updatedItems[0].method, SessionDataMethod.Remove, "First item removed"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 1, "One value for first item removed"); + equal( + updatedValues[0], + "first.event", + "Expected value first item was removed" + ); + equal( + updatedItems[1].method, + SessionDataMethod.Remove, + "Second item removed" + ); + updatedValues = updatedItems[1].values; + equal(updatedValues.length, 1, "One value for second item removed"); + equal( + updatedValues[0], + "second.event", + "Expected value second item was removed" + ); + checkEvents(sessionData.getSessionData("mod", "event"), []); + + info("Add and remove event in different order"); + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Remove, otherContext, ["first.event"]), + createUpdate(SessionDataMethod.Add, otherContext, ["first.event"]), + ]); + equal(updatedItems.length, 1, "One item updated"); + equal(updatedItems[0].method, SessionDataMethod.Add, "One item added"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 1, "One value added"); + equal(updatedValues[0], "first.event", "Expected value was added"); + checkEvents(sessionData.getSessionData("mod", "event"), [ + { + value: "first.event", + contextDescriptor: otherContext, + }, + ]); + + updatedItems = sessionData.applySessionData([ + createUpdate(SessionDataMethod.Add, otherContext, ["first.event"]), + createUpdate(SessionDataMethod.Remove, otherContext, ["first.event"]), + ]); + equal(updatedItems.length, 1, "No item update"); + equal(updatedItems[0].method, SessionDataMethod.Remove, "One item removed"); + updatedValues = updatedItems[0].values; + equal(updatedValues.length, 1, "One value removed"); + equal(updatedValues[0], "first.event", "Expected value was removed"); + checkEvents(sessionData.getSessionData("mod", "event"), []); +}); + +function checkEvents(events, expectedEvents) { + // Check the arrays have the same size. + equal(events.length, expectedEvents.length); + + // Check all the expectedEvents can be found in the events array. + for (const expected of expectedEvents) { + ok( + events.some( + event => + expected.contextDescriptor.type === event.contextDescriptor.type && + expected.contextDescriptor.id === event.contextDescriptor.id && + expected.value == event.value + ) + ); + } +} + +function createUpdate(method, contextDescriptor, values) { + return { + method, + moduleName: "mod", + category: "event", + contextDescriptor, + values, + }; +} diff --git a/remote/shared/messagehandler/test/xpcshell/xpcshell.ini b/remote/shared/messagehandler/test/xpcshell/xpcshell.ini new file mode 100644 index 0000000000..9199459e0a --- /dev/null +++ b/remote/shared/messagehandler/test/xpcshell/xpcshell.ini @@ -0,0 +1,6 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +[test_Errors.js] +[test_SessionData.js] -- cgit v1.2.3