diff --git a/README.md b/README.md index 2e69c09..c4edfe5 100644 --- a/README.md +++ b/README.md @@ -278,6 +278,17 @@ only its damage. It is a `Repainter` like the native backends: `Repaint`, from any goroutine, draws the next animation frame, so a result that arrives with nobody touching the page still reaches it. +The tab is readable by a screen reader. The tree's accessibility nodes (role, +name, value, focus, disabled, read-only, multiline, range, a stable key) become +webcanvas's accessibility mirror, a hidden DOM subtree beside the canvas, and +every `TextView` or `CodeEditor` gets webcanvas's text layer, so a text field is +one `textbox` that the browser's find, selection and assistive technology see. +Names come from widgets and reach the DOM only as text. A field with no name is +still published, as on the native bridges. The conversion is one exported +package, `github.com/go-widgets/window/webmirror`, which any scene drawn on a +webcanvas can reuse (`webmirror.Host` implements `webcanvas.A11yAware` and +`webcanvas.TextInputHost` over one or several widget trees). + ```html diff --git a/go.mod b/go.mod index a284fc0..60aadb5 100644 --- a/go.mod +++ b/go.mod @@ -17,8 +17,9 @@ require ( github.com/go-widgets/android v0.16.0 github.com/go-widgets/gpupainter v0.4.0 github.com/go-widgets/painter v0.17.0 - github.com/go-widgets/toolkit v0.356.0 - github.com/go-widgets/webcanvas v0.7.0 + github.com/go-widgets/textinput v0.1.1 + github.com/go-widgets/toolkit v0.370.0 + github.com/go-widgets/webcanvas v0.14.1 github.com/godbus/dbus/v5 v5.2.4 golang.org/x/sys v0.49.0 ) @@ -36,5 +37,5 @@ require ( github.com/go-richdoc/richdoc v0.7.0 // indirect github.com/go-typeset/bidi v0.3.1 // indirect github.com/go-widgets/mvvm v0.15.0 // indirect - github.com/go-widgets/textinput v0.1.0 // indirect + github.com/go-widgets/vcs v0.1.0 // indirect ) diff --git a/go.sum b/go.sum index 84d1e97..ba01e32 100644 --- a/go.sum +++ b/go.sum @@ -55,12 +55,14 @@ github.com/go-widgets/mvvm v0.15.0 h1:VvZiLB7gm+5CIEeQH3GNI53ytUndzOznT/fJvaG8hO github.com/go-widgets/mvvm v0.15.0/go.mod h1:ZJj5M8zDg/gdzcvdWnJgZj/fcV0qe84fALq+EQVzUOM= github.com/go-widgets/painter v0.17.0 h1:PB/Xr0flFR9qL2/RPhbJOeyU6PPzcGyf3Qx4RRngowg= github.com/go-widgets/painter v0.17.0/go.mod h1:8kERcZUoWNyLIas1NsMWiYynvvyx+hcB2cNIh0Bx0yc= -github.com/go-widgets/textinput v0.1.0 h1:ifx1RjbPIWtlokoBLV1ZwlH+0CeixoMbswlqhNvYNmw= -github.com/go-widgets/textinput v0.1.0/go.mod h1:Y/k3PDtUEMQ/4psCVKdmUStNWa/EsVAMcCKG2lEtQ5c= -github.com/go-widgets/toolkit v0.356.0 h1:perOh1Tcdw6VkxyaVmXvPHjt6lhsFchgIQjYgAXoGYc= -github.com/go-widgets/toolkit v0.356.0/go.mod h1:dAs4uFSnRAV0aJ1IZkxQld5PP5ZdZDOS9bf9DF4jqQ8= -github.com/go-widgets/webcanvas v0.7.0 h1:6+A1Hw9BBhBUmXEqceElUrc7M5HClVBpWBPEXBpkT2o= -github.com/go-widgets/webcanvas v0.7.0/go.mod h1:GF9MyBmSlH/aNk4RjNwnC0LfAT3fng33EOSW7+r4LHs= +github.com/go-widgets/textinput v0.1.1 h1:3NZD5CJm5XYSWA3YzQ64tXC86gnEQ0UlNPL5qJeIUSs= +github.com/go-widgets/textinput v0.1.1/go.mod h1:Y/k3PDtUEMQ/4psCVKdmUStNWa/EsVAMcCKG2lEtQ5c= +github.com/go-widgets/toolkit v0.370.0 h1:XjeCt7m+g3cj273qYJu4CAZdTdXWnbUseoURZStBUWk= +github.com/go-widgets/toolkit v0.370.0/go.mod h1:zVKiLPMvy2igZZtU7IQ1rqoH31+kb1z2C6PptbCwaHI= +github.com/go-widgets/vcs v0.1.0 h1:1pZQygSsKBWZepAgsTdqh1bwHBjPwaLM4p7bAAONGnk= +github.com/go-widgets/vcs v0.1.0/go.mod h1:c2SbtsVVLJUJAf/vOeMKkFWZRFyMAeDrY8rfyUyIevs= +github.com/go-widgets/webcanvas v0.14.1 h1:FKCY96ERrfLGX2D6QZmUGZ32qbwlAPFOMidkuY3E3UM= +github.com/go-widgets/webcanvas v0.14.1/go.mod h1:H0lf7ZOm5QLap3MyizCjb2ArVszjZd8lDLk1SRRCfZk= github.com/godbus/dbus/v5 v5.2.4 h1:F2gUCy++NMIxGmo9J+z5le/+ZC8W1OgeR51P4i9ZB/U= github.com/godbus/dbus/v5 v5.2.4/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= diff --git a/internal/tab/tree.go b/internal/tab/tree.go index 7726c04..b41d8bf 100644 --- a/internal/tab/tree.go +++ b/internal/tab/tree.go @@ -16,9 +16,11 @@ import ( "sync/atomic" "github.com/go-widgets/painter" + "github.com/go-widgets/textinput" "github.com/go-widgets/toolkit" "github.com/go-widgets/webcanvas" "github.com/go-widgets/window/internal/dnd" + "github.com/go-widgets/window/webmirror" ) // damageRenderer is the opt-in incremental repaint, as the other back-ends @@ -47,8 +49,24 @@ var ( _ webcanvas.Scroller = (*Tree)(nil) _ webcanvas.ModifierAware = (*Tree)(nil) _ webcanvas.RepaintAware = (*Tree)(nil) + _ webcanvas.A11yAware = (*Tree)(nil) + _ webcanvas.TextInputHost = (*Tree)(nil) ) +// host is the tree as webmirror sees it: the one conversion from the toolkit's +// accessibility tree to webcanvas's mirror and text layer, shared with the +// gallery. +func (t *Tree) host() webmirror.Host { + return webmirror.Host{Sources: func() []webmirror.Source { return webmirror.Single(t.root) }} +} + +// A11yNodes is the tree as the accessibility mirror shows it, read after each +// frame is drawn. +func (t *Tree) A11yNodes() []webcanvas.A11yNode { return t.host().A11yNodes() } + +// TextInputFor gives a TextView (and a CodeEditor) its text layer. +func (t *Tree) TextInputFor(key string) textinput.Client { return t.host().TextInputFor(key) } + // NewTree adapts root, laid out on a w×h surface painted with theme. func NewTree(root toolkit.Widget, w, h int, theme *toolkit.Theme) *Tree { if theme == nil { diff --git a/internal/tab/tree_a11y_test.go b/internal/tab/tree_a11y_test.go new file mode 100644 index 0000000..f43674c --- /dev/null +++ b/internal/tab/tree_a11y_test.go @@ -0,0 +1,46 @@ +// Copyright (c) the go-widgets/window authors. All rights reserved. +// +// SPDX-License-Identifier: BSD-3-Clause + +package tab + +import ( + "testing" + + "github.com/go-widgets/toolkit" +) + +// A Tree is what the browser tab hands webcanvas, so what it publishes is what +// a screen reader gets: the buttons by name and a text view as one textbox with +// its text layer. +func TestTreePublishesItsWidgets(t *testing.T) { + btn := toolkit.NewButton("Run", func() {}) + tv := toolkit.NewTextView("print(1)") + tv.AccessibleName = "Cell 1" + root := toolkit.NewVBox() + root.AddFixed(btn, 24) + root.AddFixed(tv, 90) + tr := NewTree(root, 320, 200, nil) + root.SetBounds(toolkit.Rect{W: 320, H: 200}) + + var button, text int + var tvKey string + for _, n := range tr.A11yNodes() { + switch { + case n.Role == "button" && n.Name == "Run": + button++ + case n.Role == "textbox" && n.Name == "Cell 1": + text++ + tvKey = n.Key + } + } + if button != 1 || text != 1 { + t.Fatalf("buttons %d, textboxes %d; want 1 and 1 (%+v)", button, text, tr.A11yNodes()) + } + if c := tr.TextInputFor(tvKey); c == nil { + t.Error("the text view has no text layer") + } + if c := tr.TextInputFor("nope"); c != nil { + t.Error("an unknown key got a text layer") + } +} diff --git a/webmirror/webmirror.go b/webmirror/webmirror.go new file mode 100644 index 0000000..0440637 --- /dev/null +++ b/webmirror/webmirror.go @@ -0,0 +1,175 @@ +// Copyright (c) the go-widgets/window authors. All rights reserved. +// +// SPDX-License-Identifier: BSD-3-Clause + +// Package webmirror feeds go-widgets/webcanvas's accessibility mirror and text +// layer from a toolkit widget tree. It is the one place a toolkit.A11yNode +// becomes a webcanvas.A11yNode, shared by the browser-tab backend of this +// module and by any other scene that draws on a webcanvas (the gallery). +// +// It is plain Go with no build tag: the conversion is tested natively, and only +// webcanvas itself touches the DOM. +// +// What it publishes follows internal/a11yrole, the rule the native bridges +// share: a node with no area is dropped, a static node with no name is dropped, +// and a nameless interactive node (a text field) is kept so a screen reader +// still announces the control. Names and values are passed through verbatim; +// webcanvas writes them with textContent and setAttribute only, so nothing here +// builds markup. +package webmirror + +import ( + "github.com/go-widgets/textinput" + "github.com/go-widgets/toolkit" + "github.com/go-widgets/webcanvas" + "github.com/go-widgets/window/internal/a11yrole" +) + +// Source is one widget tree a scene publishes. A scene made of several +// independent roots (the gallery draws a menu bar, three columns and a status +// bar, each its own tree) gives each a distinct Prefix, because every tree's +// own keys start "#0" and two trees of the same type would collide. +type Source struct { + // Prefix is put before the tree's keys ("|"). Empty for a + // scene with a single tree: the toolkit's keys are then published as they + // are. + Prefix string + // Root is the tree. + Root toolkit.Widget +} + +// Single is the sources of a scene with one tree. +func Single(root toolkit.Widget) []Source { return []Source{{Root: root}} } + +// Host implements [webcanvas.A11yAware] and [webcanvas.TextInputHost] over the +// trees Sources returns. Embed it in a scene, or call it from the scene's two +// methods. Sources is asked on every call, so a scene whose roots change (a +// dialog opened, a tab switched) needs no extra wiring. +type Host struct { + Sources func() []Source +} + +var ( + _ webcanvas.A11yAware = Host{} + _ webcanvas.TextInputHost = Host{} +) + +// A11yNodes returns the accessible nodes of every source, in reading order. +func (h Host) A11yNodes() []webcanvas.A11yNode { + if h.Sources == nil { + return nil + } + return Nodes(h.Sources()...) +} + +// TextInputFor returns the text-input client of the widget published under +// key, or nil when that widget is not a text-input client (an Entry is not one +// yet) or the key is unknown. +func (h Host) TextInputFor(key string) textinput.Client { + if h.Sources == nil { + return nil + } + return TextInput(key, h.Sources()...) +} + +// Nodes converts the accessibility trees of srcs into webcanvas nodes. +func Nodes(srcs ...Source) []webcanvas.A11yNode { + var out []webcanvas.A11yNode + for _, s := range srcs { + if s.Root == nil { + continue + } + for _, n := range a11yrole.Nodes(s.Root) { + out = append(out, Convert(n, s.Prefix)) + } + } + return out +} + +// Convert is the one conversion from a toolkit node to a webcanvas node. The +// role is the WAI-ARIA name the toolkit role already is; the value keeps the +// toolkit's words ("checked", "pressed", "selected"), which webcanvas maps to +// aria-checked and aria-pressed. +func Convert(n toolkit.A11yNode, prefix string) webcanvas.A11yNode { + return webcanvas.A11yNode{ + Key: Key(prefix, n.Key), + Role: string(n.Role), + Name: n.Name, + Value: n.Value, + X: n.Rect.X, + Y: n.Rect.Y, + W: n.Rect.W, + H: n.Rect.H, + Focused: n.Focused, + Disabled: n.Disabled, + ReadOnly: n.ReadOnly, + Multiline: n.Multiline, + HasRange: n.HasRange, + Min: n.Min, + Max: n.Max, + Now: n.Now, + } +} + +// Key is the published key of a toolkit key under prefix. +func Key(prefix, key string) string { + if prefix == "" { + return key + } + return prefix + "|" + key +} + +// TextInput returns the textinput.Client of the widget published under key in +// srcs, or nil. The toolkit walk reports nodes, not widgets, so the widgets are +// collected by the same traversal (same order, same filter) and paired with +// the nodes by position; if the two ever disagree the answer is nil and the +// node stays a plain textbox, never the wrong widget. +func TextInput(key string, srcs ...Source) textinput.Client { + for _, s := range srcs { + if s.Root == nil { + continue + } + nodes := toolkit.WalkA11y(s.Root) + ws := accessibleWidgets(s.Root) + if len(ws) != len(nodes) { + continue + } + for i, n := range nodes { + if Key(s.Prefix, n.Key) != key { + continue + } + if info := ws[i].(toolkit.Accessible).A11y(); info.Role != n.Role { + return nil + } + c, _ := ws[i].(textinput.Client) + return c + } + } + return nil +} + +// childContainer is the interface toolkit.WalkA11y descends through. +type childContainer interface{ Children() []toolkit.Widget } + +// accessibleWidgets is the widgets toolkit.WalkA11y turns into nodes, in the +// same order: accessible, and not RolePresentation, descending through every +// child container. +func accessibleWidgets(root toolkit.Widget) []toolkit.Widget { + var out []toolkit.Widget + var walk func(w toolkit.Widget) + walk = func(w toolkit.Widget) { + if w == nil { + return + } + if a, ok := w.(toolkit.Accessible); ok && a.A11y().Role != toolkit.RolePresentation { + out = append(out, w) + } + if c, ok := w.(childContainer); ok { + for _, k := range c.Children() { + walk(k) + } + } + } + walk(root) + return out +} diff --git a/webmirror/webmirror_test.go b/webmirror/webmirror_test.go new file mode 100644 index 0000000..a7b81ef --- /dev/null +++ b/webmirror/webmirror_test.go @@ -0,0 +1,294 @@ +// Copyright (c) the go-widgets/window authors. All rights reserved. +// +// SPDX-License-Identifier: BSD-3-Clause + +package webmirror + +import ( + "reflect" + "testing" + + "github.com/go-widgets/painter" + "github.com/go-widgets/toolkit" + "github.com/go-widgets/webcanvas" +) + +// fake is a container with a chosen A11yInfo, so the table can say exactly what +// each field of the toolkit's node becomes. +type fake struct { + toolkit.Base + info toolkit.A11yInfo + kids []toolkit.Widget +} + +func (f *fake) A11y() toolkit.A11yInfo { return f.info } +func (f *fake) Children() []toolkit.Widget { return f.kids } +func (f *fake) Draw(painter.Painter, *toolkit.Theme) {} +func (f *fake) OnEvent(toolkit.Event) {} + +func leaf(info toolkit.A11yInfo, r toolkit.Rect) *fake { + f := &fake{info: info} + f.SetBounds(r) + return f +} + +func area(x int) toolkit.Rect { return toolkit.Rect{X: x, Y: 2 * x, W: 30, H: 10} } + +func TestConvertEveryField(t *testing.T) { + in := toolkit.A11yNode{ + A11yInfo: toolkit.A11yInfo{ + Role: toolkit.RoleSlider, Name: "Volume", Value: "7 of 10", + Focused: true, Disabled: true, ReadOnly: true, Multiline: true, + HasRange: true, Min: 0, Max: 10, Now: 7, + }, + Rect: toolkit.Rect{X: 1, Y: 2, W: 3, H: 4}, + Key: "VBox#0/Slider#1", + } + want := webcanvas.A11yNode{ + Key: "VBox#0/Slider#1", Role: "slider", Name: "Volume", Value: "7 of 10", + X: 1, Y: 2, W: 3, H: 4, + Focused: true, Disabled: true, ReadOnly: true, Multiline: true, + HasRange: true, Min: 0, Max: 10, Now: 7, + } + if got := Convert(in, ""); got != want { + t.Errorf("Convert =\n%+v\nwant\n%+v", got, want) + } + want.Key = "col|VBox#0/Slider#1" + if got := Convert(in, "col"); got != want { + t.Errorf("Convert with prefix = %q, want %q", got.Key, want.Key) + } +} + +func TestNodesTable(t *testing.T) { + cases := []struct { + name string + info toolkit.A11yInfo + rect toolkit.Rect + want []webcanvas.A11yNode // nil: dropped + }{ + {"button", toolkit.A11yInfo{Role: toolkit.RoleButton, Name: "Save"}, area(1), + []webcanvas.A11yNode{{Key: "fake#0", Role: "button", Name: "Save", X: 1, Y: 2, W: 30, H: 10}}}, + {"pressed button keeps its value", toolkit.A11yInfo{Role: toolkit.RoleButton, Name: "Bold", Value: "pressed"}, area(1), + []webcanvas.A11yNode{{Key: "fake#0", Role: "button", Name: "Bold", Value: "pressed", X: 1, Y: 2, W: 30, H: 10}}}, + {"checked checkbox", toolkit.A11yInfo{Role: toolkit.RoleCheckbox, Name: "Agree", Value: "checked"}, area(1), + []webcanvas.A11yNode{{Key: "fake#0", Role: "checkbox", Name: "Agree", Value: "checked", X: 1, Y: 2, W: 30, H: 10}}}, + {"disabled", toolkit.A11yInfo{Role: toolkit.RoleButton, Name: "Off", Disabled: true}, area(1), + []webcanvas.A11yNode{{Key: "fake#0", Role: "button", Name: "Off", Disabled: true, X: 1, Y: 2, W: 30, H: 10}}}, + {"focused multiline textbox", toolkit.A11yInfo{Role: toolkit.RoleTextbox, Name: "Notes", Value: "a\nb", Focused: true, Multiline: true}, area(1), + []webcanvas.A11yNode{{Key: "fake#0", Role: "textbox", Name: "Notes", Value: "a\nb", Focused: true, Multiline: true, X: 1, Y: 2, W: 30, H: 10}}}, + {"range", toolkit.A11yInfo{Role: toolkit.RoleSlider, Name: "Vol", HasRange: true, Min: 1, Max: 9, Now: 4}, area(1), + []webcanvas.A11yNode{{Key: "fake#0", Role: "slider", Name: "Vol", HasRange: true, Min: 1, Max: 9, Now: 4, X: 1, Y: 2, W: 30, H: 10}}}, + // window#153/#154: a nameless field is still published, and its value + // stays the value. + {"nameless textbox is kept", toolkit.A11yInfo{Role: toolkit.RoleTextbox, Value: "hello"}, area(1), + []webcanvas.A11yNode{{Key: "fake#0", Role: "textbox", Value: "hello", X: 1, Y: 2, W: 30, H: 10}}}, + {"nameless button is kept", toolkit.A11yInfo{Role: toolkit.RoleButton}, area(1), + []webcanvas.A11yNode{{Key: "fake#0", Role: "button", X: 1, Y: 2, W: 30, H: 10}}}, + {"nameless text is dropped", toolkit.A11yInfo{Role: toolkit.RoleText}, area(1), nil}, + {"nameless group is dropped", toolkit.A11yInfo{Role: toolkit.RoleGroup}, area(1), nil}, + {"zero area is dropped", toolkit.A11yInfo{Role: toolkit.RoleButton, Name: "Hidden"}, toolkit.Rect{}, nil}, + {"markup stays text", toolkit.A11yInfo{Role: toolkit.RoleButton, Name: ``}, area(1), + []webcanvas.A11yNode{{Key: "fake#0", Role: "button", Name: ``, X: 1, Y: 2, W: 30, H: 10}}}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + got := Nodes(Single(leaf(c.info, c.rect))...) + if !reflect.DeepEqual(got, c.want) { + t.Errorf("Nodes =\n%+v\nwant\n%+v", got, c.want) + } + }) + } +} + +func TestNodesPrefixKeepsTwoTreesApart(t *testing.T) { + mk := func(name string) toolkit.Widget { + return leaf(toolkit.A11yInfo{Role: toolkit.RoleButton, Name: name}, area(1)) + } + got := Nodes(Source{Prefix: "a", Root: mk("one")}, Source{Prefix: "b", Root: mk("two")}, Source{Prefix: "c"}) + if len(got) != 2 || got[0].Key != "a|fake#0" || got[1].Key != "b|fake#0" { + t.Fatalf("Nodes = %+v, want keys a|fake#0 and b|fake#0 (a nil root publishes nothing)", got) + } + if got[0].Key == got[1].Key { + t.Fatal("two trees share a key") + } +} + +func TestNodesKeepReadingOrderAndKeys(t *testing.T) { + root := &fake{info: toolkit.A11yInfo{Role: toolkit.RoleGroup, Name: "Form"}} + root.SetBounds(toolkit.Rect{W: 100, H: 100}) + root.kids = []toolkit.Widget{ + leaf(toolkit.A11yInfo{Role: toolkit.RoleButton, Name: "A"}, area(1)), + leaf(toolkit.A11yInfo{Role: toolkit.RoleButton, Name: "B"}, area(2)), + } + got := Nodes(Single(root)...) + var keys []string + for _, n := range got { + keys = append(keys, n.Key+"="+n.Name) + } + want := []string{"fake#0=Form", "fake#0/fake#0=A", "fake#0/fake#1=B"} + if !reflect.DeepEqual(keys, want) { + t.Errorf("keys = %v, want %v", keys, want) + } +} + +func TestNilHostAndNilRoot(t *testing.T) { + var h Host + if h.A11yNodes() != nil || h.TextInputFor("x") != nil { + t.Error("a Host with no Sources answered") + } + if Nodes() != nil || TextInput("x", Source{}) != nil { + t.Error("nothing to read must give nothing") + } +} + +func TestRealWidgets(t *testing.T) { + btn := toolkit.NewButton("Save", func() {}) + chk := toolkit.NewCheckButton("Agree", true) + ent := toolkit.NewEntry("a@b.c") + ent.AccessibleName = "Email" + root := toolkit.NewVBox() + root.AddFixed(btn, 20) + root.AddFixed(chk, 20) + root.AddFixed(ent, 20) + root.SetBounds(toolkit.Rect{W: 200, H: 60}) + byName := map[string]webcanvas.A11yNode{} + for _, n := range Nodes(Single(root)...) { + byName[n.Role+":"+n.Name] = n + } + for _, k := range []string{"button:Save", "checkbox:Agree", "textbox:Email"} { + if _, ok := byName[k]; !ok { + t.Errorf("%s not published; got %v", k, byName) + } + } + if c := byName["checkbox:Agree"]; c.Value != "checked" { + t.Errorf("checkbox value = %q, want checked", c.Value) + } + if e := byName["textbox:Email"]; e.Value != "a@b.c" || e.Multiline { + t.Errorf("entry = %+v", e) + } +} + +func TestTextInputForTextView(t *testing.T) { + tv := toolkit.NewTextView("hello\nworld") + tv.AccessibleName = "Notes" + ent := toolkit.NewEntry("x") + ent.AccessibleName = "One line" + root := toolkit.NewVBox() + root.AddFixed(ent, 20) + root.AddFixed(tv, 80) + root.SetBounds(toolkit.Rect{W: 200, H: 120}) + + var tvKey, entKey string + for _, n := range Nodes(Single(root)...) { + switch n.Name { + case "Notes": + tvKey = n.Key + if !n.Multiline || n.Role != "textbox" { + t.Errorf("text view node = %+v", n) + } + case "One line": + entKey = n.Key + } + } + if tvKey == "" || entKey == "" { + t.Fatalf("keys not found: %q %q", tvKey, entKey) + } + h := Host{Sources: func() []Source { return Single(root) }} + c := h.TextInputFor(tvKey) + if c == nil { + t.Fatal("the TextView has no text-input client") + } + if got := c.(*toolkit.TextView); got != tv { + t.Error("the client is not the TextView that was published") + } + if h.TextInputFor(entKey) != nil { + t.Error("an Entry is not a text-input client yet") + } + if h.TextInputFor("no/such#0") != nil { + t.Error("an unknown key answered") + } + if got := len(h.A11yNodes()); got != len(Nodes(Single(root)...)) { + t.Errorf("Host.A11yNodes = %d nodes", got) + } +} + +func TestTextInputForPrefixAndMismatch(t *testing.T) { + tv := toolkit.NewTextView("x") + tv.AccessibleName = "N" + tv.SetBounds(toolkit.Rect{W: 50, H: 50}) + srcs := []Source{{Prefix: "p", Root: tv}} + var key string + for _, n := range Nodes(srcs...) { + key = n.Key + } + if TextInput(key, srcs...) == nil { + t.Errorf("prefixed key %q gave no client", key) + } + if TextInput("x|"+key, srcs...) != nil || TextInput(toolkit.WalkA11y(tv)[0].Key, srcs...) != nil { + t.Error("a key without the right prefix answered") + } +} + +// A presentation widget is looked through, as WalkA11y does: the widgets and +// the nodes must still pair up. +func TestPairingSurvivesPresentationAndNil(t *testing.T) { + tv := toolkit.NewTextView("x") + tv.AccessibleName = "N" + tv.SetBounds(toolkit.Rect{Y: 10, W: 50, H: 50}) + box := &fake{info: toolkit.A11yInfo{Role: toolkit.RolePresentation}, kids: []toolkit.Widget{nil, tv}} + box.SetBounds(toolkit.Rect{W: 60, H: 60}) + var key string + for _, n := range Nodes(Single(box)...) { + key = n.Key + } + if c := TextInput(key, Single(box)...); c == nil || c.(*toolkit.TextView) != tv { + t.Errorf("key %q did not reach the TextView", key) + } +} + +// Widgets that report an A11yInfo whose role changes between the two walks +// must not hand back the wrong client. +type shifty struct { + fake + calls int +} + +func (s *shifty) A11y() toolkit.A11yInfo { + s.calls++ + info := s.info + if s.calls > 2 { + info.Role = toolkit.RoleButton + } + return info +} + +func TestPairingRefusesAChangedWidget(t *testing.T) { + s := &shifty{fake: fake{info: toolkit.A11yInfo{Role: toolkit.RoleTextbox, Name: "N"}}} + s.SetBounds(toolkit.Rect{W: 5, H: 5}) + if c := TextInput("shifty#0", Single(s)...); c != nil { + t.Error("a widget that changed role was answered") + } +} + +// A count that cannot pair gives no client. +func TestUnpairableTreeGivesNil(t *testing.T) { + // The root reports one node on the first walk, none on the second. + l := &flicker{} + l.SetBounds(toolkit.Rect{W: 5, H: 5}) + if c := TextInput("flicker#0", Single(l)...); c != nil { + t.Error("unpairable tree answered") + } +} + +type flicker struct { + fake + calls int +} + +func (f *flicker) A11y() toolkit.A11yInfo { + f.calls++ + if f.calls == 1 { + return toolkit.A11yInfo{Role: toolkit.RoleTextbox, Name: "N"} + } + return toolkit.A11yInfo{Role: toolkit.RolePresentation} +}