Files
openchamber-android-native/third_party/mygo/power_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

148 lines
4.4 KiB
Go

//go:build !js
package mygo
import (
"testing"
"time"
"github.com/egoist/mygo/internal/wait"
)
func TestPower(t *testing.T) {
var got []string
record := func(event string) func() { return func() { got = append(got, event) } }
offs := []func(){
Power.OnSuspend(record("suspend")),
Power.OnResume(record("resume")),
Power.OnLockScreen(record("lock")),
Power.OnUnlockScreen(record("unlock")),
}
defer func() {
for _, off := range offs {
off()
}
}()
wait.Until(t, "the backend asked to watch power events", func() bool { return onMainValue(func() bool { return fb.Watching }) })
onMain(func() {
for _, e := range []string{"suspend", "resume", "lock-screen", "unlock-screen", "unknown"} {
fb.EmitPower(e)
}
})
if want := "suspend resume lock unlock"; join(got) != want {
t.Errorf("events = %q, want %q", join(got), want)
}
release := Power.KeepAwake("testing", true)
if n := onMainValue(func() int { return fb.Awake }); n != 1 {
t.Errorf("awake = %d", n)
}
release()
release() // once
if n := onMainValue(func() int { return fb.Awake }); n != 0 {
t.Errorf("awake after release = %d", n)
}
release = Power.KeepRunning("serving")
if n := onMainValue(func() int { return fb.Running }); n != 1 {
t.Errorf("running = %d", n)
}
if n := onMainValue(func() int { return fb.Awake }); n != 0 {
t.Errorf("KeepRunning kept the computer awake (%d)", n)
}
release()
release() // once
if n := onMainValue(func() int { return fb.Running }); n != 0 {
t.Errorf("running after release = %d", n)
}
if Power.IsOnBattery() || Power.IdleTime() != 42*time.Second {
t.Error("IsOnBattery or IdleTime")
}
}
func join(s []string) string {
out := ""
for i, x := range s {
if i > 0 {
out += " "
}
out += x
}
return out
}
func TestDockMenuAndRelaunch(t *testing.T) {
m := NewMenu([]*MenuItem{{Label: "New Window", ID: "new"}})
App.Dock.SetMenu(m)
if App.Dock.Menu() != m {
t.Error("Dock.Menu")
}
dm := onMainValue(func() any { return fb.DockMenu })
if dm == nil || len(fb.DockMenu.Items) != 1 || fb.DockMenu.Items[0].Label != "New Window" {
t.Errorf("dock menu = %+v", dm)
}
App.Dock.SetMenu(nil)
if onMainValue(func() bool { return fb.DockMenu != nil }) {
t.Error("SetMenu(nil) kept the menu")
}
// A relaunch whose quit is canceled does nothing. Before the windows
// close, it lets the app it starts take the foreground, which the
// app gives up as they close (Windows).
canceled := make(chan struct{}, 1)
allowed := onMainValue(func() int { return fb.ForegroundAllowed })
off := App.OnBeforeQuit(func(e *QuitEvent) {
if fb.ForegroundAllowed != allowed+1 {
t.Errorf("the foreground was passed on %d times before the quit", fb.ForegroundAllowed-allowed)
}
e.PreventDefault()
canceled <- struct{}{}
})
defer off()
App.Relaunch("--after-restart")
select {
case <-canceled:
case <-time.After(wait.Guard):
t.Fatal("Relaunch did not start quitting")
}
if onMainValue(func() bool { return App.relaunch || App.relaunchArgs != nil }) {
t.Error("a canceled relaunch is still pending, or its arguments")
}
}
func TestPermissionHandler(t *testing.T) {
w, fw := testWindow(t, WindowOptions{})
ask := func(kinds []string, origin string) bool {
return onMainValue(func() bool { return fw.H.PermissionRequested(kinds, origin) })
}
// By default the app's own pages get what they ask for.
if !ask([]string{"camera"}, "mygo://localhost") || ask([]string{"camera"}, "https://evil.example") {
t.Error("default permission decisions")
}
var got PermissionRequest
w.Page().SetPermissionHandler(func(req PermissionRequest) bool {
got = req
return req.Origin == "https://meet.example"
})
if !ask([]string{"camera", "microphone"}, "https://meet.example") || ask([]string{"geolocation"}, "mygo://localhost") {
t.Error("the handler's decisions were not applied")
}
if got.Origin != "mygo://localhost" || len(got.Permissions) != 1 || got.Permissions[0] != PermissionGeolocation {
t.Errorf("last request = %+v", got)
}
w.Page().SetPermissionHandler(nil)
if !ask([]string{"camera"}, "mygo://localhost") {
t.Error("SetPermissionHandler(nil) did not restore the default")
}
}
func TestClearBrowsingDataFake(t *testing.T) {
before := onMainValue(func() int { return fb.Cleared })
if err := App.ClearBrowsingData(); err != nil {
t.Fatal(err)
}
if onMainValue(func() int { return fb.Cleared }) != before+1 {
t.Error("the backend did not clear")
}
}