forked from ElectronNET/Electron.NET
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBrowserWindowTests.cs
More file actions
315 lines (266 loc) · 11.6 KB
/
BrowserWindowTests.cs
File metadata and controls
315 lines (266 loc) · 11.6 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
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
namespace ElectronNET.IntegrationTests.Tests
{
using System.Runtime.Versioning;
using ElectronNET.API;
using ElectronNET.API.Entities;
using ElectronNET.Common;
using ElectronNET.IntegrationTests.Common;
[Collection("ElectronCollection")]
public class BrowserWindowTests : IntegrationTestBase
{
public BrowserWindowTests(ElectronFixture fx) : base(fx)
{
}
[IntegrationFact]
public async Task Can_set_and_get_title()
{
const string title = "Integration Test Title";
this.MainWindow.SetTitle(title);
await Task.Delay(500.ms());
var roundTrip = await this.MainWindow.GetTitleAsync();
roundTrip.Should().Be(title);
}
[IntegrationFact]
public async Task Can_resize_and_get_size()
{
this.MainWindow.SetSize(643, 482);
await Task.Delay(500.ms());
var size = await this.MainWindow.GetSizeAsync();
size.Should().HaveCount(2);
size[0].Should().Be(643);
size[1].Should().Be(482);
}
[IntegrationFact]
public async Task Can_set_progress_bar_and_clear()
{
this.MainWindow.SetProgressBar(0.5);
// No direct getter; rely on absence of error. Try changing again.
this.MainWindow.SetProgressBar(-1); // clears
await Task.Delay(50.ms());
}
[IntegrationFact(SkipOnWsl = true)]
public async Task Can_set_and_get_position()
{
this.MainWindow.SetPosition(134, 246);
await Task.Delay(500.ms());
var pos = await this.MainWindow.GetPositionAsync();
pos.Should().BeEquivalentTo([134, 246]);
}
[IntegrationFact]
public async Task Can_set_and_get_bounds()
{
var bounds = new Rectangle { X = 10, Y = 20, Width = 400, Height = 300 };
this.MainWindow.SetBounds(bounds);
await Task.Delay(500.ms());
var round = await this.MainWindow.GetBoundsAsync();
round.Should().BeEquivalentTo(bounds);
round.Width.Should().Be(400);
round.Height.Should().Be(300);
}
[IntegrationFact]
public async Task Can_set_and_get_content_bounds()
{
var bounds = new Rectangle { X = 0, Y = 0, Width = 300, Height = 200 };
this.MainWindow.SetContentBounds(bounds);
await Task.Delay(500.ms());
var round = await this.MainWindow.GetContentBoundsAsync();
round.Width.Should().BeGreaterThan(0);
round.Height.Should().BeGreaterThan(0);
}
[IntegrationFact]
public async Task Show_hide_visibility_roundtrip()
{
BrowserWindow window = null;
try
{
window = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions { Show = true }, "about:blank");
await Task.Delay(100.ms());
window.Show();
await Task.Delay(500.ms());
(await window.IsVisibleAsync()).Should().BeTrue();
window.Hide();
await Task.Delay(500.ms());
(await window.IsVisibleAsync()).Should().BeFalse();
}
finally
{
window?.Destroy();
}
}
[IntegrationFact]
public async Task AlwaysOnTop_toggle_and_query()
{
this.MainWindow.SetAlwaysOnTop(true);
await Task.Delay(500.ms());
(await this.MainWindow.IsAlwaysOnTopAsync()).Should().BeTrue();
this.MainWindow.SetAlwaysOnTop(false);
await Task.Delay(500.ms());
(await this.MainWindow.IsAlwaysOnTopAsync()).Should().BeFalse();
}
[IntegrationFact]
[SupportedOSPlatform(Linux)]
[SupportedOSPlatform(Windows)]
public async Task MenuBar_auto_hide_and_visibility()
{
this.MainWindow.SetAutoHideMenuBar(true);
await Task.Delay(500.ms());
(await this.MainWindow.IsMenuBarAutoHideAsync()).Should().BeTrue();
this.MainWindow.SetMenuBarVisibility(false);
await Task.Delay(500.ms());
(await this.MainWindow.IsMenuBarVisibleAsync()).Should().BeFalse();
this.MainWindow.SetMenuBarVisibility(true);
await Task.Delay(500.ms());
(await this.MainWindow.IsMenuBarVisibleAsync()).Should().BeTrue();
}
[IntegrationFact]
public async Task ReadyToShow_event_fires_after_content_ready()
{
BrowserWindow window = null;
try
{
window = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions { Show = false }, "about:blank");
var tcs = new TaskCompletionSource();
window.OnReadyToShow += () => tcs.TrySetResult();
// Trigger a navigation and wait for DOM ready so the renderer paints, which emits ready-to-show
var domReadyTcs = new TaskCompletionSource();
window.WebContents.OnDomReady += () => domReadyTcs.TrySetResult();
await Task.Delay(500.ms());
await window.WebContents.LoadURLAsync("about:blank");
await domReadyTcs.Task;
var completed = await Task.WhenAny(tcs.Task, Task.Delay(3.seconds()));
completed.Should().Be(tcs.Task);
}
finally
{
window?.Destroy();
}
}
[IntegrationFact]
public async Task PageTitleUpdated_event_fires_on_title_change()
{
var window = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions { Show = true }, "about:blank");
var tcs = new TaskCompletionSource<string>();
window.OnPageTitleUpdated += title => tcs.TrySetResult(title);
// Navigate and wait for DOM ready, then change the document.title to trigger the event
var domReadyTcs = new TaskCompletionSource();
window.WebContents.OnDomReady += () => domReadyTcs.TrySetResult();
await Task.Delay(500.ms());
await window.WebContents.LoadURLAsync("about:blank");
await domReadyTcs.Task;
await window.WebContents.ExecuteJavaScriptAsync<string>("document.title='NewTitle';");
// Wait for event up to a short timeout
var completed2 = await Task.WhenAny(tcs.Task, Task.Delay(3.seconds()));
completed2.Should().Be(tcs.Task);
(await tcs.Task).Should().Be("NewTitle");
}
[IntegrationFact]
public async Task Resize_event_fires_on_size_change()
{
var window = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions { Show = false }, "about:blank");
var resized = false;
window.OnResize += () => resized = true;
await Task.Delay(500.ms());
window.SetSize(500, 400);
await Task.Delay(300.ms());
resized.Should().BeTrue();
}
[IntegrationFact]
public async Task Progress_bar_and_always_on_top_toggle()
{
var win = this.MainWindow;
win.SetProgressBar(0.5);
await Task.Delay(50.ms());
win.SetProgressBar(0.8, new ProgressBarOptions());
await Task.Delay(50.ms());
win.SetAlwaysOnTop(true);
await Task.Delay(500.ms());
(await win.IsAlwaysOnTopAsync()).Should().BeTrue();
win.SetAlwaysOnTop(false);
await Task.Delay(500.ms());
(await win.IsAlwaysOnTopAsync()).Should().BeFalse();
}
[IntegrationFact]
[SupportedOSPlatform(Linux)]
[SupportedOSPlatform(Windows)]
public async Task Menu_bar_visibility_and_auto_hide()
{
var win = this.MainWindow;
win.SetAutoHideMenuBar(true);
await Task.Delay(500.ms());
(await win.IsMenuBarAutoHideAsync()).Should().BeTrue();
win.SetMenuBarVisibility(true);
await Task.Delay(500.ms());
(await win.IsMenuBarVisibleAsync()).Should().BeTrue();
}
[IntegrationFact]
public async Task Parent_child_relationship_roundtrip()
{
var child = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions { Show = false, Width = 300, Height = 200 }, "about:blank");
this.MainWindow.SetParentWindow(null); // ensure top-level
child.SetParentWindow(this.MainWindow);
await Task.Delay(500.ms());
var parent = await child.GetParentWindowAsync();
parent.Id.Should().Be(this.MainWindow.Id);
var kids = await this.MainWindow.GetChildWindowsAsync();
kids.Select(k => k.Id).Should().Contain(child.Id);
child.Destroy();
}
[IntegrationFact]
[SupportedOSPlatform(MacOS)]
public async Task Represented_filename_and_edited_flags()
{
var win = this.MainWindow;
var temp = Path.Combine(Path.GetTempPath(), "electronnet_test.txt");
File.WriteAllText(temp, "test");
win.SetRepresentedFilename(temp);
await Task.Delay(500.ms());
var represented = await win.GetRepresentedFilenameAsync();
represented.Should().Be(temp);
win.SetDocumentEdited(true);
await Task.Delay(500.ms());
var edited = await win.IsDocumentEditedAsync();
edited.Should().BeTrue();
win.SetDocumentEdited(false);
}
[IntegrationFact]
public async Task BoundsChanged_event_fires_with_updated_bounds()
{
BrowserWindow window = null;
try
{
window = await Electron.WindowManager.CreateWindowAsync(
new BrowserWindowOptions { Show = true, Width = 300, Height = 200 },
"about:blank");
await Task.Delay(5.seconds());
var tcs = new TaskCompletionSource<Rectangle>(TaskCreationOptions.RunContinuationsAsynchronously);
window.OnBoundsChanged += bounds => tcs.TrySetResult(bounds);
await Task.Delay(500.ms());
var target = new Rectangle { X = 25, Y = 35, Width = 420, Height = 310 };
window.SetBounds(target);
var completed = await Task.WhenAny(tcs.Task, Task.Delay(3.seconds()));
completed.Should().Be(tcs.Task);
var observed = await tcs.Task;
observed.Width.Should().Be(target.Width);
observed.Height.Should().Be(target.Height);
}
finally
{
window?.Destroy();
}
}
[IntegrationFact]
public async Task BoundsChanged_event_can_fire_on_resize_of_existing_window()
{
var win = this.MainWindow;
var tcs = new TaskCompletionSource<Rectangle>(TaskCreationOptions.RunContinuationsAsynchronously);
win.OnBoundsChanged += bounds => tcs.TrySetResult(bounds);
await Task.Delay(500.ms());
win.SetBounds(new Rectangle { X = 10, Y = 10, Width = 560, Height = 440 });
var completed = await Task.WhenAny(tcs.Task, Task.Delay(3.seconds()));
completed.Should().Be(tcs.Task);
var boundsObserved = await tcs.Task;
boundsObserved.Width.Should().Be(560);
boundsObserved.Height.Should().Be(440);
}
}
}