Files
zerorigin-toolbox/internal/action/desktop_test.go
T
zerorigin 0b8b9b0775 迁移到 mygo 单进程架构并接入签名自更新
- 单进程:托盘常驻 + 原生设置窗口(internal/settings),移除 Wails 前端与 IPC
- 触发器(鼠标角落 / 边缘滚轮 / 热键)+ 规则引擎 + 动作注册表
- 调节提示条:截屏 + glass.Blur 三层毛玻璃、背景跟随屏幕变化、悬停光感与拖动
- 音量 Core Audio COM、亮度 DDC/CI(gamma 兜底)、多显示器求平均
- 配置热重载 + 版本迁移 v1→v5、便携模式、崩溃取证三件套
- updater/native 插件 + 托盘「检查更新…」,watchdog 识别更新重启
- 自建 Gitea 发布:scripts/publish-gitea.ps1(updates.url 指向 latest Release)
2026-11-16 10:31:44 +08:00

114 lines
3.8 KiB
Go

package action
import (
"context"
"errors"
"fmt"
"testing"
"time"
pwin "zerorigin/internal/platform/windows"
)
// restoreDesktopFakes 在测试结束时恢复平台函数,避免影响其他用例。
func restoreDesktopFakes(t *testing.T, com func(int32) error, taskView func(int32, time.Duration) error, count func() (int, error)) {
t.Helper()
oldCOM, oldTaskView, oldCount := switchDesktopCOM, switchDesktopViaTaskView, virtualDesktopCount
t.Cleanup(func() {
switchDesktopCOM = oldCOM
switchDesktopViaTaskView = oldTaskView
virtualDesktopCount = oldCount
})
switchDesktopCOM = com
switchDesktopViaTaskView = taskView
virtualDesktopCount = count
}
// TestSwitchDesktopNoTaskViewFallbackOnSingleDesktop 验证单桌面时 COM 返回
// ErrNotEnoughDesktops,不得回退任务视图(否则任务视图会留在前台)。
func TestSwitchDesktopNoTaskViewFallbackOnSingleDesktop(t *testing.T) {
taskViewCalls := 0
restoreDesktopFakes(t,
func(int32) error { return fmt.Errorf("%w (found 1)", pwin.ErrNotEnoughDesktops) },
func(int32, time.Duration) error { taskViewCalls++; return nil },
nil,
)
err := (&switchDesktopAction{}).Execute(context.Background(), map[string]any{
"mode": "right", "method": "com", "taskview_delay_ms": 0,
})
if !errors.Is(err, pwin.ErrNotEnoughDesktops) {
t.Fatalf("Execute error = %v, want ErrNotEnoughDesktops", err)
}
if taskViewCalls != 0 {
t.Fatalf("task view fallback called %d times, want 0 on single desktop", taskViewCalls)
}
}
// TestSwitchDesktopCOMErrorFallsBackToTaskView 验证普通 COM 错误仍回退
// 任务视图,并透传方向与等待时长。
func TestSwitchDesktopCOMErrorFallsBackToTaskView(t *testing.T) {
gotDir := int32(0)
gotDelay := time.Duration(0)
restoreDesktopFakes(t,
func(int32) error { return errors.New("com unavailable") },
func(dir int32, delay time.Duration) error {
gotDir, gotDelay = dir, delay
return nil
},
nil,
)
err := (&switchDesktopAction{}).Execute(context.Background(), map[string]any{
"mode": "left", "method": "com", "taskview_delay_ms": 500,
})
if err != nil {
t.Fatalf("Execute error = %v, want nil (fallback succeeds)", err)
}
if gotDir != -1 || gotDelay != 500*time.Millisecond {
t.Fatalf("fallback got dir=%d delay=%v, want -1/500ms", gotDir, gotDelay)
}
}
// TestSwitchDesktopTaskViewGuardSingleDesktop 验证显式 taskview 方式在
// 单桌面时提前失败,不打开任务视图。
func TestSwitchDesktopTaskViewGuardSingleDesktop(t *testing.T) {
comCalls, taskViewCalls := 0, 0
restoreDesktopFakes(t,
func(int32) error { comCalls++; return nil },
func(int32, time.Duration) error { taskViewCalls++; return nil },
func() (int, error) { return 1, nil },
)
err := (&switchDesktopAction{}).Execute(context.Background(), map[string]any{
"mode": "right", "method": "taskview",
})
if !errors.Is(err, pwin.ErrNotEnoughDesktops) {
t.Fatalf("Execute error = %v, want ErrNotEnoughDesktops", err)
}
if comCalls != 0 || taskViewCalls != 0 {
t.Fatalf("switches called (com=%d taskview=%d), want none", comCalls, taskViewCalls)
}
}
// TestSwitchDesktopTaskViewCountFailureProceeds 验证数量查询失败不阻断
// taskview 方式(保持旧行为,失败由动作本身报错)。
func TestSwitchDesktopTaskViewCountFailureProceeds(t *testing.T) {
gotDelay := time.Duration(0)
restoreDesktopFakes(t,
func(int32) error { return nil },
func(_ int32, delay time.Duration) error { gotDelay = delay; return nil },
func() (int, error) { return 0, errors.New("query failed") },
)
err := (&switchDesktopAction{}).Execute(context.Background(), map[string]any{
"mode": "right", "method": "taskview", "taskview_delay_ms": 900,
})
if err != nil {
t.Fatalf("Execute error = %v, want nil", err)
}
if gotDelay != 900*time.Millisecond {
t.Fatalf("delay = %v, want 900ms", gotDelay)
}
}