PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/test/2.0/moon-unit/System.Windows.Data/CollectionViewGroupTest.cs

http://github.com/mono/moon
C# | 319 lines | 230 code | 51 blank | 38 comment | 5 complexity | 8acbf610692411474ca51a0a62adccdb MD5 | raw file
Possible License(s): CC-BY-SA-3.0, MIT, LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. //
  2. // CollectionViewGroup Unit Tests
  3. //
  4. // Contact:
  5. // Moonlight List (moonlight-list@lists.ximian.com)
  6. //
  7. // Copyright 2010 Novell, Inc.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Linq;
  30. using System.Net;
  31. using System.Windows;
  32. using System.Windows.Controls;
  33. using System.Windows.Documents;
  34. using System.Windows.Ink;
  35. using System.Windows.Input;
  36. using System.Windows.Media;
  37. using System.Windows.Media.Animation;
  38. using System.Windows.Shapes;
  39. using Microsoft.VisualStudio.TestTools.UnitTesting;
  40. using System.Windows.Data;
  41. using System.Collections.ObjectModel;
  42. using System.Collections.Generic;
  43. using Mono.Moonlight.UnitTesting;
  44. using System.Globalization;
  45. using System.ComponentModel;
  46. using System.Collections.Specialized;
  47. namespace MoonTest.System.Windows.Data {
  48. [TestClass]
  49. public partial class CollectionViewGroupTest {
  50. [TestMethod]
  51. public void Constructor_Null ()
  52. {
  53. var g = new ConcreteCollectionViewGroup (null);
  54. Assert.IsNull (g.Name, "#1");
  55. }
  56. [TestMethod]
  57. public void Constructor_Object ()
  58. {
  59. object o = new object ();
  60. var g = new ConcreteCollectionViewGroup (o);
  61. Assert.AreSame (o, g.Name, "#1");
  62. }
  63. [TestMethod]
  64. public void AddItem_DoNotIncrementItemCount ()
  65. {
  66. var g = new ConcreteCollectionViewGroup ("");
  67. g.ProtectedItems.Add (new object ());
  68. Assert.AreEqual (0, g.OnPropertyChangedCalled.Count, "#1");
  69. Assert.AreEqual (0, g.ProtectedItemCount, "#2");
  70. Assert.AreEqual (0, g.ItemCount, "#3");
  71. }
  72. [TestMethod]
  73. public void AddItem_IncrementItemCount ()
  74. {
  75. var g = new ConcreteCollectionViewGroup ("");
  76. g.ProtectedItems.Add (new object ());
  77. g.ProtectedItemCount++;
  78. Assert.AreEqual (1, g.OnPropertyChangedCalled.Count, "#1");
  79. Assert.AreEqual ("ItemCount", g.OnPropertyChangedCalled [0], "#2");
  80. Assert.AreEqual (1, g.ProtectedItemCount, "#3");
  81. Assert.AreEqual (1, g.ItemCount, "#4");
  82. }
  83. [TestMethod]
  84. public void GroupsAreRecreated ()
  85. {
  86. Func<object, object, bool> nameMatcher = (groupName, itemName) => (string) groupName == (string) itemName;
  87. Func<object, int, CultureInfo, object> nameCreator = (item, level, culture) => ((int) item <= 2 ? "Lower" : "Upper") + level.ToString ();
  88. var desc = new ConcretePropertyGroupDescription () {
  89. GroupNameFromItemFunc = nameCreator,
  90. NamesMatchFunc = nameMatcher
  91. };
  92. var source = new CollectionViewSource { Source = new [] { 0, 1, 2, 3, 4, 5 } };
  93. source.GroupDescriptions.Add (desc);
  94. var groups = source.View.Groups;
  95. var lowerGroup = (CollectionViewGroup) source.View.Groups [0];
  96. var upperGroup = (CollectionViewGroup) source.View.Groups [1];
  97. using (source.DeferRefresh ())
  98. using (source.View.DeferRefresh ()) {
  99. source.GroupDescriptions.Clear ();
  100. source.GroupDescriptions.Add (desc);
  101. }
  102. Assert.AreSame (groups, source.View.Groups, "#1");
  103. Assert.AreNotSame (lowerGroup, source.View.Groups [0], "#2");
  104. Assert.AreNotSame (upperGroup, source.View.Groups [1], "#3");
  105. }
  106. [TestMethod]
  107. public void OneGroupDesciption ()
  108. {
  109. Func<object, object, bool> nameMatcher = (groupName, itemName) => (string) groupName == (string) itemName;
  110. Func<object, int, CultureInfo, object> nameCreator = (item, level, culture) => ((int) item <= 2 ? "Lower" : "Upper") + level.ToString ();
  111. var desc = new ConcretePropertyGroupDescription () {
  112. GroupNameFromItemFunc = nameCreator,
  113. NamesMatchFunc = nameMatcher
  114. };
  115. var source = new CollectionViewSource { Source = new [] { 0, 1, 2, 3, 4, 5 } };
  116. using (source.View.DeferRefresh ()) {
  117. source.GroupDescriptions.Add (desc);
  118. }
  119. Assert.AreEqual (2, source.View.Groups.Count, "#1");
  120. var lowerGroup = (CollectionViewGroup) source.View.Groups [0];
  121. var upperGroup = (CollectionViewGroup) source.View.Groups [1];
  122. Assert.AreEqual ("Lower0", lowerGroup.Name, "#2");
  123. Assert.AreEqual ("Upper0", upperGroup.Name, "#3");
  124. Assert.IsTrue (lowerGroup.IsBottomLevel, "#4");
  125. Assert.IsTrue (upperGroup.IsBottomLevel, "#5");
  126. Assert.AreEqual (3, lowerGroup.ItemCount, "#6");
  127. Assert.AreEqual (3, upperGroup.ItemCount, "#7");
  128. for (int i = 0; i < 3; i++)
  129. Assert.AreEqual (i, (int) lowerGroup.Items [i], "#8." + i);
  130. for (int i = 0; i < 3; i++)
  131. Assert.AreEqual (i + 3, (int) upperGroup.Items [i], "#9." + i);
  132. }
  133. [TestMethod]
  134. public void TwoGroupDesciptions ()
  135. {
  136. /* This test generates two subgroups giving this hierarchy:
  137. * Root
  138. * Lower0
  139. * Lower1 [Will contain (0, 1, 2)]
  140. * Upper1
  141. * Upper0
  142. * Lower1
  143. * Upper1 [Will contain (3, 4, 5)]
  144. */
  145. Func<object, object, bool> nameMatcher = (groupName, itemName) => (string) groupName == (string) itemName;
  146. Func<object, int, CultureInfo, object> nameCreator = (item, level, culture) => ((int) item <= 2 ? "Lower" : "Upper") + level.ToString ();
  147. var level0 = new ConcretePropertyGroupDescription () {
  148. GroupNameFromItemFunc = nameCreator,
  149. NamesMatchFunc = nameMatcher
  150. };
  151. var level1 = new ConcretePropertyGroupDescription () {
  152. GroupNameFromItemFunc = nameCreator,
  153. NamesMatchFunc = nameMatcher
  154. };
  155. var source = new CollectionViewSource { Source = new [] { 0, 1, 2, 3, 4, 5 } };
  156. using (source.View.DeferRefresh ()) {
  157. source.GroupDescriptions.Add (level0);
  158. source.GroupDescriptions.Add (level1);
  159. }
  160. Assert.AreEqual (2, source.View.Groups.Count, "#1");
  161. var lowerGroup = (CollectionViewGroup) source.View.Groups [0];
  162. var upperGroup = (CollectionViewGroup) source.View.Groups [1];
  163. Assert.AreEqual ("Lower0", lowerGroup.Name, "#2");
  164. Assert.AreEqual ("Upper0", upperGroup.Name, "#3");
  165. Assert.IsFalse (lowerGroup.IsBottomLevel, "#4");
  166. Assert.IsFalse (upperGroup.IsBottomLevel, "#5");
  167. Assert.AreEqual (3, lowerGroup.ItemCount, "#6");
  168. Assert.AreEqual (3, upperGroup.ItemCount, "#7");
  169. Assert.AreEqual (1, lowerGroup.Items.Count, "#8");
  170. Assert.AreEqual (1, upperGroup.Items.Count, "#9");
  171. // Check the contents of Lower0
  172. var lower = (CollectionViewGroup) lowerGroup.Items [0];
  173. Assert.AreEqual ("Lower1", lower.Name, "#10");
  174. Assert.IsTrue (lower.IsBottomLevel, "#12");
  175. Assert.AreEqual (3, lower.ItemCount, "#14");
  176. Assert.AreEqual (3, lower.Items.Count, "#16");
  177. // Check the contents of Upper0
  178. var upper = (CollectionViewGroup) upperGroup.Items [0];
  179. Assert.AreEqual ("Upper1", upper.Name, "#11");
  180. Assert.IsTrue (upper.IsBottomLevel, "#13");
  181. Assert.AreEqual (3, upper.ItemCount, "#15");
  182. Assert.AreEqual (3, upper.Items.Count, "#17");
  183. }
  184. [TestMethod]
  185. public void OneItem_TwoGroups()
  186. {
  187. var o = new object();
  188. var source = new CollectionViewSource { Source = new[] { o } };
  189. source.GroupDescriptions.Add (new ConcretePropertyGroupDescription() {
  190. GroupNameFromItemFunc = (item, level, culture) => new [] { "First", "Second" }
  191. });
  192. Assert.AreEqual (2, source.View.Groups.Count, "#1");
  193. var lowerGroup = (CollectionViewGroup) source.View.Groups [0];
  194. Assert.AreEqual(1, lowerGroup.Items.Count, "#2");
  195. Assert.AreEqual(o, lowerGroup.Items [0], "#3");
  196. var upperGroup = (CollectionViewGroup)source.View.Groups[1];
  197. Assert.AreEqual(1, upperGroup.Items.Count, "#2");
  198. Assert.AreEqual(o, upperGroup.Items[0], "#3");
  199. }
  200. [TestMethod]
  201. public void OneItem_TwoGroups_Remove()
  202. {
  203. var o = new object();
  204. var source = new CollectionViewSource { Source = new List<object> { o } };
  205. source.GroupDescriptions.Add (new ConcretePropertyGroupDescription() {
  206. GroupNameFromItemFunc = (item, level, culture) => new [] { "First", "Second" }
  207. });
  208. ((IEditableCollectionView)source.View).RemoveAt(0);
  209. Assert.AreEqual(0, source.View.Cast<object>().Count(), "#1");
  210. }
  211. [TestMethod]
  212. public void OneItem_TwoGroups_Remove_Events ()
  213. {
  214. var args = new List<NotifyCollectionChangedEventArgs>();
  215. var source = new CollectionViewSource { Source = new List<object> { new object () } };
  216. source.GroupDescriptions.Add(new ConcretePropertyGroupDescription() {
  217. GroupNameFromItemFunc = (item, level, culture) => new[] { "First", "Second" }
  218. });
  219. source.View.CollectionChanged += (o, e) => args.Add(e);
  220. ((IEditableCollectionView)source.View).RemoveAt(0);
  221. Assert.AreEqual(2, args.Count, "#1");
  222. Assert.AreEqual (0, source.View.Cast<object> ().Count (), "#2");
  223. }
  224. [TestMethod]
  225. [MoonlightBug ("This is not important, we remove the two elements in the opposite order to Silverlight")]
  226. public void OneItem_TwoGroups_Remove_Events_CheckOrder ()
  227. {
  228. var args = new List<NotifyCollectionChangedEventArgs>();
  229. var source = new CollectionViewSource { Source = new List<object> { new object () } };
  230. source.GroupDescriptions.Add(new ConcretePropertyGroupDescription() {
  231. GroupNameFromItemFunc = (item, level, culture) => new[] { "First", "Second" }
  232. });
  233. source.View.CollectionChanged += (o, e) => args.Add(e);
  234. ((IEditableCollectionView)source.View).RemoveAt(0);
  235. Assert.AreEqual(2, args.Count, "#1");
  236. Assert.AreEqual(NotifyCollectionChangedAction.Remove, args[0].Action, "#2");
  237. Assert.AreEqual(1, args[0].OldStartingIndex, "#3");
  238. Assert.AreEqual(NotifyCollectionChangedAction.Remove, args[1].Action, "#4");
  239. Assert.AreEqual(0, args[1].OldStartingIndex, "#5");
  240. }
  241. }
  242. class ConcreteCollectionViewGroup : CollectionViewGroup {
  243. public List<string> OnPropertyChangedCalled = new List<string> ();
  244. public List<string> PropertyChangedRaised = new List<string> ();
  245. public ConcreteCollectionViewGroup (object name)
  246. : base (name)
  247. {
  248. PropertyChanged += (o, e) => PropertyChangedRaised.Add (e.PropertyName);
  249. }
  250. public override bool IsBottomLevel
  251. {
  252. get { return true; }
  253. }
  254. protected override void OnPropertyChanged (global::System.ComponentModel.PropertyChangedEventArgs e)
  255. {
  256. OnPropertyChangedCalled.Add (e.PropertyName);
  257. base.OnPropertyChanged (e);
  258. }
  259. public new int ProtectedItemCount {
  260. get { return base.ProtectedItemCount; }
  261. set { base.ProtectedItemCount = value; }
  262. }
  263. public new ObservableCollection<object> ProtectedItems
  264. {
  265. get { return base.ProtectedItems; }
  266. }
  267. }
  268. }