oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package sheets
4
5import (
6 "context"
7 "fmt"
8 "net/http"
9 "net/http/httptest"
10 "testing"
11 "time"
12
13 "amadan.net/rastrillo/rastrillo/harness"
14 "github.com/chromedp/cdproto/input"
15 "github.com/chromedp/chromedp"
16)
17
18// TestFirstKeystrokeTypesInABrowser: Paul, 2026-11-26, "the first
19// character typed focusses the cell and doesn't actually type into it."
20// Typing on a selected cell replaces it from the first key (typestart.js),
21// including a dead key (´ then e) and an input method's composition, and
22// F2 or a double-click edits the cell keeping what it holds. Served through the
23// production stack, so the page runs under the real CSP.
24//
25// Keys go in as the browser receives them from the operating system:
26// Input.dispatchKeyEvent for the key, then Input.imeSetComposition and
27// Input.insertText for what the keyboard layout or input method makes of
28// it, in that order, to whatever has focus by then.
29func TestFirstKeystrokeTypesInABrowser(t *testing.T) {
30 app, cookies := signIn(t)
31 sheet, cells := newSheet(t, app, cookies, "Typing")
32 for r, raw := range []string{"old A1", "old A2", "old A3", "old A4", "old A5"} {
33 postCell(t, app, cookies, cells, fmt.Sprintf(`{"r":%d,"c":0,"raw":%q}`, r, raw))
34 }
35 rig := harness.New(t, func(string) http.Handler { return productionHandler(t, app) })
36 var set []chromedp.Action[chromedp.Void]
37 for _, c := range cookies {
38 set = append(set, setCookie(rig.Origin, c))
39 }
40 rig.Run(set...)
41
42 cell := func(r int) string { return fmt.Sprintf(`td[data-x="0"][data-y="%d"]`, r) }
43 editor := func(r int) string { return cell(r) + ` input, ` + cell(r) + ` textarea` }
44 key := func(kind input.DispatchKeyEventType, key, code string, vk int64, text string, mods int64) chromedp.Action[chromedp.Void] {
45 return command(input.DispatchKeyEvent, input.DispatchKeyEventParams{
46 Type: kind, Key: key, Code: code, WindowsVirtualKeyCode: vk, NativeVirtualKeyCode: vk,
47 Text: text, UnmodifiedText: text, Modifiers: mods,
48 })
49 }
50 press := func(k, code string, vk int64, text string) []chromedp.Action[chromedp.Void] {
51 kind := input.DispatchKeyEventTypeKeyDown
52 if text == "" {
53 kind = input.DispatchKeyEventTypeRawKeyDown
54 }
55 return []chromedp.Action[chromedp.Void]{key(kind, k, code, vk, text, 0), key(input.DispatchKeyEventTypeKeyUp, k, code, vk, "", 0)}
56 }
57 compose := func(text string) chromedp.Action[chromedp.Void] {
58 n := int64(len([]rune(text)))
59 return command(input.ImeSetComposition, input.ImeSetCompositionParams{Text: text, SelectionStart: n, SelectionEnd: n})
60 }
61 commit := func(text string) chromedp.Action[chromedp.Void] {
62 return command(input.InsertText, input.InsertTextParams{Text: text})
63 }
64 // Two frames: grid.js's fill of the editor ran on the next one.
65 settle := chromedp.Sleep(80 * time.Millisecond)
66 run := func(what string, actions ...chromedp.Action[chromedp.Void]) {
67 t.Helper()
68 ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second)
69 defer cancel()
70 if err := chromedp.Do(ctx, actions...); err != nil {
71 t.Fatalf("%s: %v", what, err)
72 }
73 }
74 value := func(r int) string {
75 t.Helper()
76 var v string
77 run("read the editor", evalInto(&v, fmt.Sprintf(`(document.querySelector(%q) || {}).value || ''`, editor(r))))
78 return v
79 }
80 saved := func(r int, want string) {
81 t.Helper()
82 deadline := time.Now().Add(5 * time.Second)
83 for {
84 read := decodeGrid(t, do(t, app, as(httptest.NewRequest(http.MethodGet, cells+"?rows=6&cols=2", nil), cookies)))
85 for _, c := range read.Cells {
86 if c.R == r && c.C == 0 && c.Raw == want {
87 return
88 }
89 }
90 if time.Now().After(deadline) {
91 t.Fatalf("row %d never saved %q: %+v", r+1, want, read.Cells)
92 }
93 time.Sleep(50 * time.Millisecond)
94 }
95 }
96 selectCell := func(r int) {
97 t.Helper()
98 var holder bool
99 run(fmt.Sprintf("select row %d", r+1),
100 chromedp.Click(chromedp.CSS(cell(r))),
101 chromedp.Sleep(50*time.Millisecond),
102 evalInto(&holder, `document.activeElement && document.activeElement.hasAttribute('data-type-holder')`),
103 )
104 if !holder {
105 t.Fatalf("row %d: a selected cell must hold the keyboard in a text field, or an input method never engages", r+1)
106 }
107 }
108
109 run("open the sheet",
110 chromedp.Navigate(fmt.Sprintf("%s/sheets/%d", rig.Origin, sheet.ID)),
111 chromedp.WaitVisible(chromedp.CSS(cell(4))),
112 chromedp.Sleep(200*time.Millisecond),
113 )
114
115 // A plain key replaces the cell, and stays typed.
116 selectCell(0)
117 run("type x", append(press("x", "KeyX", 88, "x"), settle)...)
118 if got := value(0); got != "x" {
119 t.Fatalf("typing x on a selected cell: the editor holds %q, want %q", got, "x")
120 }
121 run("type y, Enter", append(append(press("y", "KeyY", 89, "y"), press("Enter", "Enter", 13, "\r")...), settle)...)
122 saved(0, "xy")
123
124 // A dead key: ´ (Option-E on a Mac) composes with the next key.
125 selectCell(1)
126 run("dead key", key(input.DispatchKeyEventTypeRawKeyDown, "Dead", "KeyE", 69, "", 1), compose("´"),
127 key(input.DispatchKeyEventTypeKeyUp, "Dead", "KeyE", 69, "", 1), settle)
128 run("e", key(input.DispatchKeyEventTypeRawKeyDown, "e", "KeyE", 69, "", 0), commit("é"),
129 key(input.DispatchKeyEventTypeKeyUp, "e", "KeyE", 69, "", 0), settle)
130 if got := value(1); got != "é" {
131 t.Fatalf("´ then e on a selected cell: the editor holds %q, want é", got)
132 }
133 run("Enter", append(press("Enter", "Enter", 13, "\r"), settle)...)
134 saved(1, "é")
135
136 // An input method: its keys arrive as "Process", and it composes.
137 selectCell(2)
138 run("k", key(input.DispatchKeyEventTypeRawKeyDown, "Process", "KeyK", 229, "", 0), compose("k"), settle)
139 run("a", key(input.DispatchKeyEventTypeRawKeyDown, "Process", "KeyA", 229, "", 0), compose("か"), settle)
140 if got := value(2); got != "か" {
141 t.Fatalf("an input method's composition: the editor holds %q, want か", got)
142 }
143 run("confirm, Enter", commit("か"), settle)
144 run("Enter", append(press("Enter", "Enter", 13, "\r"), settle)...)
145 saved(2, "か")
146
147 // Review #72, 11: after a toolbar button (Bold) the keyboard is back
148 // on the cell's holder, so a dead key composes from the first key.
149 selectCell(0)
150 var holder bool
151 run("Bold with the pointer",
152 chromedp.Click(chromedp.CSS(`button[data-tool][data-cmd="bold"]`)),
153 chromedp.Sleep(100*time.Millisecond),
154 evalInto(&holder, `document.activeElement && document.activeElement.hasAttribute('data-type-holder')`),
155 )
156 if !holder {
157 t.Fatal("after Bold the keyboard is on the button: typing goes nowhere")
158 }
159 run("dead key after Bold", key(input.DispatchKeyEventTypeRawKeyDown, "Dead", "KeyE", 69, "", 1), compose("´"),
160 key(input.DispatchKeyEventTypeKeyUp, "Dead", "KeyE", 69, "", 1), commit("é"), settle)
161 if got := value(0); got != "é" {
162 t.Fatalf("´ then e after Bold: the editor holds %q, want é", got)
163 }
164 run("Escape", append(press("Escape", "Escape", 27, ""), settle)...)
165 var label string
166 run("the holder names its cell", chromedp.Sleep(50*time.Millisecond), evalInto(&label, `document.querySelector('[data-type-holder]').getAttribute('aria-label')`))
167 if label != "A1, xy" {
168 t.Errorf("the holder reads %q to a screen reader, want the cell's reference and value", label)
169 }
170
171 // Double-click and F2 keep the cell. (Enter on a selected cell moves
172 // down, as in Excel; that is unchanged.)
173 run("double-click", chromedp.DoubleClick(chromedp.CSS(cell(3))), settle)
174 if got := value(3); got != "old A4" {
175 t.Fatalf("double-click on a cell: the editor holds %q, want its contents", got)
176 }
177 run("Escape", append(press("Escape", "Escape", 27, ""), settle)...)
178 selectCell(4)
179 run("F2", append(press("F2", "F2", 113, ""), settle)...)
180 if got := value(4); got != "old A5" {
181 t.Fatalf("F2 on a selected cell: the editor holds %q, want its contents", got)
182 }
183}
184