| 1 | //go:build browser |
| 2 | |
| 3 | package headless |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strconv" |
| 10 | "testing" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/browsertest" |
| 13 | "github.com/chromedp/cdproto/cdp" |
| 14 | "github.com/chromedp/cdproto/emulation" |
| 15 | "github.com/chromedp/chromedp" |
| 16 | ) |
| 17 | |
| 18 | // Paul: clicking anywhere in the compose box, the dock around the pill, |
| 19 | // the pill's edge, the space beside the send arrow or between the + and |
| 20 | // the pill, puts the cursor in the message; the arrow and the + keep their |
| 21 | // own clicks. Desktop with a mouse at 1280, touch at 390. |
| 22 | func TestBrowserComposerFocus(t *testing.T) { |
| 23 | _, inst, r := localEnv(t) |
| 24 | r.start(t) |
| 25 | c := inst.Cookie() |
| 26 | for _, tc := range []struct { |
| 27 | width, height int64 |
| 28 | touch bool |
| 29 | }{{1280, 900, false}, {390, 844, true}} { |
| 30 | p := openPage(t, inst.URL, c.Name, c.Value, tc.width, tc.height, !tc.touch) |
| 31 | if tc.touch { |
| 32 | p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 33 | _, err := cdp.Call(ctx, target, emulation.SetTouchEmulationEnabled, emulation.SetTouchEmulationEnabledParams{Enabled: true, MaxTouchPoints: 5}) |
| 34 | return err |
| 35 | })) |
| 36 | } |
| 37 | click := func(x, y float64) { |
| 38 | t.Helper() |
| 39 | if tc.touch { |
| 40 | p.run(chromedp.TapXY(x, y)) |
| 41 | } else { |
| 42 | p.run(chromedp.MouseClickXY(x, y)) |
| 43 | } |
| 44 | } |
| 45 | point := func(expr string) (float64, float64) { |
| 46 | t.Helper() |
| 47 | var v []float64 |
| 48 | p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 49 | var err error |
| 50 | v, err = chromedp.Evaluate[[]float64](expr)(ctx, target) |
| 51 | return err |
| 52 | })) |
| 53 | return v[0], v[1] |
| 54 | } |
| 55 | for name, expr := range map[string]string{ |
| 56 | "outer padding": `(()=>{const b=document.querySelector('#chat-composer').getBoundingClientRect();const f=document.querySelector('#composer').getBoundingClientRect();return [f.right+(b.right-f.right)/2,f.y+f.height/2]})()`, |
| 57 | "below the pill": `(()=>{const b=document.querySelector('#chat-composer').getBoundingClientRect();const f=document.querySelector('[equipo-pill]').getBoundingClientRect();return [f.x+f.width/2,f.bottom+(b.bottom-f.bottom)/2]})()`, |
| 58 | "pill edge": `(()=>{const b=document.querySelector('[equipo-pill]').getBoundingClientRect();return [b.x+2,b.y+b.height/2]})()`, |
| 59 | "beside the arrow": `(()=>{const b=document.querySelector('[equipo-send]').getBoundingClientRect();return [b.x-3,b.y+b.height/2]})()`, |
| 60 | "between + and pill": `(()=>{const a=document.querySelector('[equipo-plus]').getBoundingClientRect();const f=document.querySelector('[equipo-pill]').getBoundingClientRect();return [(a.right+f.x)/2,f.y+f.height/2]})()`, |
| 61 | } { |
| 62 | // A tap in the narrow gap beside the + lands on the + (touch |
| 63 | // adjustment snaps taps to the nearest control), as on a phone. |
| 64 | if tc.touch && name == "between + and pill" { |
| 65 | continue |
| 66 | } |
| 67 | p.check(`(()=>{document.activeElement&&document.activeElement.blur();return document.activeElement!==document.querySelector('#chat-body')})()`, "blur") |
| 68 | x, y := point(expr) |
| 69 | click(x, y) |
| 70 | p.run(browsertest.Wait(`document.activeElement===document.querySelector('#chat-body')`)) |
| 71 | t.Logf("%dpx %s: focused", tc.width, name) |
| 72 | } |
| 73 | // The + keeps its own click: its menu opens and the message keeps |
| 74 | // no focus it did not have. |
| 75 | p.check(`(()=>{document.activeElement&&document.activeElement.blur();return true})()`, "blur") |
| 76 | x, y := point(`(()=>{const b=document.querySelector('[equipo-plus]').getBoundingClientRect();return [b.x+b.width/2,b.y+b.height/2]})()`) |
| 77 | click(x, y) |
| 78 | p.run(browsertest.Wait(`!document.querySelector('[equipo-add-menu]').hidden && document.activeElement!==document.querySelector('#chat-body')`)) |
| 79 | click(x, y) |
| 80 | p.run(browsertest.Wait(`document.querySelector('[equipo-add-menu]').hidden`)) |
| 81 | // Send keeps its own click: the message goes. |
| 82 | text := "composer focus " + map[bool]string{true: "touch", false: "mouse"}[tc.touch] |
| 83 | p.check(`(()=>{const b=document.querySelector('#chat-body');b.value=`+jsonString(text)+`;b.dispatchEvent(new Event('input',{bubbles:true}));return true})()`, "type") |
| 84 | p.run(browsertest.Wait(`getComputedStyle(document.querySelector('[equipo-send]')).visibility==='visible' && getComputedStyle(document.querySelector('[equipo-send]')).opacity==='1'`)) |
| 85 | x, y = point(`(()=>{const b=document.querySelector('[equipo-send]').getBoundingClientRect();return [b.x+b.width/2,b.y+b.height/2]})()`) |
| 86 | click(x, y) |
| 87 | p.run(browsertest.Wait(`[...document.querySelectorAll('[equipo-message="own"] [equipo-message-body]')].some(e=>e.textContent===` + jsonString(text) + `)`)) |
| 88 | p.stop() |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // effectiveBackground is the colour painted behind an element: its own |
| 93 | // background, else its nearest ancestor's. |
| 94 | const effectiveBackground = `(el=>{for(;el;el=el.parentElement){const c=getComputedStyle(el).backgroundColor;if(c!=='rgba(0, 0, 0, 0)'&&c!=='transparent')return c}return getComputedStyle(document.documentElement).backgroundColor})` |
| 95 | |
| 96 | // Paul: the chat area's background matches the composer's, in both |
| 97 | // themes: the dock is the feed's colour (no band, no hard edge), a short |
| 98 | // fade from transparent to that colour separates messages at its top, |
| 99 | // and the compose box keeps its own field surface and border. Set |
| 100 | // EQUIPO_SCREENSHOTS to a directory to save composer-*.png. |
| 101 | func TestBrowserComposerDock(t *testing.T) { |
| 102 | _, inst, r := localEnv(t) |
| 103 | r.start(t) |
| 104 | c := inst.Cookie() |
| 105 | for _, tc := range []struct { |
| 106 | width, height int64 |
| 107 | pointer bool |
| 108 | }{{1280, 900, true}, {390, 844, false}} { |
| 109 | p := openPage(t, inst.URL, c.Name, c.Value, tc.width, tc.height, tc.pointer) |
| 110 | for i := 0; i < 8; i++ { |
| 111 | p.send("A message to fill the feed, number " + jsonNumber(float64(i)) + ".") |
| 112 | } |
| 113 | p.run(browsertest.Wait(`document.querySelectorAll('[equipo-message="own"]').length>=8 && document.querySelectorAll('[equipo-message="incoming"] [equipo-markdown]').length>=1`)) |
| 114 | for _, theme := range []string{"light", "dark"} { |
| 115 | p.check(`(()=>{document.documentElement.dataset.theme=`+jsonString(theme)+`;const s=document.querySelector('[equipo-scroll]');s.scrollTop=s.scrollHeight;return true})()`, "theme") |
| 116 | p.check(`(`+effectiveBackground+`)(document.querySelector('#chat-composer'))===(`+effectiveBackground+`)(document.querySelector('[equipo-scroll]'))`, theme+": the dock is not the feed's colour") |
| 117 | p.check(`(()=>{const own=getComputedStyle(document.querySelector('#chat-composer')).backgroundColor;return own==='rgba(0, 0, 0, 0)'||own===(`+effectiveBackground+`)(document.querySelector('[equipo-scroll]'))})()`, theme+": the dock paints a band") |
| 118 | p.check(`(()=>{const f=getComputedStyle(document.querySelector('#chat-composer'),'::before');return f.content!=='none'&&f.backgroundImage.includes('linear-gradient')&&f.backgroundImage.includes((`+effectiveBackground+`)(document.querySelector('[equipo-scroll]')))&&f.pointerEvents==='none'&&parseFloat(f.height)>0&&parseFloat(f.height)<=40})()`, theme+": no short fade at the dock's top") |
| 119 | p.check(`(()=>{const b=getComputedStyle(document.querySelector('[equipo-pill]'));return parseFloat(b.borderTopWidth)>=1&&b.backgroundColor!=='rgba(0, 0, 0, 0)'})()`, theme+": the compose box lost its field") |
| 120 | if dir := os.Getenv("EQUIPO_SCREENSHOTS"); dir != "" { |
| 121 | var shot []byte |
| 122 | p.run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 123 | var err error |
| 124 | shot, err = chromedp.CaptureScreenshot()(ctx, target) |
| 125 | return err |
| 126 | })) |
| 127 | if err := os.WriteFile(filepath.Join(dir, "composer-"+strconv.FormatInt(tc.width, 10)+"-"+theme+".png"), shot, 0644); err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | p.stop() |
| 133 | } |
| 134 | } |
| 135 | |