Skip to content

Commit

Permalink
fix: stop parsing unreachable webview names (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaumu3 authored Jan 22, 2024
1 parent 2294ae9 commit bd914de
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 27 deletions.
27 changes: 19 additions & 8 deletions lib/commands/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,16 @@ export interface WebviewsMapping {
webview: string;
/**
* Webview information as it is retrieved by `/json/version` CDP endpoint
*
* This value becomes `undefined` when the retrieval failed.
*/
info?: StringRecord;
/**
* Webview pages list as it is retrieved by `/json/list` CDP endpoint
*
* This value becomes `undefined` when the retrieval failed.
*/
pages: StringRecord[];
pages?: StringRecord[];
/**
* An actual webview name for switching context.
*
Expand Down Expand Up @@ -1081,7 +1085,7 @@ export type SwipeAction = [
NonReleaseTouchAction,
NonReleaseTouchAction,
NonReleaseTouchAction,
ReleaseTouchAction
ReleaseTouchAction,
];

export type TouchDragAction = [NonReleaseTouchAction, NonReleaseTouchAction, ReleaseTouchAction];
Expand All @@ -1096,16 +1100,23 @@ export interface LockOpts {

export interface DeviceidleOpts {
/** The action name to execute */
action: 'whitelistAdd'|'whitelistRemove';
action: 'whitelistAdd' | 'whitelistRemove';
/** Either a single package or multiple packages to add or remove from the idle whitelist */
packages?: string|string[];
packages?: string | string[];
}

export interface SendTrimMemoryOpts {
/** The package name to send the `trimMemory` event to */
pkg: string;
/** The actual memory trim level to be sent */
level: 'COMPLETE' | 'MODERATE' | 'BACKGROUND' | 'UI_HIDDEN' | 'RUNNING_CRITICAL' | 'RUNNING_LOW' | 'RUNNING_MODERATE';
level:
| 'COMPLETE'
| 'MODERATE'
| 'BACKGROUND'
| 'UI_HIDDEN'
| 'RUNNING_CRITICAL'
| 'RUNNING_LOW'
| 'RUNNING_MODERATE';
}

export interface SetUiModeOpts {
Expand Down Expand Up @@ -1134,14 +1145,14 @@ export interface GetUiModeOpts {
export interface SmsListResultItem {
id: string;
address: string;
person: string|null;
person: string | null;
date: string;
read: string;
status: string;
type: string;
subject: string|null;
subject: string | null;
body: string;
serviceCenter: string|null;
serviceCenter: string | null;
}

export interface SmsListResult {
Expand Down
18 changes: 12 additions & 6 deletions lib/helpers/webview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,18 @@ const WebviewHelpers: WebviewHelpers = {

const result: string[] = [];
for (const {webview, pages, proc, webviewName} of webviewsMapping) {
if (ensureWebviewsHavePages && pages?.length === 0) {
logger.info(
`Skipping the webview '${webview}' at '${proc}' ` +
`since it has reported having zero pages`,
);
continue;
if (ensureWebviewsHavePages) {
if (_.isUndefined(pages)) {
logger.info(`Skipping the webview '${webview}' at '${proc}' since it is unreachable`);
continue;
}
if (!pages?.length) {
logger.info(
`Skipping the webview '${webview}' at '${proc}' ` +
`since it has reported having zero pages`,
);
continue;
}
}
if (webviewName) {
result.push(webviewName);
Expand Down
20 changes: 11 additions & 9 deletions test/unit/commands/context-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,30 @@ describe('Context', function () {
});
describe('setContext', function () {
beforeEach(function () {
sandbox
.stub(webviewHelpers, 'getWebViewsMapping')
.returns([{webviewName: 'DEFAULT'}, {webviewName: 'WV'}, {webviewName: 'ANOTHER'}]);
sandbox.stub(webviewHelpers, 'getWebViewsMapping').returns([
{webviewName: 'DEFAULT', pages: ['PAGE']},
{webviewName: 'WV', pages: ['PAGE']},
{webviewName: 'ANOTHER', pages: ['PAGE']},
]);
sandbox.stub(driver, 'switchContext');
});
it('should switch to default context if name is null', async function () {
sandbox.stub(driver, 'defaultContextName').returns('DEFAULT');
await driver.setContext(null);
driver.switchContext.calledWithExactly('DEFAULT', [
{webviewName: 'DEFAULT'},
{webviewName: 'WV'},
{webviewName: 'ANOTHER'},
{webviewName: 'DEFAULT', pages: ['PAGE']},
{webviewName: 'WV', pages: ['PAGE']},
{webviewName: 'ANOTHER', pages: ['PAGE']},
]).should.be.true;
driver.curContext.should.be.equal('DEFAULT');
});
it('should switch to default web view if name is WEBVIEW', async function () {
sandbox.stub(driver, 'defaultWebviewName').returns('WV');
await driver.setContext(WEBVIEW_WIN);
driver.switchContext.calledWithExactly('WV', [
{webviewName: 'DEFAULT'},
{webviewName: 'WV'},
{webviewName: 'ANOTHER'},
{webviewName: 'DEFAULT', pages: ['PAGE']},
{webviewName: 'WV', pages: ['PAGE']},
{webviewName: 'ANOTHER', pages: ['PAGE']},
]).should.be.true;
driver.curContext.should.be.equal('WV');
});
Expand Down
72 changes: 68 additions & 4 deletions test/unit/webview-helper-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ describe('Webview Helpers', function () {
const webviewsMapping = await helpers.getWebViewsMapping(adb, {
androidDeviceSocket: 'webview_devtools_remote_123',
});
webViews = helpers.parseWebviewNames(webviewsMapping);
webViews = helpers.parseWebviewNames(webviewsMapping, {
ensureWebviewsHavePages: false,
});
});

it('then the unix sockets are queried', function () {
Expand Down Expand Up @@ -73,7 +75,9 @@ describe('Webview Helpers', function () {
const webviewsMapping = await helpers.getWebViewsMapping(adb, {
androidDeviceSocket: 'chrome_devtools_remote',
});
webViews = helpers.parseWebviewNames(webviewsMapping);
webViews = helpers.parseWebviewNames(webviewsMapping, {
ensureWebviewsHavePages: false,
});
});

it('then the unix sockets are queried', function () {
Expand Down Expand Up @@ -114,6 +118,62 @@ describe('Webview Helpers', function () {
});
});

describe('and page existence is ensured', function () {
let webViews;

beforeEach(async function () {
sandbox.stub(adb, 'shell').callsFake(function () {
return (
'Num RefCount Protocol Flags Type St Inode Path\n' +
'0000000000000000: 00000002 00000000 00010000 0001 01 2818 /dev/socket/ss_conn_daemon\n' +
'0000000000000000: 00000002 00000000 00010000 0001 01 9231 @mcdaemon\n' +
'0000000000000000: 00000002 00000000 00010000 0001 01 245445 @webview_devtools_remote_123\n' +
'0000000000000000: 00000002 00000000 00010000 0001 01 2826 /dev/socket/installd\n'
);
});
});

describe('and webviews are unreachable', function () {
beforeEach(async function () {
const webviewsMapping = await helpers.getWebViewsMapping(adb, {
androidDeviceSocket: 'webview_devtools_remote_123',
});
webviewsMapping.length.should.equal(1);
webviewsMapping[0].should.not.have.key('pages');
webViews = helpers.parseWebviewNames(webviewsMapping);
});

it('then the unix sockets are queried', function () {
adb.shell.calledOnce.should.be.true;
adb.shell.getCall(0).args[0].should.deep.equal(['cat', '/proc/net/unix']);
});

it('then no webviews are returned', function () {
webViews.length.should.equal(0);
});
});

describe('and webviews have no pages', function () {
beforeEach(async function () {
const webviewsMapping = await helpers.getWebViewsMapping(adb, {
androidDeviceSocket: 'webview_devtools_remote_123',
});
webviewsMapping.length.should.equal(1);
webviewsMapping[0].pages = [];
webViews = helpers.parseWebviewNames(webviewsMapping);
});

it('then the unix sockets are queried', function () {
adb.shell.calledOnce.should.be.true;
adb.shell.getCall(0).args[0].should.deep.equal(['cat', '/proc/net/unix']);
});

it('then no webviews are returned', function () {
webViews.length.should.equal(0);
});
});
});

describe('and crosswalk webviews exist', function () {
let webViews;

Expand All @@ -132,7 +192,9 @@ describe('Webview Helpers', function () {
describe('and the device socket is not specified', function () {
beforeEach(async function () {
const webviewsMapping = await helpers.getWebViewsMapping(adb);
webViews = helpers.parseWebviewNames(webviewsMapping);
webViews = helpers.parseWebviewNames(webviewsMapping, {
ensureWebviewsHavePages: false,
});
});

it('then the unix sockets are queried', function () {
Expand All @@ -151,7 +213,9 @@ describe('Webview Helpers', function () {
const webviewsMapping = await helpers.getWebViewsMapping(adb, {
androidDeviceSocket: 'com.application.myapp_devtools_remote',
});
webViews = helpers.parseWebviewNames(webviewsMapping);
webViews = helpers.parseWebviewNames(webviewsMapping, {
ensureWebviewsHavePages: false,
});
});

it('then the unix sockets are queried', function () {
Expand Down

0 comments on commit bd914de

Please sign in to comment.