forked from LykosAI/StabilityMatrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestBase.cs
195 lines (165 loc) · 6.41 KB
/
TestBase.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
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Media.Imaging;
using Avalonia.Threading;
using Avalonia.VisualTree;
using FluentAvalonia.UI.Controls;
using FluentAvalonia.UI.Windowing;
using Microsoft.Extensions.DependencyInjection;
using StabilityMatrix.Avalonia;
using StabilityMatrix.Avalonia.Controls;
using StabilityMatrix.Avalonia.Languages;
using StabilityMatrix.Avalonia.ViewModels;
using StabilityMatrix.Avalonia.ViewModels.Dialogs;
using StabilityMatrix.Avalonia.Views;
using StabilityMatrix.Avalonia.Views.Dialogs;
using StabilityMatrix.UITests.Extensions;
namespace StabilityMatrix.UITests;
public class TestBase
{
internal static IServiceProvider Services => App.Services;
internal static (AppWindow, MainWindowViewModel)? currentMainWindow;
internal virtual VerifySettings Settings
{
get
{
var settings = new VerifySettings();
settings.IgnoreMembers<MainWindowViewModel>(
vm => vm.Pages,
vm => vm.FooterPages,
vm => vm.CurrentPage
);
settings.IgnoreMember<UpdateViewModel>(vm => vm.CurrentVersionText);
settings.DisableDiff();
return settings;
}
}
internal static (AppWindow, MainWindowViewModel) GetMainWindow()
{
if (currentMainWindow is not null)
{
return currentMainWindow.Value;
}
var window = Services.GetRequiredService<MainWindow>();
var viewModel = Services.GetRequiredService<MainWindowViewModel>();
window.DataContext = viewModel;
window.Width = 1400;
window.Height = 900;
App.VisualRoot = window;
App.StorageProvider = window.StorageProvider;
App.Clipboard = window.Clipboard ?? throw new NullReferenceException("Clipboard is null");
currentMainWindow = (window, viewModel);
return currentMainWindow.Value;
}
internal static async Task DoInitialSetup()
{
var (window, _) = GetMainWindow();
if (!window.IsVisible)
{
window.Show();
await Task.Delay(300);
Dispatcher.UIThread.RunJobs();
}
try
{
var dialog = await WaitHelper.WaitForNotNullAsync(() => GetWindowDialog(window));
if (dialog.Content is SelectDataDirectoryDialog selectDataDirectoryDialog)
{
// Click continue button
var continueButton = selectDataDirectoryDialog
.GetVisualDescendants()
.OfType<Button>()
.First(b => b.Content as string == "Continue");
await window.ClickTargetAsync(continueButton);
await Task.Delay(300);
Dispatcher.UIThread.RunJobs();
// Find the one click install dialog
var oneClickDialog = await WaitHelper.WaitForConditionAsync(
() => GetWindowDialog(window),
d => d?.Content is OneClickInstallDialog
);
Assert.NotNull(oneClickDialog);
var skipButton = oneClickDialog
.GetVisualDescendants()
.OfType<Button>()
.First(b => b.Content as string == Resources.Label_SkipSetup);
await window.ClickTargetAsync(skipButton);
await Task.Delay(300);
Dispatcher.UIThread.RunJobs();
}
else if (dialog.Content is OneClickInstallDialog)
{
var skipButton = dialog
.GetVisualDescendants()
.OfType<Button>()
.First(b => b.Content as string == Resources.Label_SkipSetup);
await window.ClickTargetAsync(skipButton);
await Task.Delay(300);
Dispatcher.UIThread.RunJobs();
}
}
catch (TimeoutException)
{
// ignored
}
}
internal static async Task CloseUpdateDialog()
{
var (window, _) = GetMainWindow();
var updateTip = window.FindControl<TeachingTip>("UpdateAvailableTeachingTip");
await window.ClickTargetAsync(updateTip);
await Task.Delay(300);
Dispatcher.UIThread.RunJobs();
}
internal static BetterContentDialog? GetWindowDialog(Visual window)
{
return window
.FindDescendantOfType<VisualLayerManager>()
?.FindDescendantOfType<OverlayLayer>()
?.FindDescendantOfType<DialogHost>()
?.FindDescendantOfType<LayoutTransformControl>()
?.FindDescendantOfType<VisualLayerManager>()
?.FindDescendantOfType<BetterContentDialog>();
}
internal static IEnumerable<BetterContentDialog> EnumerateWindowDialogs(Visual window)
{
return window
.FindDescendantOfType<VisualLayerManager>()
?.FindDescendantOfType<OverlayLayer>()
?.FindDescendantOfType<DialogHost>()
?.FindDescendantOfType<LayoutTransformControl>()
?.FindDescendantOfType<VisualLayerManager>()
?.GetVisualDescendants()
.OfType<BetterContentDialog>() ?? Enumerable.Empty<BetterContentDialog>();
}
internal async Task<(BetterContentDialog, T)> WaitForDialog<T>(Visual window)
where T : Control
{
var dialogs = await WaitHelper.WaitForConditionAsync(
() => EnumerateWindowDialogs(window).ToList(),
list => list.Any(dialog => dialog.Content is T)
);
if (dialogs.Count == 0)
{
throw new InvalidOperationException("No dialogs found");
}
var contentDialog = dialogs.First(dialog => dialog.Content is T);
return (contentDialog, contentDialog.Content as T)!;
}
internal void SaveScreenshot(Visual visual)
{
var rect = new Rect(visual!.Bounds.Size);
var pixelSize = new PixelSize((int)rect.Width, (int)rect.Height);
var dpiVector = new Vector(96, 96);
using var bitmap = new RenderTargetBitmap(pixelSize, dpiVector);
using var fs = File.Open(
$"C:\\StabilityMatrix\\StabilityMatrix-avalonia\\StabilityMatrix.UITests\\"
+ Guid.NewGuid().ToString()
+ ".png",
FileMode.OpenOrCreate
);
bitmap.Render(visual);
bitmap.Save(fs);
fs.Flush();
}
}