| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | import ( |
| 6 | "net/http" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/chromedp/chromedp" |
| 11 | ) |
| 12 | |
| 13 | // A change in Deck settings is taken back by one undo and brought back |
| 14 | // by one redo. The document always went back; the field the person had |
| 15 | // just used, still focused, kept showing the new value (a focused |
| 16 | // control is not written into), so the undo looked as if it did nothing. |
| 17 | |
| 18 | type deckField struct{ name, sel, to string } |
| 19 | |
| 20 | func deckFields(themeNext string) []deckField { |
| 21 | return []deckField{ |
| 22 | {"arrive", `[name="rst-deck-arrive"]`, "rise"}, |
| 23 | {"theme", "#rst-deck-theme", themeNext}, |
| 24 | {"accent", "#rst-deck-accent", "#123456"}, |
| 25 | {"font", "#rst-deck-font-heading", "Georgia, serif"}, |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func deckThemeNext(te *tidyEditor) string { |
| 30 | var next string |
| 31 | te.rig.Run(evalInto(&next, `(() => { |
| 32 | const s = document.querySelector('#rst-deck-theme') |
| 33 | return [...s.options].map(o => o.value).find(v => v && v !== s.value) |
| 34 | })()`)) |
| 35 | return next |
| 36 | } |
| 37 | |
| 38 | func fieldValue(te *tidyEditor, sel string) string { |
| 39 | var v string |
| 40 | te.rig.Run(evalInto(&v, `document.querySelector(`+jsString(sel)+`).value`)) |
| 41 | return v |
| 42 | } |
| 43 | |
| 44 | func changeField(te *tidyEditor, sel, to string) { |
| 45 | te.rig.Run(chromedp.Sleep(700*time.Millisecond), |
| 46 | chromedp.Evaluate[chromedp.Void](changeJS(sel, to)), |
| 47 | chromedp.Evaluate[chromedp.Void](`document.querySelector(`+jsString(sel)+`).focus()`), |
| 48 | chromedp.Sleep(700*time.Millisecond)) |
| 49 | } |
| 50 | |
| 51 | // With Deck settings open and the field still focused, Cmd/Ctrl-Z takes |
| 52 | // the change back in one press and Cmd/Ctrl-Shift-Z puts it back. |
| 53 | func TestOneUndoTakesBackADeckSettingsChange(t *testing.T) { |
| 54 | te, _ := openTalkEditor(t) |
| 55 | te.rig.Run(openDeckSettings()) |
| 56 | for _, c := range deckFields(deckThemeNext(te)) { |
| 57 | t.Run(c.name, func(t *testing.T) { |
| 58 | before := fieldValue(te, c.sel) |
| 59 | changeField(te, c.sel, c.to) |
| 60 | if got := fieldValue(te, c.sel); got != c.to { |
| 61 | t.Fatalf("after the change the field reads %q, want %q", got, c.to) |
| 62 | } |
| 63 | te.rig.Run(chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.Sleep(400*time.Millisecond)) |
| 64 | if got := fieldValue(te, c.sel); got != before { |
| 65 | t.Errorf("one undo: the field reads %q, want %q", got, before) |
| 66 | } |
| 67 | te.rig.Run(chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl|chromedp.ModifierShift)), chromedp.Sleep(400*time.Millisecond)) |
| 68 | if got := fieldValue(te, c.sel); got != c.to { |
| 69 | t.Errorf("one redo: the field reads %q, want %q", got, c.to) |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | // The dialog closed, one click on the toolbar's Undo takes it back. |
| 76 | func TestOneToolbarUndoTakesBackADeckSettingsChange(t *testing.T) { |
| 77 | te, _ := openTalkEditor(t) |
| 78 | te.rig.Run(openDeckSettings()) |
| 79 | fields := deckFields(deckThemeNext(te)) |
| 80 | te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-deck-dialog]').close()`)) |
| 81 | for _, c := range fields { |
| 82 | t.Run(c.name, func(t *testing.T) { |
| 83 | te.rig.Run(openDeckSettings()) |
| 84 | before := fieldValue(te, c.sel) |
| 85 | changeField(te, c.sel, c.to) |
| 86 | te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-deck-dialog]').close()`), waitFor(`!document.querySelector('[slides-deck-dialog]').open`)) |
| 87 | te.rig.Run(chromedp.Click(chromedp.CSS(`[slides-press][data-command="undo"]`)), chromedp.Sleep(400*time.Millisecond)) |
| 88 | if got := fieldValue(te, c.sel); got != before { |
| 89 | t.Errorf("one click on Undo: the field reads %q, want %q", got, before) |
| 90 | } |
| 91 | }) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // A colleague's change arriving while a block-card field has unfinished |
| 96 | // text does not replace it. |
| 97 | func TestARemoteChangeKeepsUnfinishedTextInTheBlockCard(t *testing.T) { |
| 98 | te, token := openTalkEditor(t) |
| 99 | apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token, |
| 100 | `{"ops":[{"op":"add_block","slide":"pressslide1","id":"shapeblk0001","type":"shape","region":"body","props":{"kind":"rect"}}]}`, http.StatusOK) |
| 101 | te.rig.Run(waitFor(`document.querySelector('[slides-stage] [slides-block][data-id="shapeblk0001"]')`), |
| 102 | chromedp.Click(chromedp.CSS(`[slides-stage] [slides-block][data-id="shapeblk0001"]`)), |
| 103 | waitFor(`!document.querySelector('#rst-block-label').closest('[hidden]')`)) |
| 104 | te.rig.Run(chromedp.Focus(chromedp.CSS(`#rst-block-label`)), chromedp.KeyEvent("half-typed")) |
| 105 | apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token, |
| 106 | `{"ops":[{"op":"set_meta","title":"Renamed by a colleague"}]}`, http.StatusOK) |
| 107 | te.rig.Run(chromedp.Sleep(1500 * time.Millisecond)) |
| 108 | if got := fieldValue(te, "#rst-block-label"); got != "half-typed" { |
| 109 | t.Errorf("the field reads %q after a colleague's change, want the unfinished text", got) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Undo of a custom accent, and of a theme change, with the colour input |
| 114 | // focused: the field shows the theme's own accent each time, worked out |
| 115 | // from the restored theme and not from a stage that has not caught up. |
| 116 | func TestUndoBackToTheThemeAccentWithTheColourInputFocused(t *testing.T) { |
| 117 | te, _ := openTalkEditor(t) |
| 118 | te.rig.Run(openDeckSettings()) |
| 119 | acc0 := fieldValue(te, "#rst-deck-accent") |
| 120 | next := deckThemeNext(te) |
| 121 | changeField(te, "#rst-deck-theme", next) |
| 122 | var acc1 string |
| 123 | te.rig.Run(evalInto(&acc1, `document.querySelector('#rst-deck-accent').value`)) |
| 124 | changeField(te, "#rst-deck-accent", "#123456") |
| 125 | ctrlZ := chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl)) |
| 126 | te.rig.Run(ctrlZ, chromedp.Sleep(400*time.Millisecond)) |
| 127 | if got := fieldValue(te, "#rst-deck-accent"); got != acc1 { |
| 128 | t.Errorf("custom accent undone: the field reads %q, want the new theme's %q", got, acc1) |
| 129 | } |
| 130 | te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('#rst-deck-accent').focus()`), ctrlZ, chromedp.Sleep(400*time.Millisecond)) |
| 131 | if got := fieldValue(te, "#rst-deck-accent"); got != acc0 { |
| 132 | t.Errorf("theme undone: the field reads %q, want the first theme's %q", got, acc0) |
| 133 | } |
| 134 | } |
| 135 | |