From cfe5795d1aa4ab1db9a7a2578459424757551315 Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Thu, 18 Jan 2024 16:36:09 +0900 Subject: [PATCH 01/10] refactor: prettier types.ts --- lib/commands/types.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/commands/types.ts b/lib/commands/types.ts index 3c80ac8f..519f5039 100644 --- a/lib/commands/types.ts +++ b/lib/commands/types.ts @@ -1081,7 +1081,7 @@ export type SwipeAction = [ NonReleaseTouchAction, NonReleaseTouchAction, NonReleaseTouchAction, - ReleaseTouchAction + ReleaseTouchAction, ]; export type TouchDragAction = [NonReleaseTouchAction, NonReleaseTouchAction, ReleaseTouchAction]; @@ -1096,16 +1096,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 { @@ -1134,14 +1141,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 { From 02447e077b6891a967e95f4dff8e5748a4b86be4 Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Thu, 18 Jan 2024 16:36:22 +0900 Subject: [PATCH 02/10] fix: webview pages can be undefined --- lib/commands/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/commands/types.ts b/lib/commands/types.ts index 519f5039..b0cb0e3a 100644 --- a/lib/commands/types.ts +++ b/lib/commands/types.ts @@ -191,7 +191,7 @@ export interface WebviewsMapping { /** * Webview pages list as it is retrieved by `/json/list` CDP endpoint */ - pages: StringRecord[]; + pages?: StringRecord[]; /** * An actual webview name for switching context. * From 681aef83db91e01ff964e1bef48eb320c10fbcb8 Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Thu, 18 Jan 2024 16:20:59 +0900 Subject: [PATCH 03/10] refactor: prettier webview.ts --- lib/helpers/webview.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/helpers/webview.ts b/lib/helpers/webview.ts index c81f77bc..1cd4d3a1 100644 --- a/lib/helpers/webview.ts +++ b/lib/helpers/webview.ts @@ -323,8 +323,8 @@ async function collectWebviewsDetails( for (const item of webviewsMapping) { detailCollectors.push( (async () => { - let port: number|undefined; - let host: string|undefined; + let port: number | undefined; + let host: string | undefined; try { [host, port] = await allocateDevtoolsChannel(adb, item.proc, webviewDevtoolsPort); if (enableWebviewDetailsCollection) { From f491f1ffa5e5b0f6f5987b46df349ff10de068e4 Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Thu, 18 Jan 2024 16:22:27 +0900 Subject: [PATCH 04/10] fix: skip unreachable webview --- lib/helpers/webview.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/helpers/webview.ts b/lib/helpers/webview.ts index 1cd4d3a1..332bcb76 100644 --- a/lib/helpers/webview.ts +++ b/lib/helpers/webview.ts @@ -396,12 +396,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 === 0) { + logger.info( + `Skipping the webview '${webview}' at '${proc}' ` + + `since it has reported having zero pages`, + ); + continue; + } } if (webviewName) { result.push(webviewName); From 93e41145655eba9fe47109d640c9607482e27af4 Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Thu, 18 Jan 2024 17:01:03 +0900 Subject: [PATCH 05/10] test: add pages property to stubbed mapping --- test/unit/commands/context-specs.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/test/unit/commands/context-specs.js b/test/unit/commands/context-specs.js index 252b1142..45453f2a 100644 --- a/test/unit/commands/context-specs.js +++ b/test/unit/commands/context-specs.js @@ -69,18 +69,20 @@ 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'); }); @@ -88,9 +90,9 @@ describe('Context', 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'); }); From 62b5e9783bfd2d60b1c06fcf60292f66eb9c3bb8 Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Thu, 18 Jan 2024 17:20:36 +0900 Subject: [PATCH 06/10] test: stop ensuring pages for the existing tests --- test/unit/webview-helper-specs.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/test/unit/webview-helper-specs.js b/test/unit/webview-helper-specs.js index 3c1ad6ea..e7bba581 100644 --- a/test/unit/webview-helper-specs.js +++ b/test/unit/webview-helper-specs.js @@ -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 () { @@ -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 () { @@ -132,7 +136,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 () { @@ -151,7 +157,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 () { From d3a3c35143bff9e0b3545dfb4195647a9bcc618a Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Thu, 18 Jan 2024 17:48:22 +0900 Subject: [PATCH 07/10] test: add socket unreachable case --- test/unit/webview-helper-specs.js | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/unit/webview-helper-specs.js b/test/unit/webview-helper-specs.js index e7bba581..134c496e 100644 --- a/test/unit/webview-helper-specs.js +++ b/test/unit/webview-helper-specs.js @@ -118,6 +118,42 @@ 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 crosswalk webviews exist', function () { let webViews; From 0b7f64f1d6304ebe559aaf9afc4bc31abaef24e3 Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Thu, 18 Jan 2024 17:48:55 +0900 Subject: [PATCH 08/10] test: add no page case --- test/unit/webview-helper-specs.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/unit/webview-helper-specs.js b/test/unit/webview-helper-specs.js index 134c496e..279e270d 100644 --- a/test/unit/webview-helper-specs.js +++ b/test/unit/webview-helper-specs.js @@ -152,6 +152,26 @@ describe('Webview Helpers', 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 () { From d0057995693acb4bd35aa124890593e06940f10d Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Thu, 18 Jan 2024 17:56:10 +0900 Subject: [PATCH 09/10] docs: add comment for undefined case --- lib/commands/types.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/commands/types.ts b/lib/commands/types.ts index b0cb0e3a..4245cdad 100644 --- a/lib/commands/types.ts +++ b/lib/commands/types.ts @@ -186,10 +186,14 @@ 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[]; /** From 5e6b7ebe584eb0eb088cfd8ded6dda80f9399d24 Mon Sep 17 00:00:00 2001 From: yaumu3 Date: Mon, 22 Jan 2024 17:40:47 +0900 Subject: [PATCH 10/10] fix: no pages reported case for possible null case --- lib/helpers/webview.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/helpers/webview.ts b/lib/helpers/webview.ts index 332bcb76..f77e5119 100644 --- a/lib/helpers/webview.ts +++ b/lib/helpers/webview.ts @@ -401,7 +401,7 @@ const WebviewHelpers: WebviewHelpers = { logger.info(`Skipping the webview '${webview}' at '${proc}' since it is unreachable`); continue; } - if (pages.length === 0) { + if (!pages?.length) { logger.info( `Skipping the webview '${webview}' at '${proc}' ` + `since it has reported having zero pages`,