Files
openchamber-android-native/third_party/mygo/ui/feedback_test.go
T

343 lines
11 KiB
Go
Raw Normal View History

package ui
import (
"runtime"
"testing"
"github.com/egoist/mygo/internal/platform"
)
func TestToastAction(t *testing.T) {
notes, undone := 3, 0
tt := coreNewTester(func(c *context) {
coreColumn(c).Fill().Padding(20).Children(func() {
coreTextf(c, "%d notes", notes)
if coreButton(c, "Delete").Clicked() {
notes--
c.ToastAction("Note deleted", "Undo", func() {
notes++
undone++
})
}
})
}, 400, 300)
tt.Click("Delete")
if notes != 2 || !tt.HasText("Note deleted") || !tt.HasText("Undo") {
t.Fatalf("after Delete: %d notes, %q", notes, tt.Texts())
}
// Assistive technology reads the toast, and reaches its button.
tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn})
accessNode(t, tt.h.access, platform.RoleStatus, "Note deleted")
accessNode(t, tt.h.access, platform.RoleButton, "Undo")
// The time stops while the pointer rests on the toast.
r, _ := tt.Find("Note deleted")
tt.Move(r.X+r.W/2, r.Y+r.H/2)
if !tt.rt.toastsPaused {
t.Error("the pointer on the toast did not stop its time")
}
tt.Click("Undo")
if notes != 3 || undone != 1 || tt.HasText("Note deleted") || !tt.HasText("3 notes") {
t.Errorf("after Undo: %d notes, %d undone, %q", notes, undone, tt.Texts())
}
}
func TestCheckboxGroup(t *testing.T) {
mail, calendar := false, false
changes := 0
tt := coreNewTester(func(c *context) {
if coreCheckboxGroup(c, "Notifications", func() {
coreCheckbox(c, &mail, "Mail")
coreCheckbox(c, &calendar, "Calendar")
}).Changed() {
changes++
}
}, 300, 200)
tt.Click("Notifications")
if !mail || !calendar || changes != 1 {
t.Fatalf("a click on the group: %v %v", mail, calendar)
}
tt.Click("Mail")
tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn})
if n := accessNode(t, tt.h.access, platform.RoleCheckBox, "Notifications"); n.States&platform.AccessMixed == 0 {
t.Fatalf("with one of two: %+v", n)
}
// Mixed, a click checks them all; all, it checks none.
tt.Click("Notifications")
if !mail || !calendar {
t.Fatalf("from mixed: %v %v", mail, calendar)
}
tt.Key(0, KeySpace)
if mail || calendar {
t.Errorf("Space with all checked: %v %v", mail, calendar)
}
accessNode(t, tt.h.access, platform.RoleGroup, "Notifications")
}
func TestBreadcrumbs(t *testing.T) {
path := []string{"Macintosh HD", "Users", "ada", "Documents"}
chosen := -1
tt := coreNewTester(func(c *context) {
coreBreadcrumbs(c, path, &chosen).Label("Path")
}, 500, 100)
tt.Click("Users")
if chosen != 1 {
t.Fatalf("a click on Users: %d", chosen)
}
tt.Click("Documents")
if chosen != 1 {
t.Errorf("a click on the last item: %d", chosen)
}
tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn})
accessNode(t, tt.h.access, platform.RoleNavigation, "Path")
accessNode(t, tt.h.access, platform.RoleLink, "ada")
if n := accessNode(t, tt.h.access, platform.RoleText, "Documents"); n.Current != platform.CurrentPage {
t.Errorf("the last item is current %v, not the page", n.Current)
}
for _, n := range tt.h.access.Nodes {
if n.Role == platform.RoleLink && n.Label == "Documents" {
t.Error("the last item is a link")
}
}
}
func TestAlertDialog(t *testing.T) {
open, deleted, canceled := true, 0, 0
tt := coreNewTester(func(c *context) {
coreButton(c, "Behind")
switch coreAlertDialog(c, &open, "Delete Notes?", "You can't undo this.", "Cancel", "Delete") {
case 0:
canceled++
case 1:
deleted++
}
}, 500, 400)
tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn})
if n := accessNode(t, tt.h.access, platform.RoleAlertDialog, "Delete Notes?"); n.Description != "You can't undo this." {
t.Errorf("the alert: %+v", n)
}
if !tt.Focused("Delete") {
t.Fatal("the default button has no focus")
}
// Assistive technology sees what is in the alert, the focus with it.
if n := accessNode(t, tt.h.access, platform.RoleButton, "Delete"); tt.h.access.Focus != n.ID {
t.Errorf("the focus on %d, not the default button", tt.h.access.Focus)
}
// A click outside does nothing.
tt.ClickAt(5, 5)
if !open {
t.Fatal("a click outside closed the alert")
}
// Escape closes it, choosing no button.
tt.Key(0, KeyEscape)
if open || canceled != 0 || deleted != 0 {
t.Fatalf("Escape: open %v, %d canceled, %d deleted", open, canceled, deleted)
}
open = true
tt.Frame()
tt.Click("Cancel")
if open || canceled != 1 {
t.Fatalf("Cancel: open %v, %d canceled", open, canceled)
}
open = true
tt.Frame()
tt.Key(0, KeyEnter)
if open || deleted != 1 {
t.Errorf("Enter: open %v, %d deleted", open, deleted)
}
}
// alertLayout builds an alert in a window of the width, and returns its box
// and its buttons'.
func alertLayout(t *testing.T, w float32, title, message string, buttons ...string) (panel Rect, boxes []Rect) {
t.Helper()
open := true
tt := coreNewTester(func(c *context) {
coreAlertDialog(c, &open, title, message, buttons...)
}, int(w), 400)
tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn})
n := accessNode(t, tt.h.access, platform.RoleAlertDialog, title)
panel = Rect{float32(n.Bounds.X), float32(n.Bounds.Y), float32(n.Bounds.W), float32(n.Bounds.H)}
for _, label := range buttons {
b := accessNode(t, tt.h.access, platform.RoleButton, label)
boxes = append(boxes, Rect{float32(b.Bounds.X), float32(b.Bounds.Y), float32(b.Bounds.W), float32(b.Bounds.H)})
}
return panel, boxes
}
// An alert's buttons stay inside it, however many and however long.
func TestAlertDialogFitsButtons(t *testing.T) {
inside := func(t *testing.T, panel Rect, boxes []Rect) {
t.Helper()
for i, b := range boxes {
if b.X < panel.X || b.X+b.W > panel.X+panel.W || b.Y+b.H > panel.Y+panel.H {
t.Errorf("button %d at %+v sticks out of the alert at %+v", i, b, panel)
}
}
}
labels := []string{"Cancel", "Shut Them Down", "Keep Running"}
const long = "One message long enough that it wraps onto lines of its own in the alert rather than widening it to the whole window."
// There is room: one line of buttons, in a wider alert.
panel, boxes := alertLayout(t, 900, "Quit OpenVM?", long, labels...)
inside(t, panel, boxes)
if boxes[0].Y+boxes[0].H <= boxes[2].Y {
t.Errorf("the buttons wrapped with room to spare: %+v in %+v", boxes, panel)
}
if boxes[2].X+boxes[2].W <= boxes[1].X {
t.Errorf("buttons out of order: %+v", boxes)
}
// The message wraps: it does not make the alert as wide as it is long.
if panel.W > 600 {
t.Errorf("the alert is %v wide: the text widened it", panel.W)
}
// Two short buttons: as wide as ever.
short, boxes := alertLayout(t, 900, "Delete Notes?", long, "Cancel", "Delete")
inside(t, short, boxes)
if short.W != 300 {
t.Errorf("an alert of short buttons is %v wide, want 300", short.W)
}
// A narrow window: the alert keeps inside it, the buttons wrap.
panel, boxes = alertLayout(t, 300, "Quit OpenVM?", long, labels...)
inside(t, panel, boxes)
if panel.X < 0 || panel.X+panel.W > 300 {
t.Errorf("the alert at %+v sticks out of the window", panel)
}
if panel.W != 260 {
t.Errorf("the alert in a narrow window is %v wide, want 260", panel.W)
}
if boxes[0].Y+boxes[0].H > boxes[2].Y {
t.Errorf("the buttons did not wrap: %+v", boxes)
}
// A label longer than the window lets the alert have: the button keeps
// inside it.
panel, boxes = alertLayout(t, 200, "Quit?", "", "Cancel", "A label too long to fit in a window as narrow as this one")
inside(t, panel, boxes)
if panel.X < 0 || panel.X+panel.W > 200 {
t.Errorf("the alert at %+v sticks out of the window", panel)
}
}
// Escape closes an alert whatever its buttons say: a localized one, one
// without a Cancel, one with a single button.
func TestAlertDialogEscape(t *testing.T) {
for _, buttons := range [][]string{{"取消", "删除"}, {"Not Now", "Delete"}, {"OK"}} {
open, got := true, []int{}
tt := coreNewTester(func(c *context) {
if i := coreAlertDialog(c, &open, "Delete Notes?", "", buttons...); i != -1 {
got = append(got, i)
}
}, 500, 400)
tt.Key(0, KeyEscape)
if open || len(got) != 1 || got[0] != AlertDismissed {
t.Errorf("%q: Escape left it open %v, returned %v", buttons, open, got)
}
}
}
// An alert that must be answered stays open on Escape, setting *open back,
// until a button is clicked.
func TestAlertDialogKeepOpen(t *testing.T) {
open, chosen := true, -1
tt := coreNewTester(func(c *context) {
switch i := coreAlertDialog(c, &open, "Update required", "", "Restart"); i {
case AlertDismissed:
open = true
case -1:
default:
chosen = i
}
}, 500, 400)
tt.Key(0, KeyEscape)
tt.Frame()
if !open || !tt.Focused("Restart") {
t.Fatalf("Escape: open %v, the focus on Restart %v", open, tt.Focused("Restart"))
}
tt.Key(0, KeyEnter)
tt.Frame()
if open || chosen != 0 {
t.Errorf("Enter: open %v, chose %d", open, chosen)
}
}
func TestFindBar(t *testing.T) {
open := false
query, current, moves := "", 0, 0
text := "the cat sat on the mat with the hat"
count := func() int {
if query == "" {
return 0
}
n := 0
for i := 0; i+len(query) <= len(text); i++ {
if text[i:i+len(query)] == query {
n++
}
}
return n
}
tt := coreNewTester(func(c *context) {
coreColumn(c).Fill().Children(func() {
if c.Shortcut(Cmd, KeyF) {
open = true
}
if coreFindBar(c, &open, &query, count(), &current).Changed() {
moves++
}
coreText(c, text).Padding(10)
})
}, 600, 300)
tt.Key(Cmd, KeyF)
if !open || !tt.Focused("Find") {
t.Fatalf("Cmd+F: open %v, the field focused %v", open, tt.Focused("Find"))
}
tt.Type("the")
if count() != 3 || !tt.HasText("1 of 3") {
t.Fatalf("typed: %d matches, %q", count(), tt.Texts())
}
tt.Key(0, KeyEnter)
tt.Key(0, KeyEnter)
if current != 2 || !tt.HasText("3 of 3") {
t.Fatalf("Enter twice: %d", current)
}
tt.Key(0, KeyEnter)
if current != 0 {
t.Fatalf("round the end: %d", current)
}
tt.Key(Shift, KeyEnter)
if current != 2 || moves != 4 {
t.Fatalf("Shift+Enter: %d (%d moves)", current, moves)
}
if runtime.GOOS == "darwin" {
tt.Key(Cmd, KeyG)
} else {
// From the field, which leaves function keys to shortcuts.
tt.Key(0, KeyF3)
}
if current != 0 {
t.Errorf("the next match's shortcut: %d", current)
}
tt.Click("Next")
if current != 1 {
t.Errorf("Next: %d", current)
}
tt.Click("Find")
tt.Key(0, KeyEscape)
if open {
t.Error("Escape left the bar open")
}
// Opened again, it focuses the field with what was found before chosen.
tt.Key(Cmd, KeyF)
tt.Type("x")
if query != "x" {
t.Errorf("typed over the last query: %q", query)
}
tt.Type("yz")
tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn})
if !tt.HasText("No matches") {
t.Errorf("no matches: %q", tt.Texts())
}
accessNode(t, tt.h.access, platform.RoleStatus, "No matches")
}