package host import ( "testing" "time" ) func TestIsBorderlessFullscreenRect(t *testing.T) { monitor := WindowRect{-1920, -1080, 0, 0} for _, tt := range []struct { name string style uintptr rect WindowRect want bool }{ {"exact", wsVisible, monitor, true}, {"within two pixels", 0, WindowRect{-1918, -1078, -2, -2}, true}, {"oversized", 0, WindowRect{-1930, -1090, 10, 10}, true}, {"short on left", 0, WindowRect{-1917, -1080, 0, 0}, false}, {"short on top", 0, WindowRect{-1920, -1077, 0, 0}, false}, {"short on right", 0, WindowRect{-1920, -1080, -3, 0}, false}, {"short on bottom", 0, WindowRect{-1920, -1080, 0, -3}, false}, {"caption", wsCaption, monitor, false}, {"thick frame", wsThickFrame, monitor, false}, } { t.Run(tt.name, func(t *testing.T) { if got := isBorderlessFullscreenRect(tt.style, tt.rect, monitor); got != tt.want { t.Errorf("isBorderlessFullscreenRect(%#x, %+v, %+v) = %v, want %v", tt.style, tt.rect, monitor, got, tt.want) } }) } } func TestFullscreenFrameMasks(t *testing.T) { if windowFrameStyles != 0x00CF0000 { t.Errorf("window frame mask = %#x, want WS_OVERLAPPEDWINDOW (0x00CF0000)", windowFrameStyles) } if windowFrameExStyles != 0x00020301 { t.Errorf("extended window frame mask = %#x, want 0x00020301", windowFrameExStyles) } } func TestComputeFallbackWindowRect(t *testing.T) { for _, tt := range []struct { name string work WindowRect want WindowRect }{ {"center on offset monitor", WindowRect{-1920, 40, 0, 1040}, WindowRect{-1776, 115, -144, 965}}, {"minimum window", WindowRect{100, 10, 820, 530}, WindowRect{140, 30, 780, 510}}, {"minimum fits exactly", WindowRect{0, 0, 640, 480}, WindowRect{0, 0, 640, 480}}, {"small work area", WindowRect{-300, 20, 200, 420}, WindowRect{-300, 20, 200, 420}}, {"clamp width only", WindowRect{0, 0, 600, 1000}, WindowRect{0, 75, 600, 925}}, {"clamp height only", WindowRect{0, 0, 1200, 400}, WindowRect{90, 0, 1110, 400}}, } { t.Run(tt.name, func(t *testing.T) { if got := computeFallbackWindowRect(tt.work); got != tt.want { t.Errorf("computeFallbackWindowRect(%+v) = %+v, want %+v", tt.work, got, tt.want) } }) } } func TestSavedWindowStates(t *testing.T) { windows := fullscreenWindows{states: make(map[uintptr]savedWindowState)} original := savedWindowState{ pid: 27, style: wsCaption | wsVisible, exStyle: wsExWindowEdge, placement: windowPlacement{ShowCmd: 3, Normal: WindowRect{30, 40, 1200, 800}}, } windows.states[1] = original windows.states[2] = savedWindowState{pid: original.pid} windows.states[3] = savedWindowState{pid: 13} if got, ok := windows.lookup(1, original.pid); !ok || got != original { t.Fatalf("lookup = (%+v, %v), want (%+v, true)", got, ok, original) } if got, ok := windows.lookup(1, 13); ok || got != (savedWindowState{}) { t.Fatal("lookup returned state for a different PID") } if _, ok := windows.lookup(4, original.pid); ok { t.Fatal("lookup matched a different HWND") } livePIDs := map[uintptr]uint32{1: original.pid, 3: 14} windows.prune(func(hwnd uintptr) uint32 { return livePIDs[hwnd] }) if got, ok := windows.lookup(1, original.pid); len(windows.states) != 1 || !ok || got != original { t.Fatalf("prune should keep only the live window's original state, got %+v", windows.states) } } func TestFullscreenRestorePointSurvivesMonitorResize(t *testing.T) { windows := fullscreenWindows{states: make(map[uintptr]savedWindowState)} const hwnd, pid = 1, 27 original := savedWindowState{ pid: pid, style: wsCaption | wsVisible, exStyle: wsExWindowEdge, placement: windowPlacement{ShowCmd: 1, Normal: WindowRect{30, 40, 1200, 800}}, } oldBounds := WindowRect{0, 0, 1920, 1080} newBounds := WindowRect{0, 0, 2560, 1440} windows.remember(hwnd, original, oldBounds) borderless := savedWindowState{pid: pid, style: wsVisible, placement: windowPlacement{ShowCmd: 1, Normal: oldBounds}} if isBorderlessFullscreenRect(borderless.style, oldBounds, newBounds) { t.Fatal("old full-screen window should not cover the resized monitor") } windows.remember(hwnd, borderless, newBounds) original.fullscreenRect = newBounds if got, ok := windows.lookup(hwnd, pid); !ok || got != original { t.Fatalf("restore point = (%+v, %v), want (%+v, true)", got, ok, original) } } func TestMinimizedWindowFullscreen(t *testing.T) { monitor := WindowRect{-1920, 0, 0, 1080} work := WindowRect{-1920, 40, 0, 1080} normalFullscreen := WindowRect{-1920, -40, 0, 1040} normalBeforeMaximize := WindowRect{-1800, 30, -500, 800} fromMaximized := savedWindowState{ pid: 27, placement: windowPlacement{ShowCmd: swShowMaximized, Normal: normalBeforeMaximize}, fullscreenRect: monitor, } for _, tt := range []struct { name string style uintptr normal WindowRect saved savedWindowState want bool }{ {"cached window from maximized mode", wsVisible, normalBeforeMaximize, fromMaximized, true}, {"cached window from normal mode", wsVisible, normalFullscreen, savedWindowState{pid: 27, fullscreenRect: monitor}, true}, {"uncached borderless window", wsVisible, normalFullscreen, savedWindowState{}, true}, {"cached bounds no longer cover monitor", wsVisible, WindowRect{-1280, -40, 0, 680}, fromMaximized, false}, {"framed window", wsCaption, normalFullscreen, savedWindowState{}, false}, } { t.Run(tt.name, func(t *testing.T) { rect := minimizedWindowRect(tt.normal, 0, monitor, work, tt.saved) if got := isBorderlessFullscreenRect(tt.style, rect, monitor); got != tt.want { t.Errorf("full screen with restored rectangle %+v = %v, want %v", rect, got, tt.want) } }) } } func TestFullscreenWindowLockDoesNotBlockOtherWindows(t *testing.T) { windows := fullscreenWindows{ locks: make(map[uintptr]*fullscreenWindowLock), states: make(map[uintptr]savedWindowState), } const blockedWindow, otherWindow = 1, 2 defer windows.lock(blockedWindow)() otherState := savedWindowState{pid: 27, style: wsCaption} done := make(chan savedWindowState, 1) go func() { defer windows.lock(otherWindow)() windows.remember(otherWindow, otherState, WindowRect{}) got, _ := windows.lookup(otherWindow, otherState.pid) windows.forget(otherWindow) done <- got }() select { case got := <-done: if got != otherState { t.Errorf("other window state = %+v, want %+v", got, otherState) } case <-time.After(2 * time.Second): t.Fatal("other window blocked by an in-flight operation") } }