| 1 | //go:build browser |
| 2 | |
| 3 | package slidestest |
| 4 | |
| 5 | // Deck settings offers the person's own fonts (deckfonts.go, |
| 6 | // fixtures/deck-fonts.mjs): a pick is recorded, written as one change, |
| 7 | // served to the page, and taken back by one undo; choosing the theme's |
| 8 | // font again drops the family from the deck. |
| 9 | |
| 10 | import ( |
| 11 | "encoding/json" |
| 12 | "net/http" |
| 13 | "net/url" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | "time" |
| 17 | |
| 18 | "github.com/chromedp/chromedp" |
| 19 | |
| 20 | slidehtml "amadan.net/oficina/slides/internal/slides/render" |
| 21 | ) |
| 22 | |
| 23 | func TestDeckSettingsOffersYourFonts(t *testing.T) { |
| 24 | serif := []byte(nil) |
| 25 | // Its css name: a catalog family's own name is reserved |
| 26 | // (library.go's reservedFontName), so the upload is suffixed. |
| 27 | css := "" |
| 28 | te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { |
| 29 | serif = libFont(t, "LiberationSerif-Regular.ttf") |
| 30 | if rec := uploadFonts(t, a, cookies, upFile{"serif.ttf", serif}); rec.Code != http.StatusOK { |
| 31 | t.Fatalf("upload: %d", rec.Code) |
| 32 | } |
| 33 | css = cssOf(t, a, sha(serif)) |
| 34 | // A deck of its own rather than deckWithSlides: no notes to bind. |
| 35 | return "/d/" + apiDeck(t, a, mintAgent(t, a, cookies, "deck"), "Fonts") + "/edit" |
| 36 | }) |
| 37 | te.rig.Run( |
| 38 | chromedp.Navigate(te.rig.Origin+te.path), |
| 39 | chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), |
| 40 | waitFor(editorBound), |
| 41 | ) |
| 42 | token := mintAgent(t, te.app, te.cookies, "deck-fonts") |
| 43 | te.rig.Run(openDeckSettings()) |
| 44 | |
| 45 | var offer struct { |
| 46 | Groups []string |
| 47 | Heading []string |
| 48 | Body []string |
| 49 | Link string |
| 50 | } |
| 51 | te.rig.Run(evalInto(&offer, `(() => { |
| 52 | const d = document.querySelector('[slides-deck-dialog]') |
| 53 | const opts = name => [...d.querySelectorAll('[name="' + name + '"] optgroup option')].map(o => o.value + '=' + o.textContent.trim()) |
| 54 | const a = d.querySelector('a[href="/fonts"]') |
| 55 | return { |
| 56 | Groups: [...d.querySelectorAll('optgroup')].map(g => g.label), |
| 57 | Heading: opts('rst-deck-font-heading'), |
| 58 | Body: opts('rst-deck-font-body'), |
| 59 | Link: a ? [a.textContent.trim(), a.target, a.rel].join('|') : '' |
| 60 | } |
| 61 | })()`)) |
| 62 | if strings.Join(offer.Groups, "|") != "Your fonts|Your fonts" || |
| 63 | strings.Join(offer.Heading, "|") != css+"=Liberation Serif" || |
| 64 | strings.Join(offer.Body, "|") != css+"=Liberation Serif" || |
| 65 | offer.Link != "Upload a font…|_blank|noopener" { |
| 66 | t.Errorf("Deck settings offer %+v", offer) |
| 67 | } |
| 68 | te.rig.Screen(`[slides-deck-dialog]`, "Deck settings offering your fonts") |
| 69 | |
| 70 | meta := func() map[string]any { |
| 71 | view := apiJSON(t, te.app, http.MethodGet, "/api/v1/decks/"+te.sqid(), token, "", http.StatusOK) |
| 72 | m, _ := view["meta"].(map[string]any) |
| 73 | return m |
| 74 | } |
| 75 | until := func(what string, ok func(map[string]any) bool) map[string]any { |
| 76 | t.Helper() |
| 77 | deadline := time.Now().Add(20 * time.Second) |
| 78 | for { |
| 79 | m := meta() |
| 80 | if ok(m) { |
| 81 | return m |
| 82 | } |
| 83 | if time.Now().After(deadline) { |
| 84 | b, _ := json.Marshal(m) |
| 85 | t.Fatalf("%s: the deck's meta is %s", what, b) |
| 86 | } |
| 87 | te.rig.Run(chromedp.Sleep(100 * time.Millisecond)) |
| 88 | } |
| 89 | } |
| 90 | heading := func(m map[string]any) string { |
| 91 | f, _ := m["fonts"].(map[string]any) |
| 92 | h, _ := f["heading"].(string) |
| 93 | return h |
| 94 | } |
| 95 | files := func(m map[string]any) []any { v, _ := m["fontFiles"].([]any); return v } |
| 96 | choose := func(name, value string) { |
| 97 | sel := `[slides-deck-dialog] [name="` + name + `"]` |
| 98 | te.rig.Run(chromedp.SetValue(chromedp.CSS(sel), value), |
| 99 | chromedp.Evaluate[chromedp.Void](`document.querySelector('`+sel+`').dispatchEvent(new Event('change', { bubbles: true }))`)) |
| 100 | } |
| 101 | |
| 102 | theme, _ := meta()["theme"].(string) |
| 103 | stack := "'" + css + "', " + slidehtml.ThemeStacks()[slidehtml.ThemeID(theme)][0] |
| 104 | choose("rst-deck-font-heading", css) |
| 105 | m := until("the pick", func(m map[string]any) bool { return heading(m) == stack && len(files(m)) == 1 }) |
| 106 | if f, _ := m["fonts"].(map[string]any); f["body"] != "" { |
| 107 | t.Errorf("the pick changed the body: %v", f) |
| 108 | } |
| 109 | if fam, _ := files(m)[0].(map[string]any); fam["css"] != css { |
| 110 | t.Errorf("fontFiles is %v", files(m)) |
| 111 | } |
| 112 | var shown struct { |
| 113 | Heading string |
| 114 | Status int |
| 115 | } |
| 116 | te.rig.Run(evalInto(&shown, `(async () => ({ |
| 117 | Heading: document.querySelector('[name="rst-deck-font-heading"]').value, |
| 118 | Status: (await fetch('/d/`+te.sqid()+`/fonts/`+sha(serif)+`')).status |
| 119 | }))()`, awaitPromise)) |
| 120 | if shown.Heading != css || shown.Status != http.StatusOK { |
| 121 | t.Errorf("after the pick the heading field shows %q and the page fetches the face with %d", shown.Heading, shown.Status) |
| 122 | } |
| 123 | |
| 124 | // Undo takes the whole pick back in one step: the stack and the list |
| 125 | // go together, never one without the other. Any Deck settings change |
| 126 | // (Things arrive included) leaves an empty undo step on top of it |
| 127 | // that the first undo spends, which is the editor's own and not this |
| 128 | // field's, so undo runs until the pick has gone, at most twice. |
| 129 | te.rig.Run( |
| 130 | chromedp.Click(chromedp.CSS(`[slides-deck-dialog] button[type="submit"]`)), |
| 131 | waitFor(`!document.querySelector('[slides-deck-dialog]').open`), |
| 132 | ) |
| 133 | for undos := 1; ; undos++ { |
| 134 | te.rig.Run(chromedp.KeyEvent("z", chromedp.KeyModifiers(chromedp.ModifierCtrl)), chromedp.Sleep(time.Second)) |
| 135 | m := meta() |
| 136 | if (heading(m) == "") != (len(files(m)) == 0) { |
| 137 | b, _ := json.Marshal(m) |
| 138 | t.Fatalf("undo %d took back half the pick: %s", undos, b) |
| 139 | } |
| 140 | if heading(m) == "" { |
| 141 | break |
| 142 | } |
| 143 | if undos == 2 { |
| 144 | b, _ := json.Marshal(m) |
| 145 | t.Fatalf("two undos left the pick: %s", b) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Picked again, then the theme's font: the family leaves the deck. |
| 150 | te.rig.Run(openDeckSettings()) |
| 151 | choose("rst-deck-font-body", css) |
| 152 | until("the body pick", func(m map[string]any) bool { |
| 153 | f, _ := m["fonts"].(map[string]any) |
| 154 | b, _ := f["body"].(string) |
| 155 | return strings.HasPrefix(b, "'"+css+"', ") && len(files(m)) == 1 |
| 156 | }) |
| 157 | choose("rst-deck-font-body", "") |
| 158 | until("the theme's font again", func(m map[string]any) bool { return m["fonts"] == nil && len(files(m)) == 0 }) |
| 159 | } |
| 160 | |
| 161 | // A deck whose font is not in this person's library (a co-editor's, or |
| 162 | // one they have since deleted) still shows it in the field: selected, |
| 163 | // named, and not theirs to choose; anything else is. |
| 164 | func TestDeckSettingsShowsADeckFontYouDoNotHave(t *testing.T) { |
| 165 | var token string |
| 166 | te := newTidyEditorWith(t, dirStore(t), 1280, 800, func(a *testApp, cookies []*http.Cookie) string { |
| 167 | uploadFonts(t, a, cookies, upFile{"serif.ttf", libFont(t, "LiberationSerif-Regular.ttf")}) |
| 168 | token = mintAgent(t, a, cookies, "deck") |
| 169 | sqid := apiDeck(t, a, token, "Fonts") |
| 170 | css := cssOf(t, a, sha(libFont(t, "LiberationSerif-Regular.ttf"))) |
| 171 | useFamily(t, a, token, sqid, pickedFamily(t, a, sqid, css, cookies)) |
| 172 | if rec := post(t, a, "/fonts/delete", url.Values{"family": {css}}, cookies...); rec.Code != http.StatusSeeOther { |
| 173 | t.Fatalf("delete the family: %d", rec.Code) |
| 174 | } |
| 175 | return "/d/" + sqid + "/edit" |
| 176 | }) |
| 177 | te.rig.Run( |
| 178 | chromedp.Navigate(te.rig.Origin+te.path), |
| 179 | chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)), |
| 180 | waitFor(editorBound), |
| 181 | ) |
| 182 | te.rig.Run(openDeckSettings()) |
| 183 | type fieldState struct { |
| 184 | Value, Shown string |
| 185 | Disabled bool |
| 186 | Groups int |
| 187 | Deck int |
| 188 | } |
| 189 | state := func(name string) fieldState { |
| 190 | var s fieldState |
| 191 | te.rig.Run(evalInto(&s, `(() => { |
| 192 | const s = document.querySelector('[slides-deck-dialog] [name="`+name+`"]') |
| 193 | const o = s.selectedOptions[0] |
| 194 | return { Value: s.value, Shown: o ? o.textContent.trim() : '', Disabled: !!o && o.disabled, |
| 195 | Groups: s.querySelectorAll('optgroup').length, Deck: s.querySelectorAll('option[data-deck-font]').length } |
| 196 | })()`)) |
| 197 | return s |
| 198 | } |
| 199 | if got, want := state("rst-deck-font-heading"), (fieldState{Value: "Liberation Serif 2", Shown: "Liberation Serif", Disabled: true, Deck: 1}); got != want { |
| 200 | t.Errorf("the heading field is %+v, want %+v", got, want) |
| 201 | } |
| 202 | if got := state("rst-deck-font-body"); got.Value != "" || got.Deck != 0 { |
| 203 | t.Errorf("the body field is %+v, want Theme's and no deck font", got) |
| 204 | } |
| 205 | te.rig.Screen(`[slides-deck-dialog]`, "Deck settings showing a deck font not in your library") |
| 206 | |
| 207 | // Switching to a font of one's own still works, and the deck font goes. |
| 208 | sel := `[slides-deck-dialog] [name="rst-deck-font-heading"]` |
| 209 | te.rig.Run(chromedp.SetValue(chromedp.CSS(sel), "Georgia, serif"), |
| 210 | chromedp.Evaluate[chromedp.Void](`document.querySelector('`+sel+`').dispatchEvent(new Event('change', { bubbles: true }))`)) |
| 211 | deadline := time.Now().Add(20 * time.Second) |
| 212 | for { |
| 213 | view := apiJSON(t, te.app, http.MethodGet, "/api/v1/decks/"+te.sqid(), token, "", http.StatusOK) |
| 214 | m, _ := view["meta"].(map[string]any) |
| 215 | f, _ := m["fonts"].(map[string]any) |
| 216 | if f["heading"] == "Georgia, serif" && m["fontFiles"] == nil { |
| 217 | break |
| 218 | } |
| 219 | if time.Now().After(deadline) { |
| 220 | b, _ := json.Marshal(m) |
| 221 | t.Fatalf("after choosing Serif the deck's meta is %s", b) |
| 222 | } |
| 223 | te.rig.Run(chromedp.Sleep(100 * time.Millisecond)) |
| 224 | } |
| 225 | if got := state("rst-deck-font-heading"); got.Value != "Georgia, serif" || got.Deck != 0 { |
| 226 | t.Errorf("after choosing Serif the heading field is %+v", got) |
| 227 | } |
| 228 | } |
| 229 | |