|
11 | 11 | using Microsoft.Toolkit.Mvvm.ComponentModel;
|
12 | 12 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
13 | 13 |
|
| 14 | +#pragma warning disable SA1124 |
| 15 | + |
14 | 16 | #nullable enable
|
15 | 17 |
|
16 | 18 | namespace UnitTests.Mvvm
|
@@ -58,6 +60,86 @@ public void Test_ObservablePropertyAttribute_Events()
|
58 | 60 | Assert.AreEqual(changed.Item2, 42);
|
59 | 61 | }
|
60 | 62 |
|
| 63 | + // See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225 |
| 64 | + [TestCategory("Mvvm")] |
| 65 | + [TestMethod] |
| 66 | + public void Test_ObservablePropertyAttributeWithinRegion_Events() |
| 67 | + { |
| 68 | + var model = new SampleModel(); |
| 69 | + |
| 70 | + (PropertyChangingEventArgs, int) changing = default; |
| 71 | + (PropertyChangedEventArgs, int) changed = default; |
| 72 | + |
| 73 | + model.PropertyChanging += (s, e) => |
| 74 | + { |
| 75 | + Assert.IsNull(changing.Item1); |
| 76 | + Assert.IsNull(changed.Item1); |
| 77 | + Assert.AreSame(model, s); |
| 78 | + Assert.IsNotNull(s); |
| 79 | + Assert.IsNotNull(e); |
| 80 | + |
| 81 | + changing = (e, model.Counter); |
| 82 | + }; |
| 83 | + |
| 84 | + model.PropertyChanged += (s, e) => |
| 85 | + { |
| 86 | + Assert.IsNotNull(changing.Item1); |
| 87 | + Assert.IsNull(changed.Item1); |
| 88 | + Assert.AreSame(model, s); |
| 89 | + Assert.IsNotNull(s); |
| 90 | + Assert.IsNotNull(e); |
| 91 | + |
| 92 | + changed = (e, model.Counter); |
| 93 | + }; |
| 94 | + |
| 95 | + model.Counter = 42; |
| 96 | + |
| 97 | + Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Counter)); |
| 98 | + Assert.AreEqual(changing.Item2, 0); |
| 99 | + Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Counter)); |
| 100 | + Assert.AreEqual(changed.Item2, 42); |
| 101 | + } |
| 102 | + |
| 103 | + // See https://github.com/CommunityToolkit/WindowsCommunityToolkit/issues/4225 |
| 104 | + [TestCategory("Mvvm")] |
| 105 | + [TestMethod] |
| 106 | + public void Test_ObservablePropertyAttributeRightBelowRegion_Events() |
| 107 | + { |
| 108 | + var model = new SampleModel(); |
| 109 | + |
| 110 | + (PropertyChangingEventArgs, string?) changing = default; |
| 111 | + (PropertyChangedEventArgs, string?) changed = default; |
| 112 | + |
| 113 | + model.PropertyChanging += (s, e) => |
| 114 | + { |
| 115 | + Assert.IsNull(changing.Item1); |
| 116 | + Assert.IsNull(changed.Item1); |
| 117 | + Assert.AreSame(model, s); |
| 118 | + Assert.IsNotNull(s); |
| 119 | + Assert.IsNotNull(e); |
| 120 | + |
| 121 | + changing = (e, model.Name); |
| 122 | + }; |
| 123 | + |
| 124 | + model.PropertyChanged += (s, e) => |
| 125 | + { |
| 126 | + Assert.IsNotNull(changing.Item1); |
| 127 | + Assert.IsNull(changed.Item1); |
| 128 | + Assert.AreSame(model, s); |
| 129 | + Assert.IsNotNull(s); |
| 130 | + Assert.IsNotNull(e); |
| 131 | + |
| 132 | + changed = (e, model.Name); |
| 133 | + }; |
| 134 | + |
| 135 | + model.Name = "Bob"; |
| 136 | + |
| 137 | + Assert.AreEqual(changing.Item1?.PropertyName, nameof(SampleModel.Name)); |
| 138 | + Assert.AreEqual(changing.Item2, null); |
| 139 | + Assert.AreEqual(changed.Item1?.PropertyName, nameof(SampleModel.Name)); |
| 140 | + Assert.AreEqual(changed.Item2, "Bob"); |
| 141 | + } |
| 142 | + |
61 | 143 | [TestCategory("Mvvm")]
|
62 | 144 | [TestMethod]
|
63 | 145 | public void Test_AlsoNotifyChangeForAttribute_Events()
|
@@ -162,6 +244,16 @@ public partial class SampleModel : ObservableObject
|
162 | 244 | /// </summary>
|
163 | 245 | [ObservableProperty]
|
164 | 246 | private int data;
|
| 247 | + |
| 248 | + #region More properties |
| 249 | + |
| 250 | + [ObservableProperty] |
| 251 | + private int counter; |
| 252 | + |
| 253 | + #endregion |
| 254 | + |
| 255 | + [ObservableProperty] |
| 256 | + private string? name; |
165 | 257 | }
|
166 | 258 |
|
167 | 259 | [INotifyPropertyChanged]
|
|
0 commit comments