Repository navigation
Expand file tree
/
Copy pathrender_test.go
More file actions
201 lines (189 loc) · 7.32 KB
/
Copy pathrender_test.go
File metadata and controls
201 lines (189 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"flag"
"fmt"
"image"
"image/png"
"os"
"strings"
"testing"
"time"
"gioui.org/f32"
"gioui.org/gpu/headless"
"gioui.org/io/input"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
"gioui.org/unit"
"gioui.org/widget/material"
"loov.dev/lensm/internal/disasm"
"loov.dev/lensm/internal/gui"
)
// The render flags draw the window offscreen into a PNG, for checking a
// UI change without opening the app, e.g.
//
// go test -run TestRender . -render /tmp/out.png -render.path ./testdata/testprog -render.func main.dot
var (
renderOut = flag.String("render", "", "render the window to this PNG file")
renderCompare = flag.String("render.compare", "", "binary to compare the open one with")
renderGraph = flag.Bool("render.graph", false, "show the whole profile's graph")
renderGraphFull = flag.Bool("render.graphfull", false, "show the profile's graph filling the window")
renderProfileMenu = flag.Bool("render.profilemenu", false, "open the menu of the profile's values")
renderGraphFind = flag.String("render.graphfind", "", "search the profile graph for this")
renderHideRuntime = flag.Bool("render.hideruntime", false, "hide the runtime in the profile graph")
renderFlame = flag.Bool("render.flame", false, "draw the profile as a flame graph")
renderFlameRight = flag.Bool("render.flameright", false, "put the flame graph right of the code")
renderGraphBelow = flag.Bool("render.graphbelow", false, "put the call graph below the code")
renderFlameUp = flag.Bool("render.flameup", false, "draw the flame graph's root at the bottom")
renderProfile = flag.String("render.profile", "", "pprof profile to show")
renderBaseProfile = flag.String("render.baseprofile", "", "pprof profile to compare the profile with")
renderPath = flag.String("render.path", "", "binary or package directory to open")
renderFunc = flag.String("render.func", "", "function to open in a tab")
renderFilter = flag.String("render.filter", "", "function list filter")
renderDark = flag.Bool("render.dark", false, "use the dark theme")
renderPanel = flag.String("render.panel", "functions", "sidebar panel: functions, comments, history or diagnostics")
renderCols = flag.String("render.cols", "gnsd", "columns to show: g for Go asm, n for native asm, s for source, d for diagnostics")
renderSyntax = flag.String("render.syntax", "xcode", "syntax style")
renderSize = flag.String("render.size", "1400x900", "window size in dp")
renderScale = flag.Float64("render.scale", 2, "pixels per dp")
renderHover = flag.String("render.hover", "", "pointer position in dp, as x,y")
renderFind = flag.String("render.find", "", "open the find bar with this query")
renderQuick = flag.String("render.quick", "", "open the go-to-function popup with this query")
renderKeys = flag.Bool("render.shortcuts", false, "open the keyboard shortcut sheet")
renderMenu = flag.Bool("render.menu", false, "open the app menu, drawn in the window except on macOS")
renderSettings = flag.Int("render.settings", -1, "open the settings on this page")
)
// TestRender is skipped unless -render names an output file. It starts
// from the default settings, so the user's settings don't change it.
func TestRender(t *testing.T) {
if *renderOut == "" {
t.Skip("set -render to draw the window")
}
var w, h int
if _, err := fmt.Sscanf(*renderSize, "%dx%d", &w, &h); err != nil {
t.Fatalf("-render.size %q: %v", *renderSize, err)
}
scale := float32(*renderScale)
size := image.Pt(int(float32(w)*scale), int(float32(h)*scale))
theme := material.NewTheme()
theme.Shaper = text.NewShaper(text.WithCollection(gui.LoadFonts("")))
theme.TextSize = unit.Sp(12)
ui := NewFileUI(&gui.Windows{}, theme)
ui.settingsPath = "" // never write the real settings
st := DefaultAppSettings()
st.Dark = *renderDark
st.ShowGoAsm = strings.Contains(*renderCols, "g")
st.ShowNativeAsm = strings.Contains(*renderCols, "n")
st.ShowSource = strings.Contains(*renderCols, "s")
st.HideDiagnostics = !strings.Contains(*renderCols, "d")
st.SyntaxStyle = *renderSyntax
ui.split.Ratio = st.SidebarRatio
ui.graphSplit.Axis = layout.Horizontal
ui.graphSplit.Ratio = st.GraphRatio
ui.applySettings(st)
panels := map[string]int{"functions": panelFunctions, "comments": panelComments, "history": panelHistory, "diagnostics": panelMarks, "callers": panelCallers, "summary": panelSummary, "graph": panelGraph}
panel, ok := panels[*renderPanel]
if !ok {
t.Fatalf("unknown -render.panel %q", *renderPanel)
}
ui.Panel = panel
ui.Config = FileUIConfig{Path: *renderPath, Context: 3, ProfilePath: *renderProfile, BaseProfilePath: *renderBaseProfile}
if *renderPath != "" {
file, err := loadFile(*renderPath)
if err != nil {
t.Fatal(err)
}
ui.SetFile(file)
defer ui.File.Close()
}
ui.ShowGraph = *renderGraph || *renderGraphFull
ui.GraphFull = *renderGraphFull
ui.ProfileMenu.Open = *renderProfileMenu
ui.GraphTools.Find.SetText(*renderGraphFind)
ui.GraphTools.HideRuntime.Value = *renderHideRuntime
ui.GraphTools.Flame = *renderFlame
ui.Settings.FlameRight = *renderFlameRight
ui.Settings.CallGraphBelow = *renderGraphBelow
ui.Settings.FlameUp = *renderFlameUp
if *renderCompare != "" {
other, err := loadFile(*renderCompare)
if err != nil {
t.Fatal(err)
}
ui.setCompare(*renderCompare, other)
defer ui.setCompare("", nil)
}
ui.Funcs.SetFilter(*renderFilter)
if *renderFunc != "" {
if ui.File == nil {
t.Fatal("-render.func needs -render.path")
}
fn := disasm.FindFunc(ui.File, *renderFunc)
if fn == nil {
t.Fatalf("no function %q", *renderFunc)
}
ui.openTab(fn, false)
}
if *renderSettings >= 0 {
ui.openSettings()
ui.SettingsDialog.Nav.Selected = *renderSettings
}
ui.AppMenu.Open = *renderMenu
ui.Shortcuts.Open = *renderKeys
var hover f32.Point
if *renderHover != "" {
if _, err := fmt.Sscanf(*renderHover, "%f,%f", &hover.X, &hover.Y); err != nil {
t.Fatalf("-render.hover %q: %v", *renderHover, err)
}
}
win, err := headless.NewWindow(size.X, size.Y)
if err != nil {
t.Fatal(err)
}
defer win.Release()
var router input.Router
var ops op.Ops
// Code loads on its own goroutine, and hover and the popups need a
// frame to settle, so draw a few frames and keep the last. The clock
// steps a second a frame, past the tooltip delay.
start := time.Now()
for i := range 4 {
ops.Reset()
gtx := layout.Context{
Ops: &ops,
Now: start.Add(time.Duration(i) * time.Second),
Metric: unit.Metric{PxPerDp: scale, PxPerSp: scale},
Constraints: layout.Exact(size),
Source: router.Source(),
}
if i == 0 && *renderFind != "" {
ui.Find.Editor.SetText(*renderFind)
ui.openFind(gtx)
}
if i == 0 && *renderQuick != "" {
ui.QuickOpen.Editor.SetText(*renderQuick)
ui.QuickOpen.Show(gtx)
}
ui.Layout(gtx)
router.Frame(gtx.Ops)
if i == 1 && *renderHover != "" {
router.Queue(pointer.Event{Kind: pointer.Move, Source: pointer.Mouse, Position: hover.Mul(scale)})
}
if err := win.Frame(gtx.Ops); err != nil {
t.Fatal(err)
}
}
img := image.NewRGBA(image.Rectangle{Max: size})
if err := win.Screenshot(img); err != nil {
t.Fatal(err)
}
f, err := os.Create(*renderOut)
if err != nil {
t.Fatal(err)
}
defer f.Close()
if err := png.Encode(f, img); err != nil {
t.Fatal(err)
}
}