test(dictation): cover dictation into a focused browser page

This commit is contained in:
Kelvin Amoaba
2026-11-09 08:09:42 +00:00
parent 54d3939bb6
commit ed484d48d5
3 changed files with 91 additions and 1 deletions
@@ -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')
})
})
@@ -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()
@@ -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()