oficina / home Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3// The browser checks: Home in a real Chromium. Not part of the plain
4// suite, because they need a Chromium (on PATH, via RASTRILLO_CHROME, or
5// in a Playwright cache):
6//
7// make test-browser
8//
9// `make test-vet` vets this file with the tag, so it cannot stop
10// compiling unnoticed.
11package home
12
13import (
14 "context"
15 "fmt"
16 "net/http"
17 "os"
18 "os/exec"
19 "strings"
20 "testing"
21 "time"
22
23 "amadan.net/rastrillo/rastrillo/harness"
24 "github.com/chromedp/cdproto/emulation"
25 "github.com/chromedp/chromedp"
26)
27
28// textControls is every control a tap puts a caret in. A checkbox, a
29// radio, a file picker or a button raises no keyboard, so iOS never
30// zooms for them.
31const textControls = `input:not([type=checkbox]):not([type=radio]):not([type=hidden]):not([type=file]):not([type=submit]):not([type=button]):not([type=color]):not([type=range]), select, textarea`
32
33// useChrome points rastrillo's harness at a Chromium it would not find by
34// itself: when RASTRILLO_CHROME is unset and no Chromium is on PATH, the
35// newest one in a Playwright cache, the macOS layouts included
36// (playwrightChrome, chromefinder_test.go). Call it before harness.New.
37func useChrome(t *testing.T) {
38 t.Helper()
39 if os.Getenv("RASTRILLO_CHROME") != "" {
40 return
41 }
42 for _, name := range []string{"chromium", "chromium-browser", "google-chrome", "google-chrome-stable"} {
43 if _, err := exec.LookPath(name); err == nil {
44 return
45 }
46 }
47 home, err := os.UserHomeDir()
48 if err != nil {
49 return
50 }
51 if p := playwrightChrome(home); p != "" {
52 t.Setenv("RASTRILLO_CHROME", p)
53 }
54}
55
56// asHost serves h as if every request had arrived for host. The rig's
57// browser talks to http://localhost:<port>, and Home answers only its two
58// configured hosts (serveKnownHost); this is the browser-side half of the
59// production TLS front, which routes by Host.
60func asHost(host string, h http.Handler) func(string) http.Handler {
61 return func(string) http.Handler {
62 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
63 r.Host = host
64 h.ServeHTTP(w, r)
65 })
66 }
67}
68
69// TestNoZoomOnTap: iOS Safari zooms the whole page into any text control
70// whose type is under 16px, and rastrillo's base type is 14px. Its
71// tokens.css brings text controls up to 16px where the pointer is coarse
72// ("Touch: the two zooms nobody asked for"); an app on a tokens.css
73// without that block, or with a rule of its own that sets a control
74// smaller, zooms on the first tap of every field.
75//
76// Every app's sign-in goes through id.*'s field, so that is the one that
77// matters most. The team-home pages (the directory, the team chooser,
78// signed out, Open source) are measured too: they carry no text field
79// today, and this keeps any they gain honest.
80//
81// Emulated as pointer: coarse, which is how the CSS decides.
82func TestNoZoomOnTap(t *testing.T) {
83 f := newMountedChooser(t)
84 cookie := f.session(t, chooserPerson)
85 // Two teams with Docs, so /apps/docs renders the chooser rather than
86 // redirecting to the one team.
87 f.team(t, "A1", "alpha", "Alpha", chooserPerson)
88 f.team(t, "B2", "beta", "Beta", chooserPerson)
89
90 type page struct{ path, ready string }
91 hosts := []struct {
92 name string
93 handler func(string) http.Handler
94 signIn *http.Cookie
95 pages []page
96 }{
97 {"id.*", asHost(credentialHost, f.p.credential.Router()), nil, []page{
98 {"/signin", `input[name="address"]`},
99 {"/passkey/confirm", `input[name="code"]`},
100 }},
101 {"directory, signed out", asHost(directoryHost, f.p.directory.Router()), nil, []page{
102 {"/signed-out", `main`},
103 {"/help/open-source", `main`},
104 }},
105 {"directory, signed in", asHost(directoryHost, f.p.directory.Router()), cookie, []page{
106 {"/", `[home-apps]`},
107 {docsEntryPath, `button[name="team"]`},
108 }},
109 }
110 useChrome(t)
111 for _, host := range hosts {
112 rig := harness.New(t, host.handler)
113 run := func(what string, actions ...chromedp.Action[chromedp.Void]) {
114 t.Helper()
115 ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second)
116 defer cancel()
117 if err := chromedp.Do(ctx, actions...); err != nil {
118 at, _ := chromedp.Run(rig.Context(), chromedp.Location())
119 t.Fatalf("%s %s: %v (the browser is at %s)", host.name, what, err, at)
120 }
121 }
122 // Touch emulation, then a reload on each page so it re-evaluates
123 // its media queries. SetEmulatedMedia's "pointer" feature is not
124 // emulated by this Chromium; the coarse check below is what
125 // proves the emulation took.
126 run("touch emulation", cdpStep(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5}))
127 if c := host.signIn; c != nil {
128 run("sign in", browserCookie(c.Name, c.Value, rig.Origin+"/", c.HttpOnly, c.Secure))
129 }
130 for _, p := range host.pages {
131 var coarse bool
132 var small []string
133 run(p.path,
134 chromedp.Navigate(rig.Origin+p.path),
135 chromedp.Reload(),
136 chromedp.WaitVisible(chromedp.CSS(p.ready)),
137 harness.Into(&coarse, chromedp.Evaluate[bool](`matchMedia('(pointer: coarse)').matches`)),
138 harness.Into(&small, chromedp.Evaluate[[]string](smallControls(textControls))),
139 )
140 if !coarse {
141 t.Fatalf("%s %s: the touch-screen emulation did not apply; this would measure desktop styles", host.name, p.path)
142 }
143 if len(small) > 0 {
144 t.Errorf("%s %s: iOS zooms when these are tapped (under 16px on a touch screen): %s", host.name, p.path, strings.Join(small, "; "))
145 }
146 }
147 }
148}
149
150// smallControls is a script listing each visible control matching sel
151// whose computed font-size is under 16px, described well enough to find.
152func smallControls(sel string) string {
153 return fmt.Sprintf(`[...document.querySelectorAll(%q)]
154 .filter(e => e.getClientRects().length > 0)
155 .filter(e => parseFloat(getComputedStyle(e).fontSize) < 16)
156 .map(e => e.tagName.toLowerCase() + (e.name ? '[name=' + e.name + ']' : '') + ' ' + getComputedStyle(e).fontSize)`, sel)
157}
158