| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | // A family that gains a face during the visit (final review, I4): upload |
| 6 | // a bold for a family the deck already uses, pick the family again, and |
| 7 | // the open editor loads the new set before the change shows, then has |
| 8 | // exactly its faces on the page; undo and redo move between the sets. |
| 9 | |
| 10 | import ( |
| 11 | "encoding/json" |
| 12 | "net/http" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "github.com/chromedp/chromedp" |
| 17 | |
| 18 | "amadan.net/oficina/slides/internal/slides/fonts" |
| 19 | ) |
| 20 | |
| 21 | func TestDeckFontAGrownFamilyLoadsItsNewFaces(t *testing.T) { |
| 22 | var regular, bold []byte |
| 23 | te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { |
| 24 | regular = brandFont(t, "LiberationSerif-Regular.ttf", "Brand") |
| 25 | if rec := uploadFonts(t, a, cookies, upFile{"brand.ttf", regular}); rec.Code != http.StatusOK { |
| 26 | t.Fatalf("upload: %d", rec.Code) |
| 27 | } |
| 28 | return "/d/" + apiDeck(t, a, mintAgent(t, a, cookies, "deck"), "Grown") + "/edit" |
| 29 | }) |
| 30 | te.rig.Run( |
| 31 | chromedp.Navigate(te.rig.Origin+te.path), |
| 32 | chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), |
| 33 | waitFor(editorBound), |
| 34 | ) |
| 35 | token := mintAgent(t, te.app, te.cookies, "deck-fonts") |
| 36 | faces := func() int { |
| 37 | view := apiJSON(t, te.app, http.MethodGet, "/api/v1/decks/"+te.sqid(), token, "", http.StatusOK) |
| 38 | m, _ := view["meta"].(map[string]any) |
| 39 | files, _ := m["fontFiles"].([]any) |
| 40 | if len(files) == 0 { |
| 41 | return 0 |
| 42 | } |
| 43 | fam, _ := files[0].(map[string]any) |
| 44 | fs, _ := fam["faces"].([]any) |
| 45 | return len(fs) |
| 46 | } |
| 47 | until := func(what string, ok func() bool) { |
| 48 | t.Helper() |
| 49 | deadline := time.Now().Add(20 * time.Second) |
| 50 | for !ok() { |
| 51 | if time.Now().After(deadline) { |
| 52 | t.Fatalf("%s: never", what) |
| 53 | } |
| 54 | te.rig.Run(chromedp.Sleep(100 * time.Millisecond)) |
| 55 | } |
| 56 | } |
| 57 | // onPage is the page's Brand faces: weight, and whether loaded. |
| 58 | onPage := func() []string { |
| 59 | var out []string |
| 60 | te.rig.Run(evalInto(&out, `[...document.fonts].filter(f => f.family === 'Brand').map(f => f.weight + ':' + f.status).sort()`)) |
| 61 | return out |
| 62 | } |
| 63 | pick := func() { |
| 64 | sel := `[slides-deck-dialog] [name="rst-deck-font-heading"]` |
| 65 | te.rig.Run(openDeckSettings(), chromedp.SetValue(chromedp.CSS(sel), "Brand"), |
| 66 | chromedp.Evaluate[chromedp.Void](`document.querySelector('`+sel+`').dispatchEvent(new Event('change', { bubbles: true }))`)) |
| 67 | } |
| 68 | closeSettings := func() { |
| 69 | te.rig.Run(chromedp.Click(chromedp.CSS(`[slides-deck-dialog] button[type="submit"]`)), |
| 70 | waitFor(`!document.querySelector('[slides-deck-dialog]').open`)) |
| 71 | } |
| 72 | same := func(got []string, want ...string) bool { |
| 73 | b1, _ := json.Marshal(got) |
| 74 | b2, _ := json.Marshal(want) |
| 75 | return string(b1) == string(b2) |
| 76 | } |
| 77 | |
| 78 | pick() |
| 79 | until("the regular pick", func() bool { return faces() == 1 }) |
| 80 | until("the regular face on the page", func() bool { return same(onPage(), "400:loaded") }) |
| 81 | closeSettings() |
| 82 | |
| 83 | // A bold arrives in the library; the family is picked again. |
| 84 | bold = brandBold(t) |
| 85 | if rec := uploadFonts(t, te.app, te.cookies, upFile{"brand-bold.ttf", bold}); rec.Code != http.StatusOK { |
| 86 | t.Fatalf("upload the bold: %d", rec.Code) |
| 87 | } |
| 88 | pick() |
| 89 | until("the grown pick", func() bool { return faces() == 2 }) |
| 90 | until("both faces on the page, and only those", func() bool { return same(onPage(), "400:loaded", "700:loaded") }) |
| 91 | closeSettings() |
| 92 | |
| 93 | // Undo to the regular set (Deck settings leaves an empty undo step |
| 94 | // on top, the editor's own: at most two undos), then redo. |
| 95 | for undos := 1; faces() != 1; undos++ { |
| 96 | if undos > 2 { |
| 97 | t.Fatalf("two undos left %d faces", faces()) |
| 98 | } |
| 99 | te.rig.Run(chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.Sleep(time.Second)) |
| 100 | } |
| 101 | until("the regular set on the page after undo", func() bool { return same(onPage(), "400:loaded") }) |
| 102 | te.rig.Run(chromedp.KeyEvent("y", chromedp.KeyModifiers(chromedp.ModifierCtrl))) |
| 103 | until("the grown set after redo", func() bool { return faces() == 2 }) |
| 104 | until("both faces on the page after redo", func() bool { return same(onPage(), "400:loaded", "700:loaded") }) |
| 105 | } |
| 106 | |
| 107 | // brandBold is Liberation Serif Bold called Brand, weight 700. |
| 108 | func brandBold(t *testing.T) []byte { |
| 109 | t.Helper() |
| 110 | f, err := fonts.Parse(libFont(t, "LiberationSerif-Bold.ttf")) |
| 111 | if err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | out, err := f.Renamed("Brand", 700, false) |
| 115 | if err != nil { |
| 116 | t.Fatal(err) |
| 117 | } |
| 118 | return out |
| 119 | } |
| 120 | |