mirror of
https://github.com/stablyai/orca.git
synced 2026-11-15 10:27:33 +00:00
test(browser): name the foreground window in Windows holder replies
This commit is contained in:
@@ -86,12 +86,11 @@ for (const [name, run] of scenarios) {
|
||||
await holder.take()
|
||||
await run(orcaPage, async (pageId) => {
|
||||
// Why: a guest press that activates Orca's window pulls it over the app the user is in.
|
||||
expect(await holder.foreground()).toEqual({ holder: true, pid: expect.any(Number) })
|
||||
expect(await holder.foreground()).toMatchObject({ holder: true })
|
||||
// The same check must see Orca once it really is activated, or the one above proves nothing.
|
||||
const orcaPid = await electronApp.evaluate(() => process.pid)
|
||||
await ownedWindow.evaluate((window) => window.focus())
|
||||
await expect
|
||||
.poll(() => holder.foreground())
|
||||
.toEqual({ holder: false, pid: electronApp.process().pid })
|
||||
await expect.poll(() => holder.foreground()).toMatchObject({ holder: false, pid: orcaPid })
|
||||
await realKeystrokeReachesTerminal(orcaPage, electronApp, holder.press)(pageId)
|
||||
})
|
||||
} finally {
|
||||
|
||||
@@ -5,6 +5,7 @@ Add-Type -ReferencedAssemblies System.Windows.Forms -TypeDefinition @'
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Windows.Forms;
|
||||
|
||||
@@ -15,6 +16,7 @@ public static class OrcaForegroundHolder {
|
||||
[DllImport("user32.dll")] static extern bool BringWindowToTop(IntPtr hwnd);
|
||||
[DllImport("user32.dll")] static extern bool AllowSetForegroundWindow(int processId);
|
||||
[DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint processId);
|
||||
[DllImport("user32.dll", CharSet = CharSet.Unicode)] static extern int GetWindowText(IntPtr hwnd, StringBuilder text, int count);
|
||||
[DllImport("user32.dll")] static extern bool AttachThreadInput(uint attach, uint attachTo, bool enable);
|
||||
[DllImport("kernel32.dll")] static extern uint GetCurrentThreadId();
|
||||
[DllImport("user32.dll")] static extern short VkKeyScan(char ch);
|
||||
@@ -76,7 +78,9 @@ public static class OrcaForegroundHolder {
|
||||
IntPtr foreground = GetForegroundWindow();
|
||||
uint processId;
|
||||
GetWindowThreadProcessId(foreground, out processId);
|
||||
return (foreground == form.Handle ? "holder " : "other ") + processId;
|
||||
StringBuilder title = new StringBuilder(256);
|
||||
GetWindowText(foreground, title, title.Capacity);
|
||||
return (foreground == form.Handle ? "holder " : "other ") + processId + " " + title;
|
||||
}
|
||||
|
||||
static bool Take() {
|
||||
|
||||
@@ -12,8 +12,8 @@ const REPLY_TIMEOUT_MS = 15_000
|
||||
export type WindowsForegroundHolder = {
|
||||
/** Activates the holder and lifts the foreground lock, so an Orca activation attempt would succeed. */
|
||||
take: () => Promise<void>
|
||||
/** Which window has the real foreground: the holder, or another process by pid. */
|
||||
foreground: () => Promise<{ holder: boolean; pid: number }>
|
||||
/** Which window has the real foreground: the holder, or another process's window. */
|
||||
foreground: () => Promise<{ holder: boolean; pid: number; title: string }>
|
||||
/** Presses one key through the OS input queue, so it reaches whichever window is in front. */
|
||||
press: (key: string) => Promise<void>
|
||||
close: () => Promise<void>
|
||||
@@ -96,8 +96,8 @@ export async function startWindowsForegroundHolder(): Promise<WindowsForegroundH
|
||||
await send('take')
|
||||
},
|
||||
foreground: async () => {
|
||||
const [owner, pid] = (await send('foreground')).split(' ')
|
||||
return { holder: owner === 'holder', pid: Number(pid) }
|
||||
const [owner, pid, ...title] = (await send('foreground')).split(' ')
|
||||
return { holder: owner === 'holder', pid: Number(pid), title: title.join(' ') }
|
||||
},
|
||||
press: async (key) => {
|
||||
await send(`key ${key}`)
|
||||
|
||||
Reference in New Issue
Block a user