diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /devtools/client/shared/remote-debugging/adb/xpcshell/test_prepare-tcp-connection.js | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/shared/remote-debugging/adb/xpcshell/test_prepare-tcp-connection.js')
-rw-r--r-- | devtools/client/shared/remote-debugging/adb/xpcshell/test_prepare-tcp-connection.js | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/devtools/client/shared/remote-debugging/adb/xpcshell/test_prepare-tcp-connection.js b/devtools/client/shared/remote-debugging/adb/xpcshell/test_prepare-tcp-connection.js new file mode 100644 index 0000000000..c3f2b2f7e0 --- /dev/null +++ b/devtools/client/shared/remote-debugging/adb/xpcshell/test_prepare-tcp-connection.js @@ -0,0 +1,79 @@ +"use strict"; + +const DEVICE_ID = "FAKE_DEVICE_ID"; +const SOCKET_PATH = "fake/socket/path"; + +/** + * Test the prepare-tcp-connection command in isolation. + */ +add_task(async function testParseFileUri() { + info("Enable devtools.testing for this test to allow mocked modules"); + const Services = require("Services"); + Services.prefs.setBoolPref("devtools.testing", true); + registerCleanupFunction(() => { + Services.prefs.clearUserPref("devtools.testing"); + }); + + // Mocks are not supported for the regular DevTools loader. + info("Create a BrowserLoader to enable mocks in the test"); + const { BrowserLoader } = ChromeUtils.import( + "resource://devtools/client/shared/browser-loader.js" + ); + const mockedRequire = BrowserLoader({ + baseURI: "resource://devtools/client/shared/remote-debugging/adb", + window: {}, + }).require; + + // Prepare a mocked version of the run-command.js module to test + // prepare-tcp-connection in isolation. + info("Create a run-command module mock"); + let createdPort; + const mock = { + runCommand: command => { + // Check that we only receive the expected command and extract the port + // number. + + // The command should follow the pattern + // `host-serial:${deviceId}:forward:tcp:${localPort};${devicePort}` + // with devicePort = `localfilesystem:${socketPath}` + const socketPathRe = SOCKET_PATH.replace(/\//g, "\\/"); + const regexp = new RegExp( + `host-serial:${DEVICE_ID}:forward:tcp:(\\d+);localfilesystem:${socketPathRe}` + ); + const matches = regexp.exec(command); + equal(matches.length, 2, "The command is the expected"); + createdPort = matches[1]; + + // Finally return a Promise-like object. + return { + then: () => {}, + }; + }, + }; + + info("Enable the mocked run-command module"); + const { setMockedModule, removeMockedModule } = mockedRequire( + "devtools/client/shared/browser-loader-mocks" + ); + setMockedModule( + mock, + "devtools/client/shared/remote-debugging/adb/commands/run-command" + ); + registerCleanupFunction(() => { + removeMockedModule( + "devtools/client/shared/remote-debugging/adb/commands/run-command" + ); + }); + + const { prepareTCPConnection } = mockedRequire( + "devtools/client/shared/remote-debugging/adb/commands/prepare-tcp-connection" + ); + + const port = await prepareTCPConnection(DEVICE_ID, SOCKET_PATH); + ok(!Number.isNaN(port), "prepareTCPConnection returned a valid port"); + equal( + port, + Number(createdPort), + "prepareTCPConnection returned the port sent to the host-serial command" + ); +}); |