| 1 | //go:build browser |
| 2 | |
| 3 | // A slide drawn at a press, held in a real Chromium: what only layout, |
| 4 | // computed style and the accessibility tree can show. The page is the |
| 5 | // test's own: render's output and slides.css, no app route. |
| 6 | package slidestest |
| 7 | |
| 8 | import ( |
| 9 | "encoding/json" |
| 10 | "fmt" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/slides/internal/slides/deck" |
| 14 | "amadan.net/oficina/slides/internal/slides/render" |
| 15 | "github.com/chromedp/chromedp" |
| 16 | ) |
| 17 | |
| 18 | func TestWaitingBoxIsHiddenAndKeepsItsSpace(t *testing.T) { |
| 19 | rig, _ := newRig(t) |
| 20 | link := func(word, href string) deck.Text { return deck.Text{{{T: word, Href: href}}} } |
| 21 | d := &deck.Deck{ |
| 22 | Meta: deck.Meta{Title: "Steps", Theme: "paper", Aspect: "16:9", Arrive: "fade"}, |
| 23 | Slides: []deck.Slide{{ |
| 24 | ID: "slide0001", Template: "title-body", Layout: "flow", |
| 25 | Blocks: []deck.Block{ |
| 26 | {ID: "blk0001", Type: "heading", Region: "title", Text: deck.Text{{{T: "Steps"}}}}, |
| 27 | {ID: "blk0002", Type: "text", Region: "body", Step: 1, Text: link("later", "https://example.com/later")}, |
| 28 | {ID: "blk0003", Type: "text", Region: "body", Text: link("now", "https://example.com/now")}, |
| 29 | }, |
| 30 | }}, |
| 31 | } |
| 32 | at := func(press int, id string) string { |
| 33 | out, err := render.SlideWith(d, 0, render.Desktop, render.Options{Reveal: &press}) |
| 34 | if err != nil { |
| 35 | t.Fatalf("render at press %d: %v", press, err) |
| 36 | } |
| 37 | return fmt.Sprintf(`<div id=%q style="width:1280px;position:absolute;left:0;top:%s">%s</div>`, id, map[string]string{"p0": "0", "p1": "1200px"}[id], out) |
| 38 | } |
| 39 | markup, _ := json.Marshal(at(0, "p0") + at(1, "p1")) |
| 40 | |
| 41 | var raw string |
| 42 | rig.Run( |
| 43 | chromedp.EmulateViewport(1280, 900), |
| 44 | chromedp.Navigate(rig.Origin+"/signin"), |
| 45 | evalValue(nil, `(() => { |
| 46 | const link = document.createElement('link'); |
| 47 | link.rel = 'stylesheet'; |
| 48 | link.href = '/static/slides.css'; |
| 49 | document.head.appendChild(link); |
| 50 | document.body.insertAdjacentHTML('beforeend', `+string(markup)+`); |
| 51 | return new Promise(res => link.addEventListener('load', () => res('ok'))); |
| 52 | })()`, awaitPromise), |
| 53 | evalInto(&raw, `(() => { |
| 54 | const boxes = id => [...document.querySelectorAll('#'+id+' [data-id]')].map(e => { |
| 55 | const r = e.getBoundingClientRect(), p = e.closest('[slides-slide]').getBoundingClientRect(); |
| 56 | return [e.dataset.id, r.x-p.x, r.y-p.y, r.width, r.height]; |
| 57 | }); |
| 58 | const later = document.querySelector('#p0 [data-id=blk0002]'); |
| 59 | const shown = document.querySelector('#p1 [data-id=blk0002]'); |
| 60 | later.querySelector('a').focus(); |
| 61 | const focusedLater = document.activeElement === later.querySelector('a'); |
| 62 | shown.querySelector('a').focus(); |
| 63 | return JSON.stringify({p0: boxes('p0'), p1: boxes('p1'), |
| 64 | laterVis: getComputedStyle(later).visibility, shownVis: getComputedStyle(shown).visibility, |
| 65 | focusedLater}); |
| 66 | })()`), |
| 67 | ) |
| 68 | var got struct { |
| 69 | P0, P1 [][]any |
| 70 | LaterVis, ShownVis string |
| 71 | FocusedLater bool |
| 72 | } |
| 73 | if err := json.Unmarshal([]byte(raw), &got); err != nil { |
| 74 | t.Fatalf("read the page: %v\n%s", err, raw) |
| 75 | } |
| 76 | if len(got.P0) != 4 { |
| 77 | t.Fatalf("press 0 has %d boxes, want 4 (the slide and its blocks): %v", len(got.P0), got.P0) |
| 78 | } |
| 79 | if fmt.Sprint(got.P0) != fmt.Sprint(got.P1) { |
| 80 | t.Errorf("the boxes moved between press 0 and press 1:\n p0 %v\n p1 %v", got.P0, got.P1) |
| 81 | } |
| 82 | if got.LaterVis != "hidden" { |
| 83 | t.Errorf("a waiting box has visibility %q, want hidden", got.LaterVis) |
| 84 | } |
| 85 | if got.ShownVis != "visible" { |
| 86 | t.Errorf("an arrived box has visibility %q, want visible", got.ShownVis) |
| 87 | } |
| 88 | if got.FocusedLater { |
| 89 | t.Error("a link in a waiting box took focus") |
| 90 | } |
| 91 | if roles := axNamed(t, rig, "#p0 [data-id=blk0002]", "later"); len(roles) != 0 { |
| 92 | t.Errorf("a waiting box is in the accessibility tree: %v", roles) |
| 93 | } |
| 94 | if roles := axNamed(t, rig, "#p0 [data-id=blk0003]", "now"); len(roles) == 0 { |
| 95 | t.Error("the control, a visible link, is not in the accessibility tree: the check proves nothing") |
| 96 | } |
| 97 | if roles := axNamed(t, rig, "#p1 [data-id=blk0002]", "later"); len(roles) == 0 { |
| 98 | t.Error("a box that has arrived is not in the accessibility tree") |
| 99 | } |
| 100 | } |
| 101 | |