oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3// The editor's round-2 polish, held in a real Chromium: the strip does
4// not draw over its own words, a reload is not somebody else arriving,
5// and a modal dialog owns the keyboard. Each of these was a defect a
6// person could see (polish audit, slides.md), and none of them is
7// visible to a markup test — they are geometry, a sessionStorage and a
8// key event's route through the page.
9package slidestest
10
11import (
12 "fmt"
13 "testing"
14 "time"
15
16 "amadan.net/rastrillo/rastrillo/harness"
17 "github.com/chromedp/chromedp"
18)
19
20const editorBound = `document.querySelector('[slides-stage] [data-block="heading"]')?.getAttribute('contenteditable') === 'true'`
21
22// openEditor signs in and opens the sample deck's editor, bound.
23func openEditor(t *testing.T) (*harness.Rig, *fakeMailer, string) {
24 t.Helper()
25 rig, mailer := newRig(t)
26 browserSignIn(t, rig, mailer)
27 var deck string
28 rig.Run(evalInto(&deck, `(document.querySelector('a[href^="/d/"]')?.getAttribute('href') ?? '').replace(/\/edit$/, '')`))
29 if !deckPath.MatchString(deck) {
30 t.Fatalf("the deck list links no deck: %q", deck)
31 }
32 rig.Run(
33 chromedp.Navigate(rig.Origin+deck+"/edit"),
34 chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)),
35 waitFor(editorBound),
36 )
37 return rig, mailer, deck
38}
39
40// stripGeometry measures every strip item: its label, its avatars and
41// its menu trigger, and reports each overlap, each trigger under the
42// 24px target minimum, and each avatar box outside its thumbnail. An
43// empty answer is the pass. Items outside the strip's scrollport are
44// measured too — getBoundingClientRect does not care.
45const stripGeometry = `(() => {
46 const r = el => el && el.getBoundingClientRect()
47 const hit = (a, b) => a && b && a.width > 0 && b.width > 0 &&
48 a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom
49 const inside = (a, b) => a.left >= b.left - 0.5 && a.right <= b.right + 0.5 && a.top >= b.top - 0.5 && a.bottom <= b.bottom + 0.5
50 const out = []
51 let avatars = 0
52 for (const li of document.querySelectorAll('[slides-strip-item]')) {
53 const id = li.dataset.slideId
54 const label = r(li.querySelector('[slides-strip-label]'))
55 const thumb = r(li.querySelector('[slides-thumb]'))
56 const av = r(li.querySelector('[slides-presence-avatars]'))
57 const menu = r(li.querySelector('[slides-strip-menu] > summary'))
58 if (av) avatars++
59 if (hit(label, av)) out.push(id + ': the label is under the avatars')
60 if (hit(label, menu)) out.push(id + ': the label is under the menu trigger')
61 if (av && !inside(av, thumb)) out.push(id + ': the avatars are outside the thumbnail')
62 if (menu && (menu.width < 24 || menu.height < 24)) out.push(id + ': the menu trigger is ' + menu.width + 'x' + menu.height + ', under 24x24')
63 if (menu && (menu.width > 32 || menu.height > 32)) out.push(id + ': the menu trigger is ' + menu.width + 'x' + menu.height + ', not a compact icon button')
64 if (menu && !inside(menu, thumb)) out.push(id + ': the menu trigger is outside the thumbnail')
65 }
66 if (avatars === 0) out.push('no strip item carries an avatar — nothing was measured')
67 return out.join('\n')
68})()`
69
70// TestBrowserStripDoesNotOverlapItself (polish audit, "PA RRENT
71// SLIDE"): with another tab's avatar on the current slide, the item's
72// label is clear of the avatars and of the menu trigger, the trigger is
73// a compact 24px-or-more target, and the words "Current slide" are for
74// a screen reader only — at 1440, where the strip is a column, and at
75// 800, where it is a row.
76func TestBrowserStripDoesNotOverlapItself(t *testing.T) {
77 rig, _, deck := openEditor(t)
78 var slide string
79 rig.Run(evalInto(&slide, `document.querySelector('[slides-editor]').dataset.slide`))
80
81 // A real second tab, so the avatar is presence-view.mjs's own and
82 // sits wherever the bundle puts it.
83 tab2, cancel := chromedp.NewContext(rig.Context())
84 defer cancel()
85 runOn(t, tab2,
86 chromedp.Navigate(rig.Origin+deck+"/edit"),
87 chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)),
88 waitFor(editorBound),
89 )
90 rig.Run(waitFor(`document.querySelector('[slides-strip-item][data-slide-id="` + slide + `"] [slides-presence-avatar]') !== null`))
91
92 for _, width := range []int64{1440, 800} {
93 rig.Run(
94 chromedp.EmulateViewport(width, 900),
95 // Park the pointer away from the strip: hover is one of the
96 // states that shows a menu trigger, and this is the resting
97 // look being measured.
98 chromedp.MouseEvent(chromedp.MouseMoved, float64(width)-5, 895),
99 chromedp.Sleep(300*time.Millisecond),
100 )
101 var problems string
102 rig.Run(evalInto(&problems, stripGeometry))
103 if problems != "" {
104 t.Errorf("at %dpx the strip overlaps itself:\n%s", width, problems)
105 }
106
107 // The resting menu trigger of an item that is neither current
108 // nor hovered nor focused is not drawn over its thumbnail.
109 var restingOpacity string
110 rig.Run(evalInto(&restingOpacity, `getComputedStyle(document.querySelector('[slides-strip-item]:not([data-current="true"]) [slides-strip-menu] > summary')).opacity`))
111 if restingOpacity != "0" {
112 t.Errorf("at %dpx a resting item's menu trigger has opacity %s, want 0 (shown on hover, focus or current only)", width, restingOpacity)
113 }
114 var currentOpacity string
115 rig.Run(evalInto(&currentOpacity, `getComputedStyle(document.querySelector('[slides-strip-item][data-current="true"] [slides-strip-menu] > summary')).opacity`))
116 if currentOpacity != "1" {
117 t.Errorf("at %dpx the current item's menu trigger has opacity %s, want 1", width, currentOpacity)
118 }
119
120 // "Current slide" is in the accessible name and not on screen.
121 var current struct {
122 Text string `json:"text"`
123 Width float64 `json:"width"`
124 Height float64 `json:"height"`
125 Upper string `json:"upper"`
126 }
127 rig.Run(evalInto(&current, `(() => {
128 const el = document.querySelector('[slides-strip-item][data-current="true"] [slides-strip-current]')
129 const b = el.getBoundingClientRect()
130 return { text: el.textContent, width: b.width, height: b.height, upper: getComputedStyle(el).textTransform }
131 })()`))
132 if current.Text == "" {
133 t.Errorf("at %dpx the current item lost its 'Current slide' text — a screen reader still needs it", width)
134 }
135 if current.Width > 1 || current.Height > 1 {
136 t.Errorf("at %dpx 'Current slide' is visible (%gx%g), want it for screen readers only", width, current.Width, current.Height)
137 }
138 if current.Upper == "uppercase" {
139 t.Errorf("at %dpx 'Current slide' is still uppercase", width)
140 }
141
142 // Hover and keyboard focus each show a resting item's trigger.
143 var at struct {
144 X float64 `json:"x"`
145 Y float64 `json:"y"`
146 }
147 rig.Run(evalInto(&at, `(() => {
148 const li = document.querySelector('[slides-strip-item]:not([data-current="true"])')
149 li.scrollIntoView({ block: 'nearest', inline: 'nearest' })
150 const b = li.getBoundingClientRect()
151 return { x: b.left + b.width / 2, y: b.top + b.height / 2 }
152 })()`))
153 var hoverOpacity string
154 rig.Run(
155 chromedp.MouseEvent(chromedp.MouseMoved, at.X, at.Y),
156 chromedp.Sleep(300*time.Millisecond),
157 evalInto(&hoverOpacity, `[...document.querySelectorAll('[slides-strip-item]:not([data-current="true"])')].filter(li => li.matches(':hover')).map(li => getComputedStyle(li.querySelector('[slides-strip-menu] > summary')).opacity).join(',')`),
158 )
159 if hoverOpacity != "1" {
160 t.Errorf("at %dpx a hovered item's menu trigger has opacity %q, want 1", width, hoverOpacity)
161 }
162 var focusOpacity string
163 rig.Run(
164 chromedp.MouseEvent(chromedp.MouseMoved, float64(width)-5, 895),
165 chromedp.Evaluate[chromedp.Void](`document.querySelectorAll('[slides-strip-item]:not([data-current="true"]) [slides-strip-link]')[1].focus()`),
166 chromedp.Sleep(300*time.Millisecond),
167 evalInto(&focusOpacity, `getComputedStyle(document.activeElement.closest('[slides-strip-item]').querySelector('[slides-strip-menu] > summary')).opacity`),
168 )
169 if focusOpacity != "1" {
170 t.Errorf("at %dpx a focused item's menu trigger has opacity %q, want 1", width, focusOpacity)
171 }
172 rig.Screen(`[slides-strip-nav]`, fmt.Sprintf("the strip at %dpx", width))
173 }
174}
175