//go:build browser package slidestest import ( "net/http" "testing" "time" "github.com/chromedp/chromedp" ) // A change in Deck settings is taken back by one undo and brought back // by one redo. The document always went back; the field the person had // just used, still focused, kept showing the new value (a focused // control is not written into), so the undo looked as if it did nothing. type deckField struct{ name, sel, to string } func deckFields(themeNext string) []deckField { return []deckField{ {"arrive", `[name="rst-deck-arrive"]`, "rise"}, {"theme", "#rst-deck-theme", themeNext}, {"accent", "#rst-deck-accent", "#123456"}, {"font", "#rst-deck-font-heading", "Georgia, serif"}, } } func deckThemeNext(te *tidyEditor) string { var next string te.rig.Run(evalInto(&next, `(() => { const s = document.querySelector('#rst-deck-theme') return [...s.options].map(o => o.value).find(v => v && v !== s.value) })()`)) return next } func fieldValue(te *tidyEditor, sel string) string { var v string te.rig.Run(evalInto(&v, `document.querySelector(`+jsString(sel)+`).value`)) return v } func changeField(te *tidyEditor, sel, to string) { te.rig.Run(chromedp.Sleep(700*time.Millisecond), chromedp.Evaluate[chromedp.Void](changeJS(sel, to)), chromedp.Evaluate[chromedp.Void](`document.querySelector(`+jsString(sel)+`).focus()`), chromedp.Sleep(700*time.Millisecond)) } // With Deck settings open and the field still focused, Cmd/Ctrl-Z takes // the change back in one press and Cmd/Ctrl-Shift-Z puts it back. func TestOneUndoTakesBackADeckSettingsChange(t *testing.T) { te, _ := openTalkEditor(t) te.rig.Run(openDeckSettings()) for _, c := range deckFields(deckThemeNext(te)) { t.Run(c.name, func(t *testing.T) { before := fieldValue(te, c.sel) changeField(te, c.sel, c.to) if got := fieldValue(te, c.sel); got != c.to { t.Fatalf("after the change the field reads %q, want %q", got, c.to) } te.rig.Run(chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.Sleep(400*time.Millisecond)) if got := fieldValue(te, c.sel); got != before { t.Errorf("one undo: the field reads %q, want %q", got, before) } te.rig.Run(chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl|chromedp.ModifierShift)), chromedp.Sleep(400*time.Millisecond)) if got := fieldValue(te, c.sel); got != c.to { t.Errorf("one redo: the field reads %q, want %q", got, c.to) } }) } } // The dialog closed, one click on the toolbar's Undo takes it back. func TestOneToolbarUndoTakesBackADeckSettingsChange(t *testing.T) { te, _ := openTalkEditor(t) te.rig.Run(openDeckSettings()) fields := deckFields(deckThemeNext(te)) te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-deck-dialog]').close()`)) for _, c := range fields { t.Run(c.name, func(t *testing.T) { te.rig.Run(openDeckSettings()) before := fieldValue(te, c.sel) changeField(te, c.sel, c.to) te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-deck-dialog]').close()`), waitFor(`!document.querySelector('[slides-deck-dialog]').open`)) te.rig.Run(chromedp.Click(chromedp.CSS(`[slides-press][data-command="undo"]`)), chromedp.Sleep(400*time.Millisecond)) if got := fieldValue(te, c.sel); got != before { t.Errorf("one click on Undo: the field reads %q, want %q", got, before) } }) } } // A colleague's change arriving while a block-card field has unfinished // text does not replace it. func TestARemoteChangeKeepsUnfinishedTextInTheBlockCard(t *testing.T) { te, token := openTalkEditor(t) apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token, `{"ops":[{"op":"add_block","slide":"pressslide1","id":"shapeblk0001","type":"shape","region":"body","props":{"kind":"rect"}}]}`, http.StatusOK) te.rig.Run(waitFor(`document.querySelector('[slides-stage] [slides-block][data-id="shapeblk0001"]')`), chromedp.Click(chromedp.CSS(`[slides-stage] [slides-block][data-id="shapeblk0001"]`)), waitFor(`!document.querySelector('#rst-block-label').closest('[hidden]')`)) te.rig.Run(chromedp.Focus(chromedp.CSS(`#rst-block-label`)), chromedp.KeyEvent("half-typed")) apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token, `{"ops":[{"op":"set_meta","title":"Renamed by a colleague"}]}`, http.StatusOK) te.rig.Run(chromedp.Sleep(1500 * time.Millisecond)) if got := fieldValue(te, "#rst-block-label"); got != "half-typed" { t.Errorf("the field reads %q after a colleague's change, want the unfinished text", got) } } // Undo of a custom accent, and of a theme change, with the colour input // focused: the field shows the theme's own accent each time, worked out // from the restored theme and not from a stage that has not caught up. func TestUndoBackToTheThemeAccentWithTheColourInputFocused(t *testing.T) { te, _ := openTalkEditor(t) te.rig.Run(openDeckSettings()) acc0 := fieldValue(te, "#rst-deck-accent") next := deckThemeNext(te) changeField(te, "#rst-deck-theme", next) var acc1 string te.rig.Run(evalInto(&acc1, `document.querySelector('#rst-deck-accent').value`)) changeField(te, "#rst-deck-accent", "#123456") ctrlZ := chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl)) te.rig.Run(ctrlZ, chromedp.Sleep(400*time.Millisecond)) if got := fieldValue(te, "#rst-deck-accent"); got != acc1 { t.Errorf("custom accent undone: the field reads %q, want the new theme's %q", got, acc1) } te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.querySelector('#rst-deck-accent').focus()`), ctrlZ, chromedp.Sleep(400*time.Millisecond)) if got := fieldValue(te, "#rst-deck-accent"); got != acc0 { t.Errorf("theme undone: the field reads %q, want the first theme's %q", got, acc0) } }