Files
zerorigin-toolbox/internal/action/volumeToast_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

205 lines
5.0 KiB
Go

package action
import (
"sync"
"testing"
"time"
)
// resetToastForTest 清空去抖状态,避免测试间相互影响。
func resetToastForTest() {
toastMu.Lock()
defer toastMu.Unlock()
if toastTimer != nil {
toastTimer.Stop()
toastTimer = nil
}
toastNext = nil
}
// swapToastForTest 替换去抖窗口与底层通知函数,返回还原函数。
func swapToastForTest(window time.Duration, fn func(title, text string) error) func() {
oldWindow := volumeToastDebounceWindow
oldFunc := notifyFunc
volumeToastDebounceWindow = window
notifyFunc = fn
return func() {
volumeToastDebounceWindow = oldWindow
notifyFunc = oldFunc
}
}
// TestVolumeToastDebounce 验证去抖:窗口内 5 次调用只触发 1 次底层通知,
// 且显示的是最后一次(合并后)的内容;窗口之外的新调用再触发 1 次。
func TestVolumeToastDebounce(t *testing.T) {
var (
mu sync.Mutex
calls int
gotText string
)
restore := swapToastForTest(30*time.Millisecond, func(title, text string) error {
mu.Lock()
calls++
gotText = text
mu.Unlock()
return nil
})
defer restore()
resetToastForTest()
defer resetToastForTest()
// 瞬间连按 5 次(远小于 30ms 窗口,必然合并为 1 次)。
for i := 0; i < 5; i++ {
notifyVolume(40+i, "up")
}
// 等待远超窗口,合并后的最后一次触发应只发生一次。
time.Sleep(150 * time.Millisecond)
mu.Lock()
if calls != 1 {
mu.Unlock()
t.Fatalf("5 calls within window -> %d underlying calls, want 1", calls)
}
if gotText != "音量: 44%" {
t.Errorf("merged text = %q, want %q", gotText, "音量: 44%")
}
mu.Unlock()
// 窗口之外的新一轮调用应再次触发一次(且不被上一轮干扰)。
notifyVolume(50, "down")
time.Sleep(150 * time.Millisecond)
mu.Lock()
if calls != 2 {
mu.Unlock()
t.Fatalf("second round should trigger 1 more call, got %d total", calls)
}
mu.Unlock()
}
// TestVolumeToastText 验证音量调节 Toast 文本格式("音量: 40%")。
func TestVolumeToastText(t *testing.T) {
var (
mu sync.Mutex
gotTitle string
gotText string
)
restore := swapToastForTest(10*time.Millisecond, func(title, text string) error {
mu.Lock()
gotTitle, gotText = title, text
mu.Unlock()
return nil
})
defer restore()
resetToastForTest()
defer resetToastForTest()
notifyVolume(40, "up")
time.Sleep(80 * time.Millisecond)
mu.Lock()
defer mu.Unlock()
if gotText != "音量: 40%" {
t.Errorf("volume up text = %q, want %q", gotText, "音量: 40%")
}
if gotTitle != volumeToastTitle {
t.Errorf("volume up title = %q, want %q", gotTitle, volumeToastTitle)
}
}
// TestVolumeToastMuteText 验证静音 Toast 文本("已静音"/"未静音")。
func TestVolumeToastMuteText(t *testing.T) {
var (
mu sync.Mutex
gotText string
)
restore := swapToastForTest(10*time.Millisecond, func(title, text string) error {
mu.Lock()
gotText = text
mu.Unlock()
return nil
})
defer restore()
resetToastForTest()
defer resetToastForTest()
notifyVolume(0, "mute")
time.Sleep(80 * time.Millisecond)
mu.Lock()
if gotText != "已静音" {
t.Errorf("mute text = %q, want %q", gotText, "已静音")
}
mu.Unlock()
notifyVolume(0, "unmute")
time.Sleep(80 * time.Millisecond)
mu.Lock()
if gotText != "未静音" {
t.Errorf("unmute text = %q, want %q", gotText, "未静音")
}
mu.Unlock()
}
// TestToastDisabledGate 验证全局开关关闭时既不显示新通知,
// 也会清掉已经排队但尚未显示的通知。
func TestToastDisabledGate(t *testing.T) {
var (
mu sync.Mutex
calls int
)
restore := swapToastForTest(10*time.Millisecond, func(string, string) error {
mu.Lock()
calls++
mu.Unlock()
return nil
})
defer restore()
resetToastForTest()
defer resetToastForTest()
SetToastEnabled(true)
defer SetToastEnabled(true)
SetToastEnabled(false)
notifyVolume(30, "up")
time.Sleep(50 * time.Millisecond)
mu.Lock()
if calls != 0 {
mu.Unlock()
t.Fatalf("disabled toast produced %d notifications, want 0", calls)
}
mu.Unlock()
// 先排队再关闭:关闭时必须取消待发通知。
SetToastEnabled(true)
notifyVolume(30, "up")
SetToastEnabled(false)
time.Sleep(50 * time.Millisecond)
mu.Lock()
if calls != 0 {
mu.Unlock()
t.Fatalf("pending toast should be cancelled after disable, got %d", calls)
}
mu.Unlock()
}
// TestVolumeSchemaToastDefaultOff 验证音量/静音动作默认不弹额外通知
// (系统自身已有 OSD),用户仍可在规则里显式打开。
func TestVolumeSchemaToastDefaultOff(t *testing.T) {
for _, id := range []string{"volume_up", "volume_down", "toggle_mute"} {
act, err := New(id)
if err != nil {
t.Fatalf("%s: %v", id, err)
}
var found bool
for _, p := range act.Schema().Params {
if p.Name == "toast" {
found = true
if p.Type != "bool" || p.Default != false {
t.Errorf("%s toast = type %q default %v, want bool false", id, p.Type, p.Default)
}
}
}
if !found {
t.Errorf("%s missing toast param", id)
}
}
}