| 1 | //go:build browser |
| 2 | |
| 3 | // The phone document screen (design/phone-web, slice P1) in a real |
| 4 | // Chromium with a touch screen: estilo's compact bar and this page's large |
| 5 | // title, ••• and the fold (brand/phone.js). |
| 6 | // |
| 7 | // CGO_ENABLED=0 go test -tags browser -run Phone ./internal/docs/ |
| 8 | package docs_test |
| 9 | |
| 10 | import ( |
| 11 | "context" |
| 12 | "net/http" |
| 13 | "net/http/httptest" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "amadan.net/rastrillo/rastrillo/harness" |
| 19 | "github.com/chromedp/cdproto/emulation" |
| 20 | "github.com/chromedp/chromedp" |
| 21 | |
| 22 | "github.com/carlosframework/docs/internal/docs" |
| 23 | ) |
| 24 | |
| 25 | type phoneProbe struct { |
| 26 | Coarse, Compact, Folded, Editing bool |
| 27 | Bar, Aa, More, Back float64 |
| 28 | Title, Menubar, Toast, Strip bool |
| 29 | Sheet bool |
| 30 | Active string |
| 31 | Rows []float64 |
| 32 | Disabled int |
| 33 | Sub string |
| 34 | ScrollW, Width float64 |
| 35 | } |
| 36 | |
| 37 | const phoneProbeJS = `(() => { |
| 38 | const shown = (s) => { const e = document.querySelector(s); return !!e && e.getClientRects().length > 0 }; |
| 39 | const h = (s) => { const e = document.querySelector(s); return e && e.getClientRects().length ? e.getBoundingClientRect().height : 0 }; |
| 40 | const w = (s) => { const e = document.querySelector(s); return e && e.getClientRects().length ? e.getBoundingClientRect().width : 0 }; |
| 41 | const panel = document.querySelector('[docs-more-panel]'); |
| 42 | const rows = panel && document.querySelector('[docs-more]').open ? Array.from(panel.querySelectorAll(':scope > [estilo-menu-item], :scope > a, :scope > button, [docs-more-help] > summary')).filter((e) => e.getClientRects().length).map((e) => e.getBoundingClientRect().height) : []; |
| 43 | return { |
| 44 | Coarse: matchMedia('(pointer: coarse)').matches, |
| 45 | Compact: matchMedia('(max-width: 703.98px), (max-height: 480px) and (pointer: coarse)').matches, |
| 46 | Folded: document.querySelector('[estilo-bar]').hasAttribute('estilo-bar-folded'), |
| 47 | Editing: !!document.querySelector('[docs-title-form][data-editing]'), |
| 48 | Bar: h('[estilo-bar]'), Aa: h('[docs-aa]'), More: h('[docs-more] > summary'), Back: w('[estilo-bar-brand]'), |
| 49 | Title: shown('[docs-title-block]'), Menubar: shown('[estilo-bar-menubar]'), Toast: shown('[estilo-save-status~="floating"]'), Strip: shown('[docs-bar]'), |
| 50 | Sheet: shown('[docs-inspector]') && getComputedStyle(document.querySelector('[docs-inspector]')).position === 'fixed', |
| 51 | Active: (document.activeElement && document.activeElement.getAttribute('name')) || '', |
| 52 | Rows: rows, |
| 53 | Disabled: panel ? panel.querySelectorAll('[data-docs-proxy][disabled]').length : -1, |
| 54 | Sub: (document.querySelector('[docs-subtitle]') || {}).textContent || '', |
| 55 | ScrollW: document.documentElement.scrollWidth, Width: innerWidth, |
| 56 | }; |
| 57 | })()` |
| 58 | |
| 59 | func TestThePhoneDocumentScreen(t *testing.T) { |
| 60 | var app *docs.App |
| 61 | rig := harness.New(t, func(origin string) http.Handler { |
| 62 | var err error |
| 63 | app, err = docs.NewForTestWith(t, docs.Config{Origin: origin}) |
| 64 | if err != nil { |
| 65 | t.Fatalf("NewForTestWith: %v", err) |
| 66 | } |
| 67 | return app.Handler() |
| 68 | }) |
| 69 | alice := signInCookies(t, app, "alice@example.com") |
| 70 | sameOrigin := func(cookies []*http.Cookie, path, body string) *httptest.ResponseRecorder { |
| 71 | req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body)) |
| 72 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 73 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 74 | for _, c := range cookies { |
| 75 | req.AddCookie(c) |
| 76 | } |
| 77 | rec := httptest.NewRecorder() |
| 78 | app.Handler().ServeHTTP(rec, req) |
| 79 | return rec |
| 80 | } |
| 81 | created := sameOrigin(alice, "/documents", "title=Minutes") |
| 82 | if created.Code != http.StatusSeeOther { |
| 83 | t.Fatalf("POST /documents: %d", created.Code) |
| 84 | } |
| 85 | sqid := strings.TrimPrefix(created.Header().Get("Location"), "/d/") |
| 86 | bob := signInCookies(t, app, "bob@example.com") |
| 87 | if rec := sameOrigin(alice, "/d/"+sqid+"/share", "address=bob@example.com&role=reader"); rec.Code >= 400 { |
| 88 | t.Fatalf("share: %d", rec.Code) |
| 89 | } |
| 90 | |
| 91 | run := func(what string, actions ...chromedp.Action[chromedp.Void]) { |
| 92 | t.Helper() |
| 93 | ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second) |
| 94 | defer cancel() |
| 95 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 96 | t.Fatalf("%s: %v", what, err) |
| 97 | } |
| 98 | } |
| 99 | probe := func(what string) phoneProbe { |
| 100 | t.Helper() |
| 101 | var p phoneProbe |
| 102 | run(what, evalInto(&p, phoneProbeJS)) |
| 103 | return p |
| 104 | } |
| 105 | signIn := func(cookies []*http.Cookie) { |
| 106 | var set []chromedp.Action[chromedp.Void] |
| 107 | for _, c := range cookies { |
| 108 | set = append(set, setCookie(rig.Origin, c)) |
| 109 | } |
| 110 | run("sign in", set...) |
| 111 | } |
| 112 | settle := chromedp.Sleep(400 * time.Millisecond) |
| 113 | |
| 114 | // A phone: 390x844, a finger. |
| 115 | run("a phone", command(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5}), |
| 116 | command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 390, Height: 844, DeviceScaleFactor: 2, Mobile: true})) |
| 117 | signIn(alice) |
| 118 | run("the document", chromedp.Navigate(rig.Origin+"/d/"+sqid), chromedp.Reload(), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), |
| 119 | // The editor enables the menu bar's commands, and ••• mirrors them. |
| 120 | chromedp.WaitNotPresent(chromedp.CSS(`[estilo-menubar] [data-docs-command="word_count"][disabled]`)), settle) |
| 121 | p := probe("at rest") |
| 122 | if !p.Coarse || !p.Compact { |
| 123 | t.Fatalf("the phone emulation did not apply (coarse %v, compact %v)", p.Coarse, p.Compact) |
| 124 | } |
| 125 | if p.Bar < 51.5 || p.Bar > 52.5 || p.Aa < 44 || p.More < 44 { |
| 126 | t.Errorf("the bar is %.1fpx with Aa %.1f and ••• %.1f; want one 52px row of 44px tools", p.Bar, p.Aa, p.More) |
| 127 | } |
| 128 | if !p.Title || p.Menubar || p.Toast || p.Strip || p.Folded { |
| 129 | t.Errorf("at rest: title %v, menu bar %v, corner toast %v, strip %v, folded %v; want the large title alone", p.Title, p.Menubar, p.Toast, p.Strip, p.Folded) |
| 130 | } |
| 131 | if p.ScrollW > p.Width { |
| 132 | t.Errorf("the page scrolls sideways: %v in %v", p.ScrollW, p.Width) |
| 133 | } |
| 134 | |
| 135 | // •••: a drop-down of 44px rows, the editor's commands enabled. |
| 136 | run("open •••", chromedp.Click(chromedp.CSS(`[docs-more] > summary`)), settle) |
| 137 | p = probe("••• open") |
| 138 | if len(p.Rows) < 10 { |
| 139 | t.Fatalf("••• shows %d rows", len(p.Rows)) |
| 140 | } |
| 141 | for _, h := range p.Rows { |
| 142 | if h < 43.5 { |
| 143 | t.Errorf("a ••• row is %.1fpx, under 44", h) |
| 144 | } |
| 145 | } |
| 146 | if p.Disabled != 0 { |
| 147 | t.Errorf("%d of •••'s editor rows are still disabled once the editor has mounted", p.Disabled) |
| 148 | } |
| 149 | |
| 150 | // A command run from ••• closes it and leaves focus on ••• (Fable on |
| 151 | // #179): the menu bar's item it ran is not drawn on a phone. |
| 152 | var back struct{ Open, OnMore, Paged bool } |
| 153 | run("Paged from •••", chromedp.Click(chromedp.CSS(`[data-docs-proxy="view_paged"]`)), settle, |
| 154 | evalInto(&back, `({ Open: document.querySelector('[docs-more]').open, OnMore: document.activeElement === document.querySelector('[docs-more] > summary'), Paged: document.querySelector('[docs-canvas]').getAttribute('data-view') === 'paged' })`)) |
| 155 | if back.Open || !back.OnMore || !back.Paged { |
| 156 | t.Errorf("after Paged from •••: ••• open %v, focus on ••• %v, paged %v", back.Open, back.OnMore, back.Paged) |
| 157 | } |
| 158 | run("Infinite again", chromedp.Click(chromedp.CSS(`[docs-more] > summary`)), settle, chromedp.Click(chromedp.CSS(`[data-docs-proxy="view_infinite"]`)), settle) |
| 159 | |
| 160 | // Rename turns the large title into its field, focused; Esc puts it back. |
| 161 | run("Rename", chromedp.Click(chromedp.CSS(`[docs-more] > summary`)), settle, chromedp.Click(chromedp.CSS(`[data-docs-rename]`)), settle) |
| 162 | if p = probe("renaming"); !p.Editing || p.Active != "title" { |
| 163 | t.Errorf("••• ▸ Rename: editing %v, focus on %q", p.Editing, p.Active) |
| 164 | } |
| 165 | run("Esc", chromedp.KeyEvent("\u001b"), settle) |
| 166 | if p = probe("after Esc"); p.Editing { |
| 167 | t.Error("Esc left the title in its field") |
| 168 | } |
| 169 | |
| 170 | // On a touch screen there is no Esc: the hint about it is not shown, |
| 171 | // and Cancel puts the title back. Clearing the field and leaving it is |
| 172 | // a cancel too, never a rename to Untitled (Fable on #179). Neither |
| 173 | // leaves the page. |
| 174 | type renaming struct { |
| 175 | Editing, Hint, Cancel, Stayed bool |
| 176 | Value, Title string |
| 177 | } |
| 178 | state := `({ Editing: !!document.querySelector('[docs-title-form][data-editing]'), Hint: !!document.querySelector('[docs-title-hint]').getClientRects().length, Cancel: !!document.querySelector('[docs-title-cancel]').getClientRects().length, Stayed: window.__stayed === true, Value: document.querySelector('[docs-title-input]').value, Title: document.querySelector('[docs-title-text]').textContent })` |
| 179 | var r renaming |
| 180 | run("rename, on touch", chromedp.Evaluate[chromedp.Void](`window.__stayed = true`), chromedp.Click(chromedp.CSS(`[docs-title-edit]`)), settle, evalInto(&r, state)) |
| 181 | if !r.Editing || r.Hint || !r.Cancel { |
| 182 | t.Errorf("renaming on touch: editing %v, the Esc hint shown %v, Cancel shown %v", r.Editing, r.Hint, r.Cancel) |
| 183 | } |
| 184 | run("Cancel", chromedp.Evaluate[chromedp.Void](`document.querySelector('[docs-title-input]').value = 'Changed'; true`), chromedp.Click(chromedp.CSS(`[docs-title-cancel]`)), settle, evalInto(&r, state)) |
| 185 | if r.Editing || r.Value != "Minutes" || !r.Stayed { |
| 186 | t.Errorf("after Cancel: editing %v, the field %q, still the same page %v", r.Editing, r.Value, r.Stayed) |
| 187 | } |
| 188 | run("clear and leave", chromedp.Click(chromedp.CSS(`[docs-title-edit]`)), settle, |
| 189 | chromedp.Evaluate[chromedp.Void](`document.querySelector('[docs-title-input]').value = ''; document.querySelector('[docs-title-input]').blur(); true`), |
| 190 | chromedp.Sleep(800*time.Millisecond), evalInto(&r, state)) |
| 191 | if r.Editing || r.Value != "Minutes" || r.Title != "Minutes" || !r.Stayed { |
| 192 | t.Errorf("after clearing the field and leaving it: editing %v, the field %q, the title %q, still the same page %v; want it put back", r.Editing, r.Value, r.Title, r.Stayed) |
| 193 | } |
| 194 | |
| 195 | // Typing, then renaming at once: the rename is a fetch that changes the |
| 196 | // title in place, so the page never leaves while an edit is still |
| 197 | // being saved (Fable on #179: a blur rename was a full-page POST, and |
| 198 | // iOS does not reliably send what is pending as a page goes). The |
| 199 | // words and the new title are both there after a reload. |
| 200 | var kept struct { |
| 201 | Stayed bool |
| 202 | Title, Inline string |
| 203 | } |
| 204 | run("type, then rename", chromedp.Evaluate[chromedp.Void](`window.__stayed = true; (() => { const el = document.querySelector('[docs-text]'); el.focus(); document.execCommand('insertText', false, 'zebra crossing'); return true })()`), |
| 205 | chromedp.Click(chromedp.CSS(`[docs-title-edit]`)), |
| 206 | chromedp.Evaluate[chromedp.Void](`(() => { const i = document.querySelector('[docs-title-input]'); i.value = 'Renamed at once'; i.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); return true })()`), |
| 207 | chromedp.Sleep(1500*time.Millisecond), |
| 208 | evalInto(&kept, `({ Stayed: window.__stayed === true, Title: document.querySelector('[docs-title-text]').textContent, Inline: document.querySelector('[docs-inline-title] b').textContent })`)) |
| 209 | if !kept.Stayed || kept.Title != "Renamed at once" || kept.Inline != "Renamed at once" { |
| 210 | t.Errorf("type then rename: same page %v, title %q, inline title %q; want it renamed in place", kept.Stayed, kept.Title, kept.Inline) |
| 211 | } |
| 212 | var after struct{ Text, Title string } |
| 213 | run("reload", chromedp.Reload(), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), settle, |
| 214 | evalInto(&after, `({ Text: document.querySelector('[docs-text]').textContent, Title: document.querySelector('[docs-title-text]').textContent })`)) |
| 215 | if !strings.Contains(after.Text, "zebra crossing") || after.Title != "Renamed at once" { |
| 216 | t.Errorf("after a reload: the text %q, the title %q; an edit or the rename was lost", after.Text, after.Title) |
| 217 | } |
| 218 | |
| 219 | // Aa opens the Format sheet (P2), and again closes it. |
| 220 | run("Aa", chromedp.Click(chromedp.CSS(`[docs-aa]`)), settle) |
| 221 | if p = probe("Aa"); !p.Sheet { |
| 222 | t.Error("Aa did not open the Format sheet") |
| 223 | } |
| 224 | run("Aa again", chromedp.Click(chromedp.CSS(`[docs-aa]`)), settle) |
| 225 | if p = probe("Aa again"); p.Sheet { |
| 226 | t.Error("Aa again did not close the Format sheet") |
| 227 | } |
| 228 | |
| 229 | // The fold: past the large title the bar shows its inline title, and |
| 230 | // Back is its 44px chevron. |
| 231 | run("scroll", chromedp.Evaluate[chromedp.Void](`document.querySelector('[docs-page]').style.minBlockSize = '3000px'; window.scrollTo(0, 600)`), chromedp.Sleep(600*time.Millisecond)) |
| 232 | if p = probe("scrolled"); !p.Folded || p.Back < 43.5 { |
| 233 | t.Errorf("scrolled past the title: folded %v, Back %.1fpx wide", p.Folded, p.Back) |
| 234 | } |
| 235 | run("scroll back", chromedp.Evaluate[chromedp.Void](`window.scrollTo(0, 0)`), chromedp.Sleep(600*time.Millisecond)) |
| 236 | if p = probe("back at the top"); p.Folded { |
| 237 | t.Error("back at the top, the bar is still folded") |
| 238 | } |
| 239 | |
| 240 | // A reader: Back and ••• only, and "View only". |
| 241 | signIn(bob) |
| 242 | run("the reader's document", chromedp.Navigate(rig.Origin+"/d/"+sqid), chromedp.WaitVisible(chromedp.CSS(`[docs-title-block]`)), settle) |
| 243 | if p = probe("reader"); p.Aa != 0 || p.More < 44 || !strings.Contains(p.Sub, "docs.show.viewonly") { |
| 244 | t.Errorf("reader: Aa %.1f, ••• %.1f, subtitle %q", p.Aa, p.More, p.Sub) |
| 245 | } |
| 246 | |
| 247 | // From 704px none of it: the 56px header, no large title, never folded. |
| 248 | signIn(alice) |
| 249 | run("an iPad", command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 768, Height: 1024, DeviceScaleFactor: 2, Mobile: true}), |
| 250 | chromedp.Navigate(rig.Origin+"/d/"+sqid), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), settle) |
| 251 | if p = probe("768"); p.Compact || p.Title || !p.Menubar || p.Folded || p.Bar < 55.5 || p.Bar > 56.5 { |
| 252 | t.Errorf("at 768px: compact %v, large title %v, menu bar %v, folded %v, bar %.1fpx", p.Compact, p.Title, p.Menubar, p.Folded, p.Bar) |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // A save that fails is said, visibly and to assistive technology, at every |
| 257 | // width: in the corner toast at 1280px, under the large title on a phone |
| 258 | // at rest, and in the folded bar's inline line once the title has scrolled |
| 259 | // away (Fable on 5bcd269, B1: the editor mounted the first |
| 260 | // [estilo-save-status] in the page, which became the phone's subtitle, |
| 261 | // display:none on a desktop). The failure is real: the owner deletes the |
| 262 | // document under the open page, and the next save is refused. |
| 263 | func TestASaveFailureIsSaidAtEveryWidth(t *testing.T) { |
| 264 | var app *docs.App |
| 265 | rig := harness.New(t, func(origin string) http.Handler { |
| 266 | var err error |
| 267 | app, err = docs.NewForTestWith(t, docs.Config{Origin: origin}) |
| 268 | if err != nil { |
| 269 | t.Fatalf("NewForTestWith: %v", err) |
| 270 | } |
| 271 | return app.Handler() |
| 272 | }) |
| 273 | alice := signInCookies(t, app, "alice@example.com") |
| 274 | post := func(path, body string) *httptest.ResponseRecorder { |
| 275 | req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body)) |
| 276 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 277 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 278 | for _, c := range alice { |
| 279 | req.AddCookie(c) |
| 280 | } |
| 281 | rec := httptest.NewRecorder() |
| 282 | app.Handler().ServeHTTP(rec, req) |
| 283 | return rec |
| 284 | } |
| 285 | run := func(what string, actions ...chromedp.Action[chromedp.Void]) { |
| 286 | t.Helper() |
| 287 | ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second) |
| 288 | defer cancel() |
| 289 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 290 | t.Fatalf("%s: %v", what, err) |
| 291 | } |
| 292 | } |
| 293 | var set []chromedp.Action[chromedp.Void] |
| 294 | for _, c := range alice { |
| 295 | set = append(set, setCookie(rig.Origin, c)) |
| 296 | } |
| 297 | run("sign in", set...) |
| 298 | |
| 299 | type said struct { |
| 300 | Text, State, Live string |
| 301 | Shown bool |
| 302 | } |
| 303 | // what reports the element's text and state, whether it is drawn, and |
| 304 | // whether it is a polite live region nothing hides from assistive |
| 305 | // technology. |
| 306 | what := func(sel string) string { |
| 307 | return `(() => { |
| 308 | const e = document.querySelector('` + sel + `') |
| 309 | if (!e) return { Text: '', State: '', Live: '', Shown: false } |
| 310 | const cs = getComputedStyle(e) |
| 311 | const hidden = !!e.closest('[aria-hidden="true"]') |
| 312 | return { Text: e.textContent.trim(), State: e.getAttribute('data-state') || '', Live: hidden ? 'hidden' : (e.getAttribute('aria-live') || ''), Shown: e.getClientRects().length > 0 && cs.visibility === 'visible' && cs.display !== 'none' } |
| 313 | })()` |
| 314 | } |
| 315 | fail := func(name string, metrics emulation.SetDeviceMetricsOverrideParams, touch bool) { |
| 316 | t.Helper() |
| 317 | created := post("/documents", "title="+name) |
| 318 | if created.Code != http.StatusSeeOther { |
| 319 | t.Fatalf("POST /documents: %d", created.Code) |
| 320 | } |
| 321 | sqid := strings.TrimPrefix(created.Header().Get("Location"), "/d/") |
| 322 | run(name+": open", command(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: touch, MaxTouchPoints: 5}), |
| 323 | command(emulation.SetDeviceMetricsOverride, metrics), |
| 324 | chromedp.Navigate(rig.Origin+"/d/"+sqid), chromedp.Reload(), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), chromedp.Sleep(500*time.Millisecond)) |
| 325 | if rec := post("/d/"+sqid+"/delete", ""); rec.Code != http.StatusSeeOther { |
| 326 | t.Fatalf("delete: %d", rec.Code) |
| 327 | } |
| 328 | run(name+": type and wait for the refusal", |
| 329 | chromedp.Evaluate[chromedp.Void](`(() => { const el = document.querySelector('[docs-text]'); el.focus(); document.execCommand('insertText', false, 'lost words'); return true })()`), |
| 330 | chromedp.WaitReady(chromedp.CSS(`[estilo-save-status~="floating"][data-state="failed"]`)), chromedp.Sleep(300*time.Millisecond)) |
| 331 | } |
| 332 | |
| 333 | // A desktop: the corner toast. |
| 334 | fail("Desktop", emulation.SetDeviceMetricsOverrideParams{Width: 1280, Height: 900, DeviceScaleFactor: 1}, false) |
| 335 | var toast said |
| 336 | run("the toast", evalInto(&toast, what(`[estilo-save-status~="floating"]`))) |
| 337 | if !toast.Shown || toast.Text != "docs.editor.status.failed" || toast.Live != "polite" { |
| 338 | t.Errorf("at 1280px the failure is %+v; want the toast to show and announce it", toast) |
| 339 | } |
| 340 | |
| 341 | // A phone at rest: the subtitle under the large title. |
| 342 | phone := emulation.SetDeviceMetricsOverrideParams{Width: 390, Height: 844, DeviceScaleFactor: 2, Mobile: true} |
| 343 | fail("Phone", phone, true) |
| 344 | var sub said |
| 345 | run("the subtitle", evalInto(&sub, what(`[estilo-save-status~="subtitle"]`))) |
| 346 | if !sub.Shown || sub.Text != "docs.editor.status.failed" || sub.Live != "polite" { |
| 347 | t.Errorf("on a phone at rest the failure is %+v; want the subtitle to show and announce it", sub) |
| 348 | } |
| 349 | |
| 350 | // Scrolled: the folded bar's inline line says it, first and toned. |
| 351 | run("scroll", chromedp.Evaluate[chromedp.Void](`document.querySelector('[docs-page]').style.minBlockSize = '3000px'; window.scrollTo(0, 600); true`), chromedp.Sleep(700*time.Millisecond)) |
| 352 | var inline said |
| 353 | var first bool |
| 354 | run("the inline line", evalInto(&inline, what(`[docs-inline-state]`)), |
| 355 | evalInto(&first, `(() => { const s = document.querySelector('[docs-inline-sub]'); return s.firstElementChild === document.querySelector('[docs-inline-state]') && s.firstChild === s.firstElementChild })()`)) |
| 356 | if !inline.Shown || inline.Text != "docs.editor.status.failed" || inline.State != "failed" || !first { |
| 357 | t.Errorf("folded, the failure is %+v (first in its line: %v); want it shown, toned and first", inline, first) |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | // A rename that fails never leaves the page (Fable's delta on #179, F1): |
| 362 | // refused by the server or with no connection, the title goes back, the |
| 363 | // field reopens holding what was typed, the hint slot says it could not |
| 364 | // rename, and the words typed into the document are still there. A |
| 365 | // rename succeeds only on the server's 204 (F2). The newest rename wins: |
| 366 | // an older one failing late does not undo it (F3). |
| 367 | func TestARenameThatFailsStaysOnThePage(t *testing.T) { |
| 368 | var app *docs.App |
| 369 | rig := harness.New(t, func(origin string) http.Handler { |
| 370 | var err error |
| 371 | app, err = docs.NewForTestWith(t, docs.Config{Origin: origin}) |
| 372 | if err != nil { |
| 373 | t.Fatalf("NewForTestWith: %v", err) |
| 374 | } |
| 375 | return app.Handler() |
| 376 | }) |
| 377 | alice := signInCookies(t, app, "alice@example.com") |
| 378 | req := httptest.NewRequest(http.MethodPost, "/documents", strings.NewReader("title=Minutes")) |
| 379 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 380 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 381 | for _, c := range alice { |
| 382 | req.AddCookie(c) |
| 383 | } |
| 384 | rec := httptest.NewRecorder() |
| 385 | app.Handler().ServeHTTP(rec, req) |
| 386 | sqid := strings.TrimPrefix(rec.Header().Get("Location"), "/d/") |
| 387 | run := func(what string, actions ...chromedp.Action[chromedp.Void]) { |
| 388 | t.Helper() |
| 389 | ctx, cancel := context.WithTimeout(rig.Context(), 25*time.Second) |
| 390 | defer cancel() |
| 391 | if err := chromedp.Do(ctx, actions...); err != nil { |
| 392 | t.Fatalf("%s: %v", what, err) |
| 393 | } |
| 394 | } |
| 395 | var set []chromedp.Action[chromedp.Void] |
| 396 | for _, c := range alice { |
| 397 | set = append(set, setCookie(rig.Origin, c)) |
| 398 | } |
| 399 | run("a phone", append(set, command(emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5}), |
| 400 | command(emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: 390, Height: 844, DeviceScaleFactor: 2, Mobile: true}), |
| 401 | chromedp.Navigate(rig.Origin+"/d/"+sqid), chromedp.Reload(), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), chromedp.Sleep(400*time.Millisecond))...) |
| 402 | |
| 403 | type after struct { |
| 404 | Stayed, Editing, Error bool |
| 405 | Value, Title, Said string |
| 406 | } |
| 407 | state := `({ Stayed: window.__stayed === true, Editing: !!document.querySelector('[docs-title-form][data-editing]'), Error: !!document.querySelector('[docs-title-error]') && document.querySelector('[docs-title-error]').getClientRects().length > 0, Said: (document.querySelector('[docs-title-error]') || {}).textContent || '', Value: document.querySelector('[docs-title-input]').value, Title: document.querySelector('[docs-title-text]').textContent })` |
| 408 | rename := func(title string) chromedp.Action[chromedp.Void] { |
| 409 | return chromedp.Evaluate[chromedp.Void](`(() => { |
| 410 | document.querySelector('[docs-title-edit]').click() |
| 411 | const i = document.querySelector('[docs-title-input]') |
| 412 | i.value = ` + "`" + title + "`" + ` |
| 413 | i.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })) |
| 414 | return true |
| 415 | })()`) |
| 416 | } |
| 417 | check := func(what, typed string, a after) { |
| 418 | t.Helper() |
| 419 | if !a.Stayed || a.Title != "Minutes" || !a.Editing || a.Value != typed || !a.Error || a.Said != "docs.chrome.title.failed" { |
| 420 | t.Errorf("%s: %+v; want the same page, the title back to Minutes, the field open with what was typed and the failure said", what, a) |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | // Refused: a title longer than the server takes (1024 bytes) is a 400. |
| 425 | long := strings.Repeat("x", 1100) |
| 426 | var a after |
| 427 | run("refused", chromedp.Evaluate[chromedp.Void](`window.__stayed = true; (() => { const el = document.querySelector('[docs-text]'); el.focus(); document.execCommand('insertText', false, 'kept words'); return true })()`), |
| 428 | rename(long), chromedp.Sleep(1200*time.Millisecond), evalInto(&a, state)) |
| 429 | check("refused", long, a) |
| 430 | run("cancel", chromedp.Click(chromedp.CSS(`[docs-title-cancel]`)), chromedp.Sleep(300*time.Millisecond)) |
| 431 | |
| 432 | // No connection: the page's fetch of the rename fails. |
| 433 | run("offline", chromedp.Evaluate[chromedp.Void](`(() => { const f = window.fetch; window.__fetch = f; window.fetch = (u, o) => String(u).endsWith('/title') ? Promise.reject(new TypeError('Failed to fetch')) : f(u, o); return true })()`), |
| 434 | rename("Offline name"), chromedp.Sleep(800*time.Millisecond), evalInto(&a, state)) |
| 435 | check("offline", "Offline name", a) |
| 436 | run("cancel again", chromedp.Click(chromedp.CSS(`[docs-title-cancel]`)), chromedp.Sleep(300*time.Millisecond)) |
| 437 | |
| 438 | // Stale: the first rename fails late, after a second has succeeded. |
| 439 | run("stale", chromedp.Evaluate[chromedp.Void](`(() => { const f = window.__fetch; let first = true; window.fetch = (u, o) => { if (String(u).endsWith('/title') && first) { first = false; return new Promise((_, no) => setTimeout(() => no(new TypeError('late')), 700)) } return f(u, o) }; return true })()`), |
| 440 | rename("First try"), chromedp.Sleep(100*time.Millisecond), rename("Second try"), chromedp.Sleep(1500*time.Millisecond), evalInto(&a, state)) |
| 441 | if !a.Stayed || a.Title != "Second try" || a.Editing || a.Error { |
| 442 | t.Errorf("stale: %+v; want Second try kept, the field closed and nothing said", a) |
| 443 | } |
| 444 | |
| 445 | // The words typed before the failures, and the newest title, are kept. |
| 446 | var kept struct{ Text, Title string } |
| 447 | run("reload", chromedp.Reload(), chromedp.WaitVisible(chromedp.CSS(`[contenteditable="true"]`)), chromedp.Sleep(400*time.Millisecond), |
| 448 | evalInto(&kept, `({ Text: document.querySelector('[docs-text]').textContent, Title: document.querySelector('[docs-title-text]').textContent })`)) |
| 449 | if !strings.Contains(kept.Text, "kept words") || kept.Title != "Second try" { |
| 450 | t.Errorf("after a reload: text %q, title %q", kept.Text, kept.Title) |
| 451 | } |
| 452 | } |
| 453 | |