| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/rastrillo/rastrillo/blobs" |
| 11 | "amadan.net/rastrillo/rastrillo/harness" |
| 12 | "github.com/chromedp/cdproto/input" |
| 13 | "github.com/chromedp/chromedp" |
| 14 | "github.com/chromedp/chromedp/kb" |
| 15 | |
| 16 | "amadan.net/oficina/mapa/internal/mapa" |
| 17 | ) |
| 18 | |
| 19 | // A page's look in a real Chromium (columns and icons spec §9): LOOK7, |
| 20 | // no shift with a banner, an image icon and chips with icons, before |
| 21 | // and after the pictures load and when the look row shows on hover; |
| 22 | // and LOOK10, the picker by keyboard, with axe. |
| 23 | |
| 24 | // lookBoxes is the title, the look row and every block of the body, |
| 25 | // with their boxes. |
| 26 | const lookBoxes = `JSON.stringify(Array.prototype.map.call( |
| 27 | document.querySelectorAll("[mapa-banner], h1[mapa-title], " + |
| 28 | "[mapa-look-row], article[mapa-body], " + |
| 29 | "article[mapa-body] :is(p, h2, h3, li, a[mapa-chip])"), |
| 30 | function (block) { |
| 31 | var box = block.getBoundingClientRect(); |
| 32 | return [block.nodeName, box.left, box.top, box.width, box.height].join(" "); |
| 33 | }))` |
| 34 | |
| 35 | // lookedPage is a team page with an image icon and a banner, linking |
| 36 | // to a page with an emoji icon, made by Bo. |
| 37 | func lookedPage(t *testing.T, store *mapa.Store) mapa.Page { |
| 38 | t.Helper() |
| 39 | ctx := context.Background() |
| 40 | other, err := store.CreateTeamPage(ctx, Bo, "Compass", "One.") |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if _, err := store.SetLook(ctx, Bo, other.ID, mapa.LookChange{ |
| 45 | SetIcon: true, Icon: &mapa.Icon{Emoji: "🧭"}}); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | page, err := store.CreateTeamPage(ctx, Bo, "Pilot", |
| 49 | "# Plan\n\nSee [[Compass]] before the pilot.\n\n"+ |
| 50 | strings.Repeat("A paragraph that fills the page. ", 40)) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | icon, band := addImage(t, store, Bo, 64, 64), addImage(t, store, Bo, |
| 55 | 1600, 500) |
| 56 | _, err = store.SetLook(ctx, Bo, page.ID, mapa.LookChange{ |
| 57 | SetIcon: true, Icon: &mapa.Icon{Image: icon.PublicID}, |
| 58 | SetBanner: true, Banner: &mapa.Banner{Image: band.PublicID, Y: 30}}) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | return page |
| 63 | } |
| 64 | |
| 65 | func TestLook7TheLookShiftsNothing(t *testing.T) { |
| 66 | held := &heldGets{} |
| 67 | rig, store := browserRigWith(t, 0, func(inner blobs.Store) blobs.Store { |
| 68 | held.Store = inner |
| 69 | return held |
| 70 | }) |
| 71 | t.Cleanup(held.let) |
| 72 | path := mapa.PagePath(lookedPage(t, store)) |
| 73 | drive(t, rig, onNewDocument(shiftRecorder)) |
| 74 | for _, dark := range []bool{false, true} { |
| 75 | for _, width := range []int64{1440, 1000, 390} { |
| 76 | for _, live := range []bool{false, true} { |
| 77 | lookUnmoved(t, rig, held, path, width, live, dark) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // lookUnmoved loads path with the pictures held, measures, lets them |
| 84 | // come, hovers the page head, and finds nothing moved. |
| 85 | func lookUnmoved(t *testing.T, rig *harness.Rig, held *heldGets, |
| 86 | path string, width int64, live, dark bool) { |
| 87 | t.Helper() |
| 88 | held.hold() |
| 89 | drive(t, rig, scripts(live), colorScheme(dark), |
| 90 | chromedp.EmulateViewport(width, 900), |
| 91 | chromedp.Evaluate[chromedp.Void](`location.href = `+ |
| 92 | "`"+rig.Origin+path+"`"), |
| 93 | chromedp.WaitVisible("article[mapa-body] p")) |
| 94 | if live { |
| 95 | drive(t, rig, chromedp.WaitVisible(mounted)) |
| 96 | } |
| 97 | before := read(t, rig, chromedp.Evaluate[string](lookBoxes)) |
| 98 | held.let() |
| 99 | eventually(t, "the banner and icon loaded", func() bool { |
| 100 | return read(t, rig, chromedp.Evaluate[bool](`Array.prototype.every.call( |
| 101 | document.querySelectorAll('[mapa-banner] img, h1[mapa-title] img'), |
| 102 | function (img) { return img.complete && img.naturalWidth > 0; })`)) |
| 103 | }) |
| 104 | drive(t, rig, chromedp.MouseEvent( |
| 105 | input.DispatchMouseEventTypeMouseMoved, 200, 120)) |
| 106 | after := read(t, rig, chromedp.Evaluate[string](lookBoxes)) |
| 107 | if after != before { |
| 108 | t.Errorf("%dpx, scripts %v, dark %v: the page moved:\n%s\n%s", |
| 109 | width, live, dark, before, after) |
| 110 | } |
| 111 | shifts := read(t, rig, chromedp.Evaluate[string]( |
| 112 | `JSON.stringify(window.__shifts || [])`)) |
| 113 | if shifts != "[]" { |
| 114 | t.Errorf("%dpx: layout shifts: %s", width, shifts) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | func TestLook10ThePickerByKeyboard(t *testing.T) { |
| 119 | rig, store := browserRig(t, 0) |
| 120 | titledPage(t, rig, store, "Pilot", "One.") |
| 121 | drive(t, rig, chromedp.Focus(`[data-mapa-look="icon"]`), |
| 122 | chromedp.KeyEvent("\r"), |
| 123 | chromedp.WaitVisible(`[mapa-picker] [role="option"]`)) |
| 124 | if found := axeSerious(t, rig); found != "" { |
| 125 | t.Fatalf("axe, the picker open:\n%s", found) |
| 126 | } |
| 127 | drive(t, rig, chromedp.KeyEvent("compass"), |
| 128 | chromedp.WaitVisible(`[mapa-picker] [role="option"][data-emoji="🧭"]`), |
| 129 | chromedp.KeyEvent("\r"), |
| 130 | chromedp.WaitVisible(`h1[mapa-title] [mapa-icon]`)) |
| 131 | shown := evaluate(t, rig, |
| 132 | `document.querySelector("h1[mapa-title] [mapa-icon]").textContent`) |
| 133 | if shown != "🧭" { |
| 134 | t.Fatalf("the title shows %q", shown) |
| 135 | } |
| 136 | if found := axeSerious(t, rig); found != "" { |
| 137 | t.Fatalf("axe, a page with an icon:\n%s", found) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // LOOK10's banner half: Reposition by keyboard, then Save position. |
| 142 | func TestLook10TheBannerByKeyboard(t *testing.T) { |
| 143 | rig, store := browserRigWith(t, 0, nil) |
| 144 | page := lookedPage(t, store) |
| 145 | drive(t, rig, scripts(true), chromedp.EmulateViewport(1440, 900), |
| 146 | chromedp.Navigate(rig.Origin+mapa.PagePath(page)), |
| 147 | chromedp.WaitVisible(mounted), |
| 148 | chromedp.Focus(`[data-mapa-banner="reposition"]`), |
| 149 | chromedp.KeyEvent("\r"), |
| 150 | chromedp.WaitVisible(`[mapa-banner-tools] button`)) |
| 151 | eventually(t, "Save position has focus", func() bool { |
| 152 | return evaluate(t, rig, `document.activeElement.textContent`) == |
| 153 | "Save position" |
| 154 | }) |
| 155 | drive(t, rig, chromedp.KeyEvent(kb.ArrowDown), |
| 156 | chromedp.KeyEvent(kb.ArrowDown)) |
| 157 | if moved := evaluate(t, rig, `document.querySelector("[mapa-banner]")`+ |
| 158 | `.getAttribute("data-y")`); moved != "20" { |
| 159 | t.Fatalf("the arrows moved the banner to %s", moved) |
| 160 | } |
| 161 | drive(t, rig, chromedp.KeyEvent("\r")) |
| 162 | eventually(t, "the banner moved and was saved", func() bool { |
| 163 | return evaluate(t, rig, `(function () { |
| 164 | var band = document.querySelector("[mapa-banner]"); |
| 165 | return band && !band.hasAttribute("data-mapa-moving") ? |
| 166 | band.getAttribute("data-y") : ""; |
| 167 | })()`) == "20" |
| 168 | }) |
| 169 | if found := axeSerious(t, rig); found != "" { |
| 170 | t.Fatalf("axe, a page with a banner:\n%s", found) |
| 171 | } |
| 172 | } |
| 173 | |