Mercurial > silverbladetech
annotate SilverlightValidation/SilverlightValidation.Tests/TestSupport/NotifyPropertyChangedEventWatcher.cs @ 97:1adc1ae981ea
Tests added to SilverlightValidation.Tests
author | stevenhollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sat, 05 May 2012 16:39:00 +0100 |
parents | |
children |
rev | line source |
---|---|
97
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
1 using System; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
2 using System.Collections.Generic; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
3 using System.ComponentModel; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
4 using System.Diagnostics; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
5 using System.Diagnostics.CodeAnalysis; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
6 using System.Globalization; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
7 using System.Linq.Expressions; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
8 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
9 namespace SilverlightValidation.Tests.TestSupport |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
10 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
11 /// <summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
12 /// A helper class used by NotifyPropertyChangedAssertHelper to monitor PropertyChangedEvent notifications. |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
13 /// </summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
14 public class NotifyPropertyChangedEventWatcher : Disposable |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
15 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
16 private readonly Dictionary<string, int> _raisedCounts = new Dictionary<string, int>(); |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
17 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
18 // A reference to the instance of the class implementing INotifyPropertyChanged |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
19 private INotifyPropertyChanged _model; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
20 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
21 /// <summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
22 /// Initializes a new instance of the <see cref = "NotifyPropertyChangedEventWatcher" /> class. |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
23 /// </summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
24 /// <param name = "model">The model.</param> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
25 public NotifyPropertyChangedEventWatcher(INotifyPropertyChanged model) |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
26 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
27 if (model == null) throw new ArgumentNullException("model"); |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
28 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
29 _model = model; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
30 _model.PropertyChanged += OnPropertyChanged; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
31 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
32 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
33 [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters"), |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
34 SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures")] |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
35 public int GetRaisedCount<T>(Expression<Func<T>> propertyExpression) |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
36 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
37 return GetRaisedCount(PropertySupport.ExtractPropertyName(propertyExpression)); |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
38 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
39 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
40 public int GetRaisedCount(string propertyName) |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
41 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
42 propertyName = string.IsNullOrEmpty(propertyName) ? string.Empty : propertyName; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
43 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
44 PropertySupport.VerifyPropertyName(_model, propertyName); |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
45 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
46 return _raisedCounts.ContainsKey(propertyName) ? _raisedCounts[propertyName] : 0; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
47 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
48 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
49 private void RecordPropertyChanged(string propertyName) |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
50 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
51 if (_raisedCounts.ContainsKey(propertyName)) |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
52 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
53 _raisedCounts[propertyName]++; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
54 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
55 else |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
56 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
57 _raisedCounts[propertyName] = 1; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
58 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
59 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
60 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
61 /// <summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
62 /// The OnPropertyChanged event handler. |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
63 /// </summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
64 /// <remarks> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
65 /// Records how many times the monitored property name has changed. |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
66 /// </remarks> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
67 private void OnPropertyChanged(object sender, PropertyChangedEventArgs eventArgs) |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
68 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
69 // If PropertyName is null or string.Empty then it's the 'all properties' changed event. |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
70 RecordPropertyChanged(string.IsNullOrEmpty(eventArgs.PropertyName) ? string.Empty : eventArgs.PropertyName); |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
71 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
72 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
73 /// <summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
74 /// Releases unmanaged and - optionally - managed resources |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
75 /// </summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
76 /// <param name = "disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
77 protected override void Dispose(bool disposing) |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
78 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
79 if (null != _model) |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
80 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
81 var model = _model; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
82 _model = null; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
83 model.PropertyChanged -= OnPropertyChanged; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
84 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
85 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
86 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
87 /// <summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
88 /// Traces the event counts. |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
89 /// </summary> |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
90 [Conditional("DEBUG")] |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
91 public void TraceEventCounts() |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
92 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
93 foreach (var pair in _raisedCounts) |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
94 { |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
95 string name = string.IsNullOrEmpty(pair.Key) ? "all properties" : pair.Key; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
96 int value = pair.Value; |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
97 |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
98 Debug.WriteLine(string.Format( |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
99 CultureInfo.InvariantCulture, @"Property '{0}' raised {1} times.", name, value)); |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
100 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
101 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
102 } |
1adc1ae981ea
Tests added to SilverlightValidation.Tests
stevenhollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
103 } |