//go:build browser
// A slide drawn at a press, held in a real Chromium: what only layout,
// computed style and the accessibility tree can show. The page is the
// test's own: render's output and slides.css, no app route.
package slidestest
import (
"encoding/json"
"fmt"
"testing"
"amadan.net/oficina/slides/internal/slides/deck"
"amadan.net/oficina/slides/internal/slides/render"
"github.com/chromedp/chromedp"
)
func TestWaitingBoxIsHiddenAndKeepsItsSpace(t *testing.T) {
rig, _ := newRig(t)
link := func(word, href string) deck.Text { return deck.Text{{{T: word, Href: href}}} }
d := &deck.Deck{
Meta: deck.Meta{Title: "Steps", Theme: "paper", Aspect: "16:9", Arrive: "fade"},
Slides: []deck.Slide{{
ID: "slide0001", Template: "title-body", Layout: "flow",
Blocks: []deck.Block{
{ID: "blk0001", Type: "heading", Region: "title", Text: deck.Text{{{T: "Steps"}}}},
{ID: "blk0002", Type: "text", Region: "body", Step: 1, Text: link("later", "https://example.com/later")},
{ID: "blk0003", Type: "text", Region: "body", Text: link("now", "https://example.com/now")},
},
}},
}
at := func(press int, id string) string {
out, err := render.SlideWith(d, 0, render.Desktop, render.Options{Reveal: &press})
if err != nil {
t.Fatalf("render at press %d: %v", press, err)
}
return fmt.Sprintf(`
%s
`, id, map[string]string{"p0": "0", "p1": "1200px"}[id], out)
}
markup, _ := json.Marshal(at(0, "p0") + at(1, "p1"))
var raw string
rig.Run(
chromedp.EmulateViewport(1280, 900),
chromedp.Navigate(rig.Origin+"/signin"),
evalValue(nil, `(() => {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = '/static/slides.css';
document.head.appendChild(link);
document.body.insertAdjacentHTML('beforeend', `+string(markup)+`);
return new Promise(res => link.addEventListener('load', () => res('ok')));
})()`, awaitPromise),
evalInto(&raw, `(() => {
const boxes = id => [...document.querySelectorAll('#'+id+' [data-id]')].map(e => {
const r = e.getBoundingClientRect(), p = e.closest('[slides-slide]').getBoundingClientRect();
return [e.dataset.id, r.x-p.x, r.y-p.y, r.width, r.height];
});
const later = document.querySelector('#p0 [data-id=blk0002]');
const shown = document.querySelector('#p1 [data-id=blk0002]');
later.querySelector('a').focus();
const focusedLater = document.activeElement === later.querySelector('a');
shown.querySelector('a').focus();
return JSON.stringify({p0: boxes('p0'), p1: boxes('p1'),
laterVis: getComputedStyle(later).visibility, shownVis: getComputedStyle(shown).visibility,
focusedLater});
})()`),
)
var got struct {
P0, P1 [][]any
LaterVis, ShownVis string
FocusedLater bool
}
if err := json.Unmarshal([]byte(raw), &got); err != nil {
t.Fatalf("read the page: %v\n%s", err, raw)
}
if len(got.P0) != 4 {
t.Fatalf("press 0 has %d boxes, want 4 (the slide and its blocks): %v", len(got.P0), got.P0)
}
if fmt.Sprint(got.P0) != fmt.Sprint(got.P1) {
t.Errorf("the boxes moved between press 0 and press 1:\n p0 %v\n p1 %v", got.P0, got.P1)
}
if got.LaterVis != "hidden" {
t.Errorf("a waiting box has visibility %q, want hidden", got.LaterVis)
}
if got.ShownVis != "visible" {
t.Errorf("an arrived box has visibility %q, want visible", got.ShownVis)
}
if got.FocusedLater {
t.Error("a link in a waiting box took focus")
}
if roles := axNamed(t, rig, "#p0 [data-id=blk0002]", "later"); len(roles) != 0 {
t.Errorf("a waiting box is in the accessibility tree: %v", roles)
}
if roles := axNamed(t, rig, "#p0 [data-id=blk0003]", "now"); len(roles) == 0 {
t.Error("the control, a visible link, is not in the accessibility tree: the check proves nothing")
}
if roles := axNamed(t, rig, "#p1 [data-id=blk0002]", "later"); len(roles) == 0 {
t.Error("a box that has arrived is not in the accessibility tree")
}
}