-
Notifications
You must be signed in to change notification settings - Fork 690
/
Given_Binder.inpc.cs
222 lines (188 loc) · 5.43 KB
/
Given_Binder.inpc.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
using CommonServiceLocator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Uno.Extensions;
using Uno.UI.DataBinding;
using Microsoft.UI.Xaml.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using Uno.Disposables;
using Uno.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml.Controls;
using System.Threading;
using Uno.UI.Xaml;
using System.Diagnostics;
using System.Globalization;
namespace Uno.UI.Tests.BinderTests
{
[TestClass]
public partial class Given_Binder_INPC
{
[TestMethod]
public void When_INPC_Update_SameReference()
{
var SUT = new Binder_INPC_Data();
SUT.SetBinding(Binder_INPC_Data.MyValueProperty, new Binding { Path = new PropertyPath("Class1.Value") });
var master = new Binder_INPC_Base_Class();
SUT.DataContext = master;
Assert.AreEqual(SUT.MyValue, master.Class1.Value);
Assert.AreEqual(2, master.Class1.ValueGetCount);
Assert.AreEqual(0, master.Class1.ValueSetCount);
master.Update();
Assert.AreEqual(SUT.MyValue, master.Class1.Value);
Assert.AreEqual(4, master.Class1.ValueGetCount);
Assert.AreEqual(1, master.Class1.ValueSetCount);
}
[TestMethod]
public void When_INPC_Update_SameReference_Converter()
{
var SUT = new Binder_INPC_Data();
SUT.SetBinding(
Binder_INPC_Data.MyValueProperty,
new Binding
{
Path = new PropertyPath("Value"),
Converter = new Binder_INPC_DummyConverter()
});
var master = new Binder_INPC_Base_Class();
SUT.DataContext = master;
Assert.AreEqual(1, master.ValueGetCount);
Assert.AreEqual(0, master.ValueSetCount);
master.RaiseUpdated();
Assert.AreEqual(SUT.MyValue, master.Value);
Assert.AreEqual(3, master.ValueGetCount);
Assert.AreEqual(0, master.ValueSetCount);
}
[TestMethod]
public void When_INPC_Raise_All_Updated()
{
var SUT = new Binder_INPC_Data();
SUT.SetBinding(
Binder_INPC_Data.MyValueProperty,
new Binding
{
Path = new PropertyPath("Class1.Value")
});
SUT.SetBinding(
Binder_INPC_Data.MyValue2Property,
new Binding
{
Path = new PropertyPath("Value"),
Converter = new Binder_INPC_DummyConverter()
});
var master = new Binder_INPC_Base_Class();
SUT.DataContext = master;
master.RaiseAllUpdated();
Assert.AreEqual(SUT.MyValue, master.Class1.Value);
Assert.AreEqual(SUT.MyValue2, master.Value);
}
}
public partial class Binder_INPC_Data : DependencyObject
{
public string MyValuePropertyValueDuringChange { get; private set; }
public string MyValue
{
get => GetMyValueValue();
set => SetMyValueValue(value);
}
[GeneratedDependencyProperty(DefaultValue = "")]
public static DependencyProperty MyValueProperty { get; } = CreateMyValueProperty();
private void OnMyValueChanged(string oldValue, string newValue)
{
MyValuePropertyValueDuringChange = MyValue;
}
public string MyValue2
{
get => GetMyValue2Value();
set => SetMyValue2Value(value);
}
[GeneratedDependencyProperty(DefaultValue = "")]
public static DependencyProperty MyValue2Property { get; } = CreateMyValue2Property();
}
public class Binder_INPC_Base_Class : Binder_INPC_BaseViewModel
{
private string _value = Guid.NewGuid().ToString();
public int ValueGetCount { get; private set; }
public int ValueSetCount { get; private set; }
public Binder_INPC_Class1 Class1 { get; set; } = new Binder_INPC_Class1();
public string Value
{
get
{
ValueGetCount++;
return _value;
}
set
{
ValueSetCount++;
_value = value;
}
}
public void Update()
{
Class1.Value = (Binder_INPC_Class1.IndexCounter++).ToString(CultureInfo.InvariantCulture);
Value = (Binder_INPC_Class1.IndexCounter++).ToString(CultureInfo.InvariantCulture);
RaiseUpdated();
}
public void RaiseUpdated()
{
OnPropertyChanged(nameof(Class1));
OnPropertyChanged(nameof(Value));
}
public void RaiseAllUpdated()
{
Class1.Value = (Binder_INPC_Class1.IndexCounter++).ToString(CultureInfo.InvariantCulture);
Value = (Binder_INPC_Class1.IndexCounter++).ToString(CultureInfo.InvariantCulture);
OnPropertyChanged(string.Empty);
}
}
public class Binder_INPC_Class1
{
public static int IndexCounter = 0;
private string _value = (IndexCounter++).ToString(CultureInfo.InvariantCulture);
public int ValueGetCount { get; set; }
public int ValueSetCount { get; set; }
public string Value
{
get
{
ValueGetCount++;
return _value;
}
set
{
ValueSetCount++;
_value = value;
}
}
}
public class Binder_INPC_BaseViewModel : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
=> PropertyChanged?.Invoke(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
public class Binder_INPC_DummyConverter : IValueConverter
{
public static int ConvertCount { get; set; }
public static int ConvertBackCount { get; set; }
public object Convert(object value, Type targetType, object parameter, string language)
{
ConvertCount++;
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
ConvertBackCount++;
return value;
}
}
}