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

93 lines
2.8 KiB
Go

package ui
import (
"math"
"testing"
)
type keyItem struct{ title string }
// keyedStates builds a box keyed by each key under one parent, and returns
// the local state of each.
func keyedStates(c *Context, keys []any) []*int {
var states []*int
Column(c).Children(func() {
for _, k := range keys {
e := Box(c.Key(k)).Size(10, 10)
states = append(states, Local(e, "n", func() int { return 0 }))
}
})
return states
}
// A pointer keys the object it points to: its element keeps its state as
// the object changes, and two objects alike keep a state each. The Tester
// panics on two siblings with one key.
func TestPointerKeysFollowTheirObjects(t *testing.T) {
a, b := &keyItem{"a"}, &keyItem{"b"}
var states []*int
tt := NewTester(func(c *Context) { states = keyedStates(c, []any{a, b}) }, 50, 50)
*states[0], *states[1] = 1, 2
a.title = "renamed"
tt.Frame()
if *states[0] != 1 {
t.Fatalf("renaming the object reset its element's state to %d", *states[0])
}
b.title = "renamed" // alike, and still another object
tt.Frame()
if states[0] == states[1] || *states[1] != 2 {
t.Fatalf("two objects alike share a state: %d and %d", *states[0], *states[1])
}
}
// Keys equal in value but of different types are different keys, as in a
// map.
func TestKeysOfDifferentTypesDiffer(t *testing.T) {
type id int32
var states []*int
NewTester(func(c *Context) {
states = keyedStates(c, []any{int(1), int64(1), uint64(1), int32(1), uint32(1), id(1), [1]int32{1}, keyItem{"a"}})
}, 50, 50)
seen := map[*int]bool{}
for _, s := range states {
if seen[s] {
t.Fatal("two keys of different types share a state")
}
seen[s] = true
}
}
// A key Go cannot compare, as a slice, still keys its element by its value.
func TestUncomparableKeys(t *testing.T) {
var states []*int
tt := NewTester(func(c *Context) { states = keyedStates(c, []any{[]int{1}, []int{2}}) }, 50, 50)
*states[0] = 1
tt.Frame()
if *states[0] != 1 || states[0] == states[1] {
t.Fatalf("slice keys: states %d and %d", *states[0], *states[1])
}
}
// A key not equal to itself, as a NaN, keeps its element's state from
// frame to frame, though a map would hash it anew each time.
func TestNaNKeys(t *testing.T) {
nan := math.NaN()
var states []*int
tt := NewTester(func(c *Context) { states = keyedStates(c, []any{nan, [1]float64{nan}}) }, 50, 50)
*states[0], *states[1] = 1, 2
tt.Frame()
if *states[0] != 1 || *states[1] != 2 {
t.Fatalf("NaN keys lost their states: %d and %d", *states[0], *states[1])
}
}
// Keying by a pointer or a struct allocates nothing, frame after frame.
func TestKeysAllocateNothing(t *testing.T) {
p, v := &keyItem{"a"}, keyItem{"b"}
for _, k := range []any{p, v, int32(1), 1, "a"} {
if n := testing.AllocsPerRun(100, func() { keyedID(1, k) }); n != 0 {
t.Errorf("keying by %T allocates %v times", k, n)
}
}
}