Files
zerorigin-toolbox/internal/platform/windows/virtualdesktop_test.go
T
zerorigin2019 0a1c1e82ea chore: 初始化仓库(零源工具箱 0.1.0 基线)
- Go 主进程 cmd/zerorigin(托盘常驻 + 触发器/规则/动作引擎)
- Wails v3 设置进程 cmd/settings(独立 module)
- 监督进程 cmd/watchdog(-supervise 异常重启)
- 构建脚本 build.bat / NSIS 安装包 deploy/、便携版 portable/
- 架构与 MVP 计划文档 docs/、阶段 0-4 验收记录 deploy/acceptance-0-4.md
2026-08-05 00:42:45 +08:00

120 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package windows
import (
"testing"
"time"
"golang.org/x/sys/windows/registry"
)
// TestVirtualDesktopQueryMatchesRegistry 验证 COM 查询的桌面列表与当前桌面
// 和注册表(Explorer\VirtualDesktops)一致 —— 接口 IID 与 vtable 槽位正确性的
// 端到端证据。headless/无 explorer 环境自动跳过。
func TestVirtualDesktopQueryMatchesRegistry(t *testing.T) {
procCoInitializeEx.Call(0, coinitMultithreaded)
defer procCoUninitialize.Call()
m, err := openDesktopManager()
if err != nil {
t.Skipf("virtual desktop COM unavailable (explorer not running?): %v", err)
}
defer m.close()
ids, err := m.desktopIDs()
if err != nil {
t.Fatalf("desktopIDs: %v", err)
}
if len(ids) < 1 {
t.Fatal("no desktops enumerated")
}
cur, err := m.currentDesktopIndex(ids)
if err != nil {
t.Fatalf("currentDesktopIndex: %v", err)
}
t.Logf("COM: %d desktops, current index %d", len(ids), cur)
// 注册表对照:CurrentVirtualDesktop(16 字节 GUID)
k, err := registry.OpenKey(registry.CURRENT_USER,
`SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops`, registry.READ)
if err != nil {
t.Skipf("registry key unavailable: %v", err)
}
defer k.Close()
raw, _, err := k.GetBinaryValue("CurrentVirtualDesktop")
if err != nil || len(raw) != 16 {
t.Skipf("no CurrentVirtualDesktop registry value: %v", err)
}
var regGUID comGUID
copy(regGUID.Data4[:], raw[8:16])
regGUID.Data1 = uint32(raw[0]) | uint32(raw[1])<<8 | uint32(raw[2])<<16 | uint32(raw[3])<<24
regGUID.Data2 = uint16(raw[4]) | uint16(raw[5])<<8
regGUID.Data3 = uint16(raw[6]) | uint16(raw[7])<<8
if ids[cur] != regGUID {
t.Fatalf("current desktop mismatch: COM=%v registry=%v", ids[cur], regGUID)
}
t.Logf("registry cross-check OK: current desktop GUID matches")
}
// TestSwitchDesktopCOM 验证完整切换链路(向右切 + 向左切回)。
// headless 会话无用户影响;结束前恢复原桌面。
func TestSwitchDesktopCOM(t *testing.T) {
procCoInitializeEx.Call(0, coinitMultithreaded)
defer procCoUninitialize.Call()
// 切换前基线
ids0, idx0, err := queryDesktopState()
if err != nil {
t.Skipf("query before: %v", err)
}
if len(ids0) < 2 {
t.Skipf("only %d desktop(s), nothing to switch", len(ids0))
}
// 向右切
if err := SwitchDesktopCOM(1); err != nil {
t.Fatalf("SwitchDesktopCOM(1): %v", err)
}
time.Sleep(200 * time.Millisecond) // 等待 explorer 生效
_, idx1, err := queryDesktopState()
if err != nil {
t.Fatalf("query after switch: %v", err)
}
if idx1 == idx0 {
t.Fatal("desktop did not change after SwitchDesktopCOM(1)")
}
t.Logf("switched to another desktop OK (idx %d -> %d)", idx0, idx1)
// 向左切回
if err := SwitchDesktopCOM(-1); err != nil {
t.Fatalf("SwitchDesktopCOM(-1): %v", err)
}
time.Sleep(200 * time.Millisecond)
_, idx2, err := queryDesktopState()
if err != nil {
t.Fatalf("query after switch back: %v", err)
}
if idx2 != idx0 {
t.Fatal("desktop did not return to original")
}
t.Logf("switched back to original desktop OK")
}
// queryDesktopState 返回全部桌面 GUID 列表与当前索引。
func queryDesktopState() ([]comGUID, int, error) {
m, err := openDesktopManager()
if err != nil {
return nil, 0, err
}
defer m.close()
ids, err := m.desktopIDs()
if err != nil {
return nil, 0, err
}
idx, err := m.currentDesktopIndex(ids)
if err != nil {
return nil, 0, err
}
return ids, idx, nil
}