oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package mapatest
4
5import (
6 "context"
7 "fmt"
8 "strings"
9 "testing"
10 "time"
11
12 "amadan.net/rastrillo/rastrillo/harness"
13 "github.com/chromedp/cdproto/cdp"
14 "github.com/chromedp/cdproto/input"
15 "github.com/chromedp/cdproto/network"
16 "github.com/chromedp/chromedp"
17)
18
19// New team page (level-up §4.7) and the rail's tooltips (§3.2), in
20// Chromium (build note 77). The sidebar scrolls (overflow: auto), and
21// everything that left it was clipped: the popover's right side in the
22// open sidebar, all of it beside the rail, and every tooltip. Each check
23// asks the page what a pointer would hit at the corners of what must be
24// seen, so a box that is laid out but cut off fails.
25
26// opener is the sidebar's New team page control, as either shape has
27// drawn it (a <details>' summary before build note 77, now a button).
28const opener = `[mapa-side-foot] ` +
29 `:is(summary, [popovertarget])[mapa-side-item]`
30
31// sideCookie sets the mapa-sidebar cookie, or deletes it for "".
32func sideCookie(rig *harness.Rig,
33 state string) chromedp.Action[chromedp.Void] {
34 return chromedp.Func(func(ctx context.Context,
35 target *chromedp.Target) error {
36 where := rig.Origin + "/"
37 if state == "" {
38 _, err := cdp.Call(ctx, target, network.DeleteCookies,
39 network.DeleteCookiesParams{Name: "mapa-sidebar", URL: where})
40 return err
41 }
42 _, err := cdp.Call(ctx, target, network.SetCookie,
43 network.SetCookieParams{Name: "mapa-sidebar", Value: state,
44 URL: where})
45 return err
46 })
47}
48
49// cornersHit is, for each part inside box, every corner (3px in, past
50// a rounded border) where the topmost element is not inside box: ""
51// when the whole of every part can be seen and clicked.
52const cornersHit = `(function (box, parts) {
53 var holder = document.querySelector(box), misses = [];
54 parts.forEach(function (part) {
55 var el = holder && holder.querySelector(part);
56 if (!el) { misses.push(part + " is not drawn"); return; }
57 var r = el.getBoundingClientRect();
58 [[r.left + 3, r.top + 3], [r.right - 3, r.top + 3],
59 [r.left + 3, r.bottom - 3], [r.right - 3, r.bottom - 3]
60 ].forEach(function (p) {
61 var hit = document.elementFromPoint(p[0], p[1]);
62 if (hit && holder.contains(hit)) return;
63 misses.push(part + " at " + Math.round(p[0]) + "," + Math.round(p[1]) +
64 " is " + (hit ? hit.tagName.toLowerCase() : "off the screen"));
65 });
66 });
67 return misses.join("; ");
68})(%q, ["input[name=title]", "button[type=submit]"])`
69
70// sideCase is one sidebar state at one width: the cookie ("" for none)
71// and what the sidebar shows then.
72type sideCase struct {
73 width int64
74 cookie, show string
75}
76
77var sideCases = []sideCase{
78 {1400, "", "open"}, {1400, "rail", "rail"}, {1400, "open", "open"},
79 {900, "", "rail"}, {900, "open", "open"}, {900, "rail", "rail"},
80}
81
82// From the sidebar, in every state, scripts off and on: the whole form
83// is on top, focus is in the title, and Enter creates the page and opens
84// it. At 390 there is no sidebar: File → New team page is the screen.
85func TestNewTeamPageShowsTheWholeForm(t *testing.T) {
86 rig, _ := browserRig(t, 1)
87 for _, live := range []bool{false, true} {
88 for number, each := range sideCases {
89 name := fmt.Sprintf("%dpx, %s (cookie %q), scripts %v",
90 each.width, each.show, each.cookie, live)
91 drive(t, rig, scripts(live), sideCookie(rig, each.cookie),
92 chromedp.EmulateViewport(each.width, 877),
93 chromedp.Navigate(rig.Origin+"/pages"),
94 chromedp.WaitVisible("main h1"),
95 chromedp.Click(opener), chromedp.WaitVisible("#mapa-new-title"))
96 title := fmt.Sprintf("Sidebar %d %v", number, live)
97 createFrom(t, rig, name, "form[mapa-new-page-form]",
98 "mapa-new-title", title)
99 }
100 name := fmt.Sprintf("390px, File menu, scripts %v", live)
101 drive(t, rig, scripts(live), sideCookie(rig, ""),
102 chromedp.EmulateViewport(390, 844),
103 chromedp.Navigate(rig.Origin+"/pages"),
104 chromedp.Click(fileMenuOpener),
105 chromedp.Click(`#mapa-menu-file a[href="/new"]`),
106 chromedp.WaitVisible(`main input[name="title"]`))
107 createFrom(t, rig, name, `main form[action="/pages"]`, "title",
108 fmt.Sprintf("Phone %v", live))
109 }
110}
111
112// createFrom checks the open form, then types title into whatever has
113// focus and presses Enter, as a person would, and waits for the page.
114func createFrom(t *testing.T, rig *harness.Rig, name, form, field,
115 title string) {
116 t.Helper()
117 if misses := read(t, rig, chromedp.Evaluate[string](
118 fmt.Sprintf(cornersHit, form))); misses != "" {
119 t.Errorf("%s: the form is not all on top: %s", name, misses)
120 }
121 focus := read(t, rig, chromedp.Evaluate[string](
122 `document.activeElement.id || document.activeElement.name || ""`))
123 if focus != field {
124 t.Errorf("%s: focus is on %q, not the title", name, focus)
125 }
126 drive(t, rig, chromedp.KeyEvent(title), chromedp.KeyEvent("\r"))
127 if !landsOn(t, rig, title) {
128 t.Errorf("%s: Enter did not open a page titled %q", name, title)
129 }
130}
131
132// landsOn waits up to five seconds for a page titled title.
133func landsOn(t *testing.T, rig *harness.Rig, title string) bool {
134 t.Helper()
135 check := fmt.Sprintf(`location.pathname.indexOf("/p/") === 0 &&
136 (document.querySelector("h1[mapa-title]") || {}).textContent === %q`,
137 title)
138 for range 50 {
139 if read(t, rig, chromedp.Evaluate[bool](check)) {
140 return true
141 }
142 time.Sleep(100 * time.Millisecond)
143 }
144 return false
145}
146
147// A taken title is refused where it was typed, and what was typed
148// stays: in the popover with scripts, on the New page screen without.
149func TestNewTeamPageRefusesATakenTitleInPlace(t *testing.T) {
150 rig, store := browserRig(t, 0)
151 if _, err := store.CreateTeamPage(context.Background(), Bo,
152 "Onboarding", ""); err != nil {
153 t.Fatal(err)
154 }
155 rig.Allow("POST", "/pages", 422)
156 for _, live := range []bool{false, true} {
157 drive(t, rig, scripts(live), chromedp.EmulateViewport(1400, 877),
158 chromedp.Navigate(rig.Origin+"/pages"),
159 chromedp.Click(opener), chromedp.WaitVisible("#mapa-new-title"),
160 chromedp.KeyEvent("Onboarding"), chromedp.KeyEvent("\r"),
161 chromedp.Sleep(1200*time.Millisecond))
162 where := `[mapa-new-page]:popover-open`
163 if !live {
164 where = "main"
165 }
166 said := read(t, rig, chromedp.Evaluate[string](fmt.Sprintf(
167 `(document.querySelector(%q) || {}).textContent || ""`,
168 where+" [rst-field-error]:not([hidden])")))
169 kept := read(t, rig, chromedp.Evaluate[string](fmt.Sprintf(
170 `(document.querySelector(%q) || {}).value || ""`,
171 where+` input[name="title"]`)))
172 if !strings.Contains(said, "already exists") || kept != "Onboarding" {
173 t.Errorf("scripts %v: refusal %q, field %q", live, said, kept)
174 }
175 }
176}
177
178// The New page screen's Create and Cancel sit under its one field, as
179// on every other form (they were under a borderless body field half the
180// window tall).
181func TestTheNewPageScreenKeepsItsButtonsUnderTheField(t *testing.T) {
182 rig, _ := browserRig(t, 1)
183 gap := `Math.round(document.querySelector('main [rst-form-foot]')` +
184 `.getBoundingClientRect().top - document.querySelector(` +
185 `'main input[name="title"]').getBoundingClientRect().bottom)`
186 for _, live := range []bool{false, true} {
187 for _, size := range [][2]int64{{1402, 877}, {390, 844}} {
188 drive(t, rig, scripts(live),
189 chromedp.EmulateViewport(size[0], size[1]),
190 chromedp.Navigate(rig.Origin+"/new"),
191 chromedp.WaitVisible(`main input[name="title"]`))
192 if got := read(t, rig, chromedp.Evaluate[int](gap)); got < 0 ||
193 got > 48 {
194 t.Errorf("%dx%d, scripts %v: the buttons are %dpx under "+
195 "the field", size[0], size[1], live, got)
196 }
197 }
198 }
199}
200
201// labelSeen is "" when the hovered row's tooltip can be hit at all four
202// corners; it takes pointer events for the moment it is asked.
203const labelSeen = `(function (row) {
204 var label = document.querySelector(row + " [mapa-side-label]");
205 label.style.pointerEvents = "auto";
206 var r = label.getBoundingClientRect(), misses = [];
207 [[r.left + 3, r.top + 3], [r.right - 3, r.top + 3],
208 [r.left + 3, r.bottom - 3], [r.right - 3, r.bottom - 3]
209 ].forEach(function (p) {
210 if (!label.contains(document.elementFromPoint(p[0], p[1]))) {
211 misses.push(Math.round(p[0]) + "," + Math.round(p[1]));
212 }
213 });
214 label.style.pointerEvents = "";
215 return misses.length ? label.textContent + " cut off at " +
216 misses.join(" ") : "";
217})(%q)`
218
219// Every row of the rail shows its whole tooltip on hover: by width with
220// no cookie, and by cookie on a wide window.
221func TestTheRailsTooltipsAreWhole(t *testing.T) {
222 rig, _ := browserRig(t, 1, harness.WithHover())
223 mark := `Array.prototype.filter.call(document.querySelectorAll(` +
224 `"[mapa-side] [mapa-side-item]"), function (row) {` +
225 ` return row.offsetParent !== null; }).map(function (row, i) {` +
226 ` row.setAttribute("data-gate-row", i); return i; }).length`
227 for _, each := range []sideCase{{900, "", "rail"}, {1400, "rail", "rail"}} {
228 drive(t, rig, sideCookie(rig, each.cookie),
229 chromedp.EmulateViewport(each.width, 877),
230 chromedp.Navigate(rig.Origin+"/pages"),
231 chromedp.WaitVisible("main h1"))
232 rows := read(t, rig, chromedp.Evaluate[int](mark))
233 for row := range rows {
234 selector := fmt.Sprintf(`[data-gate-row="%d"]`, row)
235 drive(t, rig, rest(selector))
236 if miss := read(t, rig, chromedp.Evaluate[string](
237 fmt.Sprintf(labelSeen, selector))); miss != "" {
238 t.Errorf("%dpx (cookie %q): %s", each.width, each.cookie, miss)
239 }
240 }
241 }
242}
243
244// rest moves a hovering mouse to the middle of selector's element.
245func rest(selector string) chromedp.Action[chromedp.Void] {
246 return chromedp.Func(func(ctx context.Context,
247 target *chromedp.Target) error {
248 box, err := chromedp.Run(ctx, chromedp.Dimensions(selector))
249 if err != nil {
250 return err
251 }
252 quad := box.Content
253 across, down := (quad[0]+quad[4])/2, (quad[1]+quad[5])/2
254 return chromedp.Do(ctx, chromedp.MouseEvent(
255 input.DispatchMouseEventTypeMouseMoved, across, down),
256 chromedp.Sleep(80*time.Millisecond))
257 })
258}
259