| 1 | //go:build browser |
| 2 | |
| 3 | // The phone's Format sheet and quick format bar (design/phone-web, slice |
| 4 | // P2), in a real Chromium with a touch screen. Chromium raises no |
| 5 | // on-screen keyboard, so visualViewport is replaced before any script runs |
| 6 | // by one the test drives: it shrinks as a keyboard would and scrolls inside |
| 7 | // the layout viewport as iOS's does. Mobile Safari is checked by hand on |
| 8 | // the simulator (the discussion has the record). |
| 9 | // |
| 10 | // CGO_ENABLED=0 go test -tags browser -run FormatSheet ./internal/docs/ |
| 11 | package docs_test |
| 12 | |
| 13 | import ( |
| 14 | "context" |
| 15 | "math" |
| 16 | "net/http" |
| 17 | "net/http/httptest" |
| 18 | "strings" |
| 19 | "testing" |
| 20 | "time" |
| 21 | |
| 22 | "amadan.net/rastrillo/rastrillo/harness" |
| 23 | "github.com/chromedp/cdproto/emulation" |
| 24 | "github.com/chromedp/cdproto/input" |
| 25 | "github.com/chromedp/cdproto/page" |
| 26 | "github.com/chromedp/chromedp" |
| 27 | |
| 28 | "github.com/carlosframework/docs/internal/docs" |
| 29 | ) |
| 30 | |
| 31 | // fakeViewport stands in for visualViewport: __keyboard(h, top) raises a |
| 32 | // keyboard h tall with the visual viewport scrolled top pixels into the |
| 33 | // layout one, and innerHeight shrink pixels short. With ?novv in the address there is no visualViewport at all. |
| 34 | const fakeViewport = `(() => { |
| 35 | if (location.search.includes('novv')) { |
| 36 | Object.defineProperty(window, 'visualViewport', { configurable: true, get: () => undefined }) |
| 37 | return |
| 38 | } |
| 39 | const vv = new EventTarget() |
| 40 | Object.assign(vv, { width: innerWidth, height: innerHeight, offsetTop: 0, offsetLeft: 0, pageTop: 0, pageLeft: 0, scale: 1 }) |
| 41 | Object.defineProperty(window, 'visualViewport', { configurable: true, get: () => vv }) |
| 42 | // shrink makes innerHeight under-report by that much, as Mobile Safari's |
| 43 | // does once a short document is scrolled with the keyboard up. |
| 44 | const real = () => document.documentElement.clientHeight |
| 45 | window.__keyboard = (h, top, shrink) => { |
| 46 | Object.defineProperty(window, 'innerHeight', { configurable: true, get: () => real() - (shrink || 0) }) |
| 47 | vv.height = real() - h |
| 48 | vv.offsetTop = top || 0 |
| 49 | vv.dispatchEvent(new Event('resize')) |
| 50 | vv.dispatchEvent(new Event('scroll')) |
| 51 | return true |
| 52 | } |
| 53 | // A pinch-zoom: the visual viewport scale times smaller, top pixels |
| 54 | // down the layout one, and no keyboard. |
| 55 | window.__zoom = (scale, top) => { |
| 56 | Object.defineProperty(window, 'innerHeight', { configurable: true, get: () => real() }) |
| 57 | vv.scale = scale |
| 58 | vv.height = real() / scale |
| 59 | vv.offsetTop = top || 0 |
| 60 | vv.dispatchEvent(new Event('resize')) |
| 61 | vv.dispatchEvent(new Event('scroll')) |
| 62 | return true |
| 63 | } |
| 64 | })()` |
| 65 | |
| 66 | type formatProbe struct { |
| 67 | Quick string |
| 68 | QuickTop, QuickBottom, QuickH float64 |
| 69 | BarTop, BarBottom float64 |
| 70 | Folded, Sheet, Focused, Kept bool |
| 71 | SheetH, Inner float64 |
| 72 | Expanded, Bold, Chip, Size string |
| 73 | Heading, OneRow bool |
| 74 | } |
| 75 | |
| 76 | const formatProbeJS = `(() => { |
| 77 | const q = document.querySelector('[docs-quickbar]') |
| 78 | const qr = q.getBoundingClientRect() |
| 79 | const bar = document.querySelector('[estilo-bar]').getBoundingClientRect() |
| 80 | const ins = document.querySelector('[docs-inspector]') |
| 81 | const sheet = ins.getClientRects().length > 0 && getComputedStyle(ins).position === 'fixed' |
| 82 | return { |
| 83 | Quick: q.getAttribute('data-shown') || '', QuickTop: qr.top, QuickBottom: qr.bottom, QuickH: qr.height, |
| 84 | BarTop: bar.top, BarBottom: bar.bottom, |
| 85 | Folded: document.querySelector('[estilo-bar]').hasAttribute('estilo-bar-folded'), |
| 86 | Sheet: sheet, SheetH: sheet ? ins.getBoundingClientRect().height : 0, Inner: innerHeight, |
| 87 | Focused: !!document.activeElement && !!document.activeElement.closest('[docs-editor]'), |
| 88 | Kept: !!document.querySelector('[data-docs-kept]'), |
| 89 | Expanded: document.querySelector('[docs-quickbar-aa]').getAttribute('aria-expanded'), |
| 90 | Bold: document.querySelector('[docs-quickbar] [data-docs-command="strong"]').getAttribute('aria-pressed'), |
| 91 | Chip: document.querySelector('[docs-chip="heading-2"]').getAttribute('aria-pressed'), |
| 92 | Size: document.querySelector('#docs-select-size').value, |
| 93 | Heading: !!document.querySelector('[docs-text] h2'), |
| 94 | OneRow: (() => { |
| 95 | const at = (e) => { const r = e.getBoundingClientRect(); return r.top + r.height / 2 } |
| 96 | const row = [document.querySelector('#docs-select-font-combo') || document.querySelector('#docs-select-font'), document.querySelector('[docs-step="down"]'), document.querySelector('#docs-select-size-combo') || document.querySelector('#docs-select-size'), document.querySelector('[docs-step="up"]')] |
| 97 | return row.every((e) => e.getClientRects().length > 0 && Math.abs(at(e) - at(row[0])) < 2) |
| 98 | })(), |
| 99 | } |
| 100 | })()` |
| 101 | |
| 102 | func TestTheFormatSheetAndTheQuickBar(t *testing.T) { |
| 103 | var app *docs.App |
| 104 | rig := harness.New(t, func(origin string) http.Handler { |
| 105 | var err error |
| 106 | app, err = docs.NewForTestWith(t, docs.Config{Origin: origin}) |
| 107 | if err != nil { |
| 108 | t.Fatalf("NewForTestWith: %v", err) |
| 109 | } |
| 110 | return app.Handler() |
| 111 | }) |
| 112 | alice := signInCookies(t, app, "alice@example.com") |
| 113 | req := httptest.NewRequest(http.MethodPost, "/documents", strings.NewReader("title=Minutes")) |
| 114 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 115 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 116 | for _, c := range alice { |
| 117 | req.AddCookie(c) |
| 118 | } |
| 119 | rec := httptest.NewRecorder() |
| 120 | app.Handler().ServeHTTP(rec, req) |
| 121 | sqid := strings.TrimPrefix(rec.Header().Get("Location"), "/d/") |
| 122 | |
| 123 | run := func(what string, actions ...chromedp.Action[chromedp.Void]) { |
| 124 | t.Helper() |
| 125 | ctx, cancel := context.WithTimeout(rig.Context(), 25*time.Second) |
| 126 | defer cancel() |
| 127 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 128 | t.Fatalf("%s: %v", what, err) |
| 129 | } |
| 130 | } |
| 131 | probe := func(what string) formatProbe { |
| 132 | t.Helper() |
| 133 | var p formatProbe |
| 134 | run(what, evalInto(&p, formatProbeJS)) |
| 135 | return p |
| 136 | } |
| 137 | settle := chromedp.Sleep(400 * time.Millisecond) |
| 138 | near := func(a, b float64) bool { return math.Abs(a-b) <= 1.5 } |
| 139 | |
| 140 | var set []chromedp.Action[chromedp.Void] |
| 141 | for _, c := range alice { |
| 142 | set = append(set, setCookie(rig.Origin, c)) |
| 143 | } |
| 144 | run("a phone", append(set, |
| 145 | command(page.AddScriptToEvaluateOnNewDocument, page.AddScriptToEvaluateOnNewDocumentParams{Source: fakeViewport}), |
| 146 | command(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5}), |
| 147 | command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 390, Height: 844, DeviceScaleFactor: 2, Mobile: true}), |
| 148 | chromedp.Navigate(rig.Origin+"/d/"+sqid), chromedp.Reload(), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), |
| 149 | chromedp.WaitNotPresent(chromedp.CSS(`[docs-quickbar] [data-docs-command="strong"][disabled]`)), |
| 150 | chromedp.Evaluate[chromedp.Void](`(() => { const el = document.querySelector('[docs-text]'); el.focus(); document.execCommand('insertText', false, 'Alpha beta'); return true })()`), |
| 151 | settle)...) |
| 152 | |
| 153 | // The caret in the document, no keyboard: no capsule. |
| 154 | if p := probe("no keyboard"); p.Quick != "" { |
| 155 | t.Errorf("with no keyboard up the quick bar is %q", p.Quick) |
| 156 | } |
| 157 | |
| 158 | // A 336px keyboard: the capsule 8px above it, 48px tall, the bar |
| 159 | // folded. |
| 160 | run("keyboard", chromedp.Evaluate[chromedp.Void](`__keyboard(336, 0)`), settle) |
| 161 | p := probe("keyboard") |
| 162 | if p.Quick != "keyboard" || !near(p.QuickBottom, p.Inner-336-8) || !near(p.QuickH, 48) || !p.Folded { |
| 163 | t.Errorf("keyboard up: quick bar %q, bottom %.1f (want %.1f), %.1fpx tall, folded %v", p.Quick, p.QuickBottom, p.Inner-336-8, p.QuickH, p.Folded) |
| 164 | } |
| 165 | // iOS scrolls the visual viewport 200px into the layout one: the bar |
| 166 | // stays at the top of what can be seen, the capsule on the keyboard. |
| 167 | run("scrolled visual viewport", chromedp.Evaluate[chromedp.Void](`__keyboard(336, 200)`), settle) |
| 168 | p = probe("scrolled visual viewport") |
| 169 | if !near(p.BarTop, 200) || !near(p.QuickBottom, 200+p.Inner-336-8) { |
| 170 | t.Errorf("visual viewport 200px down: the bar's top at %.1f (want 200), the capsule's foot at %.1f (want %.1f)", p.BarTop, p.QuickBottom, 200+p.Inner-336-8) |
| 171 | } |
| 172 | // Mobile Safari, a short document scrolled with the keyboard up: |
| 173 | // innerHeight reads 109px short of the layout viewport. The capsule |
| 174 | // still sits on the keyboard. |
| 175 | run("innerHeight short", chromedp.Evaluate[chromedp.Void](`__keyboard(336, 221, 109)`), settle) |
| 176 | p = probe("innerHeight short") |
| 177 | if !near(p.BarTop, 221) || !near(p.QuickBottom, 221+844-336-8) { |
| 178 | t.Errorf("innerHeight 109px short: the bar's top at %.1f (want 221), the capsule's foot at %.1f (want %d)", p.BarTop, p.QuickBottom, 221+844-336-8) |
| 179 | } |
| 180 | run("back", chromedp.Evaluate[chromedp.Void](`__keyboard(336, 0)`), settle) |
| 181 | |
| 182 | // B on the capsule: the next words are bold, the keyboard stays. |
| 183 | run("B", chromedp.Click(chromedp.CSS(`[docs-quickbar] [data-docs-command="strong"]`)), settle) |
| 184 | if p = probe("B"); p.Bold != "true" || !p.Focused { |
| 185 | t.Errorf("after B: pressed %q, the document has the caret %v", p.Bold, p.Focused) |
| 186 | } |
| 187 | run("B again", chromedp.Click(chromedp.CSS(`[docs-quickbar] [data-docs-command="strong"]`)), settle) |
| 188 | |
| 189 | // Aa on the capsule with a selection: the sheet, half the screen; the |
| 190 | // keyboard goes (the document loses the caret) and the selection stays |
| 191 | // drawn; the capsule goes. |
| 192 | run("select, Aa", chromedp.Evaluate[chromedp.Void](`(() => { const p = document.querySelector('[docs-text] p'); getSelection().selectAllChildren(p); return true })()`), settle, |
| 193 | chromedp.Click(chromedp.CSS(`[docs-quickbar-aa]`)), settle) |
| 194 | p = probe("sheet open") |
| 195 | if !p.Sheet || !near(p.SheetH, p.Inner/2) || p.Focused || !p.Kept || p.Quick != "" || p.Expanded != "true" { |
| 196 | t.Errorf("sheet open: %+v; want a half-height sheet, the keyboard gone, the selection kept and drawn, no capsule", p) |
| 197 | } |
| 198 | if !p.OneRow { |
| 199 | t.Error("sheet open: the font, its size and the two steps are not on one line") |
| 200 | } |
| 201 | // A chip and a step apply at once, and the sheet and the keyboard stay |
| 202 | // as they are. |
| 203 | run("Heading 2", chromedp.Click(chromedp.CSS(`[docs-chip="heading-2"]`)), settle) |
| 204 | if p = probe("Heading 2"); !p.Heading || p.Chip != "true" || p.Focused || !p.Sheet { |
| 205 | t.Errorf("after Heading 2: heading %v, chip pressed %q, the caret back %v, sheet %v", p.Heading, p.Chip, p.Focused, p.Sheet) |
| 206 | } |
| 207 | run("size up", chromedp.Click(chromedp.CSS(`[docs-step="up"]`)), settle) |
| 208 | if p = probe("size up"); p.Size != "14" || p.Focused { |
| 209 | t.Errorf("after a step up from 12: size %q, the caret back %v", p.Size, p.Focused) |
| 210 | } |
| 211 | // × closes it and brings the keyboard back, since it was up. |
| 212 | run("×", chromedp.Click(chromedp.CSS(`[docs-sheet-close]`)), settle) |
| 213 | if p = probe("closed"); p.Sheet || !p.Focused || p.Kept || p.Expanded != "false" { |
| 214 | t.Errorf("after ×: sheet %v, the caret back %v, the selection still drawn %v, Aa %q", p.Sheet, p.Focused, p.Kept, p.Expanded) |
| 215 | } |
| 216 | // Hide keyboard on the capsule takes the caret out of the document. |
| 217 | run("hide keyboard", chromedp.Evaluate[chromedp.Void](`__keyboard(336, 0)`), settle, chromedp.Click(chromedp.CSS(`[docs-quickbar-hide]`)), settle) |
| 218 | if p = probe("hidden"); p.Focused || p.Quick != "" { |
| 219 | t.Errorf("after Hide keyboard: the caret %v, the quick bar %q", p.Focused, p.Quick) |
| 220 | } |
| 221 | // Aa in the bar opens it; Escape closes it. |
| 222 | run("Aa, Escape", chromedp.Click(chromedp.CSS(`[docs-aa]`)), settle) |
| 223 | if p = probe("Aa"); !p.Sheet { |
| 224 | t.Error("the bar's Aa did not open the sheet") |
| 225 | } |
| 226 | run("Escape", chromedp.KeyEvent("\u001b"), settle) |
| 227 | if p = probe("Escape"); p.Sheet { |
| 228 | t.Error("Escape did not close the sheet") |
| 229 | } |
| 230 | // A finger dragging the sheet's title 100px down closes it. |
| 231 | run("Aa, swipe", chromedp.Click(chromedp.CSS(`[docs-aa]`)), settle) |
| 232 | var head struct{ X, Y float64 } |
| 233 | run("head", evalInto(&head, `(() => { const r = document.querySelector('[docs-sheet-title]').getBoundingClientRect(); return { X: r.x + 20, Y: r.y + r.height / 2 } })()`)) |
| 234 | touch := func(kind input.DispatchTouchEventType, dy float64) chromedp.Action[chromedp.Void] { |
| 235 | points := []*input.TouchPoint{{X: head.X, Y: head.Y + dy}} |
| 236 | if kind == input.DispatchTouchEventTypeTouchEnd { |
| 237 | points = []*input.TouchPoint{} |
| 238 | } |
| 239 | return command(input.DispatchTouchEvent, input.DispatchTouchEventParams{Type: kind, TouchPoints: points}) |
| 240 | } |
| 241 | // It closes when the finger lifts, not while it is still down (on |
| 242 | // iOS a sheet leaving under a held finger hands the touch to the |
| 243 | // page, and the keyboard). |
| 244 | run("swipe", touch(input.DispatchTouchEventTypeTouchStart, 0), touch(input.DispatchTouchEventTypeTouchMove, 30), touch(input.DispatchTouchEventTypeTouchMove, 70), touch(input.DispatchTouchEventTypeTouchMove, 100), settle) |
| 245 | if p = probe("held"); !p.Sheet { |
| 246 | t.Error("the sheet closed under a finger still down") |
| 247 | } |
| 248 | run("lift", touch(input.DispatchTouchEventTypeTouchEnd, 100), settle) |
| 249 | if p = probe("swiped"); p.Sheet { |
| 250 | t.Error("a swipe down on the sheet's title did not close it") |
| 251 | } |
| 252 | |
| 253 | // From 704px none of it is drawn and the inspector is as it was: the |
| 254 | // font and its size share a line. |
| 255 | var desk struct { |
| 256 | Drawn []string |
| 257 | FontTop, FontEnd float64 |
| 258 | SizeTop, SizeEnd float64 |
| 259 | } |
| 260 | run("a desktop", command(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: false}), |
| 261 | command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 1280, Height: 800, DeviceScaleFactor: 1}), |
| 262 | chromedp.Navigate(rig.Origin+"/d/"+sqid), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), settle, |
| 263 | evalInto(&desk, `(() => ({ |
| 264 | Drawn: [...document.querySelectorAll('[docs-sheet-head], [docs-sheet-only], [docs-quickbar], [docs-chip]')].filter(e => e.getClientRects().length > 0).map(e => e.outerHTML.slice(0, 60)), |
| 265 | FontTop: (document.querySelector('#docs-select-font-combo') || document.querySelector('#docs-select-font')).getBoundingClientRect().top, |
| 266 | FontEnd: (document.querySelector('#docs-select-font-combo') || document.querySelector('#docs-select-font')).getBoundingClientRect().bottom, |
| 267 | SizeTop: document.querySelector('#docs-select-size').getBoundingClientRect().top, |
| 268 | SizeEnd: document.querySelector('#docs-select-size').getBoundingClientRect().bottom, |
| 269 | }))()`)) |
| 270 | if len(desk.Drawn) > 0 || desk.SizeTop >= desk.FontEnd || desk.FontTop >= desk.SizeEnd { |
| 271 | t.Errorf("at 1280px: drawn %q; the font %.1f-%.1f and its size %.1f-%.1f, want one line", desk.Drawn, desk.FontTop, desk.FontEnd, desk.SizeTop, desk.SizeEnd) |
| 272 | } |
| 273 | run("a phone again", command(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5}), |
| 274 | command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 390, Height: 844, DeviceScaleFactor: 2, Mobile: true})) |
| 275 | |
| 276 | // No visualViewport: the six buttons are a strip under the bar while |
| 277 | // the document has the caret. |
| 278 | run("no visualViewport", chromedp.Navigate(rig.Origin+"/d/"+sqid+"?novv=1"), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), |
| 279 | // A tap, not a script's focus(): a tab whose window has no focus |
| 280 | // holds back the focus events a script's focus() would raise. |
| 281 | chromedp.Click(chromedp.CSS(`[docs-text]`)), settle) |
| 282 | if p = probe("no visualViewport"); p.Quick != "strip" || !near(p.QuickTop, p.BarBottom) { |
| 283 | t.Errorf("with no visualViewport: the quick bar is %q at %.1f, want a strip under the bar at %.1f", p.Quick, p.QuickTop, p.BarBottom) |
| 284 | } |
| 285 | } |
| 286 | |