diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /devtools/client/aboutdebugging/test/browser/mocks | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/aboutdebugging/test/browser/mocks')
3 files changed, 351 insertions, 0 deletions
diff --git a/devtools/client/aboutdebugging/test/browser/mocks/helper-adb-mock.js b/devtools/client/aboutdebugging/test/browser/mocks/helper-adb-mock.js new file mode 100644 index 0000000000..f02ca02ee3 --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/mocks/helper-adb-mock.js @@ -0,0 +1,137 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Setup the loader to return the provided mock object instead of the regular adb module. + * + * @param {Object} + * mock should implement the following methods: + * - registerListener(listener) + * - getRuntimes() + * - getDevices() + * - updateRuntimes() + * - unregisterListener(listener) + */ +function enableAdbMock(mock) { + const { + setMockedModule, + } = require("resource://devtools/shared/loader/browser-loader-mocks.js"); + setMockedModule(mock, "devtools/client/shared/remote-debugging/adb/adb"); +} +/* exported enableAdbMock */ + +/** + * Update the loader to clear the mock entry for the adb module. + */ +function disableAdbMock() { + const { + removeMockedModule, + } = require("resource://devtools/shared/loader/browser-loader-mocks.js"); + removeMockedModule("devtools/client/shared/remote-debugging/adb/adb"); +} +/* exported disableAdbMock */ + +/** + * Creates a simple mock version for adb, implementing all the expected methods + * with empty placeholders. + */ +function createAdbMock() { + const adbMock = {}; + adbMock.registerListener = function (listener) { + console.log("MOCKED METHOD registerListener"); + }; + + adbMock.getRuntimes = function () { + console.log("MOCKED METHOD getRuntimes"); + }; + + adbMock.getDevices = function () { + console.log("MOCKED METHOD getDevices"); + }; + + adbMock.updateRuntimes = function () { + console.log("MOCKED METHOD updateRuntimes"); + }; + + adbMock.unregisterListener = function (listener) { + console.log("MOCKED METHOD unregisterListener"); + }; + + adbMock.once = function () { + console.log("MOCKED METHOD once"); + }; + + adbMock.isProcessStarted = function () { + console.log("MOCKED METHOD isProcessStarted"); + }; + + return { adb: adbMock }; +} +/* exported createAdbMock */ + +/** + * The adb module allows to observe runtime updates. To simulate this behaviour + * the easiest is to use an EventEmitter-decorated object that can accept listeners and + * can emit events from the test. + * + * This method will update the registerListener method of the provided + * usbRuntimesMock in order to add listeners to a mockObserver, and returns said observer + * so that the test can emit "runtime-list-updated" when needed. + */ +function addObserverMock(adbMock) { + const EventEmitter = require("resource://devtools/shared/event-emitter.js"); + + const observerMock = {}; + EventEmitter.decorate(observerMock); + adbMock.registerListener = function (listener) { + console.log("MOCKED METHOD registerListener with mock scanner"); + observerMock.on("runtime-list-updated", listener); + }; + + // NOTE FOR REVIEW: Instead of emitting "runtime-list-updated" events in the test, + // this mock could have a emitObservedEvent method, that would just emit the correct + // event. This way if the event name changes, everything remains contained in this + // method. + + return observerMock; +} +/* exported addObserverMock */ + +function createAdbProcessMock() { + const EventEmitter = require("resource://devtools/shared/event-emitter.js"); + + const mock = {}; + EventEmitter.decorate(mock); + + mock.ready = false; + + mock.start = async () => { + console.log("MOCKED METHOD start"); + mock.ready = true; + mock.emit("adb-ready"); + }; + + return { adbProcess: mock }; +} +/* exported createAdbProcessMock */ + +function enableAdbProcessMock(mock) { + const { + setMockedModule, + } = require("resource://devtools/shared/loader/browser-loader-mocks.js"); + setMockedModule( + mock, + "devtools/client/shared/remote-debugging/adb/adb-process" + ); +} +/* exported enableAdbProcessMock */ + +function disableAdbProcessMock() { + const { + removeMockedModule, + } = require("resource://devtools/shared/loader/browser-loader-mocks.js"); + removeMockedModule("devtools/client/shared/remote-debugging/adb/adb-process"); +} +/* exported disableAdbProcessMock */ diff --git a/devtools/client/aboutdebugging/test/browser/mocks/helper-client-wrapper-mock.js b/devtools/client/aboutdebugging/test/browser/mocks/helper-client-wrapper-mock.js new file mode 100644 index 0000000000..5748904bff --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/mocks/helper-client-wrapper-mock.js @@ -0,0 +1,138 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; // defined in head.js + +/* global CHROME_URL_ROOT */ + +// This head file contains helpers to create mock versions of the ClientWrapper class +// defined at devtools/client/aboutdebugging/src/modules/client-wrapper.js . + +const { + RUNTIME_PREFERENCE, +} = require("resource://devtools/client/aboutdebugging/src/constants.js"); + +// Sensible default values for runtime preferences that should be usable in most +// situations +const DEFAULT_PREFERENCES = { + [RUNTIME_PREFERENCE.CONNECTION_PROMPT]: true, + [RUNTIME_PREFERENCE.PERMANENT_PRIVATE_BROWSING]: false, + [RUNTIME_PREFERENCE.SERVICE_WORKERS_ENABLED]: true, +}; + +// Creates a simple mock ClientWrapper. +function createClientMock() { + const EventEmitter = require("resource://devtools/shared/event-emitter.js"); + const eventEmitter = {}; + EventEmitter.decorate(eventEmitter); + + return { + // add a reference to the internal event emitter so that consumers can fire client + // events. + _eventEmitter: eventEmitter, + _preferences: {}, + contentProcessFronts: [], + serviceWorkerRegistrationFronts: [], + once: (evt, listener) => { + eventEmitter.once(evt, listener); + }, + on: (evt, listener) => { + eventEmitter.on(evt, listener); + }, + off: (evt, listener) => { + eventEmitter.off(evt, listener); + }, + client: { + once: (evt, listener) => { + eventEmitter.once(evt, listener); + }, + on: (evt, listener) => { + eventEmitter.on(evt, listener); + }, + off: (evt, listener) => { + eventEmitter.off(evt, listener); + }, + }, + // no-op + close: () => {}, + // client is not closed + isClosed: () => false, + // no-op + connect: () => {}, + // no-op + getDeviceDescription: () => {}, + // Return default preference value or null if no match. + getPreference(prefName) { + if (prefName in this._preferences) { + return this._preferences[prefName]; + } + if (prefName in DEFAULT_PREFERENCES) { + return DEFAULT_PREFERENCES[prefName]; + } + return null; + }, + // no-op + createRootResourceCommand: () => { + return { + watchResources: () => new Promise(r => r()), + unwatchResources: () => {}, + }; + }, + // Empty array of addons + listAddons: () => [], + // Empty array of processes + listProcesses: () => [], + // Empty array of tabs + listTabs: () => [], + // Empty arrays of workers + listWorkers: () => ({ + otherWorkers: [], + serviceWorkers: [], + sharedWorkers: [], + }), + // no-op + getMainProcess: () => {}, + // no-op + getFront: () => {}, + // stores the preference locally (doesn't update about:config) + setPreference(prefName, value) { + this._preferences[prefName] = value; + }, + getPerformancePanelUrl: () => CHROME_URL_ROOT + "empty.html", + loadPerformanceProfiler: () => {}, + // Valid compatibility report + checkVersionCompatibility: () => { + const { + COMPATIBILITY_STATUS, + } = require("resource://devtools/client/shared/remote-debugging/version-checker.js"); + return { status: COMPATIBILITY_STATUS.COMPATIBLE }; + }, + // No traits by default but allow updates. + traits: {}, + }; +} + +// Create a ClientWrapper mock that can be used to replace the this-firefox runtime. +function createThisFirefoxClientMock() { + const mockThisFirefoxDescription = { + name: "Firefox", + channel: "nightly", + version: "63.0", + }; + + // Create a fake about:debugging tab because our test helper openAboutDebugging + // waits until about:debugging is displayed in the list of tabs. + const mockAboutDebuggingTab = { + retrieveFavicon: () => {}, + outerWindowID: 0, + traits: {}, + url: "about:debugging", + }; + + const mockThisFirefoxClient = createClientMock(); + mockThisFirefoxClient.listTabs = () => [mockAboutDebuggingTab]; + mockThisFirefoxClient.getDeviceDescription = () => mockThisFirefoxDescription; + + return mockThisFirefoxClient; +} +/* exported createThisFirefoxClientMock */ diff --git a/devtools/client/aboutdebugging/test/browser/mocks/helper-runtime-client-factory-mock.js b/devtools/client/aboutdebugging/test/browser/mocks/helper-runtime-client-factory-mock.js new file mode 100644 index 0000000000..b74c229fcf --- /dev/null +++ b/devtools/client/aboutdebugging/test/browser/mocks/helper-runtime-client-factory-mock.js @@ -0,0 +1,76 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +/** + * Setup the loader to return the provided mock object instead of the regular + * runtime-client-factory module. + * + * @param {Object} + * mock should implement the following methods: + * - createClientForRuntime(runtime) + */ +function enableRuntimeClientFactoryMock(mock) { + const { + setMockedModule, + } = require("resource://devtools/shared/loader/browser-loader-mocks.js"); + setMockedModule( + mock, + "devtools/client/aboutdebugging/src/modules/runtime-client-factory" + ); + + // When using a mocked client, we should not attempt to check default + // preferences. + mockRuntimeDefaultPreferences(); +} +/* exported enableRuntimeClientFactoryMock */ + +const mockRuntimeDefaultPreferences = function () { + const { + removeMockedModule, + setMockedModule, + } = require("resource://devtools/shared/loader/browser-loader-mocks.js"); + + const mock = { + setDefaultPreferencesIfNeeded: () => {}, + DEFAULT_PREFERENCES: [], + }; + setMockedModule( + mock, + "devtools/client/aboutdebugging/src/modules/runtime-default-preferences" + ); + + registerCleanupFunction(() => { + removeMockedModule( + "devtools/client/aboutdebugging/src/modules/runtime-default-preferences" + ); + }); +}; + +/** + * Update the loader to clear the mock entry for the runtime-client-factory module. + */ +function disableRuntimeClientFactoryMock() { + const { + removeMockedModule, + } = require("resource://devtools/shared/loader/browser-loader-mocks.js"); + removeMockedModule( + "devtools/client/aboutdebugging/src/modules/runtime-client-factory" + ); +} +/* exported disableRuntimeClientFactoryMock */ + +/** + * Creates a simple mock version for runtime-client-factory, implementing all the expected + * methods with empty placeholders. + */ +function createRuntimeClientFactoryMock() { + const RuntimeClientFactoryMock = {}; + RuntimeClientFactoryMock.createClientForRuntime = function (runtime) { + console.log("MOCKED METHOD createClientForRuntime"); + }; + + return RuntimeClientFactoryMock; +} +/* exported createRuntimeClientFactoryMock */ |