| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "fmt" |
| 8 | "net/http" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "regexp" |
| 12 | "strconv" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/chromedp/chromedp" |
| 17 | |
| 18 | "amadan.net/rastrillo/rastrillo/harness" |
| 19 | |
| 20 | "amadan.net/oficina/base/internal/equipo" |
| 21 | "amadan.net/oficina/base/internal/equipo/equipotest" |
| 22 | ) |
| 23 | |
| 24 | // noOverflow fails if the page is wider than the screen. |
| 25 | func noOverflow(t *testing.T, rig *harness.Rig, page string) { |
| 26 | t.Helper() |
| 27 | if got := harness.Evaluate[string](rig, `(() => { |
| 28 | const w = document.documentElement.clientWidth, s = document.documentElement.scrollWidth; |
| 29 | if (s <= w) return ""; |
| 30 | const wide = [...document.querySelectorAll("body *")].filter(e => e.getBoundingClientRect().right > w + 0.5) |
| 31 | .slice(0, 5).map(e => e.tagName.toLowerCase() + "." + e.className + " " + Math.round(e.getBoundingClientRect().right)); |
| 32 | return s + " > " + w + ": " + wide.join(", "); |
| 33 | })()`); got != "" { |
| 34 | t.Errorf("%s overflows the screen: %s", page, got) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // mobileShot saves a full-page PNG when BASE_BROWSER_ARTIFACTS is set. |
| 39 | func mobileShot(t *testing.T, rig *harness.Rig, name string) { |
| 40 | t.Helper() |
| 41 | dir := os.Getenv("BASE_BROWSER_ARTIFACTS") |
| 42 | if dir == "" { |
| 43 | return |
| 44 | } |
| 45 | drive(t, rig, chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 46 | _, err := chromedp.Evaluate[bool](`document.fonts.ready.then(() => true)`, chromedp.EvalAwaitPromise)(ctx, target) |
| 47 | return err |
| 48 | })) |
| 49 | var png []byte |
| 50 | drive(t, rig, chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 51 | var err error |
| 52 | png, err = chromedp.FullScreenshot(100)(ctx, target) |
| 53 | return err |
| 54 | })) |
| 55 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if err := os.WriteFile(filepath.Join(dir, name+".png"), png, 0o644); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // Base on an iPhone-sized screen: every page fits 390px, Home's top bar |
| 64 | // stays on one line, and the chat is a full-screen sheet whose composer |
| 65 | // stays on screen. |
| 66 | func TestMobile(t *testing.T) { |
| 67 | f := equipotest.New(t) |
| 68 | f.Reply = func(string) string { |
| 69 | return "You can ask me to plan work, check on a project, or start a task. What would you like to do?" |
| 70 | } |
| 71 | apps := teamApps(t) |
| 72 | var b *baseApp |
| 73 | rig := newRig(t, func(origin string) http.Handler { |
| 74 | cfg := baseConfig(origin) |
| 75 | cfg.EquipoOrigin = f.URL |
| 76 | cfg.OficinaApps = apps |
| 77 | b = newBase(t, cfg) |
| 78 | return b.handler |
| 79 | }) |
| 80 | drive(t, rig, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3)), reducedMotion("dark", false)) |
| 81 | drive(t, rig, chromedp.Navigate(rig.Origin+"/signin"), chromedp.WaitVisible(chromedp.CSS(`input[name="address"]`))) |
| 82 | noOverflow(t, rig, "sign-in") |
| 83 | installAll(t, b) |
| 84 | signInByEmail(t, rig, rig.Origin, b.mail) |
| 85 | |
| 86 | // Home: on a phone the Base button sits on one line with the wordmark |
| 87 | // and the avatar, and the Ask field is under the greeting. |
| 88 | noOverflow(t, rig, "Home") |
| 89 | if got := harness.Evaluate[string](rig, `(() => { |
| 90 | const bar = document.querySelector(".topbar"), icon = document.querySelector("a.base-btn"), field = document.querySelector("a.ask-field"); |
| 91 | const shown = [...bar.querySelectorAll(":scope > *, :scope > form > *")].filter(e => e.checkVisibility()); |
| 92 | const mids = shown.map(e => Math.round(e.getBoundingClientRect().top + e.getBoundingClientRect().height / 2)); |
| 93 | const problems = []; |
| 94 | if (Math.max(...mids) - Math.min(...mids) > 2) problems.push("top bar wraps: " + mids.join(",")); |
| 95 | if (!field.checkVisibility({ visibilityProperty: true })) problems.push("no Ask field under the greeting"); |
| 96 | if (!icon || !icon.checkVisibility({ visibilityProperty: true })) return problems.concat("no Base button").join("; "); |
| 97 | const r = icon.getBoundingClientRect(), me = document.querySelector(".me").getBoundingClientRect(); |
| 98 | if (Math.round(r.width) !== 44 || Math.round(r.height) !== 44) problems.push("Base button is " + r.width + "x" + r.height); |
| 99 | if (Math.round(me.width) !== 44) problems.push("avatar is " + me.width); |
| 100 | if (icon.getAttribute("aria-label") !== "Ask Base") problems.push("Base button labelled " + icon.getAttribute("aria-label")); |
| 101 | const used = shown.reduce((w, e) => w + e.getBoundingClientRect().width, 0); |
| 102 | if (bar.clientWidth - used < 60) problems.push("no room to spare: " + (bar.clientWidth - used)); |
| 103 | return problems.join("; "); |
| 104 | })()`); got != "" { |
| 105 | t.Errorf("Home's top bar on a phone: %s", got) |
| 106 | } |
| 107 | mobileShot(t, rig, "1-home") |
| 108 | |
| 109 | // Home on a phone is a springboard: every app, Store, People and |
| 110 | // Settings among them, in four columns inside the glass panel. |
| 111 | if got := harness.Evaluate[string](rig, springboardJS+`(`+gridNames(apps)+`)`); got != "" { |
| 112 | t.Errorf("the springboard: %s", got) |
| 113 | } |
| 114 | checkContrast(t, rig, "Home on a phone, dark", ".lede", ".app-grid .label") |
| 115 | viewportShot(t, rig, "home-phone") |
| 116 | layoutShot(t, rig, "phone-home-dark") |
| 117 | |
| 118 | // The chat before connecting. |
| 119 | drive(t, rig, chromedp.Click(chromedp.CSS(`a.base-btn`)), |
| 120 | chromedp.WaitVisible(chromedp.CSS(`#ask-panel [data-when="connect"] button`)), |
| 121 | // The sheet slides up; click once it has arrived. |
| 122 | chromedp.Poll[chromedp.Void](`document.querySelector("#ask-panel").getBoundingClientRect().top === 0`, chromedp.WithPollingTimeout(3*time.Second))) |
| 123 | mobileShot(t, rig, "2-panel-connect") |
| 124 | |
| 125 | // Connect from the panel: the code page, then back to the open panel. |
| 126 | drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel [data-when="connect"] button`)), chromedp.WaitVisible(chromedp.CSS(`p.code`))) |
| 127 | noOverflow(t, rig, "the connect page") |
| 128 | mobileShot(t, rig, "3-connect-code") |
| 129 | if err := f.Approve(harness.Evaluate[string](rig, `document.querySelector("p.code").textContent`)); err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | if _, err := b.db.Exec(`UPDATE equipo_links SET polled_at = ?`, time.Now().Add(-time.Minute).UTC()); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | drive(t, rig, chromedp.Click(chromedp.CSS(`form[action="/ask/connect/check"] button`)), |
| 136 | chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)), |
| 137 | chromedp.Poll[chromedp.Void](`document.querySelector("#ask-panel").getBoundingClientRect().top === 0`, chromedp.WithPollingTimeout(3*time.Second))) |
| 138 | |
| 139 | // Mid-conversation: the sheet fills the screen, the header is at the |
| 140 | // top, the composer on screen at the bottom, targets 44px. |
| 141 | drive(t, rig, chromedp.SendKeys(chromedp.CSS(`#ask-text`), "What can I do"), |
| 142 | chromedp.Click(chromedp.CSS(`#ask-panel form.composer button.send`)), |
| 143 | chromedp.Poll[chromedp.Void](`!document.querySelector("#ask-panel .thinking, #ask-panel .draft, #ask-panel .think-line") && [...document.querySelectorAll("#ask-panel .msg.coordinator")].some(m => m.dataset.id)`, |
| 144 | chromedp.WithPollingTimeout(10*time.Second)), |
| 145 | chromedp.Poll[chromedp.Void](`(() => { const r = document.querySelector("#ask-panel").getBoundingClientRect(); return r.left === 0 && r.top === 0 })()`, |
| 146 | chromedp.WithPollingTimeout(3*time.Second))) |
| 147 | if got := harness.Evaluate[string](rig, `(() => { |
| 148 | const vh = window.visualViewport.height, vw = window.visualViewport.width, problems = []; |
| 149 | const p = document.querySelector("#ask-panel").getBoundingClientRect(); |
| 150 | if (Math.round(p.width) !== Math.round(vw) || Math.round(p.height) !== Math.round(vh)) problems.push("sheet " + p.width + "x" + p.height); |
| 151 | const head = document.querySelector("#ask-panel .ask-head").getBoundingClientRect(); |
| 152 | if (head.top < 0 || head.top > 1) problems.push("header at " + head.top); |
| 153 | const c = document.querySelector("#ask-panel form.composer").getBoundingClientRect(); |
| 154 | if (c.top < 0 || c.bottom > vh + 0.5 || c.height === 0) problems.push("composer at " + c.top + "-" + c.bottom + " of " + vh); |
| 155 | if (document.querySelector("#ask-panel .ask-head summary, #ask-panel .ask-head a")) problems.push("the header has a menu or a link"); |
| 156 | if (head.height > 64) problems.push("header is " + head.height + "px tall"); |
| 157 | // Send shows once something is typed. |
| 158 | const ta = document.querySelector("#ask-text"); |
| 159 | ta.value = "x"; |
| 160 | ta.dispatchEvent(new Event("input")); |
| 161 | for (const a of document.querySelector("#ask-panel form.composer button.send").getAnimations()) a.finish(); // measured once it has grown in |
| 162 | for (const el of document.querySelectorAll("#ask-panel .ask-head .close, #ask-panel form.composer button.send, #ask-panel textarea")) { |
| 163 | const r = el.getBoundingClientRect(); |
| 164 | if (r.height < 44 || r.width < 44) problems.push((el.getAttribute("aria-label") || el.textContent.trim()) + " is " + r.width + "x" + r.height); |
| 165 | } |
| 166 | const field = document.querySelector("#ask-panel textarea").getBoundingClientRect(); |
| 167 | const send = document.querySelector("#ask-panel form.composer button.send").getBoundingClientRect(); |
| 168 | ta.value = ""; |
| 169 | ta.dispatchEvent(new Event("input")); |
| 170 | if (field.left > 12 || vw - send.right > 12) problems.push("composer gaps " + field.left + " and " + (vw - send.right)); |
| 171 | const st = document.querySelector("#ask-panel .ask-status"); |
| 172 | st.textContent = "Thinking…"; |
| 173 | if (Math.abs(st.getBoundingClientRect().left - field.left) > 1) problems.push("status at " + st.getBoundingClientRect().left + ", composer at " + field.left); |
| 174 | st.textContent = ""; |
| 175 | const m = document.querySelector("#ask-panel .msg.coordinator").getBoundingClientRect(); |
| 176 | if (m.right > vw) problems.push("a message overflows"); |
| 177 | return problems.join("; "); |
| 178 | })()`); got != "" { |
| 179 | t.Errorf("the chat on a phone: %s", got) |
| 180 | } |
| 181 | mobileShot(t, rig, "4-panel-conversation") |
| 182 | layoutShot(t, rig, "phone-chat-dark") |
| 183 | // Home behind the sheet is pushed back: smaller, dimmed and blurred. |
| 184 | if got := harness.Evaluate[string](rig, `(() => { const s = getComputedStyle(document.querySelector("main.home")); |
| 185 | return s.transform + " | " + s.filter; })()`); !regexp.MustCompile(`^matrix\(0\.94, 0, 0, 0\.94, [^)]*\) \| .*blur\(`).MatchString(got) { |
| 186 | t.Errorf("Home behind the open sheet: %s, want scaled to 0.94 and blurred", got) |
| 187 | } |
| 188 | mobileShot(t, rig, "4c-panel-tight") |
| 189 | drive(t, rig, chromedp.KeyEvent("\u001b"), |
| 190 | chromedp.Poll[chromedp.Void](phoneClosedJS, chromedp.WithPollingTimeout(3*time.Second))) |
| 191 | // Focus goes back to the Base button, which opened it. |
| 192 | drive(t, rig, chromedp.Poll[chromedp.Void](`document.activeElement.className === "base-btn"`, chromedp.WithPollingTimeout(3*time.Second))) |
| 193 | // Reopened from the Base button, mid-conversation: the sheet slides up. |
| 194 | drive(t, rig, chromedp.Click(chromedp.CSS(`a.base-btn`)), chromedp.Sleep(90*time.Millisecond)) |
| 195 | if top := harness.Evaluate[float64](rig, `document.querySelector("#ask-panel").getBoundingClientRect().top`); top <= 1 { |
| 196 | t.Errorf("the sheet did not slide up: at %v moments after opening", top) |
| 197 | } |
| 198 | opening := harness.Read(rig, chromedp.CaptureScreenshot()) |
| 199 | if dir := os.Getenv("BASE_LAYOUT_ARTIFACTS"); dir != "" { |
| 200 | if err := os.WriteFile(filepath.Join(dir, "phone-chat-opening.png"), opening, 0o644); err != nil { |
| 201 | t.Fatal(err) |
| 202 | } |
| 203 | } |
| 204 | drive(t, rig, chromedp.WaitVisible(chromedp.CSS(`#ask-panel .msg.coordinator`)), |
| 205 | chromedp.Poll[chromedp.Void](`document.querySelector("#ask-panel").getBoundingClientRect().top === 0`, chromedp.WithPollingTimeout(3*time.Second))) |
| 206 | mobileShot(t, rig, "4b-home-panel-open") |
| 207 | drive(t, rig, chromedp.KeyEvent("\u001b")) |
| 208 | |
| 209 | // With reduced motion there is no slide and no push-back: it just shows. |
| 210 | drive(t, rig, reducedMotion("dark", true), chromedp.Poll[chromedp.Void](phoneClosedJS, chromedp.WithPollingTimeout(3*time.Second)), |
| 211 | chromedp.Click(chromedp.CSS(`a.base-btn`)), chromedp.Sleep(30*time.Millisecond)) |
| 212 | if got := harness.Evaluate[string](rig, `(() => { const d = document.querySelector("#ask-panel").getBoundingClientRect(); |
| 213 | const h = getComputedStyle(document.querySelector("main.home")); |
| 214 | return d.top + " " + h.transform + " " + h.filter + " " + h.opacity; })()`); got != "0 none none 1" { |
| 215 | t.Errorf("with reduced motion the sheet and Home are %q (want 0 none none 1: no slide, no scale, no blur or dimming)", got) |
| 216 | } |
| 217 | drive(t, rig, chromedp.KeyEvent("\u001b"), reducedMotion("dark", false)) |
| 218 | |
| 219 | // Every inner page has an X at its top right, 44px, that goes Home. |
| 220 | for _, p := range []struct{ path, wait, name string }{ |
| 221 | {"/store", ".sheet", "5-store"}, |
| 222 | {"/people", ".sheet", "6-people"}, |
| 223 | {"/settings", ".sheet", "7-settings"}, |
| 224 | {"/ask", ".ask-page .composer", "8-ask"}, |
| 225 | } { |
| 226 | drive(t, rig, chromedp.Navigate(rig.Origin+p.path), chromedp.WaitVisible(chromedp.CSS(p.wait))) |
| 227 | noOverflow(t, rig, p.path) |
| 228 | if got := harness.Evaluate[string](rig, closeJS); got != "" { |
| 229 | t.Errorf("%s's close: %s", p.path, got) |
| 230 | } |
| 231 | mobileShot(t, rig, p.name) |
| 232 | drive(t, rig, chromedp.Click(chromedp.CSS(`.crumb a.close`)), chromedp.WaitVisible(chromedp.CSS(`.hello`))) |
| 233 | if here := harness.Read(rig, chromedp.Location()); here != rig.Origin+"/" { |
| 234 | t.Errorf("%s's close went to %s, want Home", p.path, here) |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | // closeJS describes what is wrong with an inner page's close, "" if |
| 240 | // nothing: one X at the top right of the sheet, 44px, named Close, a link |
| 241 | // to Home. |
| 242 | const closeJS = `(() => { |
| 243 | const all = document.querySelectorAll(".crumb a.close"), problems = []; |
| 244 | if (all.length !== 1) return all.length + " close links"; |
| 245 | const x = all[0], r = x.getBoundingClientRect(), sheet = x.closest(".sheet").getBoundingClientRect(); |
| 246 | if (x.getAttribute("href") !== "/") problems.push("goes to " + x.getAttribute("href")); |
| 247 | if (x.getAttribute("aria-label") !== "Close") problems.push("named " + x.getAttribute("aria-label")); |
| 248 | if (!x.checkVisibility({ visibilityProperty: true })) problems.push("not shown"); |
| 249 | if (Math.round(r.width) !== 44 || Math.round(r.height) !== 44) problems.push("is " + r.width + "x" + r.height); |
| 250 | if (sheet.right - r.right > 36 || r.top - sheet.top > 36 || r.top < sheet.top) problems.push("not at the top right: " + Math.round(sheet.right - r.right) + " from the right, " + Math.round(r.top - sheet.top) + " from the top"); |
| 251 | const w = document.querySelector(".crumb .wordmark").getBoundingClientRect(); |
| 252 | if (Math.abs((w.top + w.bottom) / 2 - (r.top + r.bottom) / 2) > 1) problems.push("not level with the wordmark"); |
| 253 | if (getComputedStyle(x).borderRadius !== "50%") problems.push("not round: " + getComputedStyle(x).borderRadius); |
| 254 | return problems.join("; "); |
| 255 | })()` |
| 256 | |
| 257 | // On a phone a double tap is two taps, never a zoom: the page and what is |
| 258 | // tapped on it are touch-action: manipulation. Pinching still zooms: the |
| 259 | // viewport sets no maximum scale and doesn't stop the person scaling it. |
| 260 | func TestMobileNoDoubleTapZoom(t *testing.T) { |
| 261 | rig, _, b := askBaseOn(t, nil, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))) |
| 262 | installAll(t, b) |
| 263 | for _, p := range []struct{ path, wait string }{ |
| 264 | {"/", ".hello"}, {"/settings", "form.segmented"}, {"/store", ".listing"}, {"/people", ".people"}, {"/ask", ".ask-page .composer"}, |
| 265 | } { |
| 266 | drive(t, rig, chromedp.Navigate(rig.Origin+p.path), chromedp.WaitVisible(chromedp.CSS(p.wait))) |
| 267 | if got := harness.Evaluate[string](rig, `(() => { |
| 268 | const problems = [], meta = document.querySelector('meta[name="viewport"]').content; |
| 269 | if (/maximum-scale|user-scalable/i.test(meta)) problems.push("the viewport stops zooming: " + meta); |
| 270 | for (const el of [document.documentElement, document.body, ...document.querySelectorAll("a, button, input, textarea, summary, label, li, p, .sheet, .glass")]) { |
| 271 | const ta = getComputedStyle(el).touchAction; |
| 272 | if (ta !== "manipulation") problems.push(el.tagName.toLowerCase() + (el.className ? "." + el.className : "") + " is touch-action: " + ta); |
| 273 | } |
| 274 | return [...new Set(problems)].slice(0, 6).join("; "); |
| 275 | })()`); got != "" { |
| 276 | t.Errorf("%s: %s", p.path, got) |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // phoneClosedJS is true once the chat on a phone has gone: no phone-open, |
| 282 | // out of sight. |
| 283 | const phoneClosedJS = `!document.documentElement.classList.contains("phone-open") && !document.querySelector("#ask-panel").checkVisibility({ visibilityProperty: true })` |
| 284 | |
| 285 | // On a phone Home opens on the springboard with Ask Base out of sight, |
| 286 | // whatever the member chose on a wide screen; the field brings it up full |
| 287 | // screen over Home, which is out of reach behind it, and X or Escape closes |
| 288 | // it. Neither is remembered. |
| 289 | func TestAskPhoneUnchanged(t *testing.T) { |
| 290 | rig, _, b := askBaseOn(t, nil, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))) |
| 291 | whole := `(() => { const r = document.querySelector("#ask-panel").getBoundingClientRect(); |
| 292 | return [r.left, r.top, r.width, r.height].map(Math.round).join(" ") === "0 0 390 844" })()` |
| 293 | for _, closed := range []bool{false, true} { |
| 294 | if _, err := b.db.Exec(`UPDATE members SET ask_closed = ?`, closed); err != nil { |
| 295 | t.Fatal(err) |
| 296 | } |
| 297 | when := fmt.Sprintf("ask_closed %v", closed) |
| 298 | drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS("a.ask-field"))) |
| 299 | if got := harness.Evaluate[string](rig, `(() => { const problems = []; |
| 300 | if (document.querySelector("#ask-panel").checkVisibility({ visibilityProperty: true })) problems.push("the chat shows"); |
| 301 | if (!document.querySelector("#ask-panel").inert) problems.push("the chat is in reach"); |
| 302 | if (document.querySelector("main.home").inert) problems.push("Home is out of reach"); |
| 303 | const apps = document.querySelector("nav.apps").getBoundingClientRect(); |
| 304 | if (apps.top > innerHeight || apps.width < 300) problems.push("no springboard: the apps at " + apps.top + ", " + apps.width + " wide"); |
| 305 | return problems.join("; "); })()`); got != "" { |
| 306 | t.Errorf("%s, Home: %s", when, got) |
| 307 | } |
| 308 | drive(t, rig, chromedp.Click(chromedp.CSS("a.ask-field")), chromedp.Poll[chromedp.Void](whole, chromedp.WithPollingTimeout(3*time.Second))) |
| 309 | // Only transform moves Home and the header back; the blur just applies. |
| 310 | if got := harness.Evaluate[string](rig, `getComputedStyle(document.querySelector("main.home")).transitionProperty + " " + getComputedStyle(document.querySelector(".topbar")).transitionProperty`); got != "transform transform" { |
| 311 | t.Errorf("%s: Home and the header transition %q, want transform alone", when, got) |
| 312 | } |
| 313 | if got := harness.Evaluate[string](rig, `[document.documentElement.classList.contains("phone-open"), document.activeElement.id, document.querySelector("main.home").inert, document.querySelector(".topbar").inert].join(" ")`); got != "true ask-text true true" { |
| 314 | t.Errorf("%s, opened by the field: %q, want phone-open, focus in the composer, Home and the header out of reach", when, got) |
| 315 | } |
| 316 | if closed { |
| 317 | mobileShot(t, rig, "9-chat-open-closed-on-desktop") |
| 318 | } else { |
| 319 | drive(t, rig, reducedMotion("light", false)) |
| 320 | layoutShot(t, rig, "phone-chat-light") |
| 321 | drive(t, rig, reducedMotion("dark", false)) |
| 322 | } |
| 323 | drive(t, rig, chromedp.Click(chromedp.CSS(`#ask-panel .ask-head button.close`)), chromedp.Poll[chromedp.Void](phoneClosedJS, chromedp.WithPollingTimeout(3*time.Second))) |
| 324 | if got := harness.Evaluate[string](rig, `document.activeElement.className + " " + document.querySelector("main.home").inert`); got != "ask-field false" { |
| 325 | t.Errorf("%s, after X: %q, want focus on the field and Home in reach", when, got) |
| 326 | } |
| 327 | drive(t, rig, chromedp.Click(chromedp.CSS("a.ask-field")), chromedp.Poll[chromedp.Void](whole, chromedp.WithPollingTimeout(3*time.Second)), |
| 328 | chromedp.KeyEvent("\u001b"), chromedp.Poll[chromedp.Void](phoneClosedJS, chromedp.WithPollingTimeout(3*time.Second))) |
| 329 | var got bool |
| 330 | if err := b.db.QueryRow(`SELECT ask_closed FROM members`).Scan(&got); err != nil || got != closed { |
| 331 | t.Errorf("%s: a phone's open and close were kept: ask_closed %v %v", when, got, err) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // Without scripts the field and the Base button go to /ask, and keep |
| 336 | // no wide-screen choice. |
| 337 | drive(t, rig, scriptsDisabled(true)) |
| 338 | defer drive(t, rig, scriptsDisabled(false)) |
| 339 | for _, opener := range []string{"a.ask-field", "a.base-btn"} { |
| 340 | drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS(opener)), chromedp.Click(chromedp.CSS(opener)), |
| 341 | chromedp.WaitVisible(chromedp.CSS(".ask-page .composer"))) |
| 342 | if here := harness.Read(rig, chromedp.Location()); here != rig.Origin+"/ask" { |
| 343 | t.Errorf("scripts off, %s led to %s, want /ask", opener, here) |
| 344 | } |
| 345 | var got bool |
| 346 | if err := b.db.QueryRow(`SELECT ask_closed FROM members`).Scan(&got); err != nil || !got { |
| 347 | t.Errorf("scripts off, %s changed the wide-screen choice: ask_closed %v %v", opener, got, err) |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | // The job view on a phone (plan decision D13): a sheet from the foot of |
| 353 | // the screen, over the dimmed chat (a scrim), the title's arrow always |
| 354 | // showing. A tap on the scrim closes it, and focus goes back to the pill. |
| 355 | // Without scripts the pill's <details> opens in place, under the pill. |
| 356 | func TestAskJobViewIsASheetOnPhones(t *testing.T) { |
| 357 | rig, f, _ := askBaseOn(t, nil, chromedp.EmulateViewport(390, 844, chromedp.EmulateMobile, chromedp.EmulateScale(3))) |
| 358 | drive(t, rig, chromedp.Navigate(rig.Origin+"/"), chromedp.WaitVisible(chromedp.CSS("a.ask-field")), |
| 359 | chromedp.Click(chromedp.CSS("a.ask-field")), chromedp.WaitVisible(chromedp.CSS(`#ask-panel textarea`)), |
| 360 | chromedp.Poll[chromedp.Void](readJS, chromedp.WithPollingTimeout(5*time.Second)), |
| 361 | chromedp.Poll[chromedp.Void](`document.querySelector("#ask-panel").getBoundingClientRect().top === 0`, chromedp.WithPollingTimeout(3*time.Second))) |
| 362 | f.Say("On it.") |
| 363 | id := f.AskJob("Building your website") |
| 364 | job := `#ask-panel li.job[data-job="` + id + `"]` |
| 365 | drive(t, rig, chromedp.WaitVisible(chromedp.CSS(job+" summary")), chromedp.Click(chromedp.CSS(job+" summary")), chromedp.Sleep(400*time.Millisecond)) |
| 366 | sheet := `(() => { const j = document.querySelector(` + strconv.Quote(job) + `), v = j.querySelector(".jobview"), s = j.querySelector("summary"), p = []; |
| 367 | if (!j.querySelector("details").open || s.getAttribute("aria-expanded") !== "true") p.push("not open"); |
| 368 | if (!v.checkVisibility()) return p.concat("the view is not drawn").join("; "); |
| 369 | const r = v.getBoundingClientRect(); |
| 370 | if (Math.abs(r.bottom - innerHeight) > 0.5 || Math.abs(r.left) > 0.5 || Math.abs(r.right - innerWidth) > 0.5) p.push("not a sheet at the foot: " + JSON.stringify(r) + " in " + innerWidth + "x" + innerHeight); |
| 371 | const mid = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2); |
| 372 | if (!v.contains(mid)) p.push("covered at its middle by " + (mid && mid.className)); |
| 373 | const scrim = document.querySelector("#ask-panel .scrim"); |
| 374 | if (!scrim || !scrim.checkVisibility()) p.push("no scrim"); |
| 375 | else if (document.elementFromPoint(innerWidth / 2, 120) !== scrim) p.push("the chat is not behind the scrim: " + document.elementFromPoint(innerWidth / 2, 120)?.className); |
| 376 | if (document.activeElement !== v.querySelector("[data-job-close]")) p.push("focus on " + document.activeElement.className); |
| 377 | if (getComputedStyle(v.querySelector(".jv-title svg")).opacity !== "1") p.push("the title's arrow hides"); |
| 378 | return p.join("; "); })()` |
| 379 | if got := harness.Evaluate[string](rig, sheet); got != "" { |
| 380 | t.Errorf("the job view on a phone: %s", got) |
| 381 | } |
| 382 | for _, scheme := range []string{"dark", "light"} { |
| 383 | drive(t, rig, prefersScheme(scheme), chromedp.Sleep(300*time.Millisecond)) |
| 384 | // Measured before the full-page shot, which leaves a phone's |
| 385 | // screenshots cut short for a moment. |
| 386 | checkContrast(t, rig, "the job view on a phone, "+scheme, job+" .jobview h3", job+" .jv-stage", job+" .jv-detail") |
| 387 | mobileShot(t, rig, "jobview-working-phone-"+scheme) |
| 388 | } |
| 389 | drive(t, rig, prefersScheme("dark")) |
| 390 | // A new revision keeps the sheet up. |
| 391 | f.SetJob(id, "ready", "Ready to try", "Have a look.", equipo.Link{Label: "Try it", URL: "https://preview.example.test/"}) |
| 392 | drive(t, rig, chromedp.Poll[chromedp.Void](`document.querySelector(`+strconv.Quote(job+" .jv-stage")+`).textContent === "Ready to try"`, chromedp.WithPollingTimeout(10*time.Second)), |
| 393 | chromedp.Sleep(300*time.Millisecond)) |
| 394 | if got := harness.Evaluate[string](rig, sheet); got != "" { |
| 395 | t.Errorf("the job view on a phone, redrawn: %s", got) |
| 396 | } |
| 397 | for _, scheme := range []string{"dark", "light"} { |
| 398 | drive(t, rig, prefersScheme(scheme), chromedp.Sleep(300*time.Millisecond)) |
| 399 | mobileShot(t, rig, "jobview-ready-phone-"+scheme) |
| 400 | } |
| 401 | drive(t, rig, prefersScheme("dark")) |
| 402 | |
| 403 | // A tap on the scrim, above the sheet, closes it. |
| 404 | drive(t, rig, chromedp.MouseClickXY(195, 120), chromedp.Sleep(300*time.Millisecond)) |
| 405 | if got := harness.Evaluate[string](rig, `(() => { const j = document.querySelector(`+strconv.Quote(job)+`), s = j.querySelector("summary"), p = []; |
| 406 | if (j.querySelector("details").open || s.getAttribute("aria-expanded") !== "false") p.push("still open"); |
| 407 | if (j.querySelector(".jobview").checkVisibility()) p.push("the view shows"); |
| 408 | if (document.querySelector("#ask-panel .scrim")?.checkVisibility()) p.push("the scrim shows"); |
| 409 | if (document.activeElement !== s) p.push("focus on " + document.activeElement.className); |
| 410 | if (!document.documentElement.classList.contains("phone-open")) p.push("the chat closed"); |
| 411 | return p.join("; "); })()`); got != "" { |
| 412 | t.Errorf("after a tap on the scrim: %s", got) |
| 413 | } |
| 414 | mobileShot(t, rig, "job-ready-phone-closed") |
| 415 | |
| 416 | // Without scripts: /ask, and the pill opens its view in place, just |
| 417 | // under its row, with no Close and no scrim. |
| 418 | drive(t, rig, scriptsDisabled(true)) |
| 419 | defer drive(t, rig, scriptsDisabled(false)) |
| 420 | page := `.ask-page li.job[data-job="` + id + `"]` |
| 421 | drive(t, rig, chromedp.Navigate(rig.Origin+"/ask"), chromedp.WaitVisible(chromedp.CSS(page+" summary")), |
| 422 | chromedp.Click(chromedp.CSS(page+" summary")), chromedp.Sleep(200*time.Millisecond)) |
| 423 | if got := harness.Evaluate[string](rig, `(() => { const j = document.querySelector(`+strconv.Quote(page)+`), v = j.querySelector(".jobview"), p = []; |
| 424 | if (!j.querySelector("details").open) p.push("closed"); |
| 425 | if (!v.checkVisibility()) return p.concat("the view is not drawn").join("; "); |
| 426 | const r = v.getBoundingClientRect(), pill = j.querySelector("summary").getBoundingClientRect(); |
| 427 | if (r.top < pill.bottom || r.top - pill.bottom > 12 || r.left < pill.left - 0.5) p.push("not under the pill: " + JSON.stringify(r) + " pill " + JSON.stringify(pill)); |
| 428 | if (v.querySelector("[data-job-close]").checkVisibility()) p.push("Close shows"); |
| 429 | if (document.querySelector(".scrim")) p.push("a scrim"); |
| 430 | if (v.querySelector(".jv-stage").textContent !== "Ready to try" || v.querySelector(".jv-go")?.textContent !== "Try it") p.push("its words: " + v.innerText); |
| 431 | return p.join("; "); })()`); got != "" { |
| 432 | t.Errorf("scripts off, the job view: %s", got) |
| 433 | } |
| 434 | mobileShot(t, rig, "jobview-ready-phone-noscript") |
| 435 | } |
| 436 | |