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 "context"
7 "testing"
8 "time"
9
10 "github.com/chromedp/cdproto/cdp"
11 "github.com/chromedp/cdproto/input"
12 "github.com/chromedp/chromedp"
13)
14
15// The in-page title's guards (build note 78; Fable's M2 on ux/fixes-1),
16// each driven by trusted input through the DevTools protocol, so the
17// browser's own default actions run: a composing Enter never renames;
18// a paste is one line of plain text; and breaks and formatting insert
19// nothing, including the Enter an IME or Android's keyboard sends as
20// keyCode 229, which the keydown handler leaves alone and only
21// beforeinput refuses.
22
23// titleHTML is the editable title's markup, to see what was inserted.
24const titleHTML = `document.querySelector("[mapa-title-text]").innerHTML`
25
26// key sends one key through the protocol: keydown, the character it
27// types if any, keyup. modifiers is the protocol's bit field (Alt 1,
28// Ctrl 2, Meta 4, Shift 8).
29func key(name, code, typed string, virtual, modifiers int64,
30 commands ...string) chromedp.Action[chromedp.Void] {
31 return chromedp.Func(func(ctx context.Context,
32 target *chromedp.Target) error {
33 down := input.DispatchKeyEventTypeRawKeyDown
34 if typed != "" {
35 down = input.DispatchKeyEventTypeKeyDown
36 }
37 for _, kind := range []input.DispatchKeyEventType{down,
38 input.DispatchKeyEventTypeKeyUp} {
39 event := input.DispatchKeyEventParams{Type: kind, Key: name,
40 Code: code, WindowsVirtualKeyCode: virtual,
41 Modifiers: modifiers}
42 if kind == down {
43 event.Text, event.Commands = typed, commands
44 }
45 _, err := cdp.Call(ctx, target, input.DispatchKeyEvent, event)
46 if err != nil {
47 return err
48 }
49 }
50 return nil
51 })
52}
53
54// composing starts an IME composition at the caret, as a Japanese
55// input method does before its candidate is chosen.
56func composing(text string) chromedp.Action[chromedp.Void] {
57 return chromedp.Func(func(ctx context.Context,
58 target *chromedp.Target) error {
59 _, err := cdp.Call(ctx, target, input.ImeSetComposition,
60 input.ImeSetCompositionParams{Text: text,
61 SelectionStart: int64(len([]rune(text))),
62 SelectionEnd: int64(len([]rune(text)))})
63 return err
64 })
65}
66
67func TestTitleAComposingEnterDoesNotRename(t *testing.T) {
68 rig, store := browserRig(t, 0)
69 for _, enter := range []struct {
70 name string
71 steps chromedp.Action[chromedp.Void]
72 }{
73 // Enter chooses the candidate: its keydown has isComposing.
74 {"isComposing", chromedp.Steps(composing("か"),
75 key("Enter", "Enter", "", 13, 0))},
76 // Safari's Enter that ends a composition says isComposing false
77 // and keyCode 229.
78 {"keyCode 229", chromedp.Steps(chromedp.KeyEvent(" X"),
79 key("Enter", "Enter", "", 229, 0))},
80 // A break that says it is composing. Chromium ends the
81 // composition before it sends a break, so this one is made in
82 // the page; an engine or keyboard that sends one must not rename.
83 {"a composing break", chromedp.Steps(chromedp.KeyEvent(" X"),
84 chromedp.Evaluate[chromedp.Void](`document.querySelector(
85 "[mapa-title-text]").dispatchEvent(new InputEvent("beforeinput", {
86 inputType: "insertParagraph", isComposing: true, bubbles: true,
87 cancelable: true }))`))},
88 } {
89 page := titledPage(t, rig, store, "Halden "+enter.name, "One.")
90 _, versions := titleOf(t, store, page.ID)
91 drive(t, rig, editTitle(), enter.steps)
92 // Past the busy rule's hold, so a rename sent would have landed.
93 time.Sleep(1200 * time.Millisecond)
94 note := evaluate(t, rig,
95 `document.querySelector("[mapa-title-note]").textContent`)
96 if title, now := titleOf(t, store, page.ID); title !=
97 "Halden "+enter.name || now != versions || note != "" {
98 t.Errorf("%s: a composing Enter renamed: %q, %d versions "+
99 "(not %d), the note %q", enter.name, title, now, versions,
100 note)
101 }
102 }
103}
104
105func TestTitleBreaksAndFormattingInsertNothing(t *testing.T) {
106 rig, store := browserRig(t, 0)
107 page := titledPage(t, rig, store, "Halden", "One.")
108 _, versions := titleOf(t, store, page.ID)
109 const shift, meta, ctrl = 8, 4, 2
110 for _, step := range []struct {
111 name string
112 press chromedp.Action[chromedp.Void]
113 }{
114 {"Enter", key("Enter", "Enter", "\r", 13, 0)},
115 {"Shift+Enter", key("Enter", "Enter", "\r", 13, shift)},
116 // An IME's or Android's Enter: keyCode 229, so keydown leaves
117 // it, and the break arrives as beforeinput.
118 {"Enter as 229", key("Enter", "Enter", "\r", 229, 0)},
119 {"Shift+Enter as 229", key("Enter", "Enter", "\r", 229, shift)},
120 {"⌘B", key("b", "KeyB", "", 66, meta)},
121 {"Ctrl+B", key("b", "KeyB", "", 66, ctrl)},
122 {"⌘I", key("i", "KeyI", "", 73, meta)},
123 } {
124 // The whole title selected: a break would replace it, and bold
125 // would wrap it.
126 drive(t, rig, selectTitle(), step.press)
127 if got := evaluate(t, rig, titleHTML); got != "Halden" {
128 t.Errorf("%s inserted: %q", step.name, got)
129 }
130 drive(t, rig, chromedp.Evaluate[chromedp.Void](
131 `document.querySelector("[mapa-title-text]")`+
132 `.textContent = "Halden"`))
133 }
134 if _, now := titleOf(t, store, page.ID); now != versions {
135 t.Fatalf("a key renamed the page: %d versions, not %d", now,
136 versions)
137 }
138}
139
140// clipboardHolds puts a selection of rich text on the clipboard by the
141// browser's own copy: bold, a link, a line break and a second line.
142func clipboardHolds() chromedp.Action[chromedp.Void] {
143 return chromedp.Steps(chromedp.Evaluate[chromedp.Void](`(function () {
144 var source = document.createElement("div");
145 source.id = "copied";
146 source.innerHTML = "<b>Bold</b> <a href='/x'>link</a><br>second" +
147 "<p>third line</p>";
148 document.body.append(source);
149 var range = document.createRange();
150 range.selectNodeContents(source);
151 getSelection().removeAllRanges();
152 getSelection().addRange(range);
153})()`), key("c", "KeyC", "", 67, 4, "copy"),
154 chromedp.Evaluate[chromedp.Void](
155 `document.getElementById("copied").remove()`))
156}
157
158func TestTitlePasteIsOneLineOfPlainText(t *testing.T) {
159 rig, store := browserRig(t, 0)
160 page := titledPage(t, rig, store, "Halden", "One.")
161 _, versions := titleOf(t, store, page.ID)
162 drive(t, rig, clipboardHolds(), editTitle(),
163 key(" ", "Space", " ", 32, 0),
164 key("v", "KeyV", "", 86, 4, "paste"))
165 if got := evaluate(t, rig, titleHTML); got !=
166 "Halden Bold link second third line" {
167 t.Fatalf("the paste left %q", got)
168 }
169 if _, now := titleOf(t, store, page.ID); now != versions {
170 t.Fatal("a paste renamed the page")
171 }
172}
173
174// Android's keyboard sends Enter as keyCode 229, which the keydown
175// handler leaves alone (it may be composing), and then a non-composing
176// beforeinput insertParagraph: that renames exactly as Enter does, and
177// inserts nothing. With a word still underlined (Gboard composes as you
178// type), the Enter ends the composition first, and the break that
179// follows is not composing: it renames too, with the word in.
180func TestTitleAndroidsEnterRenames(t *testing.T) {
181 rig, store := browserRig(t, 0)
182 for _, enter := range []struct {
183 name, want string
184 steps chromedp.Action[chromedp.Void]
185 }{
186 {"typed", "Halden typed Robotics", chromedp.Steps(
187 chromedp.KeyEvent(" Robotics"),
188 key("Enter", "Enter", "\r", 229, 0))},
189 {"composing", "Halden composing Ro", chromedp.Steps(
190 chromedp.KeyEvent(" "), composing("Ro"),
191 key("Enter", "Enter", "\r", 229, 0))},
192 } {
193 page := titledPage(t, rig, store, "Halden "+enter.name, "One.")
194 _, versions := titleOf(t, store, page.ID)
195 drive(t, rig, editTitle(), enter.steps)
196 eventually(t, enter.name+": the rename from Android's Enter",
197 func() bool {
198 title, now := titleOf(t, store, page.ID)
199 return title == enter.want && now == versions+1
200 })
201 if got := evaluate(t, rig, titleHTML); got != enter.want {
202 t.Fatalf("%s: Android's Enter left %q", enter.name, got)
203 }
204 time.Sleep(1200 * time.Millisecond)
205 if _, now := titleOf(t, store, page.ID); now != versions+1 {
206 t.Fatalf("%s: one Enter renamed %d times", enter.name,
207 now-versions)
208 }
209 }
210}
211