v0.30.0 eb3068f

webview_navigation_test.go RawHistoryFolder

1package cef
2
3import (
4	"context"
5	"testing"
6	"time"
7
8	purecef "github.com/bnema/purego-cef/cef"
9	cefmocks "github.com/bnema/purego-cef/cef/mocks"
10	"github.com/stretchr/testify/require"
11)
12
13func TestWebViewReplayPendingNavigation_LoadsQueuedURIWhenMainFrameAvailable(t *testing.T) {
14	browser := cefmocks.NewMockBrowser(t)
15	frame := cefmocks.NewMockFrame(t)
16	frame.EXPECT().GetURL().Return("").Once()
17	frame.EXPECT().LoadURL("https://github.com/bnema").Once()
18	browser.EXPECT().GetMainFrame().Return(frame).Once()
19
20	wv := &WebView{ctx: context.Background(), browser: browser, pendingURI: "https://github.com/bnema"}
21	wv.replayPendingNavigation(0)
22
23	wv.mu.RLock()
24	defer wv.mu.RUnlock()
25	require.Equal(t, "https://github.com/bnema", wv.pendingURI)
26}
27
28func TestWebViewReplayPendingNavigation_RetriesWhenMainFrameUnavailable(t *testing.T) {
29	browser := cefmocks.NewMockBrowser(t)
30	browser.EXPECT().GetMainFrame().Return(purecef.Frame(nil)).Once()
31
32	oldTask := cefNewTask
33	oldDelayed := cefPostDelayedTask
34	defer func() {
35		cefNewTask = oldTask
36		cefPostDelayedTask = oldDelayed
37	}()
38	cefNewTask = func(task purecef.Task) purecef.Task { return task }
39
40	scheduled := false
41	cefPostDelayedTask = func(threadID purecef.ThreadID, task purecef.Task, delayMs int64) int32 {
42		require.Equal(t, purecef.ThreadIDTidUi, threadID)
43		require.NotNil(t, task)
44		require.Equal(t, int64(pendingNavigationRetryDelay/time.Millisecond), delayMs)
45		scheduled = true
46		return 1
47	}
48
49	wv := &WebView{ctx: context.Background(), browser: browser, pendingURI: "https://github.com/bnema"}
50	wv.replayPendingNavigation(0)
51
52	require.True(t, scheduled)
53	wv.mu.RLock()
54	defer wv.mu.RUnlock()
55	require.Equal(t, "https://github.com/bnema", wv.pendingURI)
56}
57
58func TestWebViewSchedulePendingNavigationReplay_RetriesWhenTaskPostFails(t *testing.T) {
59	browser := cefmocks.NewMockBrowser(t)
60
61	oldTask := cefNewTask
62	oldPost := cefPostTask
63	oldDelayed := cefPostDelayedTask
64	oldAfter := cefScheduleAfter
65	defer func() {
66		cefNewTask = oldTask
67		cefPostTask = oldPost
68		cefPostDelayedTask = oldDelayed
69		cefScheduleAfter = oldAfter
70	}()
71	cefNewTask = func(task purecef.Task) purecef.Task { return task }
72
73	retried := false
74	cefPostTask = func(threadID purecef.ThreadID, task purecef.Task) int32 {
75		require.Equal(t, purecef.ThreadIDTidUi, threadID)
76		require.NotNil(t, task)
77		return 0
78	}
79	cefScheduleAfter = func(delay time.Duration, fn func()) {
80		require.Equal(t, pendingNavigationRetryDelay, delay)
81		fn()
82	}
83	cefPostDelayedTask = func(threadID purecef.ThreadID, task purecef.Task, delayMs int64) int32 {
84		require.Equal(t, purecef.ThreadIDTidUi, threadID)
85		require.NotNil(t, task)
86		require.Equal(t, int64(pendingNavigationRetryDelay/time.Millisecond), delayMs)
87		retried = true
88		return 1
89	}
90
91	wv := &WebView{ctx: context.Background(), browser: browser, pendingURI: "https://github.com/bnema"}
92	wv.schedulePendingNavigationReplay(0)
93
94	require.True(t, retried)
95}
96
97func TestWebViewReplayPendingNavigation_UsesCurrentBrowserAtExecutionTime(t *testing.T) {
98	staleBrowser := cefmocks.NewMockBrowser(t)
99	activeBrowser := cefmocks.NewMockBrowser(t)
100	frame := cefmocks.NewMockFrame(t)
101	frame.EXPECT().GetURL().Return("").Once()
102	frame.EXPECT().LoadURL("https://github.com/bnema").Once()
103	activeBrowser.EXPECT().GetMainFrame().Return(frame).Once()
104
105	oldTask := cefNewTask
106	oldPost := cefPostTask
107	defer func() {
108		cefNewTask = oldTask
109		cefPostTask = oldPost
110	}()
111	var scheduled purecef.Task
112	cefNewTask = func(task purecef.Task) purecef.Task { return task }
113	cefPostTask = func(threadID purecef.ThreadID, task purecef.Task) int32 {
114		require.Equal(t, purecef.ThreadIDTidUi, threadID)
115		scheduled = task
116		return 1
117	}
118
119	wv := &WebView{ctx: context.Background(), browser: staleBrowser, pendingURI: "https://github.com/bnema"}
120	wv.schedulePendingNavigationReplay(0)
121	wv.mu.Lock()
122	wv.browser = activeBrowser
123	wv.mu.Unlock()
124
125	require.NotNil(t, scheduled)
126	scheduled.Execute()
127}
128
129func TestWebViewLoadURI_QueuesPendingNavigationReplayForExistingBrowser(t *testing.T) {
130	browser := cefmocks.NewMockBrowser(t)
131	frame := cefmocks.NewMockFrame(t)
132	browser.EXPECT().GetMainFrame().Return(frame).Once()
133	frame.EXPECT().GetURL().Return("").Once()
134	frame.EXPECT().LoadURL("github.com/bnema").Once()
135
136	oldTask := cefNewTask
137	oldPost := cefPostTask
138	defer func() {
139		cefNewTask = oldTask
140		cefPostTask = oldPost
141	}()
142	cefNewTask = func(task purecef.Task) purecef.Task { return task }
143	cefPostTask = func(threadID purecef.ThreadID, task purecef.Task) int32 {
144		require.Equal(t, purecef.ThreadIDTidUi, threadID)
145		require.NotNil(t, task)
146		task.Execute()
147		return 1
148	}
149
150	wv := &WebView{ctx: context.Background(), browser: browser}
151	require.NoError(t, wv.LoadURI(context.Background(), "github.com/bnema"))
152
153	wv.mu.RLock()
154	defer wv.mu.RUnlock()
155	require.Equal(t, "github.com/bnema", wv.pendingURI)
156}
157
158func TestWebViewUpdateURI_ClearsMatchingPendingNavigation(t *testing.T) {
159	wv := &WebView{ctx: context.Background(), pendingURI: "https://github.com/bnema"}
160
161	wv.updateURI("https://github.com/bnema")
162
163	require.Empty(t, wv.pendingNavigationURI())
164}
165
166func TestWebViewUpdateLoadState_ClearsStalePendingNavigationAfterStartedRedirectCompletes(t *testing.T) {
167	wv := &WebView{ctx: context.Background()}
168	pending := "https://duckduckgo.com/?q=pi+agent+reddit"
169	startedAt := time.Now().Add(-time.Second)
170	wv.mu.Lock()
171	wv.setPendingNavigationLocked(pending, startedAt.Add(-time.Millisecond))
172	wv.markPendingNavigationStartedLocked(pending, startedAt)
173	wv.mu.Unlock()
174	wv.updateURI("https://www.reddit.com/r/somewhere/")
175
176	wv.updateLoadState(false, true, false)
177
178	require.Empty(t, wv.pendingNavigationURI())
179}
180
181func TestWebViewUpdateLoadState_KeepsPendingNavigationBeforeReplayStarts(t *testing.T) {
182	wv := &WebView{ctx: context.Background()}
183	pending := "https://duckduckgo.com/?q=pi+agent+reddit"
184	wv.updateURI("https://old.example/still-finishing")
185	wv.mu.Lock()
186	wv.setPendingNavigationLocked(pending, time.Now())
187	wv.mu.Unlock()
188
189	wv.updateLoadState(false, true, false)
190
191	require.Equal(t, pending, wv.pendingNavigationURI())
192}
193
194func TestWebViewUpdateLoadState_DoesNotClearPendingNavigationWhenOnlyOlderAddressWasObserved(t *testing.T) {
195	wv := &WebView{ctx: context.Background()}
196	pending := "https://duckduckgo.com/?q=pi+agent+reddit"
197	observedAt := time.Now()
198	wv.mu.Lock()
199	wv.uri = "https://old.example/final"
200	wv.loadDiagLastAddressAt = observedAt
201	wv.setPendingNavigationLocked(pending, observedAt.Add(time.Millisecond))
202	wv.markPendingNavigationStartedLocked(pending, observedAt.Add(time.Millisecond))
203	wv.mu.Unlock()
204
205	wv.updateLoadState(false, true, false)
206
207	require.Equal(t, pending, wv.pendingNavigationURI())
208}
209
210func TestWebViewUpdateLoadState_KeepsPendingNavigationWhileLoading(t *testing.T) {
211	wv := &WebView{ctx: context.Background()}
212	pending := "https://duckduckgo.com/?q=pi+agent+reddit"
213	startedAt := time.Now().Add(-time.Second)
214	wv.mu.Lock()
215	wv.setPendingNavigationLocked(pending, startedAt.Add(-time.Millisecond))
216	wv.markPendingNavigationStartedLocked(pending, startedAt)
217	wv.mu.Unlock()
218	wv.updateURI("https://www.reddit.com/r/somewhere/")
219
220	wv.updateLoadState(true, true, false)
221
222	require.Equal(t, pending, wv.pendingNavigationURI())
223}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close