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, "
")
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, "
`,
``,
} {
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(`