1package ui
2
3import (
4 "context"
5 "errors"
6 "testing"
7 "time"
8
9 "github.com/bnema/dumber/internal/application/port"
10 portmocks "github.com/bnema/dumber/internal/application/port/mocks"
11 "github.com/bnema/dumber/internal/shared/syncdispatch"
12 "github.com/bnema/dumber/internal/ui/coordinator/content"
13 layoutmocks "github.com/bnema/dumber/internal/ui/layout/mocks"
14 "github.com/bnema/dumber/internal/ui/window"
15 "github.com/bnema/puregotk/v4/gtk"
16 "github.com/stretchr/testify/assert"
17 "github.com/stretchr/testify/require"
18)
19
20type popupDestroySpy struct {
21 destroyed int
22 detached int
23 closed int
24 shown int
25}
26
27func (p *popupDestroySpy) Destroy() { p.destroyed++ }
28func (p *popupDestroySpy) DetachContent() *gtk.Widget { p.detached++; return >k.Widget{} }
29func (p *popupDestroySpy) Close() { p.closed++ }
30func (p *popupDestroySpy) Show() { p.shown++ }
31
32func TestPrepareNativePopupContentWidget_ExpandsWrappedWidget(t *testing.T) {
33 widget := layoutmocks.NewMockWidget(t)
34 gtkWidget := >k.Widget{}
35 widget.EXPECT().GtkWidget().Return(gtkWidget).Once()
36 widget.EXPECT().SetHexpand(true).Once()
37 widget.EXPECT().SetVexpand(true).Once()
38
39 got, err := prepareNativePopupContentWidget(widget)
40 require.NoError(t, err)
41 assert.Same(t, gtkWidget, got)
42}
43
44func TestPrepareNativePopupContentWidget_ErrorsWhenWrappedGTKWidgetMissing(t *testing.T) {
45 widget := layoutmocks.NewMockWidget(t)
46 widget.EXPECT().GtkWidget().Return(nil).Once()
47
48 got, err := prepareNativePopupContentWidget(widget)
49 require.Error(t, err)
50 assert.Nil(t, got)
51}
52
53func TestOpenNativePopupWindowLeavesWebViewOwnedByCallerOnSetupFailure(t *testing.T) {
54 oldNewPopupWindow := newPopupWindow
55 t.Cleanup(func() {
56 newPopupWindow = oldNewPopupWindow
57 })
58
59 expectedErr := errors.New("boom")
60 newPopupWindow = func(context.Context, *gtk.Application) (*window.PopupWindow, error) {
61 return nil, expectedErr
62 }
63
64 wv := portmocks.NewMockWebView(t)
65 app := &App{gtkApp: >k.Application{}}
66 err := app.openNativePopupWindow(context.Background(), content.NativePopupInput{PopupWebView: wv})
67 require.ErrorIs(t, err, expectedErr)
68}
69
70func TestDestroyFailedNativePopupSetupDestroysOnlyShell(t *testing.T) {
71 shell := &popupDestroySpy{}
72 destroyFailedNativePopupSetup(shell)
73 assert.Equal(t, 1, shell.destroyed)
74}
75
76func TestDispatchNativePopupLifecycleSkipsWorkWhenDispatchTimesOutBeforeStart(t *testing.T) {
77 var ran bool
78 app := &App{
79 dispatchOnMainThread: func(label string, fn func()) syncdispatch.SyncDispatchResult {
80 return syncdispatch.SyncDispatchResult{Label: label, Status: syncdispatch.SyncDispatchTimedOut, Elapsed: 5 * time.Millisecond}
81 },
82 }
83
84 app.dispatchNativePopupLifecycle("ui.native_popup.close", port.WebViewID(99), func() { ran = true })
85
86 assert.False(t, ran)
87}
88
89func TestDispatchNativePopupLifecycleTreatsCompletedAfterTimeoutAsCompleted(t *testing.T) {
90 var ran bool
91 app := &App{
92 dispatchOnMainThread: func(label string, fn func()) syncdispatch.SyncDispatchResult {
93 fn()
94 return syncdispatch.SyncDispatchResult{Label: label, Status: syncdispatch.SyncDispatchCompletedAfterTimeout, Elapsed: 10 * time.Millisecond}
95 },
96 }
97
98 app.dispatchNativePopupLifecycle("ui.native_popup.close", port.WebViewID(99), func() { ran = true })
99
100 assert.True(t, ran)
101}
102
103func TestDetachNativePopupWindowRemovesWidgetWithoutDestroyingWebView(t *testing.T) {
104 wv := portmocks.NewMockWebView(t)
105 shell := &popupDestroySpy{}
106 app := &App{nativePopupWindows: map[port.WebViewID]*nativePopupWindow{
107 1: {popupID: 1, webView: wv, popupWindow: shell},
108 }}
109
110 detached := app.releaseNativePopupWindow(1, nativePopupReleaseDetach)
111 assert.Same(t, wv, detached)
112 assert.Equal(t, 1, shell.detached)
113 assert.Equal(t, 1, shell.destroyed)
114}
115
116func TestNativePopupAbortTransfersEligibleWebViewToBrowserFallback(t *testing.T) {
117 wv := portmocks.NewMockWebView(t)
118 shell := &popupDestroySpy{}
119 app := &App{nativePopupWindows: map[port.WebViewID]*nativePopupWindow{
120 1: {popupID: 1, webView: wv, popupWindow: shell},
121 }}
122 callbackCalls := 0
123 app.abortNativePopupWindow(context.Background(), 1, content.NativePopupInput{
124 AllowBrowserWindowFallback: true,
125 OnNativeHostAbort: func(_ context.Context, got port.WebView) bool {
126 callbackCalls++
127 assert.Same(t, wv, got)
128 assert.Equal(t, 1, shell.detached, "widget must detach before fallback adoption")
129 return true
130 },
131 })
132
133 assert.Equal(t, 1, callbackCalls)
134 assert.Equal(t, 1, shell.destroyed)
135}
136
137func TestNativePopupAbortDestroysOpenerRequiredWebView(t *testing.T) {
138 wv := portmocks.NewMockWebView(t)
139 wv.EXPECT().IsDestroyed().Return(false).Once()
140 wv.EXPECT().Destroy().Once()
141 shell := &popupDestroySpy{}
142 app := &App{nativePopupWindows: map[port.WebViewID]*nativePopupWindow{
143 1: {popupID: 1, webView: wv, popupWindow: shell},
144 }}
145 app.abortNativePopupWindow(context.Background(), 1, content.NativePopupInput{
146 AllowBrowserWindowFallback: false,
147 OnNativeHostAbort: func(context.Context, port.WebView) bool { return false },
148 })
149 assert.Equal(t, 1, shell.detached)
150}
151
152func TestReleaseNativePopupWindowDestroysOnlyOnce(t *testing.T) {
153 wv := portmocks.NewMockWebView(t)
154 wv.EXPECT().IsDestroyed().Return(false).Once()
155 wv.EXPECT().Destroy().Once()
156 shell := &popupDestroySpy{}
157 app := &App{nativePopupWindows: map[port.WebViewID]*nativePopupWindow{
158 1: {popupID: 1, webView: wv, popupWindow: shell},
159 }}
160
161 app.releaseNativePopupWindow(1, nativePopupReleaseDestroy)
162 app.releaseNativePopupWindow(1, nativePopupReleaseDestroy)
163 _, ok := app.nativePopupWindows[1]
164 assert.False(t, ok)
165 assert.Equal(t, 1, shell.destroyed)
166}