v0.32.0 0617278

favicon_discovery_test.go RawHistoryFolder

1package cef
2
3import (
4	"context"
5	"testing"
6
7	"github.com/bnema/dumber/internal/application/port"
8	purecef "github.com/bnema/purego-cef/cef"
9	cefmocks "github.com/bnema/purego-cef/cef/mocks"
10	"github.com/stretchr/testify/mock"
11	"github.com/stretchr/testify/require"
12)
13
14func TestFaviconDiscoveryResolvesLinksAndFallback(t *testing.T) {
15	pageURL := "https://example.com/docs/page.html"
16	html := `<html><head>
17		<link rel="stylesheet" href="/style.css">
18		<link rel="icon" href="/favicon.png">
19		<link rel="shortcut icon" href="icons/shortcut.ico">
20		<link rel="apple-touch-icon" href="https://cdn.example.com/apple.png">
21		<link rel="icon" href="javascript:alert(1)">
22	</head></html>`
23
24	got := DiscoverFaviconCandidates(pageURL, html)
25	require.Equal(t, []string{
26		"https://example.com/favicon.png",
27		"https://example.com/docs/icons/shortcut.ico",
28		"https://cdn.example.com/apple.png",
29		"https://example.com/favicon.ico",
30	}, got)
31}
32
33func TestFaviconDiscoveryFallbackOnly(t *testing.T) {
34	require.Equal(t, []string{"https://example.com/favicon.ico"}, DiscoverFaviconCandidates("https://example.com/path", "<html></html>"))
35	require.Nil(t, DiscoverFaviconCandidates("about:blank", "<html></html>"))
36}
37
38func TestFaviconDiscoveryKeepsAbsoluteIconsForOpaquePageURL(t *testing.T) {
39	got := DiscoverFaviconCandidates("about:blank", `<link rel="icon" href="https://cdn.example.com/icon.png">`)
40	require.Equal(t, []string{"https://cdn.example.com/icon.png"}, got)
41}
42
43func TestFaviconDiscoveryIgnoresUnsafeIconURLs(t *testing.T) {
44	got := DiscoverFaviconCandidates("https://example.com/path", `<link rel="icon" href="data:image/png;base64,AAAA"><link rel="icon" href="//cdn.example.com/icon.png">`)
45	require.Equal(t, []string{"https://cdn.example.com/icon.png", "https://example.com/favicon.ico"}, got)
46}
47
48func TestFaviconDiscoveryIgnoresWhitespaceOnlyHrefs(t *testing.T) {
49	got := DiscoverFaviconCandidates("https://example.com/path", `<link rel="icon" href="  ">`)
50	require.Equal(t, []string{"https://example.com/favicon.ico"}, got)
51}
52
53func TestCEFOnFaviconUrlchangeForwardsOrderedCandidates(t *testing.T) {
54	prevDecode := decodeCEFStringList
55	decodeCEFStringList = func(list purecef.StringList) []string {
56		require.Equal(t, purecef.StringList(42), list)
57		return []string{"/favicon.png", "https://cdn.example.com/icon.ico", "javascript:alert(1)"}
58	}
59	defer func() { decodeCEFStringList = prevDecode }()
60
61	var page string
62	var icons []string
63	browser := cefmocks.NewMockBrowser(t)
64	browser.EXPECT().GetIdentifier().Return(int32(7)).Maybe()
65	wv := &WebView{ctx: context.Background(), uri: "https://example.com/docs/page", browser: browser, documentSeq: 1, callbacks: &port.WebViewCallbacks{
66		OnFaviconURLChanged: func(p string, urls []string) { page, icons = p, urls },
67	}}
68
69	(&handlerSet{wv: wv}).OnFaviconUrlchange(browser, purecef.StringList(42))
70
71	require.Equal(t, "https://example.com/docs/page", page)
72	require.Equal(t, []string{"https://example.com/favicon.png", "https://cdn.example.com/icon.ico"}, icons)
73}
74
75func TestCEFLoadingFinishedDiscoversMetadataBeforeFallback(t *testing.T) {
76	prevNewStringVisitor := cefNewStringVisitor
77	cefNewStringVisitor = func(visitor purecef.StringVisitor) purecef.StringVisitor { return visitor }
78	defer func() { cefNewStringVisitor = prevNewStringVisitor }()
79
80	browser := cefmocks.NewMockBrowser(t)
81	frame := cefmocks.NewMockFrame(t)
82	browser.EXPECT().GetIdentifier().Return(int32(7)).Maybe()
83	browser.EXPECT().GetMainFrame().Return(frame)
84	frame.EXPECT().GetSource(mock.Anything).Run(func(visitor purecef.StringVisitor) {
85		visitor.Visit(`<html><head><link rel="icon" href="/meta.png"></head></html>`)
86	}).Return()
87
88	var page string
89	var icons []string
90	wv := &WebView{ctx: context.Background(), uri: "https://example.com/docs/page", browser: browser, documentSeq: 1, callbacks: &port.WebViewCallbacks{
91		OnFaviconURLChanged: func(p string, urls []string) { page, icons = p, urls },
92	}}
93
94	(&handlerSet{wv: wv}).OnLoadingStateChange(browser, 0, 1, 0)
95
96	require.Equal(t, "https://example.com/docs/page", page)
97	require.Equal(t, []string{"https://example.com/meta.png", "https://example.com/favicon.ico"}, icons)
98}
99
100func TestCEFLoadingFinishedForwardsFallbackCandidate(t *testing.T) {
101	var page string
102	var icons []string
103	wv := &WebView{ctx: context.Background(), uri: "https://example.com/docs/page", documentSeq: 1, callbacks: &port.WebViewCallbacks{
104		OnFaviconURLChanged: func(p string, urls []string) { page, icons = p, urls },
105	}}
106
107	(&handlerSet{wv: wv}).OnLoadingStateChange(nil, 0, 1, 0)
108
109	require.Equal(t, "https://example.com/docs/page", page)
110	require.Equal(t, []string{"https://example.com/favicon.ico"}, icons)
111}
112
113func TestCEFLoadingFinishedSkipsSourceWhenEngineProvidedCandidates(t *testing.T) {
114	prevDecode := decodeCEFStringList
115	decodeCEFStringList = func(purecef.StringList) []string { return []string{"/engine.png"} }
116	defer func() { decodeCEFStringList = prevDecode }()
117
118	browser := cefmocks.NewMockBrowser(t)
119	browser.EXPECT().GetIdentifier().Return(int32(7)).Maybe()
120	calls := 0
121	wv := &WebView{ctx: context.Background(), uri: "https://example.com/page", browser: browser, documentSeq: 1, callbacks: &port.WebViewCallbacks{
122		OnFaviconURLChanged: func(string, []string) { calls++ },
123	}}
124	h := &handlerSet{wv: wv}
125	h.OnFaviconUrlchange(browser, purecef.StringList(1))
126	h.OnLoadingStateChange(browser, 0, 0, 0)
127
128	require.Equal(t, 1, calls)
129}
130
131func TestCEFLoadingFinishedCoalescesPendingSourceDiscovery(t *testing.T) {
132	prevNewStringVisitor := cefNewStringVisitor
133	cefNewStringVisitor = func(visitor purecef.StringVisitor) purecef.StringVisitor { return visitor }
134	defer func() { cefNewStringVisitor = prevNewStringVisitor }()
135
136	browser := cefmocks.NewMockBrowser(t)
137	frame := cefmocks.NewMockFrame(t)
138	browser.EXPECT().GetMainFrame().Return(frame).Once()
139	frame.EXPECT().GetSource(mock.Anything).Return().Once()
140	wv := &WebView{ctx: context.Background(), uri: "https://example.com/page", documentSeq: 1, callbacks: &port.WebViewCallbacks{
141		OnFaviconURLChanged: func(string, []string) {},
142	}}
143	h := &handlerSet{wv: wv}
144	h.OnLoadingStateChange(browser, 0, 0, 0)
145	h.OnLoadingStateChange(browser, 0, 0, 0)
146}
147
148func TestCEFLoadingFinishedDoesNotRepeatCompletedSourceDiscovery(t *testing.T) {
149	prevNewStringVisitor := cefNewStringVisitor
150	var visitor purecef.StringVisitor
151	cefNewStringVisitor = func(v purecef.StringVisitor) purecef.StringVisitor {
152		visitor = v
153		return v
154	}
155	defer func() { cefNewStringVisitor = prevNewStringVisitor }()
156
157	browser := cefmocks.NewMockBrowser(t)
158	frame := cefmocks.NewMockFrame(t)
159	browser.EXPECT().GetIdentifier().Return(int32(7)).Maybe()
160	browser.EXPECT().GetMainFrame().Return(frame).Once()
161	frame.EXPECT().GetSource(mock.Anything).Return().Once()
162	wv := &WebView{ctx: context.Background(), uri: "https://example.com/page", documentSeq: 1, callbacks: &port.WebViewCallbacks{
163		OnFaviconURLChanged: func(string, []string) {},
164	}}
165	h := &handlerSet{wv: wv}
166
167	h.OnLoadingStateChange(browser, 0, 0, 0)
168	require.NotNil(t, visitor, "loading-finished must start source discovery")
169	visitor.Visit(`<html><head><link rel="icon" href="/meta.png"></head></html>`)
170
171	// The same document finishing again must not repeat the full-document
172	// transfer; the GetMainFrame and GetSource expectations are Once.
173	h.OnLoadingStateChange(browser, 0, 0, 0)
174}

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