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