|
| 1 | +//go:build integration && linux |
| 2 | + |
| 3 | +package atspi |
| 4 | + |
| 5 | +import ( |
| 6 | + "bufio" |
| 7 | + "context" |
| 8 | + "errors" |
| 9 | + "maps" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/godbus/dbus/v5" |
| 17 | + "go.uber.org/zap" |
| 18 | + |
| 19 | + "github.com/y3owk1n/neru/internal/adapter/accessibility/ax" |
| 20 | +) |
| 21 | + |
| 22 | +// Both scan paths run against a fake application exported on a private |
| 23 | +// dbus-daemon, so the state reads and the editable-text rule are checked in |
| 24 | +// the walk and the Collection path themselves. |
| 25 | + |
| 26 | +// errStateUnavailable is what a fake element whose state read fails returns. |
| 27 | +var errStateUnavailable = errors.New("state unavailable") |
| 28 | + |
| 29 | +// fakeAccessible answers the AT-SPI calls a scan makes for one element. |
| 30 | +type fakeAccessible struct { |
| 31 | + ref accRef |
| 32 | + role string |
| 33 | + states []uint32 |
| 34 | + stateFails bool |
| 35 | + children []*fakeAccessible |
| 36 | +} |
| 37 | + |
| 38 | +func (f *fakeAccessible) GetRoleName() (string, *dbus.Error) { return f.role, nil } |
| 39 | + |
| 40 | +func (f *fakeAccessible) GetState() ([]uint32, *dbus.Error) { |
| 41 | + if f.stateFails { |
| 42 | + return nil, dbus.MakeFailedError(errStateUnavailable) |
| 43 | + } |
| 44 | + |
| 45 | + return f.states, nil |
| 46 | +} |
| 47 | + |
| 48 | +func (f *fakeAccessible) GetChildren() ([]accRef, *dbus.Error) { |
| 49 | + refs := make([]accRef, 0, len(f.children)) |
| 50 | + for _, child := range f.children { |
| 51 | + refs = append(refs, child.ref) |
| 52 | + } |
| 53 | + |
| 54 | + return refs, nil |
| 55 | +} |
| 56 | + |
| 57 | +func (f *fakeAccessible) GetExtents(uint32) (atspiExtents, *dbus.Error) { |
| 58 | + return atspiExtents{W: 10, H: 10}, nil |
| 59 | +} |
| 60 | + |
| 61 | +// GetMatches answers the Collection query as a toolkit does: the children |
| 62 | +// whose role is in the rule and that are showing. |
| 63 | +func (f *fakeAccessible) GetMatches( |
| 64 | + rule atspiMatchRule, |
| 65 | + _ uint32, |
| 66 | + _ int32, |
| 67 | + _ bool, |
| 68 | +) ([]accRef, *dbus.Error) { |
| 69 | + var refs []accRef |
| 70 | + |
| 71 | + for _, child := range f.children { |
| 72 | + roleID := atspiRoleNameToID[child.role] |
| 73 | + inRule := rule.Roles[roleID/atspiBitsPerWord]&(1<<(roleID%atspiBitsPerWord)) != 0 |
| 74 | + |
| 75 | + if inRule && atspiStateBit(child.states, atspiStateShowing) { |
| 76 | + refs = append(refs, child.ref) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return refs, nil |
| 81 | +} |
| 82 | + |
| 83 | +func TestClient_ScanPaths_ReadEditableTextAsEntry(t *testing.T) { |
| 84 | + address := startPrivateBus(t) |
| 85 | + |
| 86 | + app := connectBus(t, address) |
| 87 | + scanner := connectBus(t, address) |
| 88 | + |
| 89 | + busName := app.Names()[0] |
| 90 | + child := func(name, role string, states ...uint) *fakeAccessible { |
| 91 | + return &fakeAccessible{ |
| 92 | + ref: accRef{Name: busName, Path: dbus.ObjectPath("/fixture/" + name)}, |
| 93 | + role: role, |
| 94 | + states: stateBitsFor(states...), |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + stateFails := child("state_fails", atspiRoleText, atspiStateShowing, atspiStateEditable) |
| 99 | + stateFails.stateFails = true |
| 100 | + |
| 101 | + frame := child("frame", "frame", atspiStateShowing) |
| 102 | + frame.children = []*fakeAccessible{ |
| 103 | + child("editable", atspiRoleText, atspiStateShowing, atspiStateEditable), |
| 104 | + child("read_only", atspiRoleText, atspiStateShowing), |
| 105 | + child("hidden", atspiRoleText, atspiStateEditable), |
| 106 | + stateFails, |
| 107 | + child("button", "push button", atspiStateShowing), |
| 108 | + } |
| 109 | + |
| 110 | + for _, fake := range append([]*fakeAccessible{frame}, frame.children...) { |
| 111 | + for _, iface := range []string{atspiAccessibleIfc, atspiComponentIfc, atspiCollectionIfc} { |
| 112 | + err := app.Export(fake, fake.ref.Path, iface) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("export %s on %s: %v", iface, fake.ref.Path, err) |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + scans := map[string]func(*Client, map[string]struct{}) []ax.Node{ |
| 120 | + "walk": func(client *Client, roles map[string]struct{}) []ax.Node { |
| 121 | + var out []ax.Node |
| 122 | + |
| 123 | + visited := 0 |
| 124 | + client.walk(t.Context(), scanner, frame.ref, roles, 0, &out, &visited, 0, 0) |
| 125 | + |
| 126 | + return out |
| 127 | + }, |
| 128 | + "collection": func(client *Client, roles map[string]struct{}) []ax.Node { |
| 129 | + out, ok := client.collectViaCollection(t.Context(), scanner, frame.ref, roles, 0, 0) |
| 130 | + if !ok { |
| 131 | + t.Fatal("the Collection path fell back to the walk") |
| 132 | + } |
| 133 | + |
| 134 | + return out |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + tests := []struct { |
| 139 | + roles []string |
| 140 | + want map[string]string |
| 141 | + }{ |
| 142 | + { |
| 143 | + roles: []string{"text_field", "button"}, |
| 144 | + want: map[string]string{"editable": atspiRoleEntry, "button": "push button"}, |
| 145 | + }, |
| 146 | + { |
| 147 | + roles: []string{"static_text"}, |
| 148 | + want: map[string]string{"read_only": atspiRoleText}, |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + for path, scan := range scans { |
| 153 | + for _, test := range tests { |
| 154 | + t.Run(path+" "+strings.Join(test.roles, " "), func(t *testing.T) { |
| 155 | + roles := rolesSet(resolveLinuxRoles(t, test.roles...)) |
| 156 | + |
| 157 | + got := make(map[string]string) |
| 158 | + for _, node := range scan(New(zap.NewNop(), nil), roles) { |
| 159 | + got[strings.TrimPrefix(strings.Split(node.ID(), "@")[0], "/fixture/")] = node.Role() |
| 160 | + } |
| 161 | + |
| 162 | + if !maps.Equal(got, test.want) { |
| 163 | + t.Errorf("hinted %v, want %v", got, test.want) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +// startPrivateBus runs a dbus-daemon for this test alone and returns its |
| 171 | +// address. |
| 172 | +func startPrivateBus(t *testing.T) string { |
| 173 | + t.Helper() |
| 174 | + |
| 175 | + daemon, err := exec.LookPath("dbus-daemon") |
| 176 | + if err != nil { |
| 177 | + t.Skip("skipping: dbus-daemon is not installed") |
| 178 | + } |
| 179 | + |
| 180 | + ctx, cancel := context.WithCancel(context.Background()) |
| 181 | + |
| 182 | + cmd := exec.CommandContext(ctx, daemon, "--session", "--nofork", "--print-address=1", |
| 183 | + "--address=unix:path="+filepath.Join(t.TempDir(), "bus")) |
| 184 | + |
| 185 | + stdout, err := cmd.StdoutPipe() |
| 186 | + if err != nil { |
| 187 | + t.Fatalf("dbus-daemon stdout: %v", err) |
| 188 | + } |
| 189 | + |
| 190 | + err = cmd.Start() |
| 191 | + if err != nil { |
| 192 | + t.Fatalf("start dbus-daemon: %v", err) |
| 193 | + } |
| 194 | + |
| 195 | + t.Cleanup(func() { |
| 196 | + cancel() |
| 197 | + |
| 198 | + _ = cmd.Wait() |
| 199 | + }) |
| 200 | + |
| 201 | + lines := make(chan string, 1) |
| 202 | + |
| 203 | + go func() { |
| 204 | + line, _ := bufio.NewReader(stdout).ReadString('\n') |
| 205 | + lines <- strings.TrimSpace(line) |
| 206 | + }() |
| 207 | + |
| 208 | + select { |
| 209 | + case address := <-lines: |
| 210 | + if address == "" { |
| 211 | + t.Fatal("dbus-daemon printed no address") |
| 212 | + } |
| 213 | + |
| 214 | + return address |
| 215 | + case <-time.After(5 * time.Second): |
| 216 | + t.Fatal("dbus-daemon did not print its address within 5s") |
| 217 | + } |
| 218 | + |
| 219 | + return "" |
| 220 | +} |
| 221 | + |
| 222 | +func connectBus(t *testing.T, address string) *dbus.Conn { |
| 223 | + t.Helper() |
| 224 | + |
| 225 | + conn, err := dbus.Connect(address) |
| 226 | + if err != nil { |
| 227 | + t.Fatalf("connect to the private bus: %v", err) |
| 228 | + } |
| 229 | + |
| 230 | + t.Cleanup(func() { _ = conn.Close() }) |
| 231 | + |
| 232 | + return conn |
| 233 | +} |
| 234 | + |
| 235 | +// stateBitsFor packs AT-SPI state indices the way GetState returns them. |
| 236 | +func stateBitsFor(states ...uint) []uint32 { |
| 237 | + words := make([]uint32, atspiStateWords) |
| 238 | + for _, state := range states { |
| 239 | + words[state/atspiBitsPerWord] |= 1 << (state % atspiBitsPerWord) |
| 240 | + } |
| 241 | + |
| 242 | + return words |
| 243 | +} |
0 commit comments