//go:build browser package mapatest import ( "strconv" "testing" "time" "github.com/chromedp/chromedp" ) // The in-page title when its window loses focus, when the page closes, // and when a rename's reply is lost (Fable's L1 and L3 on ux/fixes-1; // build note 79). // away is the window or tab losing focus while the title is being // edited, as the page sees it: document.hasFocus() false (or the page // hidden), then the field's blur. Headless Chromium keeps every tab // focused, so the page is told what a real Cmd-Tab tells it. func away(how string) chromedp.Action[chromedp.Void] { return chromedp.Evaluate[chromedp.Void](`(function (how) { if (how === "hidden") { Object.defineProperty(document, "visibilityState", { configurable: true, get: function () { return "hidden"; } }); } else { document.hasFocus = function () { return false; }; } document.querySelector("[mapa-title-text]").blur(); window.dispatchEvent(new Event("blur")); })("` + how + `")`) } // back is the person coming back: the window has focus again, and the // browser hands it its focus. const back = `(function () { delete document.hasFocus; delete document.visibilityState; window.dispatchEvent(new Event("focus")); return String(document.activeElement === document.querySelector("[mapa-title-text]")); })()` // notRenamed fails if the page's title or its versions moved, after the // busy rule's hold, so a rename sent would have landed. func notRenamed(t *testing.T, check func() (string, int), title string, versions int, why string) { t.Helper() time.Sleep(1200 * time.Millisecond) if got, now := check(); got != title || now != versions { t.Fatalf("%s renamed: %q, %d versions, not %d", why, got, now, versions) } } func TestTitleLosingTheWindowDoesNotRename(t *testing.T) { rig, store := browserRig(t, 0) for _, how := range []string{"unfocused", "hidden"} { title := "Halden " + how page := titledPage(t, rig, store, title, "One.") check := func() (string, int) { return titleOf(t, store, page.ID) } _, versions := check() drive(t, rig, editTitle(), chromedp.KeyEvent(" Rob"), away(how)) notRenamed(t, check, title, versions, how+": leaving the window") if typed := text(t, rig, "h1[mapa-title]"); typed != title+" Rob" { t.Fatalf("%s: the edit was not kept open: %q", how, typed) } if focused := evaluate(t, rig, back); focused != "true" { t.Fatalf("%s: coming back did not give the field its focus", how) } // Coming back and leaving the field renames: Enter, then a // click into the page's text. drive(t, rig, chromedp.KeyEvent("otics")) if how == "unfocused" { drive(t, rig, chromedp.KeyEvent("\r")) } else { drive(t, rig, chromedp.Click(mounted)) } eventually(t, how+": the rename once back", func() bool { got, now := check() return got == title+" Robotics" && now == versions+1 }) } } // Closing the page with the title edited renames it, as closing commits // the page's text: here after the window lost focus, so no blur sent // it first. A rename already out when the page closes lands once. func TestTitleClosingThePageRenamesAnEditedTitle(t *testing.T) { rig, store := browserRig(t, 0) page := titledPage(t, rig, store, "Halden", "One.") check := func() (string, int) { return titleOf(t, store, page.ID) } _, versions := check() drive(t, rig, editTitle(), chromedp.KeyEvent(" Robotics"), away("unfocused")) notRenamed(t, check, "Halden", versions, "leaving the window") drive(t, rig, chromedp.Navigate(rig.Origin+"/pages")) eventually(t, "the closing rename", func() bool { got, now := check() return got == "Halden Robotics" && now == versions+1 }) acme := titledPage(t, rig, store, "Acme", "Two.") _, before := titleOf(t, store, acme.ID) drive(t, rig, editTitle(), chromedp.KeyEvent(" Ltd"), chromedp.KeyEvent("\r"), chromedp.Navigate(rig.Origin+"/pages")) time.Sleep(1500 * time.Millisecond) if got, now := titleOf(t, store, acme.ID); got != "Acme Ltd" || now != before+1 { t.Fatalf("a rename out at close: %q, %d versions (%d before)", got, now, before) } } // loseRenameReply makes the next rename's reply never arrive: the POST // reaches the server and lands, and the page sees a network error. const loseRenameReply = `(function () { var real = window.fetch, lost = false; window.fetch = function (url, init) { return real(url, init).then(function (response) { if (!lost && String(url).endsWith("/rename")) { lost = true; throw new TypeError("Failed to fetch"); } return response; }); }; })()` // A rename that landed but whose reply was lost says "Not renamed"; // Enter again is answered with that rename's revisions, so it says // Renamed and the saver's base follows it, as if the first reply had // come. func TestTitleARetriedRenameThatLandedMovesTheBase(t *testing.T) { rig, store := browserRig(t, 0) page := titledPage(t, rig, store, "Halden", "One.") first, _ := current(t, store, page.ID) drive(t, rig, chromedp.Evaluate[chromedp.Void](loseRenameReply), editTitle(), chromedp.KeyEvent(" Robotics"), chromedp.KeyEvent("\r"), chromedp.WaitVisible(`[mapa-title-note] [role="alert"]`)) renamed, _ := current(t, store, page.ID) if title, _ := titleOf(t, store, page.ID); title != "Halden Robotics" || renamed == first { t.Fatalf("the first rename did not land: %q", title) } if base := evaluate(t, rig, `mapaEditor.saver.base`); base != strconv.FormatInt(first, 10) { t.Fatalf("the saver's base moved on a lost reply: %s", base) } drive(t, rig, chromedp.Click(titleText), chromedp.KeyEvent("\r")) eventually(t, "Renamed, on the retry", func() bool { return evaluate(t, rig, `document.querySelector( "[mapa-title-note]").textContent`) == "Renamed. The old title still leads here." }) if base := evaluate(t, rig, `mapaEditor.saver.base`); base != strconv.FormatInt(renamed, 10) { t.Fatalf("the saver's base is %s, not the rename's %d", base, renamed) } if _, now := titleOf(t, store, page.ID); now != 2 { t.Fatalf("the retry wrote: %d versions", now) } }