//go:build browser
package mapatest
import (
"context"
"testing"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/input"
"github.com/chromedp/chromedp"
)
// The in-page title's guards (build note 78; Fable's M2 on ux/fixes-1),
// each driven by trusted input through the DevTools protocol, so the
// browser's own default actions run: a composing Enter never renames;
// a paste is one line of plain text; and breaks and formatting insert
// nothing, including the Enter an IME or Android's keyboard sends as
// keyCode 229, which the keydown handler leaves alone and only
// beforeinput refuses.
// titleHTML is the editable title's markup, to see what was inserted.
const titleHTML = `document.querySelector("[mapa-title-text]").innerHTML`
// key sends one key through the protocol: keydown, the character it
// types if any, keyup. modifiers is the protocol's bit field (Alt 1,
// Ctrl 2, Meta 4, Shift 8).
func key(name, code, typed string, virtual, modifiers int64,
commands ...string) chromedp.Action[chromedp.Void] {
return chromedp.Func(func(ctx context.Context,
target *chromedp.Target) error {
down := input.DispatchKeyEventTypeRawKeyDown
if typed != "" {
down = input.DispatchKeyEventTypeKeyDown
}
for _, kind := range []input.DispatchKeyEventType{down,
input.DispatchKeyEventTypeKeyUp} {
event := input.DispatchKeyEventParams{Type: kind, Key: name,
Code: code, WindowsVirtualKeyCode: virtual,
Modifiers: modifiers}
if kind == down {
event.Text, event.Commands = typed, commands
}
_, err := cdp.Call(ctx, target, input.DispatchKeyEvent, event)
if err != nil {
return err
}
}
return nil
})
}
// composing starts an IME composition at the caret, as a Japanese
// input method does before its candidate is chosen.
func composing(text string) chromedp.Action[chromedp.Void] {
return chromedp.Func(func(ctx context.Context,
target *chromedp.Target) error {
_, err := cdp.Call(ctx, target, input.ImeSetComposition,
input.ImeSetCompositionParams{Text: text,
SelectionStart: int64(len([]rune(text))),
SelectionEnd: int64(len([]rune(text)))})
return err
})
}
func TestTitleAComposingEnterDoesNotRename(t *testing.T) {
rig, store := browserRig(t, 0)
for _, enter := range []struct {
name string
steps chromedp.Action[chromedp.Void]
}{
// Enter chooses the candidate: its keydown has isComposing.
{"isComposing", chromedp.Steps(composing("か"),
key("Enter", "Enter", "", 13, 0))},
// Safari's Enter that ends a composition says isComposing false
// and keyCode 229.
{"keyCode 229", chromedp.Steps(chromedp.KeyEvent(" X"),
key("Enter", "Enter", "", 229, 0))},
// A break that says it is composing. Chromium ends the
// composition before it sends a break, so this one is made in
// the page; an engine or keyboard that sends one must not rename.
{"a composing break", chromedp.Steps(chromedp.KeyEvent(" X"),
chromedp.Evaluate[chromedp.Void](`document.querySelector(
"[mapa-title-text]").dispatchEvent(new InputEvent("beforeinput", {
inputType: "insertParagraph", isComposing: true, bubbles: true,
cancelable: true }))`))},
} {
page := titledPage(t, rig, store, "Halden "+enter.name, "One.")
_, versions := titleOf(t, store, page.ID)
drive(t, rig, editTitle(), enter.steps)
// Past the busy rule's hold, so a rename sent would have landed.
time.Sleep(1200 * time.Millisecond)
note := evaluate(t, rig,
`document.querySelector("[mapa-title-note]").textContent`)
if title, now := titleOf(t, store, page.ID); title !=
"Halden "+enter.name || now != versions || note != "" {
t.Errorf("%s: a composing Enter renamed: %q, %d versions "+
"(not %d), the note %q", enter.name, title, now, versions,
note)
}
}
}
func TestTitleBreaksAndFormattingInsertNothing(t *testing.T) {
rig, store := browserRig(t, 0)
page := titledPage(t, rig, store, "Halden", "One.")
_, versions := titleOf(t, store, page.ID)
const shift, meta, ctrl = 8, 4, 2
for _, step := range []struct {
name string
press chromedp.Action[chromedp.Void]
}{
{"Enter", key("Enter", "Enter", "\r", 13, 0)},
{"Shift+Enter", key("Enter", "Enter", "\r", 13, shift)},
// An IME's or Android's Enter: keyCode 229, so keydown leaves
// it, and the break arrives as beforeinput.
{"Enter as 229", key("Enter", "Enter", "\r", 229, 0)},
{"Shift+Enter as 229", key("Enter", "Enter", "\r", 229, shift)},
{"⌘B", key("b", "KeyB", "", 66, meta)},
{"Ctrl+B", key("b", "KeyB", "", 66, ctrl)},
{"⌘I", key("i", "KeyI", "", 73, meta)},
} {
// The whole title selected: a break would replace it, and bold
// would wrap it.
drive(t, rig, selectTitle(), step.press)
if got := evaluate(t, rig, titleHTML); got != "Halden" {
t.Errorf("%s inserted: %q", step.name, got)
}
drive(t, rig, chromedp.Evaluate[chromedp.Void](
`document.querySelector("[mapa-title-text]")`+
`.textContent = "Halden"`))
}
if _, now := titleOf(t, store, page.ID); now != versions {
t.Fatalf("a key renamed the page: %d versions, not %d", now,
versions)
}
}
// clipboardHolds puts a selection of rich text on the clipboard by the
// browser's own copy: bold, a link, a line break and a second line.
func clipboardHolds() chromedp.Action[chromedp.Void] {
return chromedp.Steps(chromedp.Evaluate[chromedp.Void](`(function () {
var source = document.createElement("div");
source.id = "copied";
source.innerHTML = "Bold link
second" +
"
third line
"; document.body.append(source); var range = document.createRange(); range.selectNodeContents(source); getSelection().removeAllRanges(); getSelection().addRange(range); })()`), key("c", "KeyC", "", 67, 4, "copy"), chromedp.Evaluate[chromedp.Void]( `document.getElementById("copied").remove()`)) } func TestTitlePasteIsOneLineOfPlainText(t *testing.T) { rig, store := browserRig(t, 0) page := titledPage(t, rig, store, "Halden", "One.") _, versions := titleOf(t, store, page.ID) drive(t, rig, clipboardHolds(), editTitle(), key(" ", "Space", " ", 32, 0), key("v", "KeyV", "", 86, 4, "paste")) if got := evaluate(t, rig, titleHTML); got != "Halden Bold link second third line" { t.Fatalf("the paste left %q", got) } if _, now := titleOf(t, store, page.ID); now != versions { t.Fatal("a paste renamed the page") } } // Android's keyboard sends Enter as keyCode 229, which the keydown // handler leaves alone (it may be composing), and then a non-composing // beforeinput insertParagraph: that renames exactly as Enter does, and // inserts nothing. With a word still underlined (Gboard composes as you // type), the Enter ends the composition first, and the break that // follows is not composing: it renames too, with the word in. func TestTitleAndroidsEnterRenames(t *testing.T) { rig, store := browserRig(t, 0) for _, enter := range []struct { name, want string steps chromedp.Action[chromedp.Void] }{ {"typed", "Halden typed Robotics", chromedp.Steps( chromedp.KeyEvent(" Robotics"), key("Enter", "Enter", "\r", 229, 0))}, {"composing", "Halden composing Ro", chromedp.Steps( chromedp.KeyEvent(" "), composing("Ro"), key("Enter", "Enter", "\r", 229, 0))}, } { page := titledPage(t, rig, store, "Halden "+enter.name, "One.") _, versions := titleOf(t, store, page.ID) drive(t, rig, editTitle(), enter.steps) eventually(t, enter.name+": the rename from Android's Enter", func() bool { title, now := titleOf(t, store, page.ID) return title == enter.want && now == versions+1 }) if got := evaluate(t, rig, titleHTML); got != enter.want { t.Fatalf("%s: Android's Enter left %q", enter.name, got) } time.Sleep(1200 * time.Millisecond) if _, now := titleOf(t, store, page.ID); now != versions+1 { t.Fatalf("%s: one Enter renamed %d times", enter.name, now-versions) } } }