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) } } }