From b49c9762477e4e2d4e8ed7f3cb1c9f82658336fc Mon Sep 17 00:00:00 2001 From: Yaroslav Serhieiev Date: Fri, 14 Sep 2018 12:00:51 +0300 Subject: [PATCH] fix: using adb exec-out instead of adb shell for commands that return a consumable output --- detox/src/devices/android/ADB.js | 74 +++++++++++---------- detox/src/devices/android/ADB.test.js | 35 +++++----- detox/src/devices/android/EmulatorTelnet.js | 12 ++-- detox/src/devices/drivers/AndroidDriver.js | 2 +- 4 files changed, 61 insertions(+), 62 deletions(-) diff --git a/detox/src/devices/android/ADB.js b/detox/src/devices/android/ADB.js index d1bb1daa2d..4f43ca2ef8 100644 --- a/detox/src/devices/android/ADB.js +++ b/detox/src/devices/android/ADB.js @@ -12,7 +12,7 @@ class ADB { async devices() { const output = (await this.adbCmd('', 'devices')).stdout; - return await this.parseAdbDevicesConsoleOutput(output); + return this.parseAdbDevicesConsoleOutput(output); } async unlockScreen(deviceId) { @@ -32,7 +32,7 @@ class ADB { async _getPowerStatus(deviceId) { const grep = pipeCommands.search.regexp; - const stdout = await this.shell(deviceId, `dumpsys power | ${grep('^[ ]*m[UW].*=')}`); + const stdout = await this.execOut(deviceId, `dumpsys power | ${grep('^[ ]*m[UW].*=')}`); return stdout .split('\n') @@ -89,7 +89,7 @@ class ADB { } async now(deviceId) { - return this.shell(deviceId, `date "+\\"%Y-%m-%d %T.000\\""`); + return this.execOut(deviceId, `date "+\\"%Y-%m-%d %T.000\\""`); } async install(deviceId, apkPath) { @@ -113,7 +113,7 @@ class ADB { const bundleIdRegex = pipeCommands.escape.inQuotedRegexp(bundleId) + '[ ]*$'; const grep = pipeCommands.search.regexp; - const processes = await this.shell(deviceId, `ps | ${grep(bundleIdRegex)}`).catch(() => ''); + const processes = await this.execOut(deviceId, `ps | ${grep(bundleIdRegex)}`).catch(() => ''); if (!processes) { return NaN; } @@ -121,12 +121,8 @@ class ADB { return parseInt(processes.split(' ').filter(Boolean)[1], 10); } - async shell(deviceId, cmd, options) { - return (await this.adbCmd(deviceId, `shell ${cmd}`, options)).stdout.trim(); - } - async getFileSize(deviceId, filename) { - const { stdout, stderr } = await this.adbCmd(deviceId, 'shell wc -c ' + filename).catch(e => e); + const { stdout, stderr } = await this.adbCmd(deviceId, 'exec-out wc -c ' + filename).catch(e => e); if (stderr.includes('No such file or directory')) { return -1; @@ -136,13 +132,13 @@ class ADB { } async isFileOpen(deviceId, filename) { - const openedByProcesses = await this.shell(deviceId, 'lsof ' + filename); + const openedByProcesses = await this.execOut(deviceId, 'lsof ' + filename); return openedByProcesses.length > 0; } async isBootComplete(deviceId) { try { - const bootComplete = await this.shell(deviceId, `getprop dev.bootcomplete`); + const bootComplete = await this.execOut(deviceId, `getprop dev.bootcomplete`); return (bootComplete === '1'); } catch (ex) { return false; @@ -150,12 +146,12 @@ class ADB { } async apiLevel(deviceId) { - const lvl = await this.shell(deviceId, `getprop ro.build.version.sdk`); + const lvl = await this.execOut(deviceId, `getprop ro.build.version.sdk`); return Number(lvl); } async screencap(deviceId, path) { - return this.adbCmd(deviceId, `shell screencap ${path}`); + await this.shell(deviceId, `screencap ${path}`); } /*** @@ -211,11 +207,38 @@ class ADB { } async pull(deviceId, src, dst = '') { - return this.adbCmd(deviceId, `pull "${src}" "${dst}"`); + await this.adbCmd(deviceId, `pull "${src}" "${dst}"`); } async rm(deviceId, path, force = false) { - return this.adbCmd(deviceId, `shell rm ${force ? '-f' : ''} "${path}"`); + await this.shell(deviceId, `rm ${force ? '-f' : ''} "${path}"`); + } + + async listInstrumentation(deviceId) { + return this.execOut(deviceId, 'pm list instrumentation'); + } + + async getInstrumentationRunner(deviceId, bundleId) { + const instrumentationRunners = await this.listInstrumentation(deviceId); + const instrumentationRunner = this._instrumentationRunnerForBundleId(instrumentationRunners, bundleId); + if (instrumentationRunner === 'undefined') { + throw new Error(`No instrumentation runner found on device ${deviceId} for package ${bundleId}`); + } + + return instrumentationRunner; + } + + _instrumentationRunnerForBundleId(instrumentationRunners, bundleId) { + const runnerForBundleRegEx = new RegExp(`^instrumentation:(.*) \\(target=${bundleId.replace(new RegExp('\\.', 'g'), "\\.")}\\)$`, 'gm'); + return _.get(runnerForBundleRegEx.exec(instrumentationRunners), [1], 'undefined'); + } + + async execOut(deviceId, cmd, options) { + return (await this.adbCmd(deviceId, `exec-out ${cmd}`, options)).stdout.trim(); + } + + async shell(deviceId, cmd, options) { + await this.adbCmd(deviceId, `shell ${cmd}`, options); } async adbCmd(deviceId, params, options) { @@ -224,7 +247,7 @@ class ADB { const retries = _.get(options, 'retries', 1); _.unset(options, 'retries'); - return await execWithRetriesAndLogs(cmd, options, undefined, retries); + return execWithRetriesAndLogs(cmd, options, undefined, retries); } /*** @@ -234,25 +257,6 @@ class ADB { const serial = deviceId ? ['-s', deviceId] : []; return spawnAndLog(this.adbBin, [...serial, ...params]); } - - async listInstrumentation(deviceId) { - return await this.shell(deviceId, 'pm list instrumentation'); - } - - instrumentationRunnerForBundleId(instrumentationRunners, bundleId) { - const runnerForBundleRegEx = new RegExp(`^instrumentation:(.*) \\(target=${bundleId.replace(new RegExp('\\.', 'g'), "\\.")}\\)$`, 'gm'); - return _.get(runnerForBundleRegEx.exec(instrumentationRunners), [1], 'undefined'); - } - - async getInstrumentationRunner(deviceId, bundleId) { - const instrumentationRunners = await this.listInstrumentation(deviceId); - const instrumentationRunner = this.instrumentationRunnerForBundleId(instrumentationRunners, bundleId); - if (instrumentationRunner === 'undefined') { - throw new Error(`No instrumentation runner found on device ${deviceId} for package ${bundleId}`); - } - - return instrumentationRunner; - } } module.exports = ADB; diff --git a/detox/src/devices/android/ADB.test.js b/detox/src/devices/android/ADB.test.js index 16c5da82cb..2ce97f592f 100644 --- a/detox/src/devices/android/ADB.test.js +++ b/detox/src/devices/android/ADB.test.js @@ -46,12 +46,14 @@ describe('ADB', () => { }); it(`pidof (success)`, async () => { - adb.shell = async () => `u0_a19 2199 1701 3554600 70264 0 0 s com.google.android.ext.services `; + jest.spyOn(adb, 'execOut').mockImplementation(async () => + `u0_a19 2199 1701 3554600 70264 0 0 s com.google.android.ext.services `); + expect(await adb.pidof('', 'com.google.android.ext.services')).toBe(2199); }); it(`pidof (failure)`, async () => { - adb.shell = async () => ``; + jest.spyOn(adb, 'execOut').mockImplementation(async () => ''); expect(await adb.pidof('', 'com.google.android.ext.services')).toBe(NaN); }); @@ -59,7 +61,8 @@ describe('ADB', () => { const deviceId = 'mockEmulator'; async function unlockScreenWithPowerStatus(mWakefulness, mUserActivityTimeoutOverrideFromWindowManager) { - adb.shell = jest.fn().mockReturnValue(` + jest.spyOn(adb, 'shell'); + jest.spyOn(adb, 'execOut').mockImplementation(async () => ` mWakefulness=${mWakefulness} mWakefulnessChanging=false mWakeLockSummary=0x0 @@ -87,10 +90,10 @@ describe('ADB', () => { beforeEach(async () => unlockScreenWithPowerStatus('Asleep', '10000')); it('should press power button first', () => - expect(adb.shell.mock.calls[1]).toEqual([deviceId, 'input keyevent KEYCODE_POWER'])); + expect(adb.shell.mock.calls[0]).toEqual([deviceId, 'input keyevent KEYCODE_POWER'])); it('should press menu afterwards', () => - expect(adb.shell.mock.calls[2]).toEqual([deviceId, 'input keyevent KEYCODE_MENU'])); + expect(adb.shell.mock.calls[1]).toEqual([deviceId, 'input keyevent KEYCODE_MENU'])); }); describe('when unlocking an awake but locked device', function() { @@ -116,11 +119,11 @@ describe('ADB', () => { it(`listInstrumentation passes the right deviceId`, async () => { const deviceId = 'aDeviceId'; - const spyShell = jest.spyOn(adb, 'shell'); + jest.spyOn(adb, 'execOut'); await adb.listInstrumentation(deviceId); - expect(spyShell).toBeCalledWith(deviceId, expect.any(String)); + expect(adb.execOut).toBeCalledWith(deviceId, 'pm list instrumentation'); }); it(`Parse 'adb device' output`, async () => { @@ -144,18 +147,7 @@ describe('ADB', () => { expect(actual).toEqual(parsedDevices); }); - it(`getInstrumentationRunner passes the right deviceId`, async () => { - const deviceId = 'aDeviceId'; - const spyRunnerForBundle = jest.spyOn(adb, 'instrumentationRunnerForBundleId'); - spyRunnerForBundle.mockReturnValue(''); - const spyShell = jest.spyOn(adb, 'shell'); - - await adb.getInstrumentationRunner(deviceId, 'com.whatever.package'); - - expect(spyShell).toBeCalledWith(deviceId, expect.any(String)); - }); - - it(`instrumentationRunnerForBundleId parses the correct runner for the package`, async () => { + it(`getInstrumentationRunner parses the correct runner for the package`, async () => { const expectedRunner = "com.example.android.apis/.app.LocalSampleInstrumentation"; const expectedPackage = "com.example.android.apis"; const instrumentationRunnersShellOutput = @@ -164,8 +156,11 @@ describe('ADB', () => { `instrumentation:${expectedRunner} (target=${expectedPackage})\n` + "instrumentation:org.chromium.webview_shell/.WebViewLayoutTestRunner (target=org.chromium.webview_shell)\n"; - const result = await adb.instrumentationRunnerForBundleId(instrumentationRunnersShellOutput, expectedPackage); + jest.spyOn(adb, 'execOut').mockImplementation(async () => instrumentationRunnersShellOutput); + + const result = await adb.getInstrumentationRunner('aDeviceId', expectedPackage); + expect(adb.execOut).toBeCalledWith('aDeviceId', 'pm list instrumentation'); expect(result).toEqual(expectedRunner); }); }); diff --git a/detox/src/devices/android/EmulatorTelnet.js b/detox/src/devices/android/EmulatorTelnet.js index 94765eb688..9b44eced42 100644 --- a/detox/src/devices/android/EmulatorTelnet.js +++ b/detox/src/devices/android/EmulatorTelnet.js @@ -36,18 +36,18 @@ class EmulatorTelnet { this.connection.shell((error, stream) => { stream.write(`${command}\n`); stream.on('data', (data) => { - const result = data.toString(); - if (result.includes('\n')) { - resolve(result); + const result = data.toString(); + if (result.includes('\n')) { + resolve(result); + } } - } ); }); }); } async avdName() { - return await this.exec('avd name'); + return this.exec('avd name'); } async kill() { @@ -61,7 +61,7 @@ class EmulatorTelnet { } async rotate() { - return await this.shell('rotate'); + return this.shell('rotate'); } } diff --git a/detox/src/devices/drivers/AndroidDriver.js b/detox/src/devices/drivers/AndroidDriver.js index 9a7fd05318..8b58707f3c 100644 --- a/detox/src/devices/drivers/AndroidDriver.js +++ b/detox/src/devices/drivers/AndroidDriver.js @@ -98,7 +98,7 @@ class AndroidDriver extends DeviceDriverBase { const pid = await this._queryPID(deviceId, bundleId); if (isNaN(pid)) { - log.warn(await this.adb.shell(deviceId, 'ps')); + log.warn(await this.adb.execOut(deviceId, 'ps')); throw new DetoxRuntimeError({ message: `Failed to find PID of the launched bundle: ${bundleId}`,