native_popup_window_test.go RawHistoryFolder
1package ui
2
3import (
4 "context"
5 "errors"
6 "testing"
7
8 "github.com/bnema/dumber/internal/application/port"
9 portmocks "github.com/bnema/dumber/internal/application/port/mocks"
10 "github.com/bnema/dumber/internal/ui/coordinator/content"
11 layoutmocks "github.com/bnema/dumber/internal/ui/layout/mocks"
12 "github.com/bnema/dumber/internal/ui/window"
13 "github.com/bnema/puregotk/v4/gtk"
14 "github.com/stretchr/testify/assert"
15 "github.com/stretchr/testify/require"
16)
17
18type popupDestroySpy struct {
19 destroyed bool
20}
21
22func (p *popupDestroySpy) Destroy() {
23 p.destroyed = true
24}
25
26func TestPrepareNativePopupContentWidget_ExpandsWrappedWidget(t *testing.T) {
27 widget := layoutmocks.NewMockWidget(t)
28 gtkWidget := >k.Widget{}
29 widget.EXPECT().GtkWidget().Return(gtkWidget).Once()
30 widget.EXPECT().SetHexpand(true).Once()
31 widget.EXPECT().SetVexpand(true).Once()
32
33 got, err := prepareNativePopupContentWidget(widget)
34 require.NoError(t, err)
35 assert.Same(t, gtkWidget, got)
36}
37
38func TestPrepareNativePopupContentWidget_ErrorsWhenWrappedGTKWidgetMissing(t *testing.T) {
39 widget := layoutmocks.NewMockWidget(t)
40 widget.EXPECT().GtkWidget().Return(nil).Once()
41
42 got, err := prepareNativePopupContentWidget(widget)
43 require.Error(t, err)
44 assert.Nil(t, got)
45}
46
47func TestOpenNativePopupWindow_DestroysWebViewWhenPopupShellCreationFails(t *testing.T) {
48 oldNewPopupWindow := newPopupWindow
49 t.Cleanup(func() {
50 newPopupWindow = oldNewPopupWindow
51 })
52
53 expectedErr := errors.New("boom")
54 newPopupWindow = func(context.Context, *gtk.Application) (*window.PopupWindow, error) {
55 return nil, expectedErr
56 }
57
58 wv := portmocks.NewMockWebView(t)
59 wv.EXPECT().IsDestroyed().Return(false).Once()
60 wv.EXPECT().Destroy().Once()
61
62 app := &App{gtkApp: >k.Application{}}
63 err := app.openNativePopupWindow(context.Background(), content.NativePopupInput{PopupWebView: wv})
64 require.ErrorIs(t, err, expectedErr)
65}
66
67func TestDestroyFailedNativePopupSetup_DestroysWebViewAndShell(t *testing.T) {
68 wv := portmocks.NewMockWebView(t)
69 wv.EXPECT().IsDestroyed().Return(false).Once()
70 wv.EXPECT().Destroy().Once()
71
72 shell := &popupDestroySpy{}
73 destroyFailedNativePopupSetup(shell, wv)
74
75 assert.True(t, shell.destroyed)
76}
77
78func TestReleaseNativePopupWindow_DestroysWebViewAndRemovesState(t *testing.T) {
79 wv := portmocks.NewMockWebView(t)
80 wv.EXPECT().IsDestroyed().Return(false).Once()
81 wv.EXPECT().Destroy().Once()
82
83 app := &App{nativePopupWindows: map[port.WebViewID]*nativePopupWindow{
84 port.WebViewID(1): {
85 popupID: port.WebViewID(1),
86 webView: wv,
87 },
88 }}
89
90 app.releaseNativePopupWindow(port.WebViewID(1), false, false)
91 _, ok := app.nativePopupWindows[port.WebViewID(1)]
92 assert.False(t, ok)
93}