| Name | Message | Date |
|---|---|---|
| 📄 InteractShould.cs | 5 days ago | |
| 📄 RenderShould.cs | 5 days ago |
📄
ui/tests/MainWindowTests/InteractShould.cs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
using System; using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; using Avalonia; using Avalonia.Controls; using Avalonia.Headless; using Avalonia.Headless.XUnit; using Avalonia.Input; using Avalonia.Threading; using Avalonia.VisualTree; using Cfa.Ui.Graph; using Cfa.Ui.Services; using Cfa.Ui.ViewModels; using Cfa.Ui.Views; using Nodify; using Shouldly; namespace Cfa.Ui.Tests.MainWindowTests; public sealed class InteractShould { [AvaloniaFact] public async Task KeepTheAddressDisabledWhileTheSolutionLoads() { // Arrange var factory = new FakeFactory { Pending = new TaskCompletionSource<IScanSession>(TaskCreationOptions.RunContinuationsAsynchronously), }; using var model = new MainViewModel(new FakeDesktop(), factory); var window = new MainWindow { DataContext = model }; window.Show(); try { // Act var loading = model.OpenSolutionCommand.ExecuteAsync(null); // Assert model.IsBusy.ShouldBeTrue(); model.OpenSolutionCommand.CanExecute(null).ShouldBeFalse(); window.FindControl<TextBox>("AddressBar")!.IsEnabled.ShouldBeFalse(); factory.Pending.SetResult(factory.Session); await loading; model.IsBusy.ShouldBeFalse(); window.FindControl<TextBox>("AddressBar")!.IsEnabled.ShouldBeTrue(); } finally { window.Close(); } } [AvaloniaFact] public async Task PanAndZoomWithoutMovingNodesOrOpeningTheirLinks() { // Arrange var desktop = new FakeDesktop(); using var model = new MainViewModel(desktop, new FakeFactory()); var window = new MainWindow { DataContext = model }; window.Show(); try { await model.OpenSolutionCommand.ExecuteAsync(null); model.Address = "Flow.cs:1"; await model.AnalyzeCommand.ExecuteAsync(null); Dispatcher.UIThread.RunJobs(); window.UpdateLayout(); var editor = window.FindControl<NodifyEditor>("Editor")!; var initialZoom = editor.ViewportZoom; var initialViewport = editor.ViewportLocation; var positions = model.Scene!.Nodes.Select(node => node.Location).ToArray(); var point = editor.TranslatePoint(new Point(120, 120), window)!.Value; // Act window.MouseDown(point, MouseButton.Right); window.MouseMove(point + new Point(90, 40), RawInputModifiers.RightMouseButton); window.MouseUp(point + new Point(90, 40), MouseButton.Right); window.MouseWheel(point, new Vector(0, 1)); // Assert editor.ViewportLocation.ShouldNotBe(initialViewport); editor.ViewportZoom.ShouldBeGreaterThan(initialZoom); model.Scene.Nodes.Select(node => node.Location).ShouldBe(positions); desktop.OpenedLink.ShouldBeNull(); } finally { window.Close(); } } [AvaloniaFact] public async Task DisplayAnErrorPopupThatCanBeDismissed() { // Arrange var window = new MainWindow(); window.Show(); var service = new DesktopServices(window); try { // Act var showing = service.ShowErrorAsync("Example failure"); var dialog = window.OwnedWindows.ShouldHaveSingleItem(); dialog.UpdateLayout(); var close = dialog.GetVisualDescendants().OfType<Button>().ShouldHaveSingleItem(); var point = close.TranslatePoint(new Point(close.Bounds.Width / 2, close.Bounds.Height / 2), dialog)!.Value; dialog.MouseDown(point, MouseButton.Left); dialog.MouseUp(point, MouseButton.Left); await showing; // Assert window.OwnedWindows.ShouldBeEmpty(); } finally { window.Close(); } } [AvaloniaFact] public async Task LoadOnceExploreWithEnterAndOpenTheClickedSourceLink() { // Arrange var desktop = new FakeDesktop(); var factory = new FakeFactory(); using var model = new MainViewModel(desktop, factory); var window = new MainWindow { DataContext = model }; window.Show(); try { var address = window.FindControl<TextBox>("AddressBar")!; address.IsEnabled.ShouldBeFalse(); // Act window.KeyPressQwerty(PhysicalKey.O, RawInputModifiers.Control); window.KeyReleaseQwerty(PhysicalKey.O, RawInputModifiers.Control); await model.OpenSolutionCommand.ExecutionTask!; address.IsEnabled.ShouldBeTrue(); address.Text = "core/src/Scanner.cs:50"; address.Focus(); window.KeyPressQwerty(PhysicalKey.Enter, RawInputModifiers.None); window.KeyReleaseQwerty(PhysicalKey.Enter, RawInputModifiers.None); await model.AnalyzeCommand.ExecutionTask!; Dispatcher.UIThread.RunJobs(); window.UpdateLayout(); var button = window .GetVisualDescendants() .OfType<Button>() .First(control => control.DataContext is GraphNode); var node = (GraphNode)button.DataContext!; var point = button .TranslatePoint(new Point(button.Bounds.Width / 2, button.Bounds.Height / 2), window)! .Value; window.MouseDown(point, MouseButton.Left); window.MouseUp(point, MouseButton.Left); if (model.OpenSourceCommand.ExecutionTask is { } openTask) { await openTask; } await model.AnalyzeCommand.ExecuteAsync(null); // Assert factory.Opens.ShouldBe(1); factory.Session.Analyses.ShouldBe(2); factory.Session.LastFile!.FullName.ShouldBe("/repo/core/src/Scanner.cs"); factory.Session.LastLine.ShouldBe(50); desktop.OpenedLink.ShouldBe(node.SourceLink); desktop.OpenedLink.ShouldStartWith("file:///repo/Flow.cs#"); desktop.Error.ShouldBeNull(); window.FindControl<NodifyEditor>("Editor")!.ItemsSource.ShouldNotBeNull(); window.GetVisualDescendants().OfType<ItemContainer>().ShouldAllBe(container => !container.IsDraggable); } finally { window.Close(); } } [AvaloniaFact] public async Task ShowBusyStateAndReportErrorsWithoutReplacingThePreviousGraph() { // Arrange var desktop = new FakeDesktop(); var factory = new FakeFactory(); using var model = new MainViewModel(desktop, factory); var window = new MainWindow { DataContext = model }; window.Show(); try { await model.OpenSolutionCommand.ExecuteAsync(null); model.Address = "Flow.cs:1"; await model.AnalyzeCommand.ExecuteAsync(null); var scene = model.Scene; factory.Session.Pending = new TaskCompletionSource<CallGraphAnalysis>( TaskCreationOptions.RunContinuationsAsynchronously ); // Act var analysis = model.AnalyzeCommand.ExecuteAsync(null); model.IsBusy.ShouldBeTrue(); window.FindControl<TextBox>("AddressBar")!.IsEnabled.ShouldBeFalse(); factory.Session.Pending.SetException( new InvalidOperationException("The selected source cannot be analyzed.") ); await analysis; // Assert model.IsBusy.ShouldBeFalse(); model.Scene.ShouldBeSameAs(scene); desktop.Error.ShouldBe("The selected source cannot be analyzed."); window.FindControl<TextBox>("AddressBar")!.IsEnabled.ShouldBeTrue(); } finally { window.Close(); } } [AvaloniaFact] public async Task RenderMirroredBranchesArrowsAndSelfLoopWithoutExtraSelfNode() { // Arrange var desktop = new FakeDesktop(); using var model = new MainViewModel(desktop, new FakeFactory()); var window = new MainWindow { DataContext = model }; window.Show(); try { await model.OpenSolutionCommand.ExecuteAsync(null); model.Address = "Flow.cs:1"; // Act await model.AnalyzeCommand.ExecuteAsync(null); Dispatcher.UIThread.RunJobs(); window.UpdateLayout(); window.CaptureRenderedFrame()?.Dispose(); using var frame = window.CaptureRenderedFrame().ShouldNotBeNull(); // Assert var scene = model.Scene.ShouldNotBeNull(); scene.Nodes.Count.ShouldBe(5); scene.Nodes.Single(node => node.Symbol.Name == "Request").IsOrigin.ShouldBeFalse(); scene.Nodes.Single(node => node.Symbol.Name == "Handle").IsOrigin.ShouldBeFalse(); scene.Edges.ShouldContain(edge => edge.IsLoopback && edge.Source == edge.Target); var buttons = window .GetVisualDescendants() .OfType<Button>() .Where(button => button.DataContext is GraphNode) .ToArray(); buttons.Length.ShouldBe(scene.Nodes.Count); buttons.ShouldAllBe(button => Equals(button.Content, "Flow." + ((GraphNode)button.DataContext!).Symbol.Name + "()") ); buttons.ShouldAllBe(button => button.Bounds.Width > 0 && button.Bounds.Height > 0); foreach (var button in buttons) { var node = (GraphNode)button.DataContext!; button.FontSize.ShouldBe(node.FontSize); button.GetVisualDescendants().OfType<TextBlock>().ShouldAllBe(text => text.FontSize == node.FontSize); } var editor = window.FindControl<NodifyEditor>("Editor")!; var bounds = scene.Nodes.Select(node => node.Bounds).Aggregate((first, next) => first.Union(next)); var center = (bounds.Center - editor.ViewportLocation) * editor.ViewportZoom; center.X.ShouldBe(editor.Bounds.Width / 2, 2); center.Y.ShouldBe(editor.Bounds.Height / 2, 2); window.GetVisualDescendants().OfType<GraphConnection>().Count().ShouldBe(scene.Edges.Count); frame.PixelSize.Width.ShouldBeGreaterThan(800); var stride = frame.PixelSize.Width * 4; var pixels = new byte[stride * frame.PixelSize.Height]; var buffer = Marshal.AllocHGlobal(pixels.Length); try { frame.CopyPixels(new PixelRect(frame.PixelSize), buffer, pixels.Length, stride); Marshal.Copy(buffer, pixels, 0, pixels.Length); } finally { Marshal.FreeHGlobal(buffer); } // The background dots are muted; bright cyan marks the actual nodes and arrowed edges. foreach (var half in new[] { 0, frame.PixelSize.Width / 2 }) { var colored = 0; for (var y = 110; y < frame.PixelSize.Height - 40; y++) { for (var x = half; x < half + frame.PixelSize.Width / 2; x++) { var index = y * stride + x * 4; if ( pixels[index + 1] > 130 && ( (pixels[index] > 170 && pixels[index + 2] < 160) || (pixels[index + 2] > 170 && pixels[index] < 160) ) ) { colored++; } } } colored.ShouldBeGreaterThan(200); } if (Environment.GetEnvironmentVariable("CFA_UI_ARTIFACTS") is { } directory) { Directory.CreateDirectory(directory); frame.Save(Path.Combine(directory, "call-graph.png")); } } finally { window.Close(); } } private sealed class FakeDesktop : IDesktopServices { public string? Error { get; private set; } public string? OpenedLink { get; private set; } public Task<FileInfo?> PickSolutionAsync() => Task.FromResult<FileInfo?>(new FileInfo("/repo/Example.slnx")); public Task ShowErrorAsync(string message) { Error = message; return Task.CompletedTask; } public Task OpenSourceAsync(string link) { OpenedLink = link; return Task.CompletedTask; } } private sealed class FakeFactory : IScanSessionFactory { public int Opens { get; private set; } public TaskCompletionSource<IScanSession>? Pending { get; set; } public FakeSession Session { get; } = new(); public Task<IScanSession> OpenAsync(FileInfo solution, CancellationToken token) { Opens++; return Pending?.Task ?? Task.FromResult<IScanSession>(Session); } } private sealed class FakeSession : IScanSession { public int Analyses { get; private set; } public FileInfo? LastFile { get; private set; } public int LastLine { get; private set; } public TaskCompletionSource<CallGraphAnalysis>? Pending { get; set; } public Task<CallGraphAnalysis> AnalyzeAsync(FileInfo file, int line, CancellationToken token) { Analyses++; LastFile = file; LastLine = line; return Pending?.Task ?? Task.FromResult(TestGraph.Create()); } public void Dispose() { } } }