| 1 | //go:build browser |
| 2 | |
| 3 | package sheets |
| 4 | |
| 5 | import ( |
| 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 | // TestFirstKeystrokeAfterLeavingTheGrid: #72's web canary. A pointer click |
| 19 | // outside the table (Bold, a menu item, the formula bar) makes Jspreadsheet |
| 20 | // drop the sheet as "current", and an editor opened by typing after it |
| 21 | // ignored Enter and Escape: Enter saved nothing, Escape cancelled nothing |
| 22 | // (the next click saved the discarded text), and Bold-then-type at once |
| 23 | // saved an empty cell when the format's repaint wiped the open editor. For |
| 24 | // each way of leaving the grid, type and press Enter (the value is saved), |
| 25 | // and type and press Escape (the cell keeps its old value, and the keyboard |
| 26 | // is back on the cell's text holder). |
| 27 | func TestFirstKeystrokeAfterLeavingTheGrid(t *testing.T) { |
| 28 | app, cookies := signIn(t) |
| 29 | sheet, cells := newSheet(t, app, cookies, "Leaving") |
| 30 | for r := 0; r < 14; r++ { |
| 31 | postCell(t, app, cookies, cells, fmt.Sprintf(`{"r":%d,"c":0,"raw":"old%d"}`, r, r)) |
| 32 | postCell(t, app, cookies, cells, fmt.Sprintf(`{"r":%d,"c":2,"raw":"c%d"}`, r, r)) |
| 33 | } |
| 34 | rig := harness.New(t, func(string) http.Handler { return productionHandler(t, app) }) |
| 35 | var set []chromedp.Action[chromedp.Void] |
| 36 | for _, c := range cookies { |
| 37 | set = append(set, setCookie(rig.Origin, c)) |
| 38 | } |
| 39 | rig.Run(set...) |
| 40 | |
| 41 | cell := func(r, c int) string { return fmt.Sprintf(`td[data-x="%d"][data-y="%d"]`, c, r) } |
| 42 | press := func(k, code string, vk int64, text string) []chromedp.Action[chromedp.Void] { |
| 43 | kind := input.DispatchKeyEventTypeKeyDown |
| 44 | if text == "" { |
| 45 | kind = input.DispatchKeyEventTypeRawKeyDown |
| 46 | } |
| 47 | return []chromedp.Action[chromedp.Void]{ |
| 48 | command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: kind, Key: k, Code: code, WindowsVirtualKeyCode: vk, NativeVirtualKeyCode: vk, Text: text, UnmodifiedText: text}), |
| 49 | command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: input.DispatchKeyEventTypeKeyUp, Key: k, Code: code, WindowsVirtualKeyCode: vk, NativeVirtualKeyCode: vk}), |
| 50 | } |
| 51 | } |
| 52 | run := func(what string, actions ...chromedp.Action[chromedp.Void]) { |
| 53 | t.Helper() |
| 54 | ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) |
| 55 | defer cancel() |
| 56 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 57 | t.Fatalf("%s: %v", what, err) |
| 58 | } |
| 59 | } |
| 60 | rawAt := func(r, col int) string { |
| 61 | read := decodeGrid(t, do(t, app, as(httptest.NewRequest(http.MethodGet, cells+"?rows=16&cols=3", nil), cookies))) |
| 62 | for _, c := range read.Cells { |
| 63 | if c.R == r && c.C == col { |
| 64 | return c.Raw |
| 65 | } |
| 66 | } |
| 67 | return "" |
| 68 | } |
| 69 | raw := func(r int) string { return rawAt(r, 0) } |
| 70 | selection := func() [4]int { |
| 71 | var s [4]int |
| 72 | run("read the selection", evalInto(&s, `(() => { const s = SheetsGrid.selection(); return [s.r, s.c, s.r2, s.c2]; })()`)) |
| 73 | return s |
| 74 | } |
| 75 | saved := func(what string, r int, want string) { |
| 76 | t.Helper() |
| 77 | deadline := time.Now().Add(4 * time.Second) |
| 78 | for raw(r) != want { |
| 79 | if time.Now().After(deadline) { |
| 80 | t.Fatalf("%s: row %d holds %q, want %q", what, r+1, raw(r), want) |
| 81 | } |
| 82 | time.Sleep(50 * time.Millisecond) |
| 83 | } |
| 84 | } |
| 85 | state := func() (editorOpen bool, holder bool) { |
| 86 | var got struct{ Editor, Holder bool } |
| 87 | run("read state", evalInto(&got, `({Editor: !!document.querySelector('td.editor'), Holder: !!(document.activeElement && document.activeElement.hasAttribute('data-type-holder'))})`)) |
| 88 | return got.Editor, got.Holder |
| 89 | } |
| 90 | menu := func(cmd string) chromedp.Action[chromedp.Void] { |
| 91 | return evalInto((*any)(nil), fmt.Sprintf(`(() => { |
| 92 | const s = document.querySelector('details[estilo-menubar-menu]:has(button[estilo-menu-item][data-cmd=%q]) > summary'); |
| 93 | s.click(); |
| 94 | return null; |
| 95 | })()`, cmd)) |
| 96 | } |
| 97 | pointerClick := func(sel string) chromedp.Action[chromedp.Void] { return chromedp.Click(chromedp.CSS(sel)) } |
| 98 | |
| 99 | ways := []struct { |
| 100 | name string |
| 101 | leave []chromedp.Action[chromedp.Void] |
| 102 | pause time.Duration |
| 103 | }{ |
| 104 | {"Bold on the toolbar", []chromedp.Action[chromedp.Void]{pointerClick(`button[data-tool][data-cmd="bold"]`)}, 250 * time.Millisecond}, |
| 105 | // Typing at once, inside the format's round trip: its repaint must |
| 106 | // not wipe the editor the key opened. |
| 107 | {"Bold on the toolbar, then typing at once", []chromedp.Action[chromedp.Void]{pointerClick(`button[data-tool][data-cmd="italic"]`)}, 0}, |
| 108 | {"Format › Bold", []chromedp.Action[chromedp.Void]{menu("bold"), chromedp.Sleep(150 * time.Millisecond), |
| 109 | pointerClick(`details[estilo-menubar-menu][open] button[estilo-menu-item][data-cmd="bold"]`)}, 250 * time.Millisecond}, |
| 110 | {"leaving the formula bar with Escape", append([]chromedp.Action[chromedp.Void]{pointerClick(`[data-formula-input]`)}, |
| 111 | press("Escape", "Escape", 27, "")...), 250 * time.Millisecond}, |
| 112 | } |
| 113 | run("open the sheet", chromedp.Navigate(fmt.Sprintf("%s/sheets/%d", rig.Origin, sheet.ID)), |
| 114 | chromedp.WaitVisible(chromedp.CSS(cell(13, 0))), chromedp.Sleep(200*time.Millisecond)) |
| 115 | for i, way := range ways { |
| 116 | enter, escape := 3*i, 3*i+2 |
| 117 | // Enter saves what was typed. |
| 118 | run(way.name+": select", pointerClick(cell(enter, 0)), chromedp.Sleep(150*time.Millisecond)) |
| 119 | run(way.name+": leave", append(way.leave, chromedp.Sleep(way.pause))...) |
| 120 | run(way.name+": type, Enter", append(append(press("q", "KeyQ", 81, "q"), chromedp.Sleep(100*time.Millisecond)), |
| 121 | append(press("Enter", "Enter", 13, "\r"), chromedp.Sleep(300*time.Millisecond))...)...) |
| 122 | if open, _ := state(); open { |
| 123 | t.Errorf("%s: Enter left the editor open", way.name) |
| 124 | } |
| 125 | saved(way.name+", Enter", enter, "q") |
| 126 | // Enter moved on, so the next value lands one row down, not over this one. |
| 127 | run(way.name+": type the next value", append(append(press("r", "KeyR", 82, "r"), chromedp.Sleep(100*time.Millisecond)), |
| 128 | append(press("Enter", "Enter", 13, "\r"), chromedp.Sleep(300*time.Millisecond))...)...) |
| 129 | saved(way.name+", the next value", enter+1, "r") |
| 130 | if got := raw(enter); got != "q" { |
| 131 | t.Errorf("%s: the second value overwrote the first: row %d holds %q", way.name, enter+1, got) |
| 132 | } |
| 133 | |
| 134 | // Escape cancels, and a click away saves nothing. |
| 135 | run(way.name+": select", pointerClick(cell(escape, 0)), chromedp.Sleep(150*time.Millisecond)) |
| 136 | run(way.name+": leave", append(way.leave, chromedp.Sleep(way.pause))...) |
| 137 | run(way.name+": type, Escape", append(append(press("q", "KeyQ", 81, "q"), chromedp.Sleep(100*time.Millisecond)), |
| 138 | append(press("Escape", "Escape", 27, ""), chromedp.Sleep(200*time.Millisecond))...)...) |
| 139 | open, holder := state() |
| 140 | if open { |
| 141 | t.Errorf("%s: Escape left the editor open", way.name) |
| 142 | } |
| 143 | if !holder { |
| 144 | t.Errorf("%s: after Escape the keyboard is not back on the cell's holder", way.name) |
| 145 | } |
| 146 | run(way.name+": click away", pointerClick(cell(15, 1)), chromedp.Sleep(300*time.Millisecond)) |
| 147 | if got := raw(escape); got != fmt.Sprintf("old%d", escape) { |
| 148 | t.Errorf("%s: Escape then a click away saved %q over old%d", way.name, got, escape) |
| 149 | } |
| 150 | |
| 151 | // The grid's own keys still work after leaving it: an arrow, an |
| 152 | // extending Shift+arrow, and Delete on what that selected. |
| 153 | top := 3 * i |
| 154 | run(way.name+": select in column C", pointerClick(cell(top, 2)), chromedp.Sleep(150*time.Millisecond)) |
| 155 | run(way.name+": leave", append(way.leave, chromedp.Sleep(250*time.Millisecond))...) |
| 156 | run(way.name+": ArrowDown", append(press("ArrowDown", "ArrowDown", 40, ""), chromedp.Sleep(150*time.Millisecond))...) |
| 157 | if s := selection(); s[0] != top+1 || s[1] != 2 { |
| 158 | t.Errorf("%s: ArrowDown left the selection at %v, want row %d", way.name, s, top+2) |
| 159 | } |
| 160 | run(way.name+": leave", append(way.leave, chromedp.Sleep(250*time.Millisecond))...) |
| 161 | run(way.name+": Shift+ArrowDown", command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: input.DispatchKeyEventTypeRawKeyDown, Key: "ArrowDown", Code: "ArrowDown", WindowsVirtualKeyCode: 40, Modifiers: 8}), |
| 162 | command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: input.DispatchKeyEventTypeKeyUp, Key: "ArrowDown", Code: "ArrowDown", WindowsVirtualKeyCode: 40, Modifiers: 8}), |
| 163 | chromedp.Sleep(150*time.Millisecond)) |
| 164 | if s := selection(); s[0] != top+1 || s[2] != top+2 { |
| 165 | t.Errorf("%s: Shift+ArrowDown selected %v, want rows %d to %d", way.name, s, top+2, top+3) |
| 166 | } |
| 167 | run(way.name+": leave", append(way.leave, chromedp.Sleep(250*time.Millisecond))...) |
| 168 | run(way.name+": Delete", append(press("Delete", "Delete", 46, ""), chromedp.Sleep(400*time.Millisecond))...) |
| 169 | if got := rawAt(top+1, 2); got != "" { |
| 170 | t.Errorf("%s: Delete left row %d of column C holding %q", way.name, top+2, got) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // TestFirstKeystrokeKeepsWhatLandsUnderAnOpenEditor: a batch for a cell |
| 176 | // whose editor is open must not wipe the editor (41ebeea), and must not be |
| 177 | // lost either. Fable's review of 58b4cb0: the skipped repaint was never |
| 178 | // applied, so C1 (=B1*2) kept showing 10 after B1 became 7 and its editor |
| 179 | // was closed, and a cell kept its old text while the server held a new one. |
| 180 | // The values arrive through the tab's own write path, so the order is fixed. |
| 181 | func TestFirstKeystrokeKeepsWhatLandsUnderAnOpenEditor(t *testing.T) { |
| 182 | app, cookies := signIn(t) |
| 183 | sheet, cells := newSheet(t, app, cookies, "Under an editor") |
| 184 | for _, body := range []string{`{"r":0,"c":0,"raw":"old0"}`, `{"r":1,"c":0,"raw":"old1"}`, `{"r":0,"c":1,"raw":"5"}`, `{"r":0,"c":2,"raw":"=B1*2"}`} { |
| 185 | postCell(t, app, cookies, cells, body) |
| 186 | } |
| 187 | rig := harness.New(t, func(string) http.Handler { return productionHandler(t, app) }) |
| 188 | var set []chromedp.Action[chromedp.Void] |
| 189 | for _, c := range cookies { |
| 190 | set = append(set, setCookie(rig.Origin, c)) |
| 191 | } |
| 192 | rig.Run(set...) |
| 193 | cell := func(r, c int) string { return fmt.Sprintf(`td[data-x="%d"][data-y="%d"]`, c, r) } |
| 194 | press := func(k, code string, vk int64, text string) []chromedp.Action[chromedp.Void] { |
| 195 | kind := input.DispatchKeyEventTypeKeyDown |
| 196 | if text == "" { |
| 197 | kind = input.DispatchKeyEventTypeRawKeyDown |
| 198 | } |
| 199 | return []chromedp.Action[chromedp.Void]{ |
| 200 | command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: kind, Key: k, Code: code, WindowsVirtualKeyCode: vk, NativeVirtualKeyCode: vk, Text: text, UnmodifiedText: text}), |
| 201 | command(input.DispatchKeyEvent, input.DispatchKeyEventParams{Type: input.DispatchKeyEventTypeKeyUp, Key: k, Code: code, WindowsVirtualKeyCode: vk, NativeVirtualKeyCode: vk}), |
| 202 | } |
| 203 | } |
| 204 | run := func(what string, actions ...chromedp.Action[chromedp.Void]) { |
| 205 | t.Helper() |
| 206 | ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) |
| 207 | defer cancel() |
| 208 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 209 | t.Fatalf("%s: %v", what, err) |
| 210 | } |
| 211 | } |
| 212 | shows := func(r, c int) string { |
| 213 | var text string |
| 214 | run("read a cell", evalInto(&text, fmt.Sprintf(`document.querySelector(%q).textContent`, cell(r, c)))) |
| 215 | return text |
| 216 | } |
| 217 | editorOpen := func() bool { |
| 218 | var open bool |
| 219 | run("editor?", evalInto(&open, `!!document.querySelector('td.editor')`)) |
| 220 | return open |
| 221 | } |
| 222 | edit := func(r, c int) { |
| 223 | t.Helper() |
| 224 | run("F2", append([]chromedp.Action[chromedp.Void]{chromedp.Click(chromedp.CSS(cell(r, c))), chromedp.Sleep(150 * time.Millisecond)}, |
| 225 | append(press("F2", "F2", 113, ""), chromedp.Sleep(150*time.Millisecond))...)...) |
| 226 | if !editorOpen() { |
| 227 | t.Fatalf("F2 opened no editor on row %d column %d", r+1, c+1) |
| 228 | } |
| 229 | } |
| 230 | write := func(r, c int, raw string) { |
| 231 | run("a write lands", evalInto((*any)(nil), fmt.Sprintf(`SheetsGrid.write(%d, %d, %q), null`, r, c, raw)), chromedp.Sleep(600*time.Millisecond)) |
| 232 | } |
| 233 | |
| 234 | run("open the sheet", chromedp.Navigate(fmt.Sprintf("%s/sheets/%d", rig.Origin, sheet.ID)), |
| 235 | chromedp.WaitVisible(chromedp.CSS(cell(1, 0))), chromedp.Sleep(200*time.Millisecond)) |
| 236 | if got := shows(0, 2); got != "10" { |
| 237 | t.Fatalf("C1 shows %q before anything, want 10", got) |
| 238 | } |
| 239 | |
| 240 | // A dependent recomputed under C1's editor, then Escape. |
| 241 | edit(0, 2) |
| 242 | write(0, 1, "7") |
| 243 | run("Escape", append(press("Escape", "Escape", 27, ""), chromedp.Sleep(200*time.Millisecond))...) |
| 244 | if got := shows(0, 2); got != "14" { |
| 245 | t.Errorf("C1 shows %q after B1 became 7 under its editor, want 14", got) |
| 246 | } |
| 247 | |
| 248 | // The cell's own new value under its editor, then Escape. |
| 249 | edit(0, 0) |
| 250 | write(0, 0, "fresh") |
| 251 | run("Escape", append(press("Escape", "Escape", 27, ""), chromedp.Sleep(200*time.Millisecond))...) |
| 252 | if got := shows(0, 0); got != "fresh" { |
| 253 | t.Errorf("A1 shows %q after a new value landed under its editor, want fresh", got) |
| 254 | } |
| 255 | |
| 256 | // Enter on text nobody changed writes nothing, so it shows the new value |
| 257 | // and does not put the old one back on the server. |
| 258 | edit(1, 0) |
| 259 | write(1, 0, "fresh2") |
| 260 | run("Enter", append(press("Enter", "Enter", 13, "\r"), chromedp.Sleep(500*time.Millisecond))...) |
| 261 | if got := shows(1, 0); got != "fresh2" { |
| 262 | t.Errorf("A2 shows %q after Enter on unchanged text, want fresh2", got) |
| 263 | } |
| 264 | read := decodeGrid(t, do(t, app, as(httptest.NewRequest(http.MethodGet, cells+"?rows=4&cols=3", nil), cookies))) |
| 265 | for _, c := range read.Cells { |
| 266 | if c.R == 1 && c.C == 0 && c.Raw != "fresh2" { |
| 267 | t.Errorf("Enter on unchanged text wrote %q over fresh2", c.Raw) |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |