//go:build browser // What fell between the quieter editor's tasks (the whole-branch review, // items A1–A4 and A7), held in a real Chromium: a nudge that Tab leaves // behind is still written; Tab gets past a box hidden on the phone; the // box's menu closes when a colleague changes the slide's layout; the // round button follows the phone frame's scroll; and the keyboard has // somewhere to be after a box is deleted or moved from its menu. package slidestest import ( "math" "net/http" "strings" "testing" "time" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" ) // boxX is block id's left edge on slide s as the API reads it, or -1. func boxX(s map[string]any, id string) float64 { b, _ := blockOf(s, id) if b == nil { return -1 } box, _ := b["box"].(map[string]any) x, ok := box["x"].(float64) if !ok { return -1 } return x } // TestQuieterNudgeThenTabKeepsBoth (A1): a nudge is written once the // burst settles, 300ms on. Nudge one shape, Tab to the next and nudge it // with no wait between the keys: both moves reach the document. func TestQuieterNudgeThenTabKeepsBoth(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, keyboardDeck) }) token := mintAgent(t, te.app, te.cookies, "nudge-tab") te.openOn("keyscanvas01") const a, b = "keysshape001", "keysshape002" // From the top of the page, Tab to the first shape: its overlay. te.rig.Run(chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`)) te.tabTo(t, `[slides-stage] [slides-canvas-overlay]`, 40) te.rig.Run( waitFor(focusOnBox(a)), chromedp.KeyEvent(kb.ArrowRight), chromedp.KeyEvent(kb.Tab), chromedp.KeyEvent(kb.ArrowRight), waitFor(focusOnBox(b)), ) deadline := time.Now().Add(10 * time.Second) for { s := te.apiSlide(t, token, "keyscanvas01") if math.Abs(boxX(s, a)-0.105) < 0.002 && math.Abs(boxX(s, b)-0.305) < 0.002 { return } if time.Now().After(deadline) { t.Fatalf("after a nudge right, Tab, a nudge right: the first shape is at x %v (want 0.105), the second at %v (want 0.305)", boxX(s, a), boxX(s, b)) } time.Sleep(100 * time.Millisecond) } } // phoneHiddenDeck: keyboardDeck with its second shape hidden on a phone. const phoneHiddenDeck = `[ {"op":"add_slide","id":"keyscanvas01","layout":"canvas","template":"blank"}, {"op":"add_block","slide":"keyscanvas01","id":"keysshape001","type":"shape","props":{"kind":"rect"},"box":{"x":0.1,"y":0.1,"w":0.4,"h":0.3},"z":1}, {"op":"add_block","slide":"keyscanvas01","id":"keysshape002","type":"shape","props":{"kind":"ellipse"},"box":{"x":0.3,"y":0.2,"w":0.4,"h":0.3},"z":0}, {"op":"add_block","slide":"keyscanvas01","id":"keyshead0001","type":"heading","props":{"level":1},"box":{"x":0.1,"y":0.7,"w":0.8,"h":0.2},"z":2,"markdown":"After the shapes"}, {"op":"set_phone","slide":"keyscanvas01","id":"keysshape002","phone":{"hidden":true}}]` // TestQuieterTabSkipsAPhoneHiddenBox (A2): in Phone view a box hidden on // the phone is not drawn, so it is not a stop: Tab from the box before // it reaches the box after it, and Shift+Tab comes back. The hidden box // is still in the Outline. func TestQuieterTabSkipsAPhoneHiddenBox(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, phoneHiddenDeck) }) te.openOn("keyscanvas01") te.showIn("keyscanvas01", "phone") // slides.css hides a box hidden on a phone (display: none), but on a // canvas slide its rule loses to the canvas box's own display (a // renderer stylesheet matter, outside this test): the rule is put // back here as slides.css means it. te.rig.Run( chromedp.Evaluate[chromedp.Void](`document.head.insertAdjacentHTML('beforeend', '')`), waitFor(`!document.querySelector('`+boxSel("keysshape002")+`').checkVisibility()`), chromedp.Evaluate[chromedp.Void](`document.activeElement.blur()`), ) te.tabTo(t, `[slides-stage] [slides-canvas-overlay]`, 40) te.rig.Run(waitFor(focusOnBox("keysshape001"))) var s tabStep te.rig.Run(chromedp.KeyEvent(kb.Tab), evalInto(&s, tabStepJS)) if s.ID != "keyshead0001" { t.Errorf("Tab from the first shape in Phone view: %+v; want the heading, past the shape hidden on a phone", s) } te.rig.Run(chromedp.KeyEvent(kb.Tab, chromedp.KeyModifiers(chromedp.ModifierShift)), evalInto(&s, tabStepJS)) if s.ID != "keysshape001" || !s.Overlay { t.Errorf("Shift+Tab from the heading in Phone view: %+v; want the first shape's overlay", s) } var rows []string te.rig.Run(openOutline(), evalInto(&rows, `[...document.querySelectorAll('#slides-outline [data-select-block]')].map(b => b.dataset.selectBlock)`)) if !slicesHas(rows, "keysshape002") { t.Errorf("the Outline lists %v; want the box hidden on a phone there too", rows) } } // TestQuieterBoxMenuClosesOnALayoutChange (A3): the menu is open on a // canvas shape and a colleague makes the slide a flow slide: the menu // closes (its rows were the canvas slide's), and opened again it holds // the flow slide's rows. func TestQuieterBoxMenuClosesOnALayoutChange(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) token := mintAgent(t, te.app, te.cookies, "colleague") te.openOn("boxcanvas001") te.selectBoxByID(t, "cshape000001") te.rig.Run(openBoxMenu()) apiJSON(t, te.app, http.MethodPost, "/api/v1/decks/"+te.sqid()+"/ops", token, `{"ops":[{"op":"set_slide","id":"boxcanvas001","layout":"flow"}]}`, http.StatusOK) te.rig.Run(waitFor(`document.querySelector('[slides-stage] [slides-slide]')?.dataset.layout === 'flow'`)) var open bool var place bool te.rig.Run( evalInto(&open, `document.querySelector('[slides-box-menu]').matches(':popover-open')`), evalInto(&place, `document.querySelector('[slides-box-menu] [data-row="block-place"]').checkVisibility()`), ) if open || place { t.Errorf("after a colleague made the slide flow, the box's menu is open %v, Position and size drawn %v; want it closed", open, place) } te.selectBoxByID(t, "cshape000001") var rows []string te.rig.Run(openBoxMenu(), evalInto(&rows, menuRowsJS)) if want := "block-format block-move-up block-move-down region block-delete"; strings.Join(rows, " ") != want { t.Errorf("opened again on the flow slide the menu draws %v, want %s", rows, want) } } // longPhoneDeck: a flow slide whose heading is followed by forty // paragraphs, far taller than a phone. func longPhoneDeck() string { paras := "" for i := range 40 { if i > 0 { paras += `\n\n` } paras += "A paragraph of a slide far longer than a phone is tall." } return `[ {"op":"add_slide","id":"longphone001","layout":"flow","template":"title-body"}, {"op":"add_block","slide":"longphone001","id":"longhead0001","type":"heading","region":"heading","props":{"level":1},"markdown":"At the top"}, {"op":"add_block","slide":"longphone001","id":"longtext0001","type":"text","region":"body","markdown":"` + paras + `"}]` } // buttonOffJS is the round button's place against the selected box's, // as "dx,dy" in whole px, or "hidden". const buttonOffJS = `(() => { const b = document.querySelector('[slides-box-more]'), box = document.querySelector('[slides-stage] [slides-block][aria-selected="true"]') if (!b.checkVisibility() || !box) return 'hidden' const r = b.getBoundingClientRect(), q = box.getBoundingClientRect() return Math.round(r.left - q.left) + ',' + Math.round(r.top - q.top) })()` // TestQuieterRoundButtonFollowsThePhoneScroll (A4): in Phone view the // phone's screen scrolls, not the stage: the round button keeps its // place on the selected box as the screen scrolls, and is not drawn once // the box has scrolled out of the screen. func TestQuieterRoundButtonFollowsThePhoneScroll(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, longPhoneDeck()) }) te.showIn("longphone001", "phone") te.selectBoxByID(t, "longhead0001") var at string te.rig.Run(evalInto(&at, buttonOffJS)) if at == "hidden" { t.Fatal("the heading selected in Phone view: the round button is not drawn") } te.rig.Run( chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-stage-frame]').scrollTop = 40`), waitFor(`(`+buttonOffJS+`) === '`+at+`'`), ) te.rig.Run( chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-stage-frame]').scrollTop = 1e6`), waitFor(`(f => { const q = document.querySelector('`+boxSel("longhead0001")+`').getBoundingClientRect(), r = f.getBoundingClientRect(); return q.bottom < r.top })(document.querySelector('[slides-stage-frame]'))`), waitFor(`!document.querySelector('[slides-box-more]').checkVisibility()`), ) te.rig.Run( chromedp.Evaluate[chromedp.Void](`document.querySelector('[slides-stage-frame]').scrollTop = 0`), waitFor(`(`+buttonOffJS+`) === '`+at+`'`), ) } // TestQuieterFocusAfterTheBoxMenu (A7): the keyboard has somewhere to be // after the menu changes the slide. Delete from the menu puts focus on // the next box, so a second Delete deletes that one; a heading moved // down from the menu keeps focus, so Shift+F10 opens its menu. func TestQuieterFocusAfterTheBoxMenu(t *testing.T) { te := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, keyboardDeck) }) token := mintAgent(t, te.app, te.cookies, "after-menu") te.openOn("keyscanvas01") te.selectBoxByID(t, "keysshape001") te.rig.Run( pressBoxMenuRow("block-delete"), waitFor(`!document.querySelector('`+boxSel("keysshape001")+`')`), waitFor(focusOnBox("keysshape002")), chromedp.KeyEvent(kb.Delete), waitFor(`!document.querySelector('`+boxSel("keysshape002")+`')`), ) te.untilSlide(t, token, "keyscanvas01", "without both shapes", func(s map[string]any) bool { a, _ := blockOf(s, "keysshape001") b, _ := blockOf(s, "keysshape002") return a == nil && b == nil }) // Moved from the menu, a heading on a flow slide keeps focus. te2 := openTidyEditorOn(t, 1280, 720, func(a *testApp, cookies []*http.Cookie) string { return quieterDeck(t, a, cookies, boxMenuDeck) }) token2 := mintAgent(t, te2.app, te2.cookies, "after-move") te2.openOn("boxflow00001") te2.selectBoxByID(t, "boxhead00001") // The stage's DOM is replaced once the move is saved: the heading's // element now is marked, to know the new one when it comes. (In a // flow slide the heading stays first on the stage, in its region.) te2.rig.Run( chromedp.Evaluate[chromedp.Void](`document.querySelector('`+boxSel("boxhead00001")+`').dataset.before = 'yes'`), pressBoxMenuRow("block-move-down"), ) te2.untilSlide(t, token2, "boxflow00001", "the heading moved down", func(s map[string]any) bool { _, i := blockOf(s, "boxhead00001") return i == 1 }) te2.rig.Run( waitFor(`!document.querySelector('`+boxSel("boxhead00001")+`').dataset.before`), waitFor(focusOnBox("boxhead00001")), chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(chromedp.ModifierShift)), waitFor(`document.querySelector('[slides-box-menu]').matches(':popover-open')`), ) var on bool te2.rig.Run(evalInto(&on, `document.querySelector('`+boxSel("boxhead00001")+`').getAttribute('aria-selected') === 'true'`)) if !on { t.Error("Shift+F10 after moving the heading opened a menu, but not the heading's") } }