| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/chromedp/chromedp" |
| 11 | ) |
| 12 | |
| 13 | // The in-page title when its window loses focus, when the page closes, |
| 14 | // and when a rename's reply is lost (Fable's L1 and L3 on ux/fixes-1; |
| 15 | // build note 79). |
| 16 | |
| 17 | // away is the window or tab losing focus while the title is being |
| 18 | // edited, as the page sees it: document.hasFocus() false (or the page |
| 19 | // hidden), then the field's blur. Headless Chromium keeps every tab |
| 20 | // focused, so the page is told what a real Cmd-Tab tells it. |
| 21 | func away(how string) chromedp.Action[chromedp.Void] { |
| 22 | return chromedp.Evaluate[chromedp.Void](`(function (how) { |
| 23 | if (how === "hidden") { |
| 24 | Object.defineProperty(document, "visibilityState", |
| 25 | { configurable: true, get: function () { return "hidden"; } }); |
| 26 | } else { |
| 27 | document.hasFocus = function () { return false; }; |
| 28 | } |
| 29 | document.querySelector("[mapa-title-text]").blur(); |
| 30 | window.dispatchEvent(new Event("blur")); |
| 31 | })("` + how + `")`) |
| 32 | } |
| 33 | |
| 34 | // back is the person coming back: the window has focus again, and the |
| 35 | // browser hands it its focus. |
| 36 | const back = `(function () { |
| 37 | delete document.hasFocus; |
| 38 | delete document.visibilityState; |
| 39 | window.dispatchEvent(new Event("focus")); |
| 40 | return String(document.activeElement === |
| 41 | document.querySelector("[mapa-title-text]")); |
| 42 | })()` |
| 43 | |
| 44 | // notRenamed fails if the page's title or its versions moved, after the |
| 45 | // busy rule's hold, so a rename sent would have landed. |
| 46 | func notRenamed(t *testing.T, check func() (string, int), title string, |
| 47 | versions int, why string) { |
| 48 | t.Helper() |
| 49 | time.Sleep(1200 * time.Millisecond) |
| 50 | if got, now := check(); got != title || now != versions { |
| 51 | t.Fatalf("%s renamed: %q, %d versions, not %d", why, got, now, |
| 52 | versions) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestTitleLosingTheWindowDoesNotRename(t *testing.T) { |
| 57 | rig, store := browserRig(t, 0) |
| 58 | for _, how := range []string{"unfocused", "hidden"} { |
| 59 | title := "Halden " + how |
| 60 | page := titledPage(t, rig, store, title, "One.") |
| 61 | check := func() (string, int) { return titleOf(t, store, page.ID) } |
| 62 | _, versions := check() |
| 63 | drive(t, rig, editTitle(), chromedp.KeyEvent(" Rob"), away(how)) |
| 64 | notRenamed(t, check, title, versions, how+": leaving the window") |
| 65 | if typed := text(t, rig, "h1[mapa-title]"); typed != title+" Rob" { |
| 66 | t.Fatalf("%s: the edit was not kept open: %q", how, typed) |
| 67 | } |
| 68 | if focused := evaluate(t, rig, back); focused != "true" { |
| 69 | t.Fatalf("%s: coming back did not give the field its focus", |
| 70 | how) |
| 71 | } |
| 72 | // Coming back and leaving the field renames: Enter, then a |
| 73 | // click into the page's text. |
| 74 | drive(t, rig, chromedp.KeyEvent("otics")) |
| 75 | if how == "unfocused" { |
| 76 | drive(t, rig, chromedp.KeyEvent("\r")) |
| 77 | } else { |
| 78 | drive(t, rig, chromedp.Click(mounted)) |
| 79 | } |
| 80 | eventually(t, how+": the rename once back", func() bool { |
| 81 | got, now := check() |
| 82 | return got == title+" Robotics" && now == versions+1 |
| 83 | }) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // Closing the page with the title edited renames it, as closing commits |
| 88 | // the page's text: here after the window lost focus, so no blur sent |
| 89 | // it first. A rename already out when the page closes lands once. |
| 90 | func TestTitleClosingThePageRenamesAnEditedTitle(t *testing.T) { |
| 91 | rig, store := browserRig(t, 0) |
| 92 | page := titledPage(t, rig, store, "Halden", "One.") |
| 93 | check := func() (string, int) { return titleOf(t, store, page.ID) } |
| 94 | _, versions := check() |
| 95 | drive(t, rig, editTitle(), chromedp.KeyEvent(" Robotics"), |
| 96 | away("unfocused")) |
| 97 | notRenamed(t, check, "Halden", versions, "leaving the window") |
| 98 | drive(t, rig, chromedp.Navigate(rig.Origin+"/pages")) |
| 99 | eventually(t, "the closing rename", func() bool { |
| 100 | got, now := check() |
| 101 | return got == "Halden Robotics" && now == versions+1 |
| 102 | }) |
| 103 | acme := titledPage(t, rig, store, "Acme", "Two.") |
| 104 | _, before := titleOf(t, store, acme.ID) |
| 105 | drive(t, rig, editTitle(), chromedp.KeyEvent(" Ltd"), |
| 106 | chromedp.KeyEvent("\r"), chromedp.Navigate(rig.Origin+"/pages")) |
| 107 | time.Sleep(1500 * time.Millisecond) |
| 108 | if got, now := titleOf(t, store, acme.ID); got != "Acme Ltd" || |
| 109 | now != before+1 { |
| 110 | t.Fatalf("a rename out at close: %q, %d versions (%d before)", |
| 111 | got, now, before) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // loseRenameReply makes the next rename's reply never arrive: the POST |
| 116 | // reaches the server and lands, and the page sees a network error. |
| 117 | const loseRenameReply = `(function () { |
| 118 | var real = window.fetch, lost = false; |
| 119 | window.fetch = function (url, init) { |
| 120 | return real(url, init).then(function (response) { |
| 121 | if (!lost && String(url).endsWith("/rename")) { |
| 122 | lost = true; |
| 123 | throw new TypeError("Failed to fetch"); |
| 124 | } |
| 125 | return response; |
| 126 | }); |
| 127 | }; |
| 128 | })()` |
| 129 | |
| 130 | // A rename that landed but whose reply was lost says "Not renamed"; |
| 131 | // Enter again is answered with that rename's revisions, so it says |
| 132 | // Renamed and the saver's base follows it, as if the first reply had |
| 133 | // come. |
| 134 | func TestTitleARetriedRenameThatLandedMovesTheBase(t *testing.T) { |
| 135 | rig, store := browserRig(t, 0) |
| 136 | page := titledPage(t, rig, store, "Halden", "One.") |
| 137 | first, _ := current(t, store, page.ID) |
| 138 | drive(t, rig, chromedp.Evaluate[chromedp.Void](loseRenameReply), |
| 139 | editTitle(), chromedp.KeyEvent(" Robotics"), chromedp.KeyEvent("\r"), |
| 140 | chromedp.WaitVisible(`[mapa-title-note] [role="alert"]`)) |
| 141 | renamed, _ := current(t, store, page.ID) |
| 142 | if title, _ := titleOf(t, store, page.ID); title != "Halden Robotics" || |
| 143 | renamed == first { |
| 144 | t.Fatalf("the first rename did not land: %q", title) |
| 145 | } |
| 146 | if base := evaluate(t, rig, `mapaEditor.saver.base`); base != |
| 147 | strconv.FormatInt(first, 10) { |
| 148 | t.Fatalf("the saver's base moved on a lost reply: %s", base) |
| 149 | } |
| 150 | drive(t, rig, chromedp.Click(titleText), chromedp.KeyEvent("\r")) |
| 151 | eventually(t, "Renamed, on the retry", func() bool { |
| 152 | return evaluate(t, rig, `document.querySelector( |
| 153 | "[mapa-title-note]").textContent`) == |
| 154 | "Renamed. The old title still leads here." |
| 155 | }) |
| 156 | if base := evaluate(t, rig, `mapaEditor.saver.base`); base != |
| 157 | strconv.FormatInt(renamed, 10) { |
| 158 | t.Fatalf("the saver's base is %s, not the rename's %d", base, |
| 159 | renamed) |
| 160 | } |
| 161 | if _, now := titleOf(t, store, page.ID); now != 2 { |
| 162 | t.Fatalf("the retry wrote: %d versions", now) |
| 163 | } |
| 164 | } |
| 165 | |