feat: add cancellable settings path search

This commit is contained in:
QiuSW
2026-07-22 15:57:02 +08:00
parent a3b766f276
commit 6f08c9c9d0
8 changed files with 384 additions and 75 deletions
+89
View File
@@ -1,7 +1,9 @@
package ui
import (
"context"
"testing"
"time"
"gioui.org/widget/material"
)
@@ -19,6 +21,51 @@ func TestShellStartsWithSemanticInstanceStatuses(t *testing.T) {
}
}
func TestShellCancelsOnlyTheActivePathSearch(t *testing.T) {
shell := NewShell(material.NewTheme())
started := make(chan struct{}, 1)
shell.OnPathSearch(func(ctx context.Context, field PathField, current string) (string, error) {
started <- struct{}{}
<-ctx.Done()
return "", ctx.Err()
}, nil)
shell.togglePathSearch(PathChromeExecutable)
if got := shell.searchButtonLabel(PathChromeExecutable); got != "取消" {
t.Fatalf("chrome search label = %q, want 取消", got)
}
if got := shell.searchButtonLabel(PathEdgeExecutable); got != "搜索" {
t.Fatalf("edge search label = %q, want 搜索", got)
}
select {
case <-started:
case <-time.After(time.Second):
t.Fatal("search did not start")
}
shell.togglePathSearch(PathChromeExecutable)
if got := shell.searchButtonLabel(PathChromeExecutable); got != "搜索" {
t.Fatalf("chrome search label = %q after cancel, want 搜索", got)
}
}
func TestShellAppliesLatestPathSearchResult(t *testing.T) {
shell := NewShell(material.NewTheme())
shell.OnPathSearch(func(context.Context, PathField, string) (string, error) {
return `C:\Browser\chrome.exe`, nil
}, nil)
shell.togglePathSearch(PathChromeExecutable)
var result pathSearchResult
select {
case result = <-shell.searchResults:
case <-time.After(time.Second):
t.Fatal("search did not produce a result")
}
shell.searchResults <- result
shell.consumeSearchResults()
if got := shell.chromePath.Text(); got != `C:\Browser\chrome.exe` {
t.Fatalf("chrome path = %q", got)
}
}
func TestShellSettingsCanBeRestored(t *testing.T) {
shell := NewShell(material.NewTheme())
shell.SetSettings(SettingsState{ChromePath: "chrome", EdgePath: "edge", DefaultDir: "profiles", LogDir: "logs", CloseOnExit: false})
@@ -26,3 +73,45 @@ func TestShellSettingsCanBeRestored(t *testing.T) {
t.Fatalf("settings were not restored")
}
}
func TestShellKeepsSettingsStateAcrossPageSwitch(t *testing.T) {
shell := NewShell(material.NewTheme())
shell.page = pageSettings
shell.dataDir.SetText(`D:\profiles\current`)
shell.pathFeedback = "正在搜索默认 User Data Dir…"
shell.page = pageInstances
shell.page = pageSettings
if got := shell.dataDir.Text(); got != `D:\profiles\current` {
t.Fatalf("data dir = %q", got)
}
if got := shell.pathFeedback; got != "正在搜索默认 User Data Dir…" {
t.Fatalf("path feedback = %q", got)
}
}
func TestShellIgnoresCancelledPathSearchResult(t *testing.T) {
shell := NewShell(material.NewTheme())
started := make(chan struct{}, 1)
shell.OnPathSearch(func(ctx context.Context, field PathField, current string) (string, error) {
started <- struct{}{}
<-ctx.Done()
return `C:\stale\chrome.exe`, nil
}, nil)
shell.togglePathSearch(PathChromeExecutable)
select {
case <-started:
case <-time.After(time.Second):
t.Fatal("search did not start")
}
shell.togglePathSearch(PathChromeExecutable)
select {
case result := <-shell.searchResults:
shell.searchResults <- result
case <-time.After(time.Second):
t.Fatal("cancelled search did not return")
}
shell.consumeSearchResults()
if got := shell.chromePath.Text(); got == `C:\stale\chrome.exe` {
t.Fatal("cancelled result overwrote the editor")
}
}