package main import "testing" func shellFixtureWindow(pid, hwnd int64, class string) obj { return obj{"pid": pid, "window_id": hwnd, "class_name": class, "is_on_screen": true, "app_name": "explorer.exe", "title": ""} } func TestUntitledShellDiscoveryKeepsAppAndPIDScope(t *testing.T) { native := []obj{shellFixtureWindow(1, 10, "Shell_TrayWnd"), shellFixtureWindow(2, 20, "NotifyIconOverflowWindow")} for _, query := range []obj{{"pid": 1}, {"app": "Explorer", "pid": 1}, {"app": "explorer.exe", "pid": 1}} { r := supplementShellWindows(obj{"structuredContent": obj{"windows": []any{}}}, query, native) windows := items(object(r["structuredContent"])["windows"]) if len(windows) != 1 || num(object(windows[0])["window_id"]) != 10 { t.Fatal(r) } } r := supplementShellWindows(obj{"structuredContent": obj{"windows": []any{}}}, obj{"app": "Brave"}, native) if len(items(object(r["structuredContent"])["windows"])) != 0 { t.Fatal("widened app scope") } broken := obj{"isError": true} if supplementShellWindows(broken, obj{}, native)["isError"] != true { t.Fatal("observation error hidden") } } func TestWinBObservationFollowsActualTaskbarFocus(t *testing.T) { args := obj{"pid": 1, "window_id": 10} requested := shellFixtureWindow(1, 10, "Progman") focus := obj{"probe_ok": true, "focus_root": shellFixtureWindow(1, 20, "Shell_TrayWnd"), "foreground_window": requested} // Foreground HWND alone would give the wrong answer. next, reason := postActionTarget(args, requested, focus, nil) if num(next["window_id"]) != 20 || reason != "associated_native_keyboard_focus" { t.Fatal(next, reason) } if num(args["window_id"]) != 10 { t.Fatal("requested target history mutated") } } func TestPostActionNeverCapturesUnrelatedApplicationOrAmbiguousMenu(t *testing.T) { args := obj{"pid": 1, "window_id": 10} requested := shellFixtureWindow(1, 10, "Progman") for _, other := range []obj{shellFixtureWindow(2, 20, "Shell_TrayWnd"), shellFixtureWindow(1, 20, "OtherApp")} { next, _ := postActionTarget(args, requested, obj{"probe_ok": true, "focus_root": other}, nil) if num(next["window_id"]) != 10 { t.Fatal("unrelated focus captured", next) } } menu := shellFixtureWindow(1, 30, "#32768") menu["owner_id"] = int64(10) menu2 := shellFixtureWindow(1, 40, "#32768") menu2["owner_id"] = int64(10) focus := obj{"probe_ok": true, "menu_active": true, "menu_owner": requested} next, reason := postActionTarget(args, requested, focus, []obj{menu, menu2}) if num(next["window_id"]) != 10 || reason != "ambiguous_active_menu_observe_candidates_explicitly" { t.Fatal(next, reason) } } func TestOwnedNativeMenuAndOverflowHaveIndependentObservationTargets(t *testing.T) { args := obj{"pid": 1, "window_id": 10} owner := shellFixtureWindow(1, 10, "Shell_TrayWnd") menu := shellFixtureWindow(1, 30, "#32768") menu["owner_id"] = int64(10) focus := obj{"probe_ok": true, "menu_active": true, "menu_owner": owner} next, _ := postActionTarget(args, owner, focus, []obj{menu}) if num(next["window_id"]) != 30 { t.Fatal(next) } next, _ = postActionTarget(args, owner, obj{"probe_ok": true, "focus_root": shellFixtureWindow(1, 40, "TopLevelWindowForOverflowXamlIsland")}, nil) if num(next["window_id"]) != 40 { t.Fatal(next) } } func TestNativeMenuWithoutGWOwnerRequiresVerifiedMenuOwnerAndThread(t *testing.T) { args := obj{"pid": 1, "window_id": 10} owner := shellFixtureWindow(1, 10, "ordinary-form") owner["thread_id"] = int64(7) menu := shellFixtureWindow(1, 30, "#32768") menu["owner_id"], menu["thread_id"] = int64(0), int64(7) focus := obj{"probe_ok": true, "menu_active": true, "menu_owner": owner} next, _ := postActionTarget(args, owner, focus, []obj{menu}) if num(next["window_id"]) != 30 { t.Fatal(next) } menu["thread_id"] = int64(8) next, _ = postActionTarget(args, owner, focus, []obj{menu}) if num(next["window_id"]) != 10 { t.Fatal("unrelated menu followed", next) } }