oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package mapatest
4
5import (
6 "bytes"
7 "context"
8 "encoding/binary"
9 "image"
10 "image/color"
11 "image/jpeg"
12 "os"
13 "path/filepath"
14 "strings"
15 "testing"
16
17 "amadan.net/rastrillo/rastrillo/blobs"
18 "amadan.net/rastrillo/rastrillo/harness"
19 "github.com/chromedp/chromedp"
20 "github.com/chromedp/chromedp/kb"
21
22 "amadan.net/oficina/mapa/internal/mapa"
23)
24
25// L10 for images (images spec §4.6): the keyboard reaches / → Image
26// and Insert → Image (the system's picker stubbed), the alt field, the
27// bubble's Alt text and Remove's confirm page; an upload that finishes
28// while the person types elsewhere moves no focus and speaks; and axe
29// finds nothing serious on a page with images.
30
31// active is the focused element's accessible name, or its tag.
32const active = `(function (node) {
33 return node.getAttribute("aria-label") || node.textContent.trim() ||
34 node.nodeName;
35})(document.activeElement)`
36
37func TestL10ImagesByKeyboard(t *testing.T) {
38 rig, store := browserRig(t, 0)
39 page := teamPage(t, rig, store, "Pictures go here.")
40 drive(t, rig, onNewDocument(filePicker),
41 chromedp.Navigate(rig.Origin+mapa.PagePath(page)),
42 chromedp.WaitVisible(mounted), caretAt(0),
43 chromedp.KeyEvent(" /ima"))
44 listShows(t, rig, "Image")
45 drive(t, rig, enter(), choose(pictureFile(t, 200, 100)),
46 chromedp.WaitVisible(altField))
47 if !strings.Contains(read(t, rig, chromedp.Evaluate[string](
48 `document.activeElement.closest("[mapa-alt]") ? "alt" : ""`)), "alt") {
49 t.Fatal("the alt field did not take focus")
50 }
51 drive(t, rig, chromedp.KeyEvent("Our whiteboard"), enter())
52 eventually(t, "focus back in the text", func() bool {
53 return read(t, rig, chromedp.Evaluate[bool](focusInEditor))
54 })
55 if got := typed(t, rig); !strings.Contains(got,
56 "![Our whiteboard](/i/") {
57 t.Fatalf("the alt text did not land: %q", got)
58 }
59 // Select the image from the keyboard, and reach its bubble.
60 drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft),
61 chromedp.WaitVisible(`[mapa-image-bubble]`),
62 chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers(
63 chromedp.ModifierAlt)))
64 if name := read(t, rig, chromedp.Evaluate[string](active)); name !=
65 "Alt text" {
66 t.Fatalf("⌥F10 went to %q", name)
67 }
68 if found := axeSerious(t, rig); found != "" {
69 t.Fatalf("axe, a page with an image and its bubble:\n%s", found)
70 }
71 drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft), enter())
72 arrive(t, rig, removePath(t, rig))
73 drive(t, rig, press(`main form button[rst-btn~="danger"]`))
74 arrive(t, rig, mapa.PagePath(page)+"?done=image-removed")
75 drive(t, rig, chromedp.WaitVisible(`[mapa-image="unavailable"]`))
76}
77
78// removePath is where Remove from Mapa went: the confirm page.
79func removePath(t *testing.T, rig *harness.Rig) string {
80 t.Helper()
81 var path string
82 eventually(t, "the confirm page", func() bool {
83 path = strings.TrimPrefix(read(t, rig, chromedp.Location()),
84 rig.Origin)
85 return strings.HasPrefix(path, "/i/")
86 })
87 return path
88}
89
90// Insert → Image… from the keyboard: the menu's summary, then its row.
91func TestL10InsertImageByKeyboard(t *testing.T) {
92 rig, store := browserRig(t, 0)
93 page := teamPage(t, rig, store, "A line.")
94 drive(t, rig, onNewDocument(filePicker),
95 chromedp.Navigate(rig.Origin+mapa.PagePath(page)),
96 chromedp.WaitVisible(mounted), caretAt(0),
97 press(`#mapa-menu-insert summary`),
98 chromedp.WaitVisible(`[data-mapa-command="insert-image"]`),
99 press(`[data-mapa-command="insert-image"]`),
100 choose(pictureFile(t, 60, 40)),
101 chromedp.WaitVisible(altField), chromedp.KeyEvent(kb.Escape))
102 eventually(t, "focus back in the text, the image in", func() bool {
103 return read(t, rig, chromedp.Evaluate[bool](focusInEditor)) &&
104 strings.Contains(typed(t, rig), "A line.![](/i/")
105 })
106}
107
108// An upload that finishes while the person types elsewhere: focus
109// stays, and the live region says "Image added".
110func TestL10AFinishedUploadMovesNoFocus(t *testing.T) {
111 held := &heldStore{}
112 rig, store := browserRigWith(t, 0, func(inner blobs.Store) blobs.Store {
113 held.Store = inner
114 return held
115 })
116 teamPage(t, rig, store, "One.\n\nTwo.")
117 held.hold()
118 t.Cleanup(held.let)
119 drive(t, rig, caretAt(0), pasteFile(pictureFile(t, 60, 40)), caretAt(1),
120 chromedp.KeyEvent(" typing"))
121 held.let()
122 eventually(t, "the image in, and said", func() bool {
123 return strings.Contains(typed(t, rig), "](/i/") &&
124 text(t, rig, "[mapa-live]") == "Image added"
125 })
126 drive(t, rig, chromedp.KeyEvent(" on"))
127 if !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) ||
128 !strings.Contains(typed(t, rig), "Two. typing on") {
129 t.Fatalf("focus moved: %q", typed(t, rig))
130 }
131}
132
133// The JPEG over 4,096 px with EXIF orientation 6 (images §4.1): it
134// arrives no larger than 4,096 px, and upright.
135func TestABigPhotoArrivesUprightAndSmaller(t *testing.T) {
136 rig, store := browserRig(t, 0)
137 page := teamPage(t, rig, store, "Photo:")
138 drive(t, rig, onNewDocument(filePicker),
139 chromedp.Navigate(rig.Origin+mapa.PagePath(page)),
140 chromedp.WaitVisible(mounted), caretAt(0),
141 press(`#mapa-menu-insert summary`),
142 press(`[data-mapa-command="insert-image"]`),
143 choose(sidewaysPhoto(t)))
144 eventually(t, "the photo in", func() bool {
145 return strings.Contains(typed(t, rig), "](/i/")
146 })
147 id := between(typed(t, rig), "](/i/", ")")
148 viewer, _ := store.Viewer(context.Background(), Ana, nil)
149 row, err := store.ReadableImage(context.Background(), viewer, id)
150 if err != nil {
151 t.Fatal(err)
152 }
153 raw, kind, err := store.ImageBytes(context.Background(), row)
154 if err != nil || kind != "image/jpeg" {
155 t.Fatalf("%s: %v", kind, err)
156 }
157 picture, err := jpeg.Decode(bytes.NewReader(raw))
158 if err != nil {
159 t.Fatal(err)
160 }
161 bounds := picture.Bounds()
162 if bounds.Dx() > 4096 || bounds.Dy() != 4096 || bounds.Dx() >= bounds.Dy() {
163 t.Fatalf("it arrived %dx%d", bounds.Dx(), bounds.Dy())
164 }
165 red, _, blue, _ := picture.At(10, 10).RGBA()
166 if red < 0xc000 || blue > 0x4000 {
167 t.Fatalf("it is not upright: the top left is %v", picture.At(10, 10))
168 }
169}
170
171// sidewaysPhoto is a 6,000 by 4,000 JPEG whose EXIF says to turn it a
172// quarter clockwise: upright it is 4,000 by 6,000, red at the top left.
173func sidewaysPhoto(t *testing.T) string {
174 t.Helper()
175 stored := image.NewRGBA(image.Rect(0, 0, 6000, 4000))
176 blue := color.RGBA{0, 0, 255, 255}
177 red := color.RGBA{255, 0, 0, 255}
178 for column := range 6000 {
179 for line := range 4000 {
180 // Turned clockwise to show, stored column c becomes shown
181 // row c, and stored row r shown column 3999-r.
182 shown := blue
183 if column < 3000 && 3999-line < 2000 {
184 shown = red
185 }
186 stored.Set(column, line, shown)
187 }
188 }
189 var out bytes.Buffer
190 err := jpeg.Encode(&out, stored, &jpeg.Options{Quality: 80})
191 if err != nil {
192 t.Fatal(err)
193 }
194 raw := out.Bytes()
195 exif := []byte("Exif\x00\x00MM\x00\x2a\x00\x00\x00\x08\x00\x01" +
196 "\x01\x12\x00\x03\x00\x00\x00\x01\x00\x06\x00\x00\x00\x00\x00\x00")
197 segment := []byte{0xff, 0xe1, 0, 0}
198 binary.BigEndian.PutUint16(segment[2:], uint16(len(exif)+2))
199 photo := append(append(append([]byte{}, raw[:2]...), segment...), exif...)
200 photo = append(photo, raw[2:]...)
201 path := filepath.Join(t.TempDir(), "sideways.jpg")
202 if err := os.WriteFile(path, photo, 0o600); err != nil {
203 t.Fatal(err)
204 }
205 return path
206}
207
208// Copy and paste between pages (images §2.2): the slice names its page,
209// so a paste into another page copies the image first, and the copy is
210// what the new page holds and attaches.
211func TestAPasteFromAnotherPageCopiesItsImages(t *testing.T) {
212 rig, store := browserRig(t, 0)
213 shown := addImage(t, store, Bo, 80, 60)
214 from, err := store.CreateTeamPage(context.Background(), Bo, "Source",
215 shown.Markdown("from the source"))
216 if err != nil {
217 t.Fatal(err)
218 }
219 page := teamPage(t, rig, store, "Pasted:")
220 slice := `{"v":1,"sources":["![from the source](/i/` + shown.PublicID +
221 `)"],"seams":[],"page":"` + strings.TrimPrefix(mapa.PagePath(from),
222 "/p/") + `"}`
223 drive(t, rig, caretAt(0), chromedp.Evaluate[chromedp.Void](`(function () {
224 var data = new DataTransfer();
225 data.setData("application/x-mapa", `+"`"+slice+"`"+`);
226 data.setData("text/plain", "![from the source](/i/`+shown.PublicID+`)");
227 mapaEditor.view.dom.dispatchEvent(new ClipboardEvent("paste",
228 { clipboardData: data, bubbles: true, cancelable: true }));
229})()`))
230 eventually(t, "a copy pasted, drawn", func() bool {
231 body := typed(t, rig)
232 return strings.Contains(body, "![from the source](/i/") &&
233 !strings.Contains(body, shown.PublicID) &&
234 read(t, rig, chromedp.Evaluate[bool](`document.querySelector(
235 'article[mapa-body] img[src^="/i/"]') !== null`))
236 })
237 drive(t, rig, chromedp.KeyEvent("s", mod()))
238 eventually(t, "the commit attaches the copy", func() bool {
239 _, body := current(t, store, page.ID)
240 return strings.Contains(body, "](/i/")
241 })
242 attachedHere(t, store, typed(t, rig), page.ID)
243}
244