package windows import ( "testing" "time" ) // TestIsFullscreenRect 验证覆盖判定边界(94%/95%/96%、无交集、跨屏)。 func TestIsFullscreenRect(t *testing.T) { screen := MonitorRect{Left: 0, Top: 0, Right: 1920, Bottom: 1080} cases := []struct { name string win MonitorRect want bool }{ {"完全覆盖", MonitorRect{0, 0, 1920, 1080}, true}, {"无边框全屏", MonitorRect{0, 0, 1920, 1080}, true}, {"任务栏遮挡(94%)", MonitorRect{0, 0, 1920, 1015}, false}, // 1015/1080 ≈ 93.9% {"任务栏遮挡(96%)", MonitorRect{0, 0, 1920, 1040}, true}, // 1040/1080 ≈ 96.3% {"普通窗口", MonitorRect{100, 100, 1200, 800}, false}, {"无交集", MonitorRect{3000, 3000, 4000, 4000}, false}, {"覆盖95%临界", MonitorRect{0, 0, 1920, 1026}, true}, // 1026/1080 = 95.0% } for _, c := range cases { if got := IsFullscreenRect(c.win, screen, 0); got != c.want { t.Errorf("%s: IsFullscreenRect = %v, want %v", c.name, got, c.want) } } } // TestIsFullscreenRectMultiMonitor 验证跨屏窗口按交集最大屏幕判定。 func TestIsFullscreenRectMultiMonitor(t *testing.T) { screen := MonitorRect{Left: 1920, Top: 0, Right: 3840, Bottom: 1080} win := MonitorRect{Left: 1900, Top: 0, Right: 3840, Bottom: 1080} // 几乎全盖副屏 if !IsFullscreenRect(win, screen, 0) { t.Fatal("near-fullscreen on secondary monitor should be true") } } // TestIsDesktopClass 验证 Shell 桌面窗口类名识别(空桌面场景回归)。 func TestIsDesktopClass(t *testing.T) { cases := []struct { cls string want bool }{ {"Progman", true}, // Shell 桌面主窗口 {"WorkerW", true}, // 壁纸层(Win11 前台焦点) {"#32769", true}, // 系统桌面窗口(GetDesktopWindow) {"SHELLDLL_DefView", true}, // 桌面图标宿主 {"SysListView32", true}, // 桌面图标列表 {"Chrome_WidgetWin_1", false}, {"ConsoleWindowClass", false}, {"CabinetWClass", false}, // 资源管理器(普通窗口,不算桌面) {"", false}, } for _, c := range cases { if got := isDesktopClass(c.cls); got != c.want { t.Errorf("isDesktopClass(%q) = %v, want %v", c.cls, got, c.want) } } } // TestIsDesktopWindowReal 集成验证:GetDesktopWindow 返回的系统桌面窗口 // (类名 #32769)必须判定为桌面(防止空桌面被误判全屏导致规则抑制)。 func TestIsDesktopWindowReal(t *testing.T) { hwnd, _, _ := procGetDesktopWindow.Call() if hwnd == 0 { t.Skip("GetDesktopWindow returned 0") } if !isDesktopWindow(hwnd) { t.Fatal("desktop window (#32769) should be detected as desktop") } } // TestFullscreenWatcherLifecycle 验证轮询器 Start/Stop 幂等与状态可读。 func TestFullscreenWatcherLifecycle(t *testing.T) { w := NewFullscreenWatcher() w.Start() w.Start() // 幂等 time.Sleep(100 * time.Millisecond) _ = w.IsFullscreen() // 可读(值不校验:取决于会话前台窗口) w.Stop() w.Stop() // 幂等 // 重启 w.Start() time.Sleep(50 * time.Millisecond) w.Stop() }