Files
zerorigin-toolbox/internal/action/volumeToast_test.go
T
zerorigin2019 0a1c1e82ea chore: 初始化仓库(零源工具箱 0.1.0 基线)
- Go 主进程 cmd/zerorigin(托盘常驻 + 触发器/规则/动作引擎)
- Wails v3 设置进程 cmd/settings(独立 module)
- 监督进程 cmd/watchdog(-supervise 异常重启)
- 构建脚本 build.bat / NSIS 安装包 deploy/、便携版 portable/
- 架构与 MVP 计划文档 docs/、阶段 0-4 验收记录 deploy/acceptance-0-4.md
2026-08-05 00:42:45 +08:00

140 lines
3.3 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 := showBalloonFunc
volumeToastDebounceWindow = window
showBalloonFunc = fn
return func() {
volumeToastDebounceWindow = oldWindow
showBalloonFunc = 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()
}