Files
openchamber-android-native/third_party/mygo/plugins/game/gamepad/gamepad_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

97 lines
2.6 KiB
Go

package gamepad
import (
"math"
"testing"
)
// fakeDevice is a pad whose state a test sets.
type fakeDevice struct {
id string
state State
gone bool
closed bool
}
func (d *fakeDevice) key() string { return d.id }
func (d *fakeDevice) name() string { return "Fake " + d.id }
func (d *fakeDevice) close() { d.closed = true }
func (d *fakeDevice) read(s *State) bool {
*s = d.state
return !d.gone
}
// withDevices has scans find devs, which a test changes.
func withDevices(t *testing.T, devs *[]device) {
t.Helper()
saved, savedEach := scanDevices, scanEach
scanDevices = func(known map[string]device) []device { return *devs }
scanEach = 0
mu.Lock()
pads, scanned = nil, scanned.AddDate(-1, 0, 0)
mu.Unlock()
t.Cleanup(func() {
scanDevices, scanEach = saved, savedEach
mu.Lock()
pads = nil
mu.Unlock()
})
}
func TestPadsConnectAndDisconnect(t *testing.T) {
a, b := &fakeDevice{id: "a"}, &fakeDevice{id: "b"}
devs := []device{a}
withDevices(t, &devs)
ps := Pads()
if len(ps) != 1 || ps[0].Name() != "Fake a" || !ps[0].Connected() {
t.Fatalf("pads %v", ps)
}
first := ps[0]
devs = []device{b, a}
ps = Pads()
if len(ps) != 2 || ps[0] != first || ps[1].Name() != "Fake b" {
t.Fatalf("pads in connecting order: %v", ps)
}
// Unplugged between scans: reading finds it gone.
a.gone = true
a.state.Buttons[South] = 1
if first.State().Pressed(South) || first.Connected() || !a.closed {
t.Error("a pad gone still reads, or is not closed")
}
// Gone from a scan.
devs = nil
if ps = Pads(); len(ps) != 0 {
t.Errorf("pads after unplugging: %v", ps)
}
}
func TestState(t *testing.T) {
var last, s State
s.Buttons[South] = 1
s.Buttons[LeftTrigger] = 0.2
s.Buttons[RightTrigger] = 0.3
if !s.Pressed(South) || s.Pressed(LeftTrigger) || !s.Pressed(RightTrigger) {
t.Error("Pressed")
}
if !s.JustPressed(last, South) || s.JustPressed(s, South) || !last.JustReleased(s, South) {
t.Error("JustPressed, JustReleased")
}
// Within the dead zone, nothing; at the rim, all the way, in every
// direction alike.
s.Axes[LeftX], s.Axes[LeftY] = 0.1, 0.05
if x, y := s.LeftStick(); x != 0 || y != 0 {
t.Errorf("rest = %v, %v", x, y)
}
s.Axes[LeftX], s.Axes[LeftY] = 0.7071, 0.7071
if x, y := s.LeftStick(); math.Abs(math.Hypot(float64(x), float64(y))-1) > 1e-3 {
t.Errorf("diagonal rim = %v, %v", x, y)
}
s.Axes[RightX] = -(Deadzone + (1-Deadzone)/2)
if x, _ := s.RightStick(); math.Abs(float64(x)+0.5) > 1e-4 {
t.Errorf("half way past the dead zone = %v", x)
}
if s.Value(ButtonCount) != 0 || s.Axis(AxisCount) != 0 {
t.Error("out of range")
}
}