//go:build browser package slidestest // Deck settings offers the person's own fonts (deckfonts.go, // fixtures/deck-fonts.mjs): a pick is recorded, written as one change, // served to the page, and taken back by one undo; choosing the theme's // font again drops the family from the deck. import ( "encoding/json" "net/http" "net/url" "strings" "testing" "time" "github.com/chromedp/chromedp" slidehtml "amadan.net/oficina/slides/internal/slides/render" ) func TestDeckSettingsOffersYourFonts(t *testing.T) { serif := []byte(nil) // Its css name: a catalog family's own name is reserved // (library.go's reservedFontName), so the upload is suffixed. css := "" te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { serif = libFont(t, "LiberationSerif-Regular.ttf") if rec := uploadFonts(t, a, cookies, upFile{"serif.ttf", serif}); rec.Code != http.StatusOK { t.Fatalf("upload: %d", rec.Code) } css = cssOf(t, a, sha(serif)) // A deck of its own rather than deckWithSlides: no notes to bind. return "/d/" + apiDeck(t, a, mintAgent(t, a, cookies, "deck"), "Fonts") + "/edit" }) te.rig.Run( chromedp.Navigate(te.rig.Origin+te.path), chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), waitFor(editorBound), ) token := mintAgent(t, te.app, te.cookies, "deck-fonts") te.rig.Run(openDeckSettings()) var offer struct { Groups []string Heading []string Body []string Link string } te.rig.Run(evalInto(&offer, `(() => { const d = document.querySelector('[slides-deck-dialog]') const opts = name => [...d.querySelectorAll('[name="' + name + '"] optgroup option')].map(o => o.value + '=' + o.textContent.trim()) const a = d.querySelector('a[href="/fonts"]') return { Groups: [...d.querySelectorAll('optgroup')].map(g => g.label), Heading: opts('rst-deck-font-heading'), Body: opts('rst-deck-font-body'), Link: a ? [a.textContent.trim(), a.target, a.rel].join('|') : '' } })()`)) if strings.Join(offer.Groups, "|") != "Your fonts|Your fonts" || strings.Join(offer.Heading, "|") != css+"=Liberation Serif" || strings.Join(offer.Body, "|") != css+"=Liberation Serif" || offer.Link != "Upload a font…|_blank|noopener" { t.Errorf("Deck settings offer %+v", offer) } te.rig.Screen(`[slides-deck-dialog]`, "Deck settings offering your fonts") meta := func() map[string]any { view := apiJSON(t, te.app, http.MethodGet, "/api/v1/decks/"+te.sqid(), token, "", http.StatusOK) m, _ := view["meta"].(map[string]any) return m } until := func(what string, ok func(map[string]any) bool) map[string]any { t.Helper() deadline := time.Now().Add(20 * time.Second) for { m := meta() if ok(m) { return m } if time.Now().After(deadline) { b, _ := json.Marshal(m) t.Fatalf("%s: the deck's meta is %s", what, b) } te.rig.Run(chromedp.Sleep(100 * time.Millisecond)) } } heading := func(m map[string]any) string { f, _ := m["fonts"].(map[string]any) h, _ := f["heading"].(string) return h } files := func(m map[string]any) []any { v, _ := m["fontFiles"].([]any); return v } choose := func(name, value string) { sel := `[slides-deck-dialog] [name="` + name + `"]` te.rig.Run(chromedp.SetValue(chromedp.CSS(sel), value), chromedp.Evaluate[chromedp.Void](`document.querySelector('`+sel+`').dispatchEvent(new Event('change', { bubbles: true }))`)) } theme, _ := meta()["theme"].(string) stack := "'" + css + "', " + slidehtml.ThemeStacks()[slidehtml.ThemeID(theme)][0] choose("rst-deck-font-heading", css) m := until("the pick", func(m map[string]any) bool { return heading(m) == stack && len(files(m)) == 1 }) if f, _ := m["fonts"].(map[string]any); f["body"] != "" { t.Errorf("the pick changed the body: %v", f) } if fam, _ := files(m)[0].(map[string]any); fam["css"] != css { t.Errorf("fontFiles is %v", files(m)) } var shown struct { Heading string Status int } te.rig.Run(evalInto(&shown, `(async () => ({ Heading: document.querySelector('[name="rst-deck-font-heading"]').value, Status: (await fetch('/d/`+te.sqid()+`/fonts/`+sha(serif)+`')).status }))()`, awaitPromise)) if shown.Heading != css || shown.Status != http.StatusOK { t.Errorf("after the pick the heading field shows %q and the page fetches the face with %d", shown.Heading, shown.Status) } // Undo takes the whole pick back in one step: the stack and the list // go together, never one without the other. Any Deck settings change // (Things arrive included) leaves an empty undo step on top of it // that the first undo spends, which is the editor's own and not this // field's, so undo runs until the pick has gone, at most twice. te.rig.Run( chromedp.Click(chromedp.CSS(`[slides-deck-dialog] button[type="submit"]`)), waitFor(`!document.querySelector('[slides-deck-dialog]').open`), ) for undos := 1; ; undos++ { te.rig.Run(chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.Sleep(time.Second)) m := meta() if (heading(m) == "") != (len(files(m)) == 0) { b, _ := json.Marshal(m) t.Fatalf("undo %d took back half the pick: %s", undos, b) } if heading(m) == "" { break } if undos == 2 { b, _ := json.Marshal(m) t.Fatalf("two undos left the pick: %s", b) } } // Picked again, then the theme's font: the family leaves the deck. te.rig.Run(openDeckSettings()) choose("rst-deck-font-body", css) until("the body pick", func(m map[string]any) bool { f, _ := m["fonts"].(map[string]any) b, _ := f["body"].(string) return strings.HasPrefix(b, "'"+css+"', ") && len(files(m)) == 1 }) choose("rst-deck-font-body", "") until("the theme's font again", func(m map[string]any) bool { return m["fonts"] == nil && len(files(m)) == 0 }) } // A deck whose font is not in this person's library (a co-editor's, or // one they have since deleted) still shows it in the field: selected, // named, and not theirs to choose; anything else is. func TestDeckSettingsShowsADeckFontYouDoNotHave(t *testing.T) { var token string te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { uploadFonts(t, a, cookies, upFile{"serif.ttf", libFont(t, "LiberationSerif-Regular.ttf")}) token = mintAgent(t, a, cookies, "deck") sqid := apiDeck(t, a, token, "Fonts") css := cssOf(t, a, sha(libFont(t, "LiberationSerif-Regular.ttf"))) useFamily(t, a, token, sqid, pickedFamily(t, a, sqid, css, cookies)) if rec := post(t, a, "/fonts/delete", url.Values{"family": {css}}, cookies...); rec.Code != http.StatusSeeOther { t.Fatalf("delete the family: %d", rec.Code) } return "/d/" + sqid + "/edit" }) te.rig.Run( chromedp.Navigate(te.rig.Origin+te.path), chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), waitFor(editorBound), ) te.rig.Run(openDeckSettings()) type fieldState struct { Value, Shown string Disabled bool Groups int Deck int } state := func(name string) fieldState { var s fieldState te.rig.Run(evalInto(&s, `(() => { const s = document.querySelector('[slides-deck-dialog] [name="`+name+`"]') const o = s.selectedOptions[0] return { Value: s.value, Shown: o ? o.textContent.trim() : '', Disabled: !!o && o.disabled, Groups: s.querySelectorAll('optgroup').length, Deck: s.querySelectorAll('option[data-deck-font]').length } })()`)) return s } if got, want := state("rst-deck-font-heading"), (fieldState{Value: "Liberation Serif 2", Shown: "Liberation Serif", Disabled: true, Deck: 1}); got != want { t.Errorf("the heading field is %+v, want %+v", got, want) } if got := state("rst-deck-font-body"); got.Value != "" || got.Deck != 0 { t.Errorf("the body field is %+v, want Theme's and no deck font", got) } te.rig.Screen(`[slides-deck-dialog]`, "Deck settings showing a deck font not in your library") // Switching to a font of one's own still works, and the deck font goes. sel := `[slides-deck-dialog] [name="rst-deck-font-heading"]` te.rig.Run(chromedp.SetValue(chromedp.CSS(sel), "Georgia, serif"), chromedp.Evaluate[chromedp.Void](`document.querySelector('`+sel+`').dispatchEvent(new Event('change', { bubbles: true }))`)) deadline := time.Now().Add(20 * time.Second) for { view := apiJSON(t, te.app, http.MethodGet, "/api/v1/decks/"+te.sqid(), token, "", http.StatusOK) m, _ := view["meta"].(map[string]any) f, _ := m["fonts"].(map[string]any) if f["heading"] == "Georgia, serif" && m["fontFiles"] == nil { break } if time.Now().After(deadline) { b, _ := json.Marshal(m) t.Fatalf("after choosing Serif the deck's meta is %s", b) } te.rig.Run(chromedp.Sleep(100 * time.Millisecond)) } if got := state("rst-deck-font-heading"); got.Value != "Georgia, serif" || got.Deck != 0 { t.Errorf("after choosing Serif the heading field is %+v", got) } }