package osd import ( "math" "testing" "github.com/egoist/mygo" "github.com/egoist/mygo/ui" ) // TestPositionIn 验证提示条在活动显示器工作区底部居中。 func TestPositionIn(t *testing.T) { for _, tc := range []struct { name string wa mygo.Rectangle want [2]int }{ {"主屏", mygo.Rectangle{X: 0, Y: 0, Width: 1920, Height: 1080}, [2]int{830, 964}}, {"副屏偏移", mygo.Rectangle{X: 1920, Y: 0, Width: 1920, Height: 1080}, [2]int{2750, 964}}, {"任务栏占用", mygo.Rectangle{X: 0, Y: 0, Width: 1920, Height: 1040}, [2]int{830, 924}}, {"工作区高度不足", mygo.Rectangle{X: 0, Y: 0, Width: 300, Height: 100}, [2]int{20, 0}}, {"工作区宽度不足", mygo.Rectangle{X: 0, Y: 0, Width: 200, Height: 100}, [2]int{0, 0}}, {"无效工作区", mygo.Rectangle{X: 10, Y: 20}, [2]int{10, 20}}, } { x, y := positionIn(tc.wa, Width, Height, "bottom", bottomMargin) if x != tc.want[0] || y != tc.want[1] { t.Errorf("%s: position = (%d,%d), want (%d,%d)", tc.name, x, y, tc.want[0], tc.want[1]) } } } // TestPositionInSides 验证四个方位与边距换算:上下水平居中、左右垂直居中, // 并把胶囊钳制在工作区内。 func TestPositionInSides(t *testing.T) { wa := mygo.Rectangle{X: 0, Y: 0, Width: 1920, Height: 1040} cases := []struct { name string position string margin int want [2]int }{ {"顶部 12", "top", 12, [2]int{830, 12}}, {"底部 64", "bottom", 64, [2]int{830, 924}}, {"左侧 20", "left", 20, [2]int{20, 494}}, {"右侧 20", "right", 20, [2]int{1640, 494}}, {"左侧 0", "left", 0, [2]int{0, 494}}, {"右侧过大钳制", "right", 1800, [2]int{660, 494}}, {"顶部边距过大钳制", "top", 1100, [2]int{830, 988}}, {"负边距按 0", "bottom", -30, [2]int{830, 988}}, {"未知位置按底部", "center", 10, [2]int{830, 978}}, } for _, tc := range cases { x, y := positionIn(wa, Width, Height, tc.position, tc.margin) if x != tc.want[0] || y != tc.want[1] { t.Errorf("%s: position = (%d,%d), want (%d,%d)", tc.name, x, y, tc.want[0], tc.want[1]) } } } // TestCapsuleRadius 验证窗口高度为偶数且面板半径正好是高度的一半, // 这样左右两端才是完整的半圆而不是普通圆角矩形。 func TestCapsuleRadius(t *testing.T) { if Height%2 != 0 { t.Fatalf("Height = %d, want even number for a half-height radius", Height) } if panelRadius != Height/2 { t.Fatalf("panelRadius = %d, want %d", panelRadius, Height/2) } } // setGeom 直接写几何快照(与 showOnMain 做的写入一致),返回还原函数。 func setGeom(visible bool, x, y, w, h int) func() { wasVisible := geomVisible.Load() wasX, wasY := geomX.Load(), geomY.Load() wasW, wasH := geomW.Load(), geomH.Load() geomVisible.Store(visible) geomX.Store(int32(x)) geomY.Store(int32(y)) geomW.Store(int32(w)) geomH.Store(int32(h)) return func() { geomVisible.Store(wasVisible) geomX.Store(wasX) geomY.Store(wasY) geomW.Store(wasW) geomH.Store(wasH) } } // TestPointerInsideAt 验证悬停判断只认胶囊区域,且隐藏时不命中。 func TestPointerInsideAt(t *testing.T) { defer setGeom(true, 830, 932, Width, Height)() for _, tc := range []struct { x, y int want bool }{ {830, 932, true}, // 左上角 {830 + Width - 1, 932 + Height - 1, true}, // 右下角内侧 {830 + Width, 932, false}, // 右侧越界 {830, 932 + Height, false}, // 下方越界 {829, 932, false}, // 左侧越界 {830, 931, false}, // 上方越界 } { if got := PointerInsideAt(tc.x, tc.y); got != tc.want { t.Errorf("PointerInsideAt(%d,%d) = %v, want %v", tc.x, tc.y, got, tc.want) } } geomVisible.Store(false) if PointerInsideAt(840, 940) { t.Error("PointerInsideAt hit while hidden") } } // TestPointerNearAt 验证"靠近"范围:胶囊外 reachMargin 内算靠近, // 再远就不算,避免光标随便停在附近提示条也不消失。 func TestPointerNearAt(t *testing.T) { defer setGeom(true, 830, 932, Width, Height)() for _, tc := range []struct { name string x, y int want bool }{ {"胶囊内", 900, 950, true}, {"正上方容差内", 900, 932 - reachMargin + 1, true}, {"正下方容差内", 900, 932 + Height + reachMargin - 1, true}, {"左侧容差内", 830 - reachMargin + 1, 950, true}, {"正上方超出容差", 900, 932 - reachMargin - 1, false}, {"下方远超出容差", 900, 932 + Height + reachMargin + 20, false}, } { if got := PointerNearAt(tc.x, tc.y); got != tc.want { t.Errorf("%s: PointerNearAt(%d,%d) = %v, want %v", tc.name, tc.x, tc.y, got, tc.want) } } geomVisible.Store(false) if PointerNearAt(900, 950) { t.Error("PointerNearAt hit while hidden") } } // TestPercentAtX 验证拖动横坐标到百分比的换算:轨道两端钳制、中点为 50%、 // 高 DPI 按实际像素宽度换算。 func TestPercentAtX(t *testing.T) { var restore func() defer func() { if restore != nil { restore() } }() set := func(scale float64) { if restore != nil { restore() } restore = setGeom(true, 830, 932, int(float64(Width)*scale), int(float64(Height)*scale)) } for _, tc := range []struct { name string scale float64 x int want int }{ {"轨道起点", 1, 830 + trackLeft, 0}, {"轨道终点", 1, 830 + trackLeft + trackWidth, 100}, {"轨道中点", 1, 830 + trackLeft + trackWidth/2, 50}, {"图标区域钳制到 0", 1, 830 + 5, 0}, {"右侧文字钳制到 100", 1, 830 + Width - 2, 100}, {"125% 缩放起点", 1.25, 830 + int(math.Round(float64(trackLeft)*1.25)), 0}, {"125% 缩放中点", 1.25, 830 + int(math.Round(float64(trackLeft+trackWidth/2)*1.25)), 50}, } { set(tc.scale) if got := PercentAtX(tc.x); got != tc.want { t.Errorf("%s: PercentAtX(%d) = %d, want %d", tc.name, tc.x, got, tc.want) } } } // TestHoverCenter 验证光标屏幕坐标到 0-1 位置的换算:径向光斑的 Center // 用相对坐标,越界(拖动时光标移出胶囊)钳制在边缘。 func TestHoverCenter(t *testing.T) { wasPos, _ := pos.Load().(string) pos.Store("bottom") defer func() { pos.Store(wasPos) }() defer setGeom(true, 830, 932, Width, Height)() for _, tc := range []struct { name string x, y int wx, wy float32 }{ {"左上角", 830, 932, 0, 0}, {"正中", 830 + Width/2, 932 + Height/2, 0.5, 0.5}, {"右下角内侧", 830 + Width - 1, 932 + Height - 1, float32(Width-1) / Width, float32(Height-1) / Height}, {"右侧越界钳制", 830 + Width + 40, 932 + Height/2, 1, 0.5}, {"上方越界钳制", 830 + Width/2, 932 - 20, 0.5, 0}, } { hoverX.Store(int32(tc.x)) hoverY.Store(int32(tc.y)) gx, gy := hoverCenter() if math.Abs(float64(gx-tc.wx)) > 1e-6 || math.Abs(float64(gy-tc.wy)) > 1e-6 { t.Errorf("%s: hoverCenter = (%v,%v), want (%v,%v)", tc.name, gx, gy, tc.wx, tc.wy) } } } // TestSetHoverHidden 验证提示条不可见时不点亮光感:钩子线程只认几何快照, // 隐藏后残留的光效必须靠 ClearHover/hideIf 收尾。 func TestSetHoverHidden(t *testing.T) { defer setGeom(false, 830, 932, Width, Height)() defer func() { hoverOn.Store(false); hoverX.Store(0); hoverY.Store(0) }() hoverOn.Store(false) hoverX.Store(0) hoverY.Store(0) SetHover(900, 950) if Hovering() { t.Error("SetHover lit the glow while the capsule is hidden") } if hoverX.Load() != 0 || hoverY.Load() != 0 { t.Errorf("SetHover wrote position while hidden: (%d,%d)", hoverX.Load(), hoverY.Load()) } } // TestClearHover 验证 ClearHover 只在亮着时切换状态,重复调用无副作用。 func TestClearHover(t *testing.T) { hoverOn.Store(true) ClearHover() if Hovering() { t.Error("ClearHover left the glow on") } // 再清一次不应 panic 或写坏节流时间。 ClearHover() if lastHoverPaint.Load() != 0 { t.Errorf("lastHoverPaint = %d, want 0 after clearing", lastHoverPaint.Load()) } } // TestCurrentKind 验证提示条类型到滚轮调节目标的映射。 func TestCurrentKind(t *testing.T) { mu.Lock() was := cur mu.Unlock() defer func() { mu.Lock() cur = was mu.Unlock() }() for _, tc := range []struct { st state want string }{ {state{kind: kindVolume}, "volume"}, {state{kind: kindMute, muted: true}, "mute"}, {state{kind: kindBrightness}, "brightness"}, } { mu.Lock() cur = tc.st mu.Unlock() if got := CurrentKind(); got != tc.want { t.Errorf("CurrentKind(%v) = %q, want %q", tc.st.kind, got, tc.want) } } } // TestInsetRect 验证内侧描边用的收缩矩形不会出现负尺寸。 func TestInsetRect(t *testing.T) { got := insetRect(ui.Rect{X: 10, Y: 20, W: 260, H: 52}, 1) want := ui.Rect{X: 11, Y: 21, W: 258, H: 50} if got != want { t.Fatalf("insetRect = %+v, want %+v", got, want) } } // TestBarWeights 验证进度条填充与剩余权重合计为 100,越界值按 0/100 钳制。 func TestBarWeights(t *testing.T) { for _, in := range []int{-1, 0, 1, 55, 87, 100, 101} { fill, rest := barWeights(in) clamped := clampPercent(in) if fill != float32(clamped) || rest != float32(100-clamped) { t.Errorf("barWeights(%d) = (%v,%v), want (%d,%d)", in, fill, rest, clamped, 100-clamped) } if fill+rest != 100 { t.Errorf("barWeights(%d) sums to %v, want 100", in, fill+rest) } } } // TestClampPercent 验证百分比越界钳制,负值(读不到音量)按 0 处理。 func TestClampPercent(t *testing.T) { for in, want := range map[int]int{-1: 0, 0: 0, 55: 55, 100: 100, 101: 100} { if got := clampPercent(in); got != want { t.Errorf("clampPercent(%d) = %d, want %d", in, got, want) } } } // TestSizeFor 验证贴左右两侧时胶囊竖起来(宽高互换),贴上下两侧时保持横向。 func TestSizeFor(t *testing.T) { for _, tc := range []struct { position string wantW int wantH int wantVertic bool }{ {"top", Width, Height, false}, {"bottom", Width, Height, false}, {"left", Height, Width, true}, {"right", Height, Width, true}, {"", Width, Height, false}, {"center", Width, Height, false}, } { w, h := sizeFor(tc.position) if w != tc.wantW || h != tc.wantH { t.Errorf("sizeFor(%q) = %dx%d, want %dx%d", tc.position, w, h, tc.wantW, tc.wantH) } } } // TestPositionInVertical 验证竖排胶囊的定位:左右两侧垂直居中,边距作用在 // 水平方向,且始终钳制在工作区内。 func TestPositionInVertical(t *testing.T) { wa := mygo.Rectangle{X: 0, Y: 0, Width: 1920, Height: 1040} w, h := sizeFor("left") for _, tc := range []struct { name string position string margin int want [2]int }{ {"左侧 20", "left", 20, [2]int{20, 390}}, {"右侧 20", "right", 20, [2]int{1848, 390}}, {"左侧 0", "left", 0, [2]int{0, 390}}, {"右侧过大钳制", "right", 1900, [2]int{868, 390}}, } { x, y := positionIn(wa, w, h, tc.position, tc.margin) if x != tc.want[0] || y != tc.want[1] { t.Errorf("%s: position = (%d,%d), want (%d,%d)", tc.name, x, y, tc.want[0], tc.want[1]) } } } // TestPercentAtY 验证竖排时纵坐标到百分比的换算:竖向进度条从下往上增长, // 轨道底部是 0%、顶部是 100%,高 DPI 按实际像素高度换算。 func TestPercentAtY(t *testing.T) { wasPos, _ := pos.Load().(string) pos.Store("right") defer func() { pos.Store(wasPos) }() var restore func() defer func() { if restore != nil { restore() } }() set := func(scale float64) { if restore != nil { restore() } w, h := sizeFor("right") restore = setGeom(true, 1868, 390, int(float64(w)*scale), int(float64(h)*scale)) } for _, tc := range []struct { name string scale float64 y int want int }{ {"轨道底部 0", 1, 390 + trackTop + trackHeight, 0}, {"轨道顶部 100", 1, 390 + trackTop, 100}, {"轨道中点", 1, 390 + trackTop + trackHeight/2, 50}, {"图标区域钳制到 100", 1, 390 + 5, 100}, {"文字区域钳制到 0", 1, 390 + Width - 2, 0}, {"125% 缩放底部", 1.25, 390 + int(math.Round(float64(trackTop+trackHeight)*1.25)), 0}, {"125% 缩放中点", 1.25, 390 + int(math.Round(float64(trackTop+trackHeight/2)*1.25)), 50}, } { set(tc.scale) if got := PercentAtY(tc.y); got != tc.want { t.Errorf("%s: PercentAtY(%d) = %d, want %d", tc.name, tc.y, got, tc.want) } } } // TestPercentAtDispatch 验证 PercentAt 按方位选择坐标轴:横排看横坐标、 // 竖排看纵坐标(同一点在两种布局下给出不同结果)。 func TestPercentAtDispatch(t *testing.T) { wasPos, _ := pos.Load().(string) pos.Store("bottom") defer func() { pos.Store(wasPos) }() // 横排:窗口 260x52 位于 (830,932),轨道水平。 defer setGeom(true, 830, 932, Width, Height)() midX := 830 + trackLeft + trackWidth/2 if got := PercentAt(midX, 932+Height); got != 50 { t.Errorf("horizontal PercentAt = %d, want 50", got) } // 竖排:同一个窗口换成 52x260,轨道竖直,横坐标不再影响百分比。 pos.Store("right") setGeom(true, 830, 932, Height, Width) if got := PercentAt(midX, 932+trackTop+trackHeight/2); got != 50 { t.Errorf("vertical PercentAt = %d, want 50", got) } } func TestBackdropDelta(t *testing.T) { base := [3]int32{230, 120, 60} if got := backdropDelta(base, 230, 120, 60); got != 0 { t.Fatalf("同色应为 0,得到 %d", got) } // 三个通道等量变化时平均差就是单通道差。 if got := backdropDelta(base, 240, 130, 70); got != 10 { t.Fatalf("各通道 +10 应为 10,得到 %d", got) } if got := backdropDelta([3]int32{0, 0, 0}, 255, 255, 255); got != 255 { t.Fatalf("黑白应为 255,得到 %d", got) } // 只变一个通道时按三通道平均摊薄:差 60 → 20。 if got := backdropDelta(base, 230, 120, 0); got != 20 { t.Fatalf("单通道差 60 应为 20,得到 %d", got) } if backdropDelta(base, 240, 130, 70) >= backdropColorDelta { t.Fatalf("各通道 +10 不应触发重截(阈值 %d)", backdropColorDelta) } if backdropDelta([3]int32{0, 0, 0}, 255, 255, 255) < backdropColorDelta { t.Fatalf("黑白切换必须触发重截(阈值 %d)", backdropColorDelta) } }