Files
Reims-Go/internal/tray/tray_test.go
T
千川白浪 107a8d73e0 refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积 (#200)
* refactor: 收拢核心管线、强化错误处理、优化热路径与更新体积

* fix: preserve download recovery and bound PBZX decoding

Keep VM diagnostics available during plugin initialization failures and verify settings through the unified driver lifecycle.
2026-12-05 20:54:41 +08:00

155 lines
5.1 KiB
Go

package tray
import (
"bytes"
"image"
"image/color"
"image/png"
"testing"
"github.com/Nwflower/reims-go/internal/i18n"
)
func findEntry(entries []menuEntry, label string) *menuEntry {
for i := range entries {
if entries[i].Label == label {
return &entries[i]
}
}
return nil
}
func TestBuildMenuPerState(t *testing.T) {
menu := buildMenu(Snapshot{Locale: i18n.ZhCN, CanSuspend: true, VMs: []VMItem{
{Name: "macos-tahoe", Label: "日常", State: "running"},
{Name: "macos-dev", State: "stopped"},
{Name: "macos-busy", State: "starting"},
{Name: "macos-sleep", State: "suspended"},
}})
if !menu[0].Default || menu[0].ID != cmdShow {
t.Fatalf("first item should be the default Show, got %+v", menu[0])
}
if last := menu[len(menu)-1]; last.ID != cmdQuit {
t.Fatalf("last item should be Quit, got %+v", last)
}
run := findEntry(menu, "日常\t运行中")
if run == nil || run.Dot != dotRunning {
t.Fatalf("running VM should use its display name and a running dot: %+v", menu)
}
if !run.Sub[0].Default || run.Sub[0].ID != vmCmd(0, ActShowWindow) {
t.Fatalf("running VM's default action should be Show Window, got %+v", run.Sub[0])
}
if len(run.Sub) < 2 || run.Sub[1].ID != vmCmd(0, ActToggleFullscreen) || run.Sub[1].Label != "切换全屏" || run.Sub[1].Default {
t.Fatalf("running VM should offer Toggle Fullscreen after Show Window: %+v", run.Sub)
}
var hasSuspend bool
for _, e := range run.Sub {
if e.ID == vmCmd(0, ActSuspend) {
hasSuspend = true
}
}
if !hasSuspend {
t.Fatalf("running VM with CanSuspend should offer Suspend: %+v", run.Sub)
}
stop := findEntry(menu, "macos-dev\t已停止")
if stop == nil || stop.Dot != dotStopped || stop.Sub[0].ID != vmCmd(1, ActStart) || !stop.Sub[0].Default {
t.Fatalf("stopped VM should default to Start: %+v", stop)
}
for _, e := range stop.Sub {
if e.ID == vmCmd(1, ActStop) || e.ID == vmCmd(1, ActShowWindow) || e.ID == vmCmd(1, ActToggleFullscreen) {
t.Fatalf("stopped VM must not offer %+v", e)
}
}
busy := findEntry(menu, "macos-busy\t启动中…")
if busy == nil || busy.Dot != dotBusy || busy.Sub[0].ID != vmCmd(2, ActForceStop) {
t.Fatalf("starting VM should only offer force stop first: %+v", busy)
}
susp := findEntry(menu, "macos-sleep\t已挂起")
if susp == nil || susp.Dot != dotSuspended || susp.Sub[0].ID != vmCmd(3, ActResume) || !susp.Sub[0].Default {
t.Fatalf("suspended VM should default to Resume: %+v", susp)
}
}
func TestSuspendedVMWithoutCompatibleRuntime(t *testing.T) {
menu := buildMenu(Snapshot{Locale: i18n.ZhCN, VMs: []VMItem{{Name: "macos-sleep", State: "suspended"}}})
vm := findEntry(menu, "macos-sleep\t已挂起")
if vm == nil {
t.Fatal("suspended VM is missing from the tray menu")
}
resume := vm.Sub[0]
if resume.ID != vmCmd(0, ActResume) || !resume.Disabled || resume.Default || resume.Label != "恢复(当前内核不支持挂起)" {
t.Fatalf("resume should be disabled and explain why: %+v", resume)
}
if discard := vm.Sub[1]; discard.ID != vmCmd(0, ActDiscardSuspend) || discard.Disabled {
t.Fatalf("discard should remain available: %+v", discard)
}
}
func TestDecodeVMCmdRoundTrip(t *testing.T) {
for i := 0; i < maxTrayVMs; i++ {
for act := ActStart; act <= ActDiscardSuspend; act++ {
gi, ga, ok := decodeVMCmd(vmCmd(i, act))
if !ok || gi != i || ga != act {
t.Fatalf("vmCmd(%d,%d) decoded to (%d,%d,%v)", i, act, gi, ga, ok)
}
}
}
for _, id := range []int{cmdShow, cmdQuit, cmdNewVM, cmdVMBase, vmCmd(0, ActDiscardSuspend+1), cmdVMBase + maxTrayVMs*vmSlot} {
if _, _, ok := decodeVMCmd(id); ok {
t.Fatalf("id %d should not decode as a VM command", id)
}
}
}
func TestDotPixels(t *testing.T) {
const size = 16
alpha := func(px []byte, x, y int) byte { return px[(y*size+x)*4+3] }
run := dotPixels(size, dotRunning)
if alpha(run, size/2, size/2) != 255 || alpha(run, 0, 0) != 0 {
t.Fatal("running dot should be solid in the centre and clear in the corner")
}
ring := dotPixels(size, dotStopped)
if alpha(ring, size/2, size/2) != 0 {
t.Fatal("stopped dot should be a ring with a clear centre")
}
paused := dotPixels(size, dotSuspended)
if alpha(paused, 0, 0) != 0 || alpha(paused, size/2-2, size/2) == 0 || alpha(paused, size/2+2, size/2) == 0 {
t.Fatal("suspended mark should draw two visible vertical pause bars")
}
for _, buf := range [][]byte{run, paused} {
for i := 0; i < len(buf); i += 4 {
if buf[i] > buf[i+3] || buf[i+1] > buf[i+3] || buf[i+2] > buf[i+3] {
t.Fatal("pixels must be premultiplied (colour ≤ alpha)")
}
}
}
}
func TestResizePNGIcon(t *testing.T) {
src := image.NewNRGBA(image.Rect(0, 0, 128, 128))
for y := 0; y < 128; y++ {
for x := 0; x < 128; x++ {
src.SetNRGBA(x, y, color.NRGBA{R: 10, G: 132, B: 255, A: 255})
}
}
var buf bytes.Buffer
if err := png.Encode(&buf, src); err != nil {
t.Fatal(err)
}
out, err := resizePNGIcon(buf.Bytes(), 32)
if err != nil {
t.Fatalf("resizePNGIcon: %v", err)
}
decoded, err := png.Decode(bytes.NewReader(out))
if err != nil {
t.Fatalf("png.Decode: %v", err)
}
if decoded.Bounds().Dx() != 32 || decoded.Bounds().Dy() != 32 {
t.Fatalf("resized icon = %dx%d, want 32x32", decoded.Bounds().Dx(), decoded.Bounds().Dy())
}
}