1package webkit
2
3import (
4 "testing"
5
6 "github.com/bnema/dumber/internal/application/dto"
7 "github.com/bnema/dumber/internal/application/port"
8 purewebkit "github.com/bnema/puregotk/v4/webkit"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11)
12
13func TestResolvePopupFeatures(t *testing.T) {
14 t.Parallel()
15
16 tests := []struct {
17 name string
18 x, y, width, height int
19 toolbar, location bool
20 resizable bool
21 wantState dto.PopupFeatureState
22 wantPopup bool
23 }{
24 {name: "default properties are featureless", toolbar: true, location: true, resizable: true, wantState: dto.PopupFeaturesNone},
25 {name: "geometry is popup like", width: 640, height: 480, toolbar: true, location: true, resizable: true, wantState: dto.PopupFeaturesSpecified, wantPopup: true},
26 {name: "hidden toolbar is popup like", toolbar: false, location: true, resizable: true, wantState: dto.PopupFeaturesSpecified, wantPopup: true},
27 {name: "hidden location bar is popup like", toolbar: true, location: false, resizable: true, wantState: dto.PopupFeaturesSpecified, wantPopup: true},
28 {name: "disabled resizing is popup like", toolbar: true, location: true, resizable: false, wantState: dto.PopupFeaturesSpecified, wantPopup: true},
29 }
30
31 for _, tt := range tests {
32 t.Run(tt.name, func(t *testing.T) {
33 t.Parallel()
34 got := popupFeaturesFromWindowProperties(tt.x, tt.y, tt.width, tt.height, tt.toolbar, tt.location, tt.resizable)
35 assert.Equal(t, tt.wantState, got.State)
36 assert.Equal(t, tt.width > 0, got.WidthSet)
37 assert.Equal(t, tt.height > 0, got.HeightSet)
38 assert.True(t, got.ToolbarVisibilitySet)
39 assert.True(t, got.LocationbarVisibilitySet)
40 assert.True(t, got.ResizableSet)
41 assert.Equal(t, tt.wantPopup && (!tt.toolbar || !tt.location || !tt.resizable), got.IsPopup)
42 })
43 }
44}
45
46func TestMapPopupRequestClassifiesLinkBlankAsFeatureless(t *testing.T) {
47 t.Parallel()
48
49 blank := mapPopupRequest(PopupRequest{
50 TargetURI: "https://example.com/new", FrameName: " _BLANK ", ParentID: 7,
51 NavigationType: purewebkit.NavigationTypeLinkClickedValue,
52 })
53 assert.Equal(t, dto.BrowserEngineWebKit, blank.Engine)
54 assert.Equal(t, dto.WindowDispositionNewTab, blank.TargetDisposition)
55 assert.Equal(t, dto.PopupFeaturesNone, blank.PopupFeatures.State)
56}
57
58func TestMapPopupRequestDefersScriptBlankFeatures(t *testing.T) {
59 t.Parallel()
60
61 script := mapPopupRequest(PopupRequest{
62 TargetURI: "https://example.com/popup", FrameName: "_blank", ParentID: 7,
63 NavigationType: purewebkit.NavigationTypeOtherValue,
64 })
65 assert.Equal(t, dto.BrowserEngineWebKit, script.Engine)
66 assert.Equal(t, dto.WindowDispositionNewPopup, script.TargetDisposition)
67 assert.Equal(t, dto.PopupFeaturesUnknown, script.PopupFeatures.State)
68}
69
70func TestPopupCreateCallbackPreservesNilNamedReuseResult(t *testing.T) {
71 wv := &WebView{}
72 wv.SetCallbacks(&port.WebViewCallbacks{OnCreate: func(req port.PopupRequest) port.WebView {
73 require.Equal(t, dto.BrowserEngineWebKit, req.Engine)
74 require.Equal(t, "shared", req.FrameName)
75 return nil // Existing named context was navigated; block WebKit creation.
76 }})
77
78 result := wv.OnCreate(PopupRequest{
79 TargetURI: "https://example.com/reused", FrameName: "shared", ParentID: 7,
80 NavigationType: purewebkit.NavigationTypeOtherValue,
81 })
82
83 require.Nil(t, result)
84}
85
86func TestResolvePopupFeaturesNilWebViewIsUnknown(t *testing.T) {
87 t.Parallel()
88 var wv *WebView
89 assert.Equal(t, dto.PopupFeaturesUnknown, wv.ResolvePopupFeatures().State)
90}