//go:build browser package mapatest import ( "context" "fmt" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/input" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" ) // New team page (level-up §4.7) and the rail's tooltips (§3.2), in // Chromium (build note 77). The sidebar scrolls (overflow: auto), and // everything that left it was clipped: the popover's right side in the // open sidebar, all of it beside the rail, and every tooltip. Each check // asks the page what a pointer would hit at the corners of what must be // seen, so a box that is laid out but cut off fails. // opener is the sidebar's New team page control, as either shape has // drawn it (a
' summary before build note 77, now a button). const opener = `[mapa-side-foot] ` + `:is(summary, [popovertarget])[mapa-side-item]` // sideCookie sets the mapa-sidebar cookie, or deletes it for "". func sideCookie(rig *harness.Rig, state string) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { where := rig.Origin + "/" if state == "" { _, err := cdp.Call(ctx, target, network.DeleteCookies, network.DeleteCookiesParams{Name: "mapa-sidebar", URL: where}) return err } _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: "mapa-sidebar", Value: state, URL: where}) return err }) } // cornersHit is, for each part inside box, every corner (3px in, past // a rounded border) where the topmost element is not inside box: "" // when the whole of every part can be seen and clicked. const cornersHit = `(function (box, parts) { var holder = document.querySelector(box), misses = []; parts.forEach(function (part) { var el = holder && holder.querySelector(part); if (!el) { misses.push(part + " is not drawn"); return; } var r = el.getBoundingClientRect(); [[r.left + 3, r.top + 3], [r.right - 3, r.top + 3], [r.left + 3, r.bottom - 3], [r.right - 3, r.bottom - 3] ].forEach(function (p) { var hit = document.elementFromPoint(p[0], p[1]); if (hit && holder.contains(hit)) return; misses.push(part + " at " + Math.round(p[0]) + "," + Math.round(p[1]) + " is " + (hit ? hit.tagName.toLowerCase() : "off the screen")); }); }); return misses.join("; "); })(%q, ["input[name=title]", "button[type=submit]"])` // sideCase is one sidebar state at one width: the cookie ("" for none) // and what the sidebar shows then. type sideCase struct { width int64 cookie, show string } var sideCases = []sideCase{ {1400, "", "open"}, {1400, "rail", "rail"}, {1400, "open", "open"}, {900, "", "rail"}, {900, "open", "open"}, {900, "rail", "rail"}, } // From the sidebar, in every state, scripts off and on: the whole form // is on top, focus is in the title, and Enter creates the page and opens // it. At 390 there is no sidebar: File → New team page is the screen. func TestNewTeamPageShowsTheWholeForm(t *testing.T) { rig, _ := browserRig(t, 1) for _, live := range []bool{false, true} { for number, each := range sideCases { name := fmt.Sprintf("%dpx, %s (cookie %q), scripts %v", each.width, each.show, each.cookie, live) drive(t, rig, scripts(live), sideCookie(rig, each.cookie), chromedp.EmulateViewport(each.width, 877), chromedp.Navigate(rig.Origin+"/pages"), chromedp.WaitVisible("main h1"), chromedp.Click(opener), chromedp.WaitVisible("#mapa-new-title")) title := fmt.Sprintf("Sidebar %d %v", number, live) createFrom(t, rig, name, "form[mapa-new-page-form]", "mapa-new-title", title) } name := fmt.Sprintf("390px, File menu, scripts %v", live) drive(t, rig, scripts(live), sideCookie(rig, ""), chromedp.EmulateViewport(390, 844), chromedp.Navigate(rig.Origin+"/pages"), chromedp.Click(fileMenuOpener), chromedp.Click(`#mapa-menu-file a[href="/new"]`), chromedp.WaitVisible(`main input[name="title"]`)) createFrom(t, rig, name, `main form[action="/pages"]`, "title", fmt.Sprintf("Phone %v", live)) } } // createFrom checks the open form, then types title into whatever has // focus and presses Enter, as a person would, and waits for the page. func createFrom(t *testing.T, rig *harness.Rig, name, form, field, title string) { t.Helper() if misses := read(t, rig, chromedp.Evaluate[string]( fmt.Sprintf(cornersHit, form))); misses != "" { t.Errorf("%s: the form is not all on top: %s", name, misses) } focus := read(t, rig, chromedp.Evaluate[string]( `document.activeElement.id || document.activeElement.name || ""`)) if focus != field { t.Errorf("%s: focus is on %q, not the title", name, focus) } drive(t, rig, chromedp.KeyEvent(title), chromedp.KeyEvent("\r")) if !landsOn(t, rig, title) { t.Errorf("%s: Enter did not open a page titled %q", name, title) } } // landsOn waits up to five seconds for a page titled title. func landsOn(t *testing.T, rig *harness.Rig, title string) bool { t.Helper() check := fmt.Sprintf(`location.pathname.indexOf("/p/") === 0 && (document.querySelector("h1[mapa-title]") || {}).textContent === %q`, title) for range 50 { if read(t, rig, chromedp.Evaluate[bool](check)) { return true } time.Sleep(100 * time.Millisecond) } return false } // A taken title is refused where it was typed, and what was typed // stays: in the popover with scripts, on the New page screen without. func TestNewTeamPageRefusesATakenTitleInPlace(t *testing.T) { rig, store := browserRig(t, 0) if _, err := store.CreateTeamPage(context.Background(), Bo, "Onboarding", ""); err != nil { t.Fatal(err) } rig.Allow("POST", "/pages", 422) for _, live := range []bool{false, true} { drive(t, rig, scripts(live), chromedp.EmulateViewport(1400, 877), chromedp.Navigate(rig.Origin+"/pages"), chromedp.Click(opener), chromedp.WaitVisible("#mapa-new-title"), chromedp.KeyEvent("Onboarding"), chromedp.KeyEvent("\r"), chromedp.Sleep(1200*time.Millisecond)) where := `[mapa-new-page]:popover-open` if !live { where = "main" } said := read(t, rig, chromedp.Evaluate[string](fmt.Sprintf( `(document.querySelector(%q) || {}).textContent || ""`, where+" [rst-field-error]:not([hidden])"))) kept := read(t, rig, chromedp.Evaluate[string](fmt.Sprintf( `(document.querySelector(%q) || {}).value || ""`, where+` input[name="title"]`))) if !strings.Contains(said, "already exists") || kept != "Onboarding" { t.Errorf("scripts %v: refusal %q, field %q", live, said, kept) } } } // The New page screen's Create and Cancel sit under its one field, as // on every other form (they were under a borderless body field half the // window tall). func TestTheNewPageScreenKeepsItsButtonsUnderTheField(t *testing.T) { rig, _ := browserRig(t, 1) gap := `Math.round(document.querySelector('main [rst-form-foot]')` + `.getBoundingClientRect().top - document.querySelector(` + `'main input[name="title"]').getBoundingClientRect().bottom)` for _, live := range []bool{false, true} { for _, size := range [][2]int64{{1402, 877}, {390, 844}} { drive(t, rig, scripts(live), chromedp.EmulateViewport(size[0], size[1]), chromedp.Navigate(rig.Origin+"/new"), chromedp.WaitVisible(`main input[name="title"]`)) if got := read(t, rig, chromedp.Evaluate[int](gap)); got < 0 || got > 48 { t.Errorf("%dx%d, scripts %v: the buttons are %dpx under "+ "the field", size[0], size[1], live, got) } } } } // labelSeen is "" when the hovered row's tooltip can be hit at all four // corners; it takes pointer events for the moment it is asked. const labelSeen = `(function (row) { var label = document.querySelector(row + " [mapa-side-label]"); label.style.pointerEvents = "auto"; var r = label.getBoundingClientRect(), misses = []; [[r.left + 3, r.top + 3], [r.right - 3, r.top + 3], [r.left + 3, r.bottom - 3], [r.right - 3, r.bottom - 3] ].forEach(function (p) { if (!label.contains(document.elementFromPoint(p[0], p[1]))) { misses.push(Math.round(p[0]) + "," + Math.round(p[1])); } }); label.style.pointerEvents = ""; return misses.length ? label.textContent + " cut off at " + misses.join(" ") : ""; })(%q)` // Every row of the rail shows its whole tooltip on hover: by width with // no cookie, and by cookie on a wide window. func TestTheRailsTooltipsAreWhole(t *testing.T) { rig, _ := browserRig(t, 1, harness.WithHover()) mark := `Array.prototype.filter.call(document.querySelectorAll(` + `"[mapa-side] [mapa-side-item]"), function (row) {` + ` return row.offsetParent !== null; }).map(function (row, i) {` + ` row.setAttribute("data-gate-row", i); return i; }).length` for _, each := range []sideCase{{900, "", "rail"}, {1400, "rail", "rail"}} { drive(t, rig, sideCookie(rig, each.cookie), chromedp.EmulateViewport(each.width, 877), chromedp.Navigate(rig.Origin+"/pages"), chromedp.WaitVisible("main h1")) rows := read(t, rig, chromedp.Evaluate[int](mark)) for row := range rows { selector := fmt.Sprintf(`[data-gate-row="%d"]`, row) drive(t, rig, rest(selector)) if miss := read(t, rig, chromedp.Evaluate[string]( fmt.Sprintf(labelSeen, selector))); miss != "" { t.Errorf("%dpx (cookie %q): %s", each.width, each.cookie, miss) } } } } // rest moves a hovering mouse to the middle of selector's element. func rest(selector string) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { box, err := chromedp.Run(ctx, chromedp.Dimensions(selector)) if err != nil { return err } quad := box.Content across, down := (quad[0]+quad[4])/2, (quad[1]+quad[5])/2 return chromedp.Do(ctx, chromedp.MouseEvent( input.DispatchMouseEventTypeMouseMoved, across, down), chromedp.Sleep(80*time.Millisecond)) }) }