1package cef
2
3import (
4 "strings"
5 "testing"
6)
7
8func TestCEFScaleProbeScriptCapturesViewportMetrics(t *testing.T) {
9 for _, want := range []string{
10 "window.devicePixelRatio",
11 "window.innerWidth",
12 "document.documentElement.clientWidth",
13 "window.visualViewport",
14 "width:100px;height:100px",
15 "[SCALE-PROBE]",
16 } {
17 if !strings.Contains(cefScaleProbeScript, want) {
18 t.Fatalf("cefScaleProbeScript missing %q", want)
19 }
20 }
21}
22
23func TestShouldRunCEFScaleProbeSkipsIntermediateHTTPStatusZero(t *testing.T) {
24 if shouldRunCEFScaleProbe("https://example.com", 0) {
25 t.Fatal("probe should skip intermediate HTTP status 0 for http URLs")
26 }
27 if !shouldRunCEFScaleProbe("https://example.com", 200) {
28 t.Fatal("probe should run for successful HTTP load")
29 }
30 if shouldRunCEFScaleProbe("about:blank", 200) {
31 t.Fatal("probe should skip about:blank")
32 }
33 if !shouldRunCEFScaleProbe("file:///tmp/probe.html", 0) {
34 t.Fatal("probe should allow non-HTTP status 0 loads")
35 }
36}
37
38func TestCEFScaleProbeSnapshotNormalizesMissingBridge(t *testing.T) {
39 s := cefScaleProbeSnapshot(nil)
40 if s.SurfaceWidth != 1 || s.SurfaceHeight != 1 {
41 t.Fatalf("surface size = %dx%d, want 1x1", s.SurfaceWidth, s.SurfaceHeight)
42 }
43 if s.SurfaceScale != 1 || s.PageZoomCompensation != 1 {
44 t.Fatalf("scales = surface %v compensation %v, want 1/1", s.SurfaceScale, s.PageZoomCompensation)
45 }
46 if s.UserZoom != 1 || s.InternalCEFFactor != 1 {
47 t.Fatalf("zoom = user %v internal %v, want 1/1", s.UserZoom, s.InternalCEFFactor)
48 }
49}
50
51func TestCEFScaleProbeSnapshotDerivesInternalFactorFromContract(t *testing.T) {
52 wv := &WebView{zoomCompensation: zoomCompensationStub{compensation: 1.75}}
53 wv.zoomFactor.Store(1.3)
54
55 s := cefScaleProbeSnapshot(wv)
56
57 if s.UserZoom != 1.3 {
58 t.Fatalf("user zoom = %v, want 1.3", s.UserZoom)
59 }
60 if s.PageZoomCompensation != 1.75 {
61 t.Fatalf("compensation = %v, want 1.75", s.PageZoomCompensation)
62 }
63 if s.InternalCEFFactor != 2.275 {
64 t.Fatalf("internal CEF factor = %v, want user zoom x compensation = 2.275", s.InternalCEFFactor)
65 }
66 fields := s.logFields()
67 if _, ok := fields["page_zoom_compensation"]; !ok {
68 t.Fatalf("probe fields %v missing page_zoom_compensation", fields)
69 }
70 if _, ok := fields["osr_backing_scale"]; ok {
71 t.Fatalf("probe fields %v must not carry a separate backing-scale formula", fields)
72 }
73}