| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | // A family uploaded in the Fonts tab Deck settings opened is offered in |
| 6 | // Deck settings once the editor's window has focus again (final review, |
| 7 | // minor): it was offered only after the editor was opened again. |
| 8 | |
| 9 | import ( |
| 10 | "net/http" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "github.com/chromedp/chromedp" |
| 15 | ) |
| 16 | |
| 17 | func TestDeckSettingsOffersAFontUploadedMeanwhile(t *testing.T) { |
| 18 | te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { |
| 19 | return "/d/" + apiDeck(t, a, mintAgent(t, a, cookies, "deck"), "Meanwhile") + "/edit" |
| 20 | }) |
| 21 | te.rig.Run( |
| 22 | chromedp.Navigate(te.rig.Origin+te.path), |
| 23 | chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), |
| 24 | waitFor(editorBound), |
| 25 | openDeckSettings(), |
| 26 | ) |
| 27 | offered := func() string { |
| 28 | var out []string |
| 29 | 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(','))`)) |
| 30 | return strings.Join(out, "|") |
| 31 | } |
| 32 | if got := offered(); got != "" { |
| 33 | t.Fatalf("an empty library offers %q", got) |
| 34 | } |
| 35 | // Uploaded in another tab, then back to the editor. |
| 36 | if rec := uploadFonts(t, te.app, te.cookies, upFile{"brand.ttf", brandFont(t, "LiberationSerif-Regular.ttf", "Brand")}); rec.Code != http.StatusOK { |
| 37 | t.Fatalf("upload: %d", rec.Code) |
| 38 | } |
| 39 | te.rig.Run(chromedp.Evaluate[chromedp.Void](`window.dispatchEvent(new Event('focus'))`)) |
| 40 | te.rig.Run(waitFor(`[...document.querySelectorAll('[slides-deck-dialog] select[name^="rst-deck-font-"] optgroup option')].length === 2`)) |
| 41 | if got := offered(); got != "Your fonts:Brand=Brand|Your fonts:Brand=Brand" { |
| 42 | t.Errorf("after the upload both fields offer %q", got) |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // The Fonts page's sample of a family is drawn in it: the face loads |
| 47 | // from the library route, and the name is set in it. |
| 48 | func TestTheFontsPageSampleIsDrawnInItsFamily(t *testing.T) { |
| 49 | te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { |
| 50 | if rec := uploadFonts(t, a, cookies, upFile{"brand.ttf", brandFont(t, "LiberationMono-Regular.ttf", "Brand")}); rec.Code != http.StatusOK { |
| 51 | t.Fatalf("upload: %d", rec.Code) |
| 52 | } |
| 53 | return "/fonts" |
| 54 | }) |
| 55 | te.rig.Run(chromedp.Navigate(te.rig.Origin+te.path), chromedp.WaitVisible(chromedp.CSS(`[slides-font-sample="1"]`))) |
| 56 | var st struct { |
| 57 | Text string |
| 58 | Loaded bool |
| 59 | Mono bool |
| 60 | } |
| 61 | te.rig.Run(evalInto(&st, `(async () => { |
| 62 | await document.fonts.ready |
| 63 | const el = document.querySelector('[slides-font-sample="1"]') |
| 64 | const fam = getComputedStyle(el).fontFamily |
| 65 | // Monospace: as wide as the same count of another letter. |
| 66 | const w = (s) => { const c = document.createElement('canvas').getContext('2d'); c.font = '20px ' + fam; return c.measureText(s).width } |
| 67 | return { Text: el.textContent, Loaded: document.fonts.check('20px slides-sample-1'), Mono: Math.abs(w('iiii') - w('MMMM')) < 0.5 } |
| 68 | })()`, awaitPromise)) |
| 69 | if st.Text != "Brand" || !st.Loaded || !st.Mono { |
| 70 | t.Errorf("the sample: %+v, want Brand drawn in its loaded (monospace) face", st) |
| 71 | } |
| 72 | } |
| 73 | |