//go:build browser package slidestest // A family that gains a face during the visit (final review, I4): upload // a bold for a family the deck already uses, pick the family again, and // the open editor loads the new set before the change shows, then has // exactly its faces on the page; undo and redo move between the sets. import ( "encoding/json" "net/http" "testing" "time" "github.com/chromedp/chromedp" "amadan.net/oficina/slides/internal/slides/fonts" ) func TestDeckFontAGrownFamilyLoadsItsNewFaces(t *testing.T) { var regular, bold []byte te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { regular = brandFont(t, "LiberationSerif-Regular.ttf", "Brand") if rec := uploadFonts(t, a, cookies, upFile{"brand.ttf", regular}); rec.Code != http.StatusOK { t.Fatalf("upload: %d", rec.Code) } return "/d/" + apiDeck(t, a, mintAgent(t, a, cookies, "deck"), "Grown") + "/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") faces := func() int { view := apiJSON(t, te.app, http.MethodGet, "/api/v1/decks/"+te.sqid(), token, "", http.StatusOK) m, _ := view["meta"].(map[string]any) files, _ := m["fontFiles"].([]any) if len(files) == 0 { return 0 } fam, _ := files[0].(map[string]any) fs, _ := fam["faces"].([]any) return len(fs) } until := func(what string, ok func() bool) { t.Helper() deadline := time.Now().Add(20 * time.Second) for !ok() { if time.Now().After(deadline) { t.Fatalf("%s: never", what) } te.rig.Run(chromedp.Sleep(100 * time.Millisecond)) } } // onPage is the page's Brand faces: weight, and whether loaded. onPage := func() []string { var out []string te.rig.Run(evalInto(&out, `[...document.fonts].filter(f => f.family === 'Brand').map(f => f.weight + ':' + f.status).sort()`)) return out } pick := func() { sel := `[slides-deck-dialog] [name="rst-deck-font-heading"]` te.rig.Run(openDeckSettings(), chromedp.SetValue(chromedp.CSS(sel), "Brand"), chromedp.Evaluate[chromedp.Void](`document.querySelector('`+sel+`').dispatchEvent(new Event('change', { bubbles: true }))`)) } closeSettings := func() { te.rig.Run(chromedp.Click(chromedp.CSS(`[slides-deck-dialog] button[type="submit"]`)), waitFor(`!document.querySelector('[slides-deck-dialog]').open`)) } same := func(got []string, want ...string) bool { b1, _ := json.Marshal(got) b2, _ := json.Marshal(want) return string(b1) == string(b2) } pick() until("the regular pick", func() bool { return faces() == 1 }) until("the regular face on the page", func() bool { return same(onPage(), "400:loaded") }) closeSettings() // A bold arrives in the library; the family is picked again. bold = brandBold(t) if rec := uploadFonts(t, te.app, te.cookies, upFile{"brand-bold.ttf", bold}); rec.Code != http.StatusOK { t.Fatalf("upload the bold: %d", rec.Code) } pick() until("the grown pick", func() bool { return faces() == 2 }) until("both faces on the page, and only those", func() bool { return same(onPage(), "400:loaded", "700:loaded") }) closeSettings() // Undo to the regular set (Deck settings leaves an empty undo step // on top, the editor's own: at most two undos), then redo. for undos := 1; faces() != 1; undos++ { if undos > 2 { t.Fatalf("two undos left %d faces", faces()) } te.rig.Run(chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.Sleep(time.Second)) } until("the regular set on the page after undo", func() bool { return same(onPage(), "400:loaded") }) te.rig.Run(chromedp.KeyEvent("y", chromedp.KeyModifiers(chromedp.ModifierCtrl))) until("the grown set after redo", func() bool { return faces() == 2 }) until("both faces on the page after redo", func() bool { return same(onPage(), "400:loaded", "700:loaded") }) } // brandBold is Liberation Serif Bold called Brand, weight 700. func brandBold(t *testing.T) []byte { t.Helper() f, err := fonts.Parse(libFont(t, "LiberationSerif-Bold.ttf")) if err != nil { t.Fatal(err) } out, err := f.Renamed("Brand", 700, false) if err != nil { t.Fatal(err) } return out }