//go:build browser package slidestest // A family uploaded in the Fonts tab Deck settings opened is offered in // Deck settings once the editor's window has focus again (final review, // minor): it was offered only after the editor was opened again. import ( "net/http" "strings" "testing" "github.com/chromedp/chromedp" ) func TestDeckSettingsOffersAFontUploadedMeanwhile(t *testing.T) { te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { return "/d/" + apiDeck(t, a, mintAgent(t, a, cookies, "deck"), "Meanwhile") + "/edit" }) te.rig.Run( chromedp.Navigate(te.rig.Origin+te.path), chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), waitFor(editorBound), openDeckSettings(), ) offered := func() string { var out []string te.rig.Run(evalInto(&out, `[...document.querySelectorAll('[slides-deck-dialog] select[name^="rst-deck-font-"] optgroup')].map((g) => g.label + ':' + [...g.querySelectorAll('option')].map((o) => o.value + '=' + o.textContent).join(','))`)) return strings.Join(out, "|") } if got := offered(); got != "" { t.Fatalf("an empty library offers %q", got) } // Uploaded in another tab, then back to the editor. if rec := uploadFonts(t, te.app, te.cookies, upFile{"brand.ttf", brandFont(t, "LiberationSerif-Regular.ttf", "Brand")}); rec.Code != http.StatusOK { t.Fatalf("upload: %d", rec.Code) } te.rig.Run(chromedp.Evaluate[chromedp.Void](`window.dispatchEvent(new Event('focus'))`)) te.rig.Run(waitFor(`[...document.querySelectorAll('[slides-deck-dialog] select[name^="rst-deck-font-"] optgroup option')].length === 2`)) if got := offered(); got != "Your fonts:Brand=Brand|Your fonts:Brand=Brand" { t.Errorf("after the upload both fields offer %q", got) } } // The Fonts page's sample of a family is drawn in it: the face loads // from the library route, and the name is set in it. func TestTheFontsPageSampleIsDrawnInItsFamily(t *testing.T) { te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { if rec := uploadFonts(t, a, cookies, upFile{"brand.ttf", brandFont(t, "LiberationMono-Regular.ttf", "Brand")}); rec.Code != http.StatusOK { t.Fatalf("upload: %d", rec.Code) } return "/fonts" }) te.rig.Run(chromedp.Navigate(te.rig.Origin+te.path), chromedp.WaitVisible(chromedp.CSS(`[slides-font-sample="1"]`))) var st struct { Text string Loaded bool Mono bool } te.rig.Run(evalInto(&st, `(async () => { await document.fonts.ready const el = document.querySelector('[slides-font-sample="1"]') const fam = getComputedStyle(el).fontFamily // Monospace: as wide as the same count of another letter. const w = (s) => { const c = document.createElement('canvas').getContext('2d'); c.font = '20px ' + fam; return c.measureText(s).width } return { Text: el.textContent, Loaded: document.fonts.check('20px slides-sample-1'), Mono: Math.abs(w('iiii') - w('MMMM')) < 0.5 } })()`, awaitPromise)) if st.Text != "Brand" || !st.Loaded || !st.Mono { t.Errorf("the sample: %+v, want Brand drawn in its loaded (monospace) face", st) } }