v0.31.0 bf0756b

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	wv := &WebView{ctx: context.Background(), uri: "https://example.com/docs/page", callbacks: &port.WebViewCallbacks{
64		OnFaviconURLChanged: func(p string, urls []string) { page, icons = p, urls },
65	}}
66
67	(&handlerSet{wv: wv}).OnFaviconUrlchange(nil, purecef.StringList(42))
68
69	require.Equal(t, "https://example.com/docs/page", page)
70	require.Equal(t, []string{"https://example.com/favicon.png", "https://cdn.example.com/icon.ico"}, icons)
71}
72
73func TestCEFLoadingFinishedDiscoversMetadataBeforeFallback(t *testing.T) {
74	prevNewStringVisitor := cefNewStringVisitor
75	cefNewStringVisitor = func(visitor purecef.StringVisitor) purecef.StringVisitor { return visitor }
76	defer func() { cefNewStringVisitor = prevNewStringVisitor }()
77
78	browser := cefmocks.NewMockBrowser(t)
79	frame := cefmocks.NewMockFrame(t)
80	browser.EXPECT().GetMainFrame().Return(frame)
81	frame.EXPECT().GetSource(mock.Anything).Run(func(visitor purecef.StringVisitor) {
82		visitor.Visit(`<html><head><link rel="icon" href="/meta.png"></head></html>`)
83	}).Return()
84
85	var page string
86	var icons []string
87	wv := &WebView{ctx: context.Background(), uri: "https://example.com/docs/page", callbacks: &port.WebViewCallbacks{
88		OnFaviconURLChanged: func(p string, urls []string) { page, icons = p, urls },
89	}}
90
91	(&handlerSet{wv: wv}).OnLoadingStateChange(browser, 0, 1, 0)
92
93	require.Equal(t, "https://example.com/docs/page", page)
94	require.Equal(t, []string{"https://example.com/meta.png", "https://example.com/favicon.ico"}, icons)
95}
96
97func TestCEFLoadingFinishedForwardsFallbackCandidate(t *testing.T) {
98	var page string
99	var icons []string
100	wv := &WebView{ctx: context.Background(), uri: "https://example.com/docs/page", callbacks: &port.WebViewCallbacks{
101		OnFaviconURLChanged: func(p string, urls []string) { page, icons = p, urls },
102	}}
103
104	(&handlerSet{wv: wv}).OnLoadingStateChange(nil, 0, 1, 0)
105
106	require.Equal(t, "https://example.com/docs/page", page)
107	require.Equal(t, []string{"https://example.com/favicon.ico"}, icons)
108}

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