Files
openchamber-android-native/third_party/mygo/plugins/updater/internal/frontend/frontend_test.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

127 lines
3.7 KiB
Go

package frontend
import (
"testing"
"github.com/egoist/mygo"
)
type fakeWindow struct {
visible, focused, inactive bool
closed []func()
}
func (w *fakeWindow) Show() { w.visible = true }
func (w *fakeWindow) ShowInactive() { w.visible, w.inactive = true, true }
func (w *fakeWindow) Focus() { w.focused = true }
func (w *fakeWindow) IsVisible() bool { return w.visible }
func (w *fakeWindow) OnClosed(fn func()) func() {
w.closed = append(w.closed, fn)
return func() { w.closed = nil }
}
// fakeApp is the app as Show sees it, on GOOS os, until the test ends.
type fakeApp struct {
active, focused bool
policy mygo.ActivationPolicy
listeners []func()
}
func useApp(t *testing.T, os string) *fakeApp {
a := &fakeApp{policy: mygo.ActivationPolicyRegular}
prev := [...]any{goos, appActive, activationPolicy, focusApp, onAppActive}
goos = os
appActive = func() bool { return a.active }
activationPolicy = func() mygo.ActivationPolicy { return a.policy }
focusApp = func() { a.focused = true }
onAppActive = func(fn func()) func() {
a.listeners = append(a.listeners, fn)
return func() { a.listeners = nil }
}
t.Cleanup(func() {
goos = prev[0].(string)
appActive = prev[1].(func() bool)
activationPolicy = prev[2].(func() mygo.ActivationPolicy)
focusApp = prev[3].(func())
onAppActive = prev[4].(func(func()) func())
})
return a
}
func (a *fakeApp) activate() {
a.active = true
for _, fn := range a.listeners {
fn()
}
}
// A background check while the app is active shows the window in front, with
// the keyboard, as Sparkle does; it used to show it behind the app's window.
func TestShowActiveApp(t *testing.T) {
for _, os := range []string{"darwin", "windows", "linux"} {
a := useApp(t, os)
a.active = true
w := &fakeWindow{}
show(w, false)
if !w.visible || !w.focused {
t.Errorf("%s: visible %v, focused %v; want the window in front", os, w.visible, w.focused)
}
}
}
// On macOS a background check while another app is in front waits for the
// app to become active, then shows the window in front, once.
func TestShowWaitsForActivation(t *testing.T) {
a := useApp(t, "darwin")
w := &fakeWindow{}
show(w, false)
if w.visible {
t.Fatal("the window showed while the app was in the background")
}
a.activate()
if !w.visible || !w.focused || w.inactive {
t.Fatalf("visible %v, focused %v, inactive %v once active; want it in front", w.visible, w.focused, w.inactive)
}
if len(a.listeners) != 0 || len(w.closed) != 0 {
t.Error("still watching the app after showing the window")
}
}
// A window closed while it waits for the app, as when its session ends,
// never shows.
func TestShowClosedWhileWaiting(t *testing.T) {
a := useApp(t, "darwin")
w := &fakeWindow{}
show(w, false)
for _, fn := range w.closed {
fn()
}
a.activate()
if w.visible {
t.Error("a closed window showed when the app became active")
}
}
// An app without a Dock icon may never become active by itself, so a
// background check brings it to the front with the window, as Sparkle does.
func TestShowAccessoryApp(t *testing.T) {
a := useApp(t, "darwin")
a.policy = mygo.ActivationPolicyAccessory
w := &fakeWindow{}
show(w, false)
if !a.focused || !w.visible || !w.focused {
t.Errorf("app focused %v, window visible %v, focused %v; want both in front", a.focused, w.visible, w.focused)
}
}
// Elsewhere a background check while another app is in front shows the
// window without the keyboard.
func TestShowInactiveElsewhere(t *testing.T) {
useApp(t, "windows")
w := &fakeWindow{}
show(w, false)
if !w.visible || w.focused {
t.Errorf("visible %v, focused %v; want it shown without the keyboard", w.visible, w.focused)
}
}