test_prepare-tcp-connection.js (2574B)
1 "use strict"; 2 3 const DEVICE_ID = "FAKE_DEVICE_ID"; 4 const SOCKET_PATH = "fake/socket/path"; 5 6 /** 7 * Test the prepare-tcp-connection command in isolation. 8 */ 9 add_task(async function testParseFileUri() { 10 info("Enable devtools.testing for this test to allow mocked modules"); 11 Services.prefs.setBoolPref("devtools.testing", true); 12 registerCleanupFunction(() => { 13 Services.prefs.clearUserPref("devtools.testing"); 14 }); 15 16 // Mocks are not supported for the regular DevTools loader. 17 info("Create a BrowserLoader to enable mocks in the test"); 18 const { BrowserLoader } = ChromeUtils.importESModule( 19 "resource://devtools/shared/loader/browser-loader.sys.mjs" 20 ); 21 const mockedRequire = BrowserLoader({ 22 baseURI: "resource://devtools/client/shared/remote-debugging/adb", 23 window: {}, 24 }).require; 25 26 // Prepare a mocked version of the run-command.js module to test 27 // prepare-tcp-connection in isolation. 28 info("Create a run-command module mock"); 29 let createdPort; 30 const mock = { 31 runCommand: command => { 32 // Check that we only receive the expected command and extract the port 33 // number. 34 35 // The command should follow the pattern 36 // `host-serial:${deviceId}:forward:tcp:${localPort};${devicePort}` 37 // with devicePort = `localfilesystem:${socketPath}` 38 const socketPathRe = SOCKET_PATH.replace(/\//g, "\\/"); 39 const regexp = new RegExp( 40 `host-serial:${DEVICE_ID}:forward:tcp:(\\d+);localfilesystem:${socketPathRe}` 41 ); 42 const matches = regexp.exec(command); 43 equal(matches.length, 2, "The command is the expected"); 44 createdPort = matches[1]; 45 46 // Finally return a Promise-like object. 47 return { 48 then: () => {}, 49 }; 50 }, 51 }; 52 53 info("Enable the mocked run-command module"); 54 const { setMockedModule, removeMockedModule } = mockedRequire( 55 "devtools/shared/loader/browser-loader-mocks" 56 ); 57 setMockedModule( 58 mock, 59 "devtools/client/shared/remote-debugging/adb/commands/run-command" 60 ); 61 registerCleanupFunction(() => { 62 removeMockedModule( 63 "devtools/client/shared/remote-debugging/adb/commands/run-command" 64 ); 65 }); 66 67 const { prepareTCPConnection } = mockedRequire( 68 "devtools/client/shared/remote-debugging/adb/commands/prepare-tcp-connection" 69 ); 70 71 const port = await prepareTCPConnection(DEVICE_ID, SOCKET_PATH); 72 ok(!Number.isNaN(port), "prepareTCPConnection returned a valid port"); 73 equal( 74 port, 75 Number(createdPort), 76 "prepareTCPConnection returned the port sent to the host-serial command" 77 ); 78 });