From 98afcc975bdec53d5c7e3e017133108bfdaa4bbc Mon Sep 17 00:00:00 2001 From: Danil Somsikov Date: Thu, 12 Oct 2023 12:19:58 +0200 Subject: [PATCH] Fix keyboard throttler injection and unrestricted logging. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: 1467464 Change-Id: I4927663fb4c9077957f7deb8686ee3c8b20b3bcc Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/4931036 Commit-Queue: Danil Somsikov Commit-Queue: Simon Zünd Auto-Submit: Danil Somsikov Reviewed-by: Simon Zünd --- front_end/ui/visual_logging/LoggingDriver.ts | 7 +++--- .../ui/visual_logging/LoggingDriver_test.ts | 23 ++++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/front_end/ui/visual_logging/LoggingDriver.ts b/front_end/ui/visual_logging/LoggingDriver.ts index 5bfe7ba6a2..c8a67ee857 100644 --- a/front_end/ui/visual_logging/LoggingDriver.ts +++ b/front_end/ui/visual_logging/LoggingDriver.ts @@ -23,7 +23,7 @@ export async function startLogging( options?: {domProcessingThrottler?: Common.Throttler.Throttler, keyboardLogThrottler?: Common.Throttler.Throttler}): Promise { domProcessingThrottler = options?.domProcessingThrottler || new Common.Throttler.Throttler(PROCESS_DOM_INTERVAL); - keyboardLogThrottler = options?.domProcessingThrottler || new Common.Throttler.Throttler(KEYBOARD_LOG_INTERVAL); + keyboardLogThrottler = options?.keyboardLogThrottler || new Common.Throttler.Throttler(KEYBOARD_LOG_INTERVAL); if (['interactive', 'complete'].includes(document.readyState)) { await processDom(); } @@ -71,9 +71,10 @@ async function processDom(): Promise { if (loggingState.config.track?.has('change')) { element.addEventListener('change', logChange, {capture: true}); } - const trackKeyDown = loggingState.config.track?.get('keydown'); + const trackKeyDown = loggingState.config.track?.has('keydown'); + const codes = loggingState.config.track?.get('keydown')?.split(',') || []; if (trackKeyDown) { - element.addEventListener('keydown', logKeyDown(trackKeyDown.split(','), keyboardLogThrottler), {capture: true}); + element.addEventListener('keydown', logKeyDown(codes, keyboardLogThrottler), {capture: true}); } loggingState.processed = true; } diff --git a/test/unittests/front_end/ui/visual_logging/LoggingDriver_test.ts b/test/unittests/front_end/ui/visual_logging/LoggingDriver_test.ts index d7bccfc121..fef5aa402b 100644 --- a/test/unittests/front_end/ui/visual_logging/LoggingDriver_test.ts +++ b/test/unittests/front_end/ui/visual_logging/LoggingDriver_test.ts @@ -30,7 +30,7 @@ describe('LoggingDriver', () => { parent.style.height = '300px'; const element = document.createElement('div') as HTMLElement; element.id = 'element'; - element.setAttribute('jslog', 'TreeItem; context:42; track: click'); + element.setAttribute('jslog', 'TreeItem; context:42; track: click, keydown'); element.style.width = '300px'; element.style.height = '300px'; parent.appendChild(element); @@ -140,4 +140,25 @@ describe('LoggingDriver', () => { assert.isFalse(recordClick.called); }); + + it('logs keydown', async () => { + const domProcessingThrottler = new Common.Throttler.Throttler(100000); + const keyboardLogThrottler = new Common.Throttler.Throttler(100000); + addLoggableElements(); + await VisualLogging.startLogging({domProcessingThrottler, keyboardLogThrottler}); + const recordKeyDown = sinon.stub( + Host.InspectorFrontendHost.InspectorFrontendHostInstance, + 'recordKeyDown', + ); + + const element = document.getElementById('element') as HTMLElement; + element.dispatchEvent(new KeyboardEvent('keydown', {'key': 'a'})); + element.dispatchEvent(new KeyboardEvent('keydown', {'key': 'b'})); + await new Promise(resolve => setTimeout(resolve, 0)); + assert.exists(keyboardLogThrottler.process); + assert.isFalse(recordKeyDown.called); + + await keyboardLogThrottler.process?.(); + assert.isTrue(recordKeyDown.calledOnce); + }); });