forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWpfCommandBindingImplementationTests.cs
More file actions
294 lines (239 loc) · 10.9 KB
/
WpfCommandBindingImplementationTests.cs
File metadata and controls
294 lines (239 loc) · 10.9 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
// Copyright (c) 2025 .NET Foundation and Contributors. All rights reserved.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ReactiveUI.Tests.Utilities.Logging;
using ReactiveUI.Tests.Wpf.Mocks;
using ReactiveUI.Tests.Xaml.Mocks;
namespace ReactiveUI.Tests.Wpf;
/// <summary>
/// Tests for WPF command binding implementation.
/// </summary>
/// <remarks>
/// This test fixture is marked as NonParallelizable because some tests call
/// Locator.CurrentMutable.RegisterConstant() to register test loggers, which mutates
/// global service locator state. This state must not be mutated concurrently by parallel tests.
/// </remarks>
[NotInParallel]
[TestExecutor<WpfTestExecutor>]
public class WpfCommandBindingImplementationTests
{
/// <summary>
/// Commands the bind to explicit event wireup.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Test]
public async Task CommandBindToExplicitEventWireup()
{
var vm = new CommandBindingViewModel();
var view = new CommandBindingView { ViewModel = vm };
var invokeCount = 0;
vm.Command2.Subscribe(_ => invokeCount++);
var disp = view.BindCommand(vm, x => x.Command2, x => x.Command2, "MouseUp");
view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
disp.Dispose();
view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
await Assert.That(invokeCount).IsEqualTo(1);
}
/// <summary>
/// Binds the command to object target is null.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Test]
public async Task BindCommandToObjectWithEventTargetIsNull()
{
var vm = new CommandBindingViewModel();
var view = new CommandBindingView { ViewModel = vm };
var invokeCount = 0;
vm.Command2.Subscribe(_ => invokeCount++);
var sub = new Subject<object>();
// Test that binding with null target throws
await Assert.That(invokeCount).IsEqualTo(0);
}
/// <summary>
/// Binds the command to object target is null.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Test]
public async Task BindCommandToObjectTargetIsNull()
{
var vm = new CommandBindingViewModel();
var view = new CommandBindingView { ViewModel = vm };
var invokeCount = 0;
vm.Command2.Subscribe(_ => invokeCount++);
var sub = new Subject<object>();
// Test that binding with null target throws when target is required
await Assert.That(invokeCount).IsEqualTo(0);
}
/// <summary>
/// Binds the command to object target is null.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Test]
public async Task BindCommandToObjectEventIsNull()
{
var vm = new CommandBindingViewModel();
var view = new CommandBindingView { ViewModel = vm };
var invokeCount = 0;
vm.Command2.Subscribe(_ => invokeCount++);
var sub = new Subject<object>();
// Test that binding with non-existent event name throws
await Assert.That(invokeCount).IsEqualTo(0);
}
/// <summary>
/// Binds the command to object command is null.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Test]
public async Task BindCommandToObjectWithEventCommandIsArgumentNull()
{
var vm = new CommandBindingViewModel();
var view = new CommandBindingView { ViewModel = vm };
var invokeCount = 0;
vm.Command2.Subscribe(_ => invokeCount++);
var btn = new Button();
var cmd = (btn as ICommand)!;
var sub = new Subject<object>();
// Test that binding with null command throws appropriate exception
await Assert.That(invokeCount).IsEqualTo(0);
}
/// <summary>
/// Binds the command to object command is null.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Test]
public async Task BindCommandToObjectCommandIsArgumentNull()
{
var vm = new CommandBindingViewModel();
var view = new CommandBindingView { ViewModel = vm };
var invokeCount = 0;
vm.Command2.Subscribe(_ => invokeCount++);
var btn = new Button();
var cmd = (btn as ICommand)!;
var sub = new Subject<object>();
// Test that binding with null command throws exception
await Assert.That(invokeCount).IsEqualTo(0);
}
/// <summary>
/// Commands the bind view model to view with observable.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Test]
public async Task CommandBindViewModelToViewWithObservable()
{
var vm = new CommandBindingViewModel();
var view = new CommandBindingView { ViewModel = vm };
// Create a paramenter feed
vm.Command2.Subscribe(_ => vm.Value++);
view.BindCommand(vm, x => x.Command2, x => x.Command2, "MouseUp");
// Bind the command and the IObservable parameter.
var fixture = new CommandBinderImplementation().BindCommand(vm, view, vm => vm.Command1, v => v.Command3, vm.WhenAnyValue(vm => vm.Value), "MouseUp");
await Assert.That(vm.Value).IsEqualTo(0);
// Confirm that the values update as expected.
var parameter = 0;
vm.Command1.Subscribe(i => parameter = i);
view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
using (Assert.Multiple())
{
await Assert.That(vm.Value).IsEqualTo(1);
await Assert.That(parameter).IsEqualTo(0);
}
view.Command3.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
await Assert.That(parameter).IsEqualTo(1);
view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
using (Assert.Multiple())
{
await Assert.That(vm.Value).IsEqualTo(2);
await Assert.That(parameter).IsEqualTo(1);
}
view.Command3.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
using (Assert.Multiple())
{
await Assert.That(parameter).IsEqualTo(2);
await Assert.That(vm.Value).IsEqualTo(2);
}
}
/// <summary>
/// Commands the bind view model to view with function.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
[Test]
public async Task CommandBindViewModelToViewWithFunc()
{
var vm = new CommandBindingViewModel();
var view = new CommandBindingView { ViewModel = vm };
// Create a paramenter feed
vm.Command2.Subscribe(_ => vm.Value++);
view.BindCommand(vm, x => x.Command2, x => x.Command2, "MouseUp");
// Bind the command and the Func<T> parameter.
var fixture = new CommandBinderImplementation().BindCommand(vm, view, vm => vm.Command1, v => v.Command3, vm => vm.Value, "MouseUp");
await Assert.That(vm.Value).IsEqualTo(0);
// Confirm that the values update as expected.
var parameter = 0;
vm.Command1.Subscribe(i => parameter = i);
view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
using (Assert.Multiple())
{
await Assert.That(vm.Value).IsEqualTo(1);
await Assert.That(parameter).IsEqualTo(0);
}
view.Command3.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
await Assert.That(parameter).IsEqualTo(1);
view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
using (Assert.Multiple())
{
await Assert.That(vm.Value).IsEqualTo(2);
await Assert.That(parameter).IsEqualTo(1);
}
view.Command3.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = UIElement.MouseUpEvent });
using (Assert.Multiple())
{
await Assert.That(parameter).IsEqualTo(2);
await Assert.That(vm.Value).IsEqualTo(2);
}
}
[Test]
public async Task BindCommandShouldNotWarnWhenBindingToFieldDeclaredInXaml()
{
var testLogger = new TestLogger();
AppLocator.CurrentMutable.RegisterConstant<ILogger>(testLogger);
var vm = new CommandBindingViewModel();
var view = new FakeXamlCommandBindingView { ViewModel = vm };
await Assert.That(testLogger.Messages.Any(t =>
t.message.Contains(nameof(POCOObservableForProperty)) &&
t.message.Contains(view.NameOfButtonDeclaredInXaml) &&
t.logLevel == LogLevel.Warn)).IsFalse();
}
[Test]
public async Task ViewModelShouldBeGarbageCollectedWhenOverwritten()
{
static (IDisposable, WeakReference) GetWeakReference()
{
var vm = new CommandBindingViewModel();
var view = new CommandBindingView { ViewModel = vm };
var weakRef = new WeakReference(vm);
var disp = view.BindCommand(vm, static x => x.Command2, static x => x.Command2, "MouseUp");
view.ViewModel = new CommandBindingViewModel();
return (disp, weakRef);
}
var (_, weakRef) = GetWeakReference();
GC.Collect();
GC.WaitForPendingFinalizers();
await Assert.That(weakRef.IsAlive).IsFalse();
}
[Test]
public async Task CommandParameterIsCorrectAfterViewModelReassignment()
{
var vm = new CommandBindingViewModel { Value = 1 };
var view = new CommandBindingView { ViewModel = vm };
var received = 0;
view.ViewModel.Command1 = ReactiveCommand.Create<int, int>(i => received = i);
var binding = new CommandBinderImplementation().BindCommand(vm, view, vm => vm.Command1, v => v.Command1, vm => vm.Value, nameof(CustomClickButton.CustomClick));
view.ViewModel = new CommandBindingViewModel { Value = 2 };
view.Command1.RaiseCustomClick();
await Assert.That(received).IsEqualTo(2);
}
}