| 1 | //go:build browser |
| 2 | |
| 3 | // The browser checks: Sheets in a real Chromium. Not part of the plain |
| 4 | // suite, because it needs a Chromium (on PATH, via RASTRILLO_CHROME, or in |
| 5 | // 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. |
| 11 | package sheets |
| 12 | |
| 13 | import ( |
| 14 | "context" |
| 15 | "fmt" |
| 16 | "net/http" |
| 17 | "strings" |
| 18 | "testing" |
| 19 | "time" |
| 20 | |
| 21 | "amadan.net/rastrillo/rastrillo/harness" |
| 22 | "github.com/chromedp/cdproto/emulation" |
| 23 | "github.com/chromedp/chromedp" |
| 24 | ) |
| 25 | |
| 26 | // textControls is every control a tap puts a caret in. A checkbox, a |
| 27 | // radio, a file picker or a button raises no keyboard, so iOS never |
| 28 | // zooms for them. |
| 29 | const 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` |
| 30 | |
| 31 | // TestNoZoomOnTap: iOS Safari zooms the whole page into any text control |
| 32 | // whose type is under 16px, and rastrillo's base type is 14px. Its |
| 33 | // tokens.css brings text controls up to 16px where the pointer is coarse |
| 34 | // ("Touch: the two zooms nobody asked for"); an app on a tokens.css |
| 35 | // without that block, or with a rule of its own that sets a control |
| 36 | // smaller, zooms on the first tap of every field. |
| 37 | // |
| 38 | // Emulated as pointer: coarse, which is how the CSS decides. The grid's |
| 39 | // cell editor is measured too: it only exists once a cell is being |
| 40 | // edited, and it is the field someone taps most. |
| 41 | func TestNoZoomOnTap(t *testing.T) { |
| 42 | app, mail := appWithRecorder(t) |
| 43 | cookies := sessionFor(t, app, mail) |
| 44 | sheet, _ := newSheet(t, app, cookies, "Zoom") |
| 45 | |
| 46 | rig := harness.New(t, func(string) http.Handler { return app.Router() }) |
| 47 | // Signed out first: the door is the field every visit starts on. |
| 48 | pages := []struct{ path, ready string }{ |
| 49 | {"/signin", `input[name="address"]`}, |
| 50 | {"", ""}, // sign in, below |
| 51 | {"/sheets", `main`}, |
| 52 | {fmt.Sprintf("/sheets/%d", sheet.ID), `td[data-x="0"][data-y="0"]`}, |
| 53 | {fmt.Sprintf("/sheets/%d/share", sheet.ID), `input[type=email], input[name=address]`}, |
| 54 | {fmt.Sprintf("/sheets/%d/comments", sheet.ID), `textarea, input[type=text]`}, |
| 55 | {fmt.Sprintf("/sheets/%d/table", sheet.ID), `main`}, |
| 56 | } |
| 57 | run := func(what string, actions ...chromedp.Action[chromedp.Void]) { |
| 58 | t.Helper() |
| 59 | ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) |
| 60 | defer cancel() |
| 61 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 62 | at, _ := chromedp.Run(rig.Context(), chromedp.Location()) |
| 63 | t.Fatalf("%s: %v (the browser is at %s)", what, err, at) |
| 64 | } |
| 65 | } |
| 66 | run("touch emulation", |
| 67 | // Touch emulation, then a reload so the page re-evaluates its |
| 68 | // media queries. SetEmulatedMedia's "pointer" feature is not |
| 69 | // emulated by this Chromium; the coarse check below is what |
| 70 | // proves the emulation took. |
| 71 | command(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5}), |
| 72 | ) |
| 73 | for _, page := range pages { |
| 74 | if page.path == "" { |
| 75 | // The session signIn's real magic-link flow minted, handed |
| 76 | // to the browser. |
| 77 | var set []chromedp.Action[chromedp.Void] |
| 78 | for _, c := range cookies { |
| 79 | set = append(set, setCookie(rig.Origin, c)) |
| 80 | } |
| 81 | run("sign in", set...) |
| 82 | continue |
| 83 | } |
| 84 | var coarse bool |
| 85 | var small []string |
| 86 | run(page.path, |
| 87 | chromedp.Navigate(rig.Origin+page.path), |
| 88 | chromedp.Reload(), |
| 89 | chromedp.WaitVisible(chromedp.CSS(page.ready)), |
| 90 | evalInto(&coarse, `matchMedia('(pointer: coarse)').matches`), |
| 91 | evalInto(&small, smallControls(textControls)), |
| 92 | ) |
| 93 | if !coarse { |
| 94 | t.Fatalf("%s: the touch-screen emulation did not apply; this would measure desktop styles", page.path) |
| 95 | } |
| 96 | if len(small) > 0 { |
| 97 | t.Errorf("%s: iOS zooms when these are tapped (under 16px on a touch screen): %s", page.path, strings.Join(small, "; ")) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // The cell editor, opened the way a double-tap opens it. |
| 102 | var small []string |
| 103 | run("the cell editor", |
| 104 | chromedp.Navigate(fmt.Sprintf("%s/sheets/%d", rig.Origin, sheet.ID)), |
| 105 | chromedp.WaitVisible(chromedp.CSS(`td[data-x="0"][data-y="0"]`)), |
| 106 | chromedp.DoubleClick(chromedp.CSS(`td[data-x="0"][data-y="0"]`)), |
| 107 | chromedp.WaitVisible(chromedp.CSS(`td[data-x="0"][data-y="0"] input, td[data-x="0"][data-y="0"] textarea, td[data-x="0"][data-y="0"] [contenteditable]`)), |
| 108 | evalInto(&small, smallControls(`td[data-x="0"][data-y="0"] input, td[data-x="0"][data-y="0"] textarea, td[data-x="0"][data-y="0"] [contenteditable]`)), |
| 109 | ) |
| 110 | if len(small) > 0 { |
| 111 | t.Errorf("the cell editor zooms when a cell is tapped: %s", strings.Join(small, "; ")) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // TestFormatMenuFitsOnTap: the Format menu's two size rows (label, a |
| 116 | // number field, Set) are sized to the panel's 15rem. On a touch screen |
| 117 | // the field is 16px type and the button is padded for a finger, and Set |
| 118 | // hung 11-21px past the panel's edge, half of it off the menu, at every |
| 119 | // width. sheets.css narrows the label column under a coarse pointer; |
| 120 | // this measures the result at 320 and 390 wide: each Set button inside |
| 121 | // its panel, and its centre landing on the button, not on whatever is |
| 122 | // behind the menu. |
| 123 | func TestFormatMenuFitsOnTap(t *testing.T) { |
| 124 | app, cookies := signIn(t) |
| 125 | sheet, _ := newSheet(t, app, cookies, "Format") |
| 126 | rig := harness.New(t, func(string) http.Handler { return app.Router() }) |
| 127 | var set []chromedp.Action[chromedp.Void] |
| 128 | for _, c := range cookies { |
| 129 | set = append(set, setCookie(rig.Origin, c)) |
| 130 | } |
| 131 | set = append(set, command(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5})) |
| 132 | rig.Run(set...) |
| 133 | for _, width := range []int64{320, 390} { |
| 134 | var coarse bool |
| 135 | var problems []string |
| 136 | ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) |
| 137 | err := chromedp.Do(ctx, |
| 138 | command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: width, Height: 844, DeviceScaleFactor: 1, Mobile: true}), |
| 139 | chromedp.Navigate(fmt.Sprintf("%s/sheets/%d", rig.Origin, sheet.ID)), |
| 140 | chromedp.Reload(), |
| 141 | chromedp.WaitVisible(chromedp.CSS(`td[data-x="0"][data-y="0"]`)), |
| 142 | evalInto(&coarse, `matchMedia('(pointer: coarse)').matches`), |
| 143 | evalInto((*any)(nil), `[...document.querySelectorAll('[estilo-menubar-title]')].find(e => e.textContent.trim() === 'Format').click()`), |
| 144 | chromedp.WaitVisible(chromedp.CSS(`[data-size-form] button`)), |
| 145 | evalInto(&problems, `[...document.querySelectorAll('[data-size-form] button')].flatMap(b => { |
| 146 | const r = b.getBoundingClientRect(), m = b.closest('[estilo-menu]').getBoundingClientRect() |
| 147 | const out = [] |
| 148 | if (r.left < m.left || r.right > m.right || r.top < m.top || r.bottom > m.bottom) |
| 149 | out.push(b.closest('form').dataset.axis + ': Set spans ' + Math.round(r.left) + '-' + Math.round(r.right) + ', its panel ' + Math.round(m.left) + '-' + Math.round(m.right)) |
| 150 | const hit = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2) |
| 151 | if (!hit || !b.contains(hit)) out.push(b.closest('form').dataset.axis + ': a tap on Set lands on ' + (hit ? hit.tagName.toLowerCase() : 'nothing')) |
| 152 | return out |
| 153 | })`), |
| 154 | ) |
| 155 | cancel() |
| 156 | if err != nil { |
| 157 | t.Fatalf("%dpx: %v", width, err) |
| 158 | } |
| 159 | if !coarse { |
| 160 | t.Fatalf("%dpx: the touch-screen emulation did not apply", width) |
| 161 | } |
| 162 | for _, p := range problems { |
| 163 | t.Errorf("%dpx: %s", width, p) |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // smallControls is a script listing each visible control matching sel |
| 169 | // whose computed font-size is under 16px, described well enough to find. |
| 170 | func smallControls(sel string) string { |
| 171 | return fmt.Sprintf(`[...document.querySelectorAll(%q)] |
| 172 | .filter(e => e.getClientRects().length > 0) |
| 173 | .filter(e => parseFloat(getComputedStyle(e).fontSize) < 16) |
| 174 | .map(e => e.tagName.toLowerCase() + (e.name ? '[name=' + e.name + ']' : '') + (e.className ? '.' + String(e.className).trim().split(/\s+/).join('.') : '') + ' ' + getComputedStyle(e).fontSize)`, sel) |
| 175 | } |
| 176 | |