PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/V2.2/trunk/CAL/Desktop/Composite.Presentation.Tests/Regions/ViewsCollectionFixture.cs

#
C# | 212 lines | 162 code | 34 blank | 16 comment | 11 complexity | 3d1bd8f58ddca686843c263fbd153604 MD5 | raw file
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Composite Application Guidance for Windows Presentation Foundation and Silverlight
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. using System.Collections;
  18. using System.Collections.ObjectModel;
  19. using System.Collections.Specialized;
  20. using System.Linq;
  21. using Microsoft.Practices.Composite.Regions;
  22. using Microsoft.Practices.Composite.Presentation.Regions;
  23. using Microsoft.VisualStudio.TestTools.UnitTesting;
  24. namespace Microsoft.Practices.Composite.Presentation.Tests.Regions
  25. {
  26. [TestClass]
  27. public class ViewsCollectionFixture
  28. {
  29. [TestMethod]
  30. public void CanWrapCollectionCollection()
  31. {
  32. var originalCollection = new ObservableCollection<ItemMetadata>();
  33. IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => true);
  34. Assert.AreEqual(0, viewsCollection.Count());
  35. var item = new object();
  36. originalCollection.Add(new ItemMetadata(item));
  37. Assert.AreEqual(1, viewsCollection.Count());
  38. Assert.AreSame(item, viewsCollection.First());
  39. }
  40. [TestMethod]
  41. public void CanFilterCollection()
  42. {
  43. var originalCollection = new ObservableCollection<ItemMetadata>();
  44. IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => x.Name == "Posible");
  45. originalCollection.Add(new ItemMetadata(new object()));
  46. Assert.AreEqual(0, viewsCollection.Count());
  47. var item = new object();
  48. originalCollection.Add(new ItemMetadata(item) { Name = "Posible" });
  49. Assert.AreEqual(1, viewsCollection.Count());
  50. Assert.AreSame(item, viewsCollection.First());
  51. }
  52. [TestMethod]
  53. public void RaisesCollectionChangedWhenFilteredCollectionChanges()
  54. {
  55. var originalCollection = new ObservableCollection<ItemMetadata>();
  56. IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => x.IsActive);
  57. bool collectionChanged = false;
  58. viewsCollection.CollectionChanged += (s, e) => collectionChanged = true;
  59. originalCollection.Add(new ItemMetadata(new object()) { IsActive = true });
  60. Assert.IsTrue(collectionChanged);
  61. }
  62. [TestMethod]
  63. public void RaisesCollectionChangedWithAddAndRemoveWhenFilteredCollectionChanges()
  64. {
  65. var originalCollection = new ObservableCollection<ItemMetadata>();
  66. IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => x.IsActive);
  67. bool addedToCollection = false;
  68. bool removedFromCollection = false;
  69. viewsCollection.CollectionChanged += (s, e) =>
  70. {
  71. if (e.Action == NotifyCollectionChangedAction.Add)
  72. {
  73. addedToCollection = true;
  74. }
  75. else if (e.Action == NotifyCollectionChangedAction.Remove)
  76. {
  77. removedFromCollection = true;
  78. }
  79. };
  80. var filteredInObject = new ItemMetadata(new object()) { IsActive = true };
  81. originalCollection.Add(filteredInObject);
  82. Assert.IsTrue(addedToCollection);
  83. Assert.IsFalse(removedFromCollection);
  84. originalCollection.Remove(filteredInObject);
  85. Assert.IsTrue(removedFromCollection);
  86. }
  87. [TestMethod]
  88. public void DoesNotRaiseCollectionChangedWhenAddingOrRemovingFilteredOutObject()
  89. {
  90. var originalCollection = new ObservableCollection<ItemMetadata>();
  91. IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => x.IsActive);
  92. bool collectionChanged = false;
  93. viewsCollection.CollectionChanged += (s, e) => collectionChanged = true;
  94. var filteredOutObject = new ItemMetadata(new object()) { IsActive = false };
  95. originalCollection.Add(filteredOutObject);
  96. originalCollection.Remove(filteredOutObject);
  97. Assert.IsFalse(collectionChanged);
  98. }
  99. [TestMethod]
  100. public void CollectionChangedPassesWrappedItemInArgumentsWhenAdding()
  101. {
  102. var originalCollection = new ObservableCollection<ItemMetadata>();
  103. var filteredInObject = new ItemMetadata(new object());
  104. originalCollection.Add(filteredInObject);
  105. IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => true);
  106. IList oldItemsPassed = null;
  107. viewsCollection.CollectionChanged += (s, e) =>
  108. {
  109. oldItemsPassed = e.OldItems;
  110. };
  111. originalCollection.Remove(filteredInObject);
  112. Assert.IsNotNull(oldItemsPassed);
  113. Assert.AreEqual(1, oldItemsPassed.Count);
  114. Assert.AreSame(filteredInObject.Item, oldItemsPassed[0]);
  115. }
  116. [TestMethod]
  117. public void CollectionChangedPassesWrappedItemInArgumentsWhenRemoving()
  118. {
  119. var originalCollection = new ObservableCollection<ItemMetadata>();
  120. IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => true);
  121. IList newItemsPassed = null;
  122. viewsCollection.CollectionChanged += (s, e) =>
  123. {
  124. newItemsPassed = e.NewItems;
  125. };
  126. var filteredInObject = new ItemMetadata(new object());
  127. originalCollection.Add(filteredInObject);
  128. Assert.IsNotNull(newItemsPassed);
  129. Assert.AreEqual(1, newItemsPassed.Count);
  130. Assert.AreSame(filteredInObject.Item, newItemsPassed[0]);
  131. }
  132. [TestMethod]
  133. public void EnumeratesWrappedItems()
  134. {
  135. var originalCollection = new ObservableCollection<ItemMetadata>()
  136. {
  137. new ItemMetadata(new object()),
  138. new ItemMetadata(new object())
  139. };
  140. IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => true);
  141. Assert.AreEqual(2, viewsCollection.Count());
  142. Assert.AreSame(originalCollection[0].Item, viewsCollection.ElementAt(0));
  143. Assert.AreSame(originalCollection[1].Item, viewsCollection.ElementAt(1));
  144. }
  145. [TestMethod]
  146. public void ChangingMetadataOnItemAddsOrRemovesItFromTheFilteredCollection()
  147. {
  148. var originalCollection = new ObservableCollection<ItemMetadata>();
  149. IViewsCollection viewsCollection = new ViewsCollection(originalCollection, x => x.IsActive);
  150. bool addedToCollection = false;
  151. bool removedFromCollection = false;
  152. viewsCollection.CollectionChanged += (s, e) =>
  153. {
  154. if (e.Action == NotifyCollectionChangedAction.Add)
  155. {
  156. addedToCollection = true;
  157. }
  158. else if (e.Action == NotifyCollectionChangedAction.Remove)
  159. {
  160. removedFromCollection = true;
  161. }
  162. };
  163. originalCollection.Add(new ItemMetadata(new object()) { IsActive = true });
  164. Assert.IsFalse(removedFromCollection);
  165. originalCollection[0].IsActive = false;
  166. Assert.AreEqual(0, viewsCollection.Count());
  167. Assert.IsTrue(removedFromCollection);
  168. Assert.IsTrue(addedToCollection);
  169. Assert.AreEqual(0, viewsCollection.Count());
  170. addedToCollection = false;
  171. removedFromCollection = false;
  172. originalCollection[0].IsActive = true;
  173. Assert.AreEqual(1, viewsCollection.Count());
  174. Assert.IsTrue(addedToCollection);
  175. Assert.IsFalse(removedFromCollection);
  176. }
  177. }
  178. }