package chrome import ( "html/template" "regexp" "strconv" "strings" "testing" "amadan.net/oficina/estilo.git/menu" ) // What Docs puts in the slots: its own menu bar and its undo/redo pair, // markup it rendered itself. const ( testMenubar = template.HTML(``) testLead = template.HTML(``) ) func stacked() Bar { b := signedIn("x") b.Menubar = testMenubar return b } func header(html string) string { return html[:strings.Index(html, ">")+1] } // The Menubar slot makes the bar stacked, and only it does: the title and // the app's menus share one stack, so the same markup serves the 56px // header from 800px and the two-row bar below it. func TestTheMenubarSlotStacksTheBar(t *testing.T) { b := stacked() html := render(t, "estilo-bar", b) if got := header(html); got != `
` { t.Errorf("a bar with a menubar opens %s", got) } stack := html[strings.Index(html, "
"):strings.Index(html, "
")] title := strings.Index(stack, "
"+string(testMenubar)+"
") if title < 0 || menus < 0 || menus < title { t.Fatalf("the stack is not the title over the app's menubar, verbatim:\n%s", stack) } if strings.Count(html, "estilo-menubar-title") != 1 { t.Error("the menubar is drawn more than once; the narrow row must be the same markup") } b.Sticky = true if got := header(render(t, "estilo-bar", b)); got != `
` { t.Errorf("a sticky stacked bar opens %s", got) } // A read-only title (a public page) stacks the same way. b.ContextEdit, b.Context = nil, "A document" html = render(t, "estilo-bar", b) if !strings.Contains(html, "
\n A document\n
") { t.Errorf("the read-only title is not in the stack:\n%s", html) } // Lead alone does not stack the bar, and neither slot set draws no // stack at all. b = signedIn("x") b.Lead = testLead html = render(t, "estilo-bar", b) if strings.Contains(header(html), "stacked") || strings.Contains(html, "estilo-bar-stack") || strings.Contains(html, "estilo-bar-menubar") { t.Errorf("a bar with only a lead is stacked:\n%s", header(html)) } html = render(t, "estilo-bar", signedIn("x")) if strings.Contains(html, "stacked") || strings.Contains(html, "estilo-bar-stack") { t.Error("a bar with no slots is stacked") } } // The lead opens the end group, before the actions, at every width, and // never goes into the Menu sheet: it is the app's, not an action. func TestTheLeadSlotOpensTheEndGroup(t *testing.T) { b := stacked() b.Lead = testLead html := render(t, "estilo-bar", b) end := html[strings.Index(html, "
"):] if !strings.HasPrefix(end, "
\n
"+string(testLead)+"
\n
") { t.Errorf("the lead is not first in the end group, before the actions:\n%s", end) } sheet := html[strings.Index(html, `
`):] sheet = sheet[:strings.Index(sheet, "")] if strings.Contains(sheet, "data-docs-command") { t.Error("the lead went into the Menu sheet") } // The end group's order stands with a lead in it. order := []string{"
", "
", "
"):strings.Index(html, "
`) { t.Errorf("the wide-only toggle is not marked in the bar:\n%s", actions) } sheet := html[strings.Index(html, `
`):strings.Index(html, "
`):strings.Index(html, "
") { t.Errorf("the sheet rules off actions it does not hold:\n%s", sheet) } // Every kind carries the mark, on its outermost element. for _, it := range []menu.Item{{Href: "/h", Label: "l"}, {Action: "/p", Label: "l"}, {Dialog: "d", Label: "l"}, {Controls: "c", Label: "l"}} { html := render(t, "estilo-bar-action", Action{Item: it, WideOnly: true}) if open := html[:strings.Index(html, ">")+1]; !strings.HasSuffix(open, " estilo-bar-wide>") { t.Errorf("%+v: the outermost element %s is not marked", it, open) } } } // The toggle kind in the bar: a button that names its region and its // state and is disabled as rendered, until the app's script binds it. func TestAToggleActionIsDisabledUntilBound(t *testing.T) { for _, c := range []struct { a Action want string }{ {Action{Item: menu.Item{Controls: "docs-inspector", Label: "Format"}}, ``}, {Action{Item: menu.Item{Controls: "docs-inspector", Expanded: true, Label: "Format"}}, ``}, {Action{Item: menu.Item{Controls: "p", Label: "Panel"}, Primary: true}, ``}, } { if got := render(t, "estilo-bar-action", c.a); got != c.want { t.Errorf("got %s\nwant %s", got, c.want) } } // The icon is the app's own markup, drawn before the label in the bar, // whatever kind the action is. icon := template.HTML(``) for kind, c := range map[string]struct { item menu.Item want string }{ "toggle": {menu.Item{Controls: "c", Label: "Format"}, `data-estilo-toggle>Format`}, "link": {menu.Item{Href: "/s", Label: "Format"}, `Format`}, "post": {menu.Item{Action: "/p", Label: "Format"}, `type="submit">Format`}, "dialog": {menu.Item{Dialog: "d", Label: "Format"}, `command="show-modal">Format`}, } { if got := render(t, "estilo-bar-action", Action{Item: c.item, Icon: icon}); !strings.HasSuffix(got, c.want) { t.Errorf("%s: the icon is not before the label: %s", kind, got) } } } // In a bar with a slot each action sits in a display: contents wrapper // the controls set is styled on (estilo's CSS never selects rst-btn), and // the primary one says so. A bar without a slot draws them bare // (TestABarWithoutSlotsIsByteForByteUnchanged). func TestSlottedBarsWrapEachActionAsAControl(t *testing.T) { for name, b := range map[string]Bar{"menubar": stacked(), "lead": func() Bar { b := signedIn("x"); b.Lead = testLead; return b }()} { html := render(t, "estilo-bar", b) actions := html[strings.Index(html, "
"):strings.Index(html, "
x-share
`, `
`, } { if !strings.Contains(actions, want) { t.Errorf("%s: the actions lack %s in\n%s", name, want, actions) } } } } // The first paint's width for the title field: a guess in columns, with // room to spare, since a guess too wide only leaves the field's edge // further out and a guess too narrow cuts the title off with scripts off. // A wide or fullwidth character (CJK, kana, Hangul, most emoji) takes two // columns and a capital one and a half, and the whole is a quarter wider // again; never under 4. Measured at ecb81fc, where Size counted runes: // 11 kanji painted 121px against the 206px they need, 20 capital Ws 193px // against 326px. func TestTheTitleSizeIsItsWidthInColumnsWithAMargin(t *testing.T) { for value, want := range map[string]int{ "": 4, "Q3": 4, "Untitled document": 22, // 1 capital + 16: 17.5 columns "Café plans": 14, // 1 capital + 9: 10.5 "日本語のタイトル": 20, // 8 wide: 16 "Wide": 10, // 4 fullwidth: 8 "Plans 🎉🎉🎉": 16, // 1 capital + 5 + 3 wide emoji: 12.5 "WWWWWWWWWWWWWWWWWWWW": 38, // 20 capitals: 30 "مستند بلا عنوان": 19, // 15: Arabic has no capitals } { if got := (TitleEdit{Value: value}).Size(); got != want { t.Errorf("Size(%q) = %d, want %d", value, got, want) } } b := stacked() b.ContextEdit.Value = "Café plans" html := render(t, "estilo-bar", b) if !strings.Contains(html, `]*>`).FindAllString(html, -1) { if !strings.Contains(d, `name="estilo-menus"`) { t.Errorf("%s is not in the estilo-menus group", d) } } } // Appearance is three equal segments, an icon over each word, the current // one pressed; the form opts out of the busy rule (a view switch holds // nothing), and it is still a plain POST form for a page without script. func TestAppearanceIsASegmentedSelectorSwitchedInPlace(t *testing.T) { html := render(t, "estilo-account", signedIn("x").Account) form := regexp.MustCompile(`
]*>`).FindString(html) if form != `` { t.Errorf("the Appearance form opens %q", form) } if !strings.Contains(html, ``) { t.Error("the Appearance form lost return_to, which the script-off POST needs") } for mode, icon := range map[string]string{"light": ``, "dark": ``} { button := regexp.MustCompile(``).FindStringSubmatch(html) if button == nil { t.Errorf("no %s segment with an icon over its word in\n%s", mode, html) continue } if (button[1] == "true") != (mode == "dark") { t.Errorf("%s is aria-pressed=%s; only the current mode is pressed", mode, button[1]) } if !strings.HasPrefix(button[2], icon) { t.Errorf("%s has the wrong icon: %s", mode, button[2]) } } if strings.Contains(html[strings.Index(html, "")], "estilo-menu-check") { t.Error("the segments still carry the check column") } } // The stacked header's public numbers: 56px from 800px, the one-row bar // plus its menu row and its 1px bottom border below (85px, as measured in // Chromium and WebKit; 84 left the border out), and WideOnly's 64rem. The 32px menubar floor // goes only inside the stacked header. func TestTheStackedBarPublishesItsHeight(t *testing.T) { src := string(CSS()) src = src[strings.Index(src, barStackMarker):] for _, re := range []string{ `@media \(min-width: 800px\) \{\s*:root:has\(\[estilo-bar~="stacked"\]\) \{ --estilo-bar-block-size: 56px; \}`, `@media \(max-width: 799\.98px\) \{\s*:root:has\(\[estilo-bar~="stacked"\]\) \{ --estilo-bar-block-size: calc\(3\.25rem \+ 2rem \+ 1px\); \}`, `@media \(max-width: 63\.99rem\) \{\s*\[estilo-bar-actions\] \[estilo-bar-wide\],\s*\[estilo-bar-actions\] \[estilo-bar-control~="wide"\] \{ display: none; \}`, `\[estilo-bar~="stacked"\] \[estilo-menubar\] \{[^}]*min-block-size: 0;`, `\[estilo-bar~="stacked"\] \[estilo-bar-brand\] img \{ inline-size: 32px; block-size: 32px; \}`, `\[estilo-bar~="stacked"\] \[estilo-menubar-menu\] > \[estilo-menu\] \{ inset-block-start: calc\(100% \+ 4\.5px\); \}`, } { if !regexp.MustCompile(re).MatchString(src) { t.Errorf("chrome.css lacks %s", re) } } // Coarse pointers: the menubar titles keep their 24px look but take // 44px of hit area, 16px up and 4px down, with no change to layout. coarse := src[strings.Index(src, "@media (min-width: 800px) {"):] coarse = coarse[strings.Index(coarse, "@media (pointer: coarse) {"):] coarse = coarse[:strings.Index(coarse, "\n }\n")] for _, want := range []string{ `[estilo-bar~="stacked"] [estilo-menubar-title] { position: relative; }`, `[estilo-bar~="stacked"] [estilo-menubar-title]::before { content: ""; position: absolute; inset-inline: 0; inset-block: -16px -4px; }`, `[estilo-bar~="stacked"] [estilo-bar-title] { z-index: 1; }`, // the title field keeps its own 24px on top } { if !strings.Contains(coarse, want) { t.Errorf("the coarse-pointer rules lack %s", want) } } // Coarse pointers raise the controls to 32px. if !regexp.MustCompile(`@media \(pointer: coarse\) \{\s*:is\(\[estilo-bar-control\] > a, [^)]*\) \{ block-size: 32px; min-block-size: 32px; \}`).MatchString(src) { t.Error("the controls do not rise to 32px on a coarse pointer") } } // A stacked title's field is sized to its title, from a guess before the // script runs: an ellipsis there would say the title was cut short when // only the guess was. The one-row bar keeps its ellipsis. func TestAStackedTitleNeverShowsAnEllipsis(t *testing.T) { src := string(CSS()) if !regexp.MustCompile(`\n\[estilo-bar~="stacked"\] \[estilo-bar-title-input\] \{[^}]*text-overflow: clip;`).MatchString(src[strings.Index(src, barStackMarker):]) { t.Error("the stacked title field still ends a short guess with an ellipsis") } } // Forced colours drop the controls' fills and shadows, so their border is // all that is left to draw them, and rastrillo's forced-colors border on // [rst-btn] loses to the controls set's border: 0. The set restores it // itself, after that rule and at its weight, and marks an expanded toggle // (Format with the inspector open) in Highlight, since its tint is gone. func TestTheControlsKeepTheirBoundaryInForcedColours(t *testing.T) { src := string(CSS()) src = src[strings.Index(src, barStackMarker):] controls := `:is([estilo-bar-control] > a, [estilo-bar-control] > button, [estilo-bar-control] > form > button)` set := strings.Index(src, controls+" {\n display: inline-flex;") at := strings.Index(src, "@media (forced-colors: active) {") if set < 0 || at < 0 || at < set { t.Fatal("no forced-colors block after the controls set's rules") } block := src[at:] block = block[:strings.Index(block, "\n}\n")] for _, want := range []string{ controls + " { border: 1px solid ButtonBorder; }", controls + `[aria-expanded="true"] { border-color: Highlight; }`, controls + `[aria-expanded="true"]:not(:focus-visible) { outline: 1px solid Highlight; }`, } { if !strings.Contains(block, want) { t.Errorf("the forced-colors block lacks %s in\n%s", want, block) } } } // From 800px the title field is pulled back by its padding and border // (a negative start margin), so its form is that much narrower than the // field, and a cap of 100% of the form trimmed chrome.js's fitted width // by 7.4px. The cap allows for the margin, so the fit stands as measured. func TestTheWideTitleCapAllowsForItsPulledBackMargin(t *testing.T) { src := string(CSS()) src = src[strings.Index(src, barStackMarker):] wide := src[strings.Index(src, "@media (min-width: 800px) {"):] rule := regexp.MustCompile(`\[estilo-bar~="stacked"\] \[estilo-bar-title-input\] \{[^}]*\}`).FindString(wide) if !strings.Contains(rule, "margin-inline-start: calc(-1 * var(--rst-sp-2) - 1px);") || !strings.Contains(rule, "max-inline-size: calc(100% + var(--rst-sp-2) + 1px);") { t.Errorf("the wide title field's cap does not give back its pulled-back margin:\n%s", rule) } } // An icon-only action (Mapa's and Fichas' Search) shows its icon alone in // the bar: its label is the accessible name and, with its key, the // tooltip; its shortcut is aria-keyshortcuts. The Menu sheet's row keeps // the label and the key. Every kind draws it the same way. func TestAnIconOnlyActionIsNamedButShowsNoText(t *testing.T) { icon := template.HTML(``) search := Action{Item: menu.Item{Href: "/search", Label: "x-search", Key: "⌘K"}, Icon: icon, IconOnly: true, KeyShortcuts: "Meta+K Control+K"} // html/template writes + as +; the attribute's value is Meta+K. want := `` if got := render(t, "estilo-bar-action", search); got != want { t.Errorf("got %s\nwant %s", got, want) } b := stacked() b.Actions = []Action{search} html := render(t, "estilo-bar", b) actions := html[strings.Index(html, "
"):strings.Index(html, "
`+want+`
`) { t.Errorf("the bar does not draw the icon-only control:\n%s", actions) } sheet := html[strings.Index(html, `
`):strings.Index(html, "
x-search⌘K`) { t.Errorf("the sheet's row lost its label or key:\n%s", sheet) } // No key: the tooltip is the label alone. noKey := search noKey.Key, noKey.KeyShortcuts = "", "" if got := render(t, "estilo-bar-action", noKey); !strings.Contains(got, ` aria-label="x-search" estilo-bar-icon rst-tip data-tip="x-search">`) || strings.Contains(got, "aria-keyshortcuts") { t.Errorf("without a key: %s", got) } // IconOnly with no icon would be an empty control: the label shows. if got := render(t, "estilo-bar-action", Action{Item: menu.Item{Href: "/s", Label: "x-search"}, IconOnly: true}); got != `x-search` { t.Errorf("IconOnly without an icon: %s", got) } for _, it := range []menu.Item{{Action: "/p", Label: "l"}, {Dialog: "d", Label: "l"}, {Controls: "c", Label: "l"}} { got := render(t, "estilo-bar-action", Action{Item: it, Icon: icon, IconOnly: true}) if !strings.Contains(got, ` aria-label="l" estilo-bar-icon rst-tip data-tip="l">`) { t.Errorf("%+v: %s", it, got) } } }