From ed484d48d57f9194fd4ef5e164f35a679be2f9fd Mon Sep 17 00:00:00 2001 From: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com> Date: Mon, 9 Nov 2026 06:16:26 +0000 Subject: [PATCH] test(dictation): cover dictation into a focused browser page --- .../browser-guest-dictation-shortcut.test.ts | 63 +++++++++++++++++++ ...ainWindow-terminal-focus-shortcuts.test.ts | 8 +++ .../dictation-insertion-target.test.ts | 21 ++++++- 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/main/browser/guest-lifecycle/input/shortcuts/browser-guest-dictation-shortcut.test.ts diff --git a/src/main/browser/guest-lifecycle/input/shortcuts/browser-guest-dictation-shortcut.test.ts b/src/main/browser/guest-lifecycle/input/shortcuts/browser-guest-dictation-shortcut.test.ts new file mode 100644 index 00000000000..a90515addec --- /dev/null +++ b/src/main/browser/guest-lifecycle/input/shortcuts/browser-guest-dictation-shortcut.test.ts @@ -0,0 +1,63 @@ +import { describe, expect, it, vi } from 'vitest' + +vi.mock('electron', async () => { + const { EventEmitter } = await import('node:events') + return { + BrowserWindow: class { + webContents = new (class extends EventEmitter { + send = vi.fn() + })() + }, + screen: { getCursorScreenPoint: vi.fn(() => ({ x: 0, y: 0 })) }, + webContents: { fromId: vi.fn() } + } +}) + +import { BrowserWindow } from 'electron' +import { setupGuestShortcutForwarding } from './browser-guest-shortcut-forwarding' + +function pressDictationInPage(isComposing: boolean) { + const guest = new BrowserWindow().webContents + const renderer = new BrowserWindow().webContents + const cleanup = setupGuestShortcutForwarding({ + browserTabId: 'tab-1', + guest, + resolveRenderer: () => renderer, + shouldForwardDictationShortcut: () => true, + getKeybindings: () => ({}) + }) + const isMac = process.platform === 'darwin' + const event: Electron.Event = { preventDefault: vi.fn(), defaultPrevented: false } + const input: Electron.Input = { + type: 'keyDown', + code: 'KeyE', + key: 'e', + meta: isMac, + control: !isMac, + alt: false, + shift: false, + isAutoRepeat: false, + isComposing, + location: 0, + modifiers: [] + } + guest.emit('before-input-event', event, input) + cleanup() + return { prevented: vi.mocked(event.preventDefault).mock.calls.length > 0, renderer } +} + +describe('dictation shortcut from a focused browser page', () => { + it('hands the chord to Orca so dictation can target the page', () => { + const { prevented, renderer } = pressDictationInPage(false) + + expect(prevented).toBe(true) + expect(renderer.send).toHaveBeenCalledWith('ui:dictationKeyDown') + }) + + it('leaves the chord to the page while an IME composition owns it', () => { + const { prevented, renderer } = pressDictationInPage(true) + + expect(prevented).toBe(false) + expect(renderer.send).not.toHaveBeenCalledWith('ui:dictationKeyDown') + }) +}) diff --git a/src/main/window/createMainWindow-terminal-focus-shortcuts.test.ts b/src/main/window/createMainWindow-terminal-focus-shortcuts.test.ts index cc721ef93d1..a0ac74981ba 100644 --- a/src/main/window/createMainWindow-terminal-focus-shortcuts.test.ts +++ b/src/main/window/createMainWindow-terminal-focus-shortcuts.test.ts @@ -15,6 +15,7 @@ vi.mock('../browser/guest-lifecycle/manager/browser-manager', async () => ) import { createMainWindow } from './createMainWindow' +import { browserManager } from '../browser/guest-lifecycle/manager/browser-manager' import { ipcMain } from 'electron' import { resetExpectedTeardownStateForTest } from '../crash-reporting/expected-teardown-state' import { browserWindowMock, resetMainWindowMocks } from './createMainWindow-test-harness' @@ -69,6 +70,10 @@ describe('createMainWindow', () => { getSettings: () => ({ windowBackgroundBlur: false, voice }) as never, updateUI: vi.fn() } as never) + // A focused browser page forwards only keydown, so it hands the chord over unless Hold needs the release. + const pageForwardsDictation = vi.mocked(browserManager.setDictationShortcutForwardingPredicate) + .mock.lastCall?.[0] + expect(pageForwardsDictation?.()).toBe(false) const isDarwin = process.platform === 'darwin' const dictationInput = { @@ -91,6 +96,7 @@ describe('createMainWindow', () => { // #18691: with no model the renderer still needs the chord to show its setup toast. voice.enabled = true + expect(pageForwardsDictation?.()).toBe(true) const noModelPreventDefault = vi.fn() windowHandlers['before-input-event']({ preventDefault: noModelPreventDefault }, dictationInput) expect(noModelPreventDefault).toHaveBeenCalledTimes(1) @@ -99,6 +105,7 @@ describe('createMainWindow', () => { webContents.send.mockClear() voice.sttModel = 'test-model' voice.dictationMode = 'hold' + expect(pageForwardsDictation?.()).toBe(false) const holdPreventDefault = vi.fn() windowHandlers['before-input-event']( { preventDefault: holdPreventDefault } as never, @@ -108,6 +115,7 @@ describe('createMainWindow', () => { expect(webContents.send).not.toHaveBeenCalledWith('ui:dictationKeyDown') voice.dictationMode = 'auto' + expect(pageForwardsDictation?.()).toBe(true) const autoPreventDefault = vi.fn() windowHandlers['before-input-event']({ preventDefault: autoPreventDefault }, dictationInput) expect(autoPreventDefault).not.toHaveBeenCalled() diff --git a/src/renderer/src/components/dictation/dictation-insertion-target.test.ts b/src/renderer/src/components/dictation/dictation-insertion-target.test.ts index d7db7df7348..1ec6d27e188 100644 --- a/src/renderer/src/components/dictation/dictation-insertion-target.test.ts +++ b/src/renderer/src/components/dictation/dictation-insertion-target.test.ts @@ -6,7 +6,7 @@ import { TEXT_CONTROL_PASTE_DIRECT_MAX_BYTES, TEXT_CONTROL_PASTE_MAX_BYTES } from '@/lib/text-control-paste' -import { insertText } from './dictation-insertion-target' +import { captureInsertionTarget, insertText } from './dictation-insertion-target' type DocumentWithExecCommand = Document & { execCommand?: (commandId: string, showUi?: boolean, value?: string) => boolean @@ -18,6 +18,7 @@ afterEach(() => { document.body.replaceChildren() vi.useRealTimers() vi.restoreAllMocks() + vi.unstubAllGlobals() Object.defineProperty(document, 'execCommand', { configurable: true, value: originalExecCommand @@ -45,6 +46,24 @@ function installExecCommandMock( } describe('dictation insertion target', () => { + it('sends text dictated into a focused browser page to that page', async () => { + const insertDictationText = vi.fn(async () => true) + vi.stubGlobal('api', { browser: { insertDictationText } }) + const viewport = document.createElement('div') + viewport.dataset.browserPageViewportId = 'page-1' + const guest = document.createElement('webview') + guest.tabIndex = 0 + viewport.appendChild(guest) + document.body.appendChild(viewport) + guest.focus() + + const target = captureInsertionTarget() + expect(target).toEqual({ kind: 'browser', browserPageId: 'page-1' }) + await expect(insertText('hello', target!)).resolves.toBe(true) + + expect(insertDictationText).toHaveBeenCalledWith({ browserPageId: 'page-1', text: 'hello' }) + }) + it('cancels chunked contenteditable dictation when the target disconnects', async () => { vi.useFakeTimers() const editor = appendContentEditable()