forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandBindingTests.cs
231 lines (182 loc) · 7.07 KB
/
CommandBindingTests.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ReactiveUI.Xaml;
using Xunit;
namespace ReactiveUI.Tests
{
public class FakeViewModel : ReactiveObject
{
public ReactiveCommand Cmd { get; protected set; }
public FakeViewModel()
{
Cmd = new ReactiveCommand();
}
}
public class FakeView : IViewFor<FakeViewModel>
{
public TextBox TheTextBox { get; protected set; }
public FakeView()
{
TheTextBox = new TextBox();
ViewModel = new FakeViewModel();
}
object IViewFor.ViewModel {
get { return ViewModel; }
set { ViewModel = (FakeViewModel)value; }
}
public FakeViewModel ViewModel { get; set; }
}
public class CreatesCommandBindingTests
{
[Fact]
public void CommandBinderBindsToButton()
{
var fixture = new CreatesCommandBindingViaCommandParameter();
var origCmd = new ReactiveAsyncCommand();
var cmd = new ReactiveCommand();
var input = new Button { Command = origCmd, CommandParameter = 42 };
Assert.True(fixture.GetAffinityForObject(input.GetType(), true) <= 0);
Assert.True(fixture.GetAffinityForObject(input.GetType(), false) > 0);
var disp = fixture.BindCommandToObject(cmd, input, Observable.Return((object)5));
Assert.Equal(cmd, input.Command);
Assert.Equal(5, input.CommandParameter);
disp.Dispose();
Assert.Equal(origCmd, input.Command);
Assert.Equal(42, input.CommandParameter);
}
[Fact]
public void EventBinderBindsToExplicitEvent()
{
var input = new NonReactiveINPCObjectMadeReactive();
var fixture = new CreatesCommandBindingViaEvent();
var cmd = new ReactiveCommand();
Assert.True(fixture.GetAffinityForObject(input.GetType(), true) > 0);
Assert.False(fixture.GetAffinityForObject(input.GetType(), false) > 0);
bool wasCalled = false;
cmd.Subscribe(_ => wasCalled = true);
var disp = fixture.BindCommandToObject<PropertyChangedEventArgs>(cmd, input, Observable.Return((object) 5), "PropertyChanged");
input.InpcProperty = new TestFixture();
Assert.True(wasCalled);
wasCalled = false;
disp.Dispose();
input.InpcProperty = new TestFixture();
Assert.False(wasCalled);
}
[Fact]
public void EventBinderBindsToExplicitInheritedEvent()
{
var fixture = new FakeView();
fixture.BindCommand(fixture.ViewModel, x => x.Cmd, x => x.TheTextBox, "MouseDown");
}
#if !SILVERLIGHT
[Fact]
public void EventBinderBindsToImplicitEvent()
{
var input = new Button();
var fixture = new CreatesCommandBindingViaEvent();
var cmd = new ReactiveCommand();
Assert.True(fixture.GetAffinityForObject(input.GetType(), false) > 0);
int invokeCount = 0;
cmd.Subscribe(_ => invokeCount += 1);
var disp = fixture.BindCommandToObject(cmd, input, Observable.Return((object) 5));
Assert.NotNull(disp);
Assert.Equal(0, invokeCount);
input.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Assert.Equal(1, invokeCount);
disp.Dispose();
input.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
Assert.Equal(1, invokeCount);
}
#endif
}
public class CommandBindViewModel : ReactiveObject
{
public ReactiveCommand _Command1;
public ReactiveCommand Command1 {
get { return _Command1; }
set { this.RaiseAndSetIfChanged(x => x.Command1, value); }
}
public ReactiveCommand _Command2;
public ReactiveCommand Command2 {
get { return _Command2; }
set { this.RaiseAndSetIfChanged(x => x.Command2, value); }
}
public CommandBindViewModel()
{
Command1 = new ReactiveCommand();
Command2 = new ReactiveCommand();
}
}
public class CommandBindView : IViewFor<CommandBindViewModel>
{
object IViewFor.ViewModel {
get { return ViewModel; }
set { ViewModel = (CommandBindViewModel)value; }
}
public CommandBindViewModel ViewModel { get; set; }
public Button Command1 { get; protected set; }
public Image Command2 { get; protected set; }
public CommandBindView()
{
Command1 = new Button();
Command2 = new Image();
}
}
public class CommandBindingImplementationTests
{
[Fact]
public void CommandBindConventionWireup()
{
var vm = new CommandBindViewModel();
var view = new CommandBindView() {ViewModel = vm};
var fixture = new CommandBinderImplementation();
Assert.Null(view.Command1.Command);
var disp = fixture.BindCommand(vm, view, x => x.Command1);
Assert.Equal(vm.Command1, view.Command1.Command);
var newCmd = new ReactiveCommand();
vm.Command1 = newCmd;
Assert.Equal(newCmd, view.Command1.Command);
disp.Dispose();
Assert.Null(view.Command1.Command);
}
[Fact]
public void CommandBindByNameWireup()
{
var vm = new CommandBindViewModel();
var view = new CommandBindView() {ViewModel = vm};
var fixture = new CommandBinderImplementation();
Assert.Null(view.Command1.Command);
var disp = fixture.BindCommand(vm, view, x => x.Command1, x => x.Command1);
Assert.Equal(vm.Command1, view.Command1.Command);
var newCmd = new ReactiveCommand();
vm.Command1 = newCmd;
Assert.Equal(newCmd, view.Command1.Command);
disp.Dispose();
Assert.Null(view.Command1.Command);
}
#if !SILVERLIGHT
[Fact]
public void CommandBindToExplicitEventWireup()
{
var vm = new CommandBindViewModel();
var view = new CommandBindView() {ViewModel = vm};
var fixture = new CommandBinderImplementation();
int invokeCount = 0;
vm.Command2.Subscribe(_ => invokeCount += 1);
var disp = fixture.BindCommand(vm, view, x => x.Command2, x => x.Command2, "MouseUp");
view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = Image.MouseUpEvent });
disp.Dispose();
view.Command2.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = Image.MouseUpEvent });
Assert.Equal(1, invokeCount);
}
#endif
}
}