PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/andreiagaita/moon
C# | 533 lines | 407 code | 90 blank | 36 comment | 0 complexity | 28413b2fe0024620c6d5bae695edd7a1 MD5 | raw file
  1. //
  2. // CollectionViewTest.cs
  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.Collections.Generic;
  30. using System.Collections.Specialized;
  31. using System.ComponentModel;
  32. using System.Linq;
  33. using System.Windows.Data;
  34. using Microsoft.VisualStudio.TestTools.UnitTesting;
  35. using Mono.Moonlight.UnitTesting;
  36. using System.Windows.Shapes;
  37. using System.Globalization;
  38. using System.Collections;
  39. using System.Windows;
  40. namespace MoonTest.System.Windows.Data {
  41. [TestClass]
  42. public class CollectionViewTest_IEditable {
  43. public IEditableCollectionView Editable {
  44. get { return (IEditableCollectionView) View; }
  45. }
  46. List<NotifyCollectionChangedEventArgs> CollectionChangedArgs;
  47. public List<object> Items {
  48. get; set;
  49. }
  50. public CollectionViewSource Source {
  51. get; set;
  52. }
  53. public ICollectionView View {
  54. get { return Source.View; }
  55. }
  56. [TestInitialize]
  57. public void Setup ()
  58. {
  59. Items = new List<object> (new [] {
  60. new object (),
  61. new object (),
  62. new object (),
  63. new object (),
  64. new object (),
  65. });
  66. CollectionChangedArgs = new List<NotifyCollectionChangedEventArgs> ();
  67. Source = new CollectionViewSource { Source = Items };
  68. View.CollectionChanged += (o, e) => CollectionChangedArgs.Add (e);
  69. }
  70. [TestMethod]
  71. public void AddNew_ActuallyAdded ()
  72. {
  73. var orig = new List<object> (Items);
  74. var o = Editable.AddNew ();
  75. Assert.AreEqual (orig.Count + 1, Items.Count, "#1");
  76. Assert.IsFalse (orig.Contains (o), "#2");
  77. }
  78. [TestMethod]
  79. public void AddNew_ArrayInt ()
  80. {
  81. // You can't add to arrays
  82. Source.Source = new int [] { 1, 2, 3 };
  83. Assert.Throws<InvalidOperationException> (() => Editable.AddNew (), "#1");
  84. }
  85. [TestMethod]
  86. public void AddNew_CancelAdd ()
  87. {
  88. var o1 = Editable.AddNew ();
  89. Assert.IsTrue (Editable.IsAddingNew, "#1");
  90. Assert.IsTrue (Items.Contains (o1), "#2");
  91. Editable.CancelNew ();
  92. Assert.IsFalse (Editable.IsAddingNew, "#3");
  93. Assert.IsFalse (Items.Contains (o1), "#4");
  94. }
  95. [TestMethod]
  96. public void AddNew_CommitFilteredItem()
  97. {
  98. // Filter everything then add a new item
  99. View.Filter = o => false;
  100. var item = Editable.AddNew();
  101. Assert.IsTrue(View.Cast<object>().Contains(item), "#1");
  102. Editable.CommitNew();
  103. Assert.IsFalse(View.Cast<object>().Contains(item), "#2");
  104. }
  105. [TestMethod]
  106. public void AddNew_DoesNotRegenerateGroups ()
  107. {
  108. var groups = View.Groups;
  109. Editable.AddNew ();
  110. Assert.AreSame (groups, View.Groups, "#1");
  111. Editable.CommitNew ();
  112. Assert.AreSame (groups, View.Groups, "#2");
  113. }
  114. [TestMethod]
  115. public void AddNew_IsAddedToGroups ()
  116. {
  117. // If we 'AddNew' the item ends up in a special group it seems
  118. View.GroupDescriptions.Add (new ConcretePropertyGroupDescription () {
  119. GroupNameFromItemFunc = (item, depth, culture) => Items.IndexOf (item) < 3 ? "First" : "Second"
  120. });
  121. var groups = View.Groups;
  122. var added = Editable.AddNew ();
  123. Assert.AreEqual (3, View.Groups.Count, "#1");
  124. Assert.IsFalse (((CollectionViewGroup) View.Groups [0]).Items.Contains (added), "#2");
  125. Assert.IsFalse (((CollectionViewGroup) View.Groups [1]).Items.Contains (added), "#3");
  126. Assert.AreSame (added, groups [2], "#4");
  127. }
  128. [TestMethod]
  129. public void AddAndCommitNew_IsAddedToGroups ()
  130. {
  131. // If we 'AddNew' the item ends up in a special group it seems
  132. View.GroupDescriptions.Add (new ConcretePropertyGroupDescription () {
  133. GroupNameFromItemFunc = (item, depth, culture) => Items.IndexOf (item) < 3 ? "First" : "Second"
  134. });
  135. var groups = View.Groups;
  136. var added = Editable.AddNew ();
  137. Editable.CommitNew ();
  138. Assert.AreEqual (2, groups.Count, "#1");
  139. Assert.IsFalse (((CollectionViewGroup) groups [0]).Items.Contains (added), "#2");
  140. Assert.IsTrue (((CollectionViewGroup) groups [1]).Items.Contains (added), "#3");
  141. Assert.AreSame (added, ((CollectionViewGroup) groups [1]).Items.Last (), "#4");
  142. Assert.AreSame (groups, View.Groups, "#5");
  143. }
  144. [TestMethod]
  145. public void AddNew_MyList_WithInt ()
  146. {
  147. Source.Source = new MyList { 1, 2, 3 };
  148. Assert.Throws<InvalidOperationException> (() => Editable.AddNew (), "#1");
  149. }
  150. [TestMethod]
  151. public void AddNew_OutOfSync ()
  152. {
  153. var initialCount = Items.Count;
  154. Items.Add (new object ());
  155. Items.Add (new object ());
  156. var alteredCount = Items.Count;
  157. Assert.AreEqual (alteredCount, View.Cast<object> ().Count (), "#1");
  158. var newItem = Editable.AddNew ();
  159. Assert.AreEqual (alteredCount + 1, View.Cast<object> ().Count (), "#2");
  160. Assert.IsTrue (View.MoveCurrentTo (Items [initialCount + 1]), "#3");
  161. Assert.AreEqual (Items [initialCount + 1], View.CurrentItem, "#4");
  162. }
  163. [TestMethod]
  164. public void AddNew_OutOfSync_Filtered ()
  165. {
  166. Source.Filter += (o, e) => e.Accepted = true;
  167. TestClonesCollection ();
  168. }
  169. [TestMethod]
  170. public void AddNew_OutOfSync_Grouped ()
  171. {
  172. Source.GroupDescriptions.Add (new ConcretePropertyGroupDescription () {
  173. GroupNameFromItemFunc = (item, depth, culture) => Items.IndexOf (item) < 3 ? "A" : "B",
  174. });
  175. TestClonesCollection ();
  176. }
  177. [TestMethod]
  178. public void AddNew_OutOfSync_Sorted ()
  179. {
  180. Source.SortDescriptions.Add (new SortDescription ("blah", ListSortDirection.Ascending));
  181. TestClonesCollection ();
  182. }
  183. void TestClonesCollection ()
  184. {
  185. var initialCount = Items.Count;
  186. Items.Add (new object ());
  187. Items.Add (new object ());
  188. var alteredCount = Items.Count;
  189. Assert.AreEqual (initialCount, View.Cast<object> ().Count (), "#1");
  190. var newItem = Editable.AddNew ();
  191. Assert.AreEqual (initialCount + 1, View.Cast<object> ().Count (), "#2");
  192. Assert.IsFalse (View.MoveCurrentTo (Items [initialCount + 1]), "#3");
  193. Assert.IsNull (View.CurrentItem, "#4");
  194. }
  195. [TestMethod]
  196. public void AddNew_ListObject_WithInt ()
  197. {
  198. Source.Source = new List<object> { 1, 2, 3 };
  199. Assert.IsInstanceOfType<object> (Editable.AddNew (), "#1");
  200. }
  201. [TestMethod]
  202. public void AddNew_ListObject_WithObject ()
  203. {
  204. Assert.IsInstanceOfType<object> (Editable.AddNew (), "#1");
  205. }
  206. [TestMethod]
  207. public void AddNew_ListObject_WithRectangles ()
  208. {
  209. Source.Source = new List<object> { new Rectangle (), new Rectangle () };
  210. Assert.IsInstanceOfType<object> (Editable.AddNew (), "#1");
  211. }
  212. [TestMethod]
  213. public void EventOrdering_EditableAddItem ()
  214. {
  215. var item = Editable.AddNew ();
  216. Assert.AreEqual (1, CollectionChangedArgs.Count, "#1");
  217. var args = CollectionChangedArgs [0];
  218. Assert.AreEqual (NotifyCollectionChangedAction.Add, args.Action, "#3");
  219. Assert.AreEqual (5, args.NewStartingIndex, "#4");
  220. Assert.AreEqual (item, args.NewItems [0], "#5");
  221. }
  222. [TestMethod]
  223. public void EventOrdering_CommitFilteredItem ()
  224. {
  225. // Filter everything then add a new item
  226. View.Filter = o => false;
  227. Editable.AddNew ();
  228. CollectionChangedArgs.Clear ();
  229. Editable.CommitNew ();
  230. Assert.AreEqual (1, CollectionChangedArgs.Count, "#1");
  231. Assert.AreEqual (NotifyCollectionChangedAction.Remove, CollectionChangedArgs[0].Action, "#2");
  232. }
  233. [TestMethod]
  234. public void EventOrdering_CommitItem_Sorted()
  235. {
  236. var items = new List<Rectangle>() {
  237. new Rectangle { Width = 0 },
  238. new Rectangle { Width = 1 },
  239. new Rectangle { Width = 3 },
  240. new Rectangle { Width = 4 },
  241. };
  242. Source.Source = items;
  243. View.CollectionChanged += (o, e) => CollectionChangedArgs.Add(e);
  244. View.SortDescriptions.Add(new SortDescription("Width", ListSortDirection.Ascending));
  245. Editable.AddNew();
  246. CollectionChangedArgs.Clear();
  247. Editable.CommitNew();
  248. Assert.AreEqual(2, CollectionChangedArgs.Count, "#1");
  249. Assert.AreEqual(NotifyCollectionChangedAction.Remove, CollectionChangedArgs[0].Action, "#2");
  250. Assert.AreEqual(4, CollectionChangedArgs[0].OldStartingIndex, "#3");
  251. Assert.AreEqual(NotifyCollectionChangedAction.Add, CollectionChangedArgs[1].Action, "#4");
  252. Assert.AreEqual(0, CollectionChangedArgs[1].NewStartingIndex, "#5");
  253. }
  254. [TestMethod]
  255. public void AddNew_Sorted ()
  256. {
  257. var items = new List<Rectangle> () {
  258. new Rectangle { Width = 0 },
  259. new Rectangle { Width = 1 },
  260. new Rectangle { Width = 3 },
  261. new Rectangle { Width = 4 },
  262. };
  263. Source.Source = items;
  264. Source.SortDescriptions.Add (new SortDescription ("Width", ListSortDirection.Ascending));
  265. var rect = Editable.AddNew () as Rectangle;
  266. Assert.AreSame (rect, View.Cast<object> ().Last (), "#1");
  267. Assert.AreSame (rect, View.CurrentItem, "#2");
  268. Assert.AreEqual (4, View.CurrentPosition, "#3");
  269. rect.Width = 2;
  270. Assert.AreSame (rect, View.Cast<object> ().Last (), "#4");
  271. Assert.AreSame (rect, View.CurrentItem, "#5");
  272. Assert.AreEqual (4, View.CurrentPosition, "#6");
  273. Editable.CommitNew ();
  274. Assert.AreSame (rect, View.Cast<object> ().ElementAt (2), "#7");
  275. Assert.AreSame (rect, View.CurrentItem, "#8");
  276. Assert.AreEqual (2, View.CurrentPosition, "#9");
  277. }
  278. [TestMethod]
  279. public void AddNew_Sorted_SameKeys ()
  280. {
  281. var items = new List<Rectangle> () {
  282. new Rectangle { Width = 0 },
  283. new Rectangle { Width = 2, Height = 1 },
  284. new Rectangle { Width = 1 },
  285. new Rectangle { Width = 2, Height = 2 },
  286. new Rectangle { Width = 3 },
  287. new Rectangle { Width = 2, Height = 3 },
  288. new Rectangle { Width = 4 },
  289. new Rectangle { Width = 2, Height = 4 },
  290. };
  291. Source.Source = items;
  292. Source.SortDescriptions.Add (new SortDescription ("Width", ListSortDirection.Ascending));
  293. var rect = Editable.AddNew () as Rectangle;
  294. Assert.AreSame (rect, View.Cast<object> ().Last (), "#1");
  295. Assert.AreSame (rect, View.CurrentItem, "#2");
  296. Assert.AreEqual (8, View.CurrentPosition, "#3");
  297. rect.Width = 2;
  298. Assert.AreSame (rect, View.Cast<object> ().Last (), "#4");
  299. Assert.AreSame (rect, View.CurrentItem, "#5");
  300. Assert.AreEqual (8, View.CurrentPosition, "#6");
  301. Editable.CommitNew ();
  302. Assert.AreSame (rect, View.CurrentItem, "#7");
  303. Assert.AreEqual (3, View.CurrentPosition, "#8");
  304. Assert.AreSame (rect, View.Cast<object> ().ElementAt (3), "#9");
  305. }
  306. [TestMethod]
  307. public void AddNew_Twice ()
  308. {
  309. var o1 = Editable.AddNew ();
  310. var o2 = Editable.AddNew ();
  311. Assert.IsTrue (Items.Contains (o1), "#1");
  312. Assert.IsTrue (Items.Contains (o2), "#2");
  313. }
  314. [TestMethod]
  315. public void AddNew_Twice_CancelAdd ()
  316. {
  317. var o1 = Editable.AddNew ();
  318. var o2 = Editable.AddNew ();
  319. Editable.CancelNew ();
  320. Assert.IsTrue (Items.Contains (o1), "#1");
  321. Assert.IsFalse (Items.Contains (o2), "#2");
  322. }
  323. [TestMethod]
  324. public void AddNew_UpdatesViewImmediately ()
  325. {
  326. var groups = View.Groups;
  327. var item = Editable.AddNew ();
  328. Assert.AreEqual (item, View.Cast<object> ().Last (), "#1");
  329. }
  330. [TestMethod]
  331. public void AddNew_WhileUpdating ()
  332. {
  333. using (View.DeferRefresh ())
  334. Assert.Throws<InvalidOperationException> (() => Editable.AddNew ());
  335. }
  336. [TestMethod]
  337. public void CommitNew_NoAdd ()
  338. {
  339. // This has no effect if there's no add new
  340. Editable.CommitNew ();
  341. }
  342. [TestMethod]
  343. public void CancelNew_NoAdd ()
  344. {
  345. // This has no effect if there's no add new
  346. Editable.CancelNew ();
  347. }
  348. [TestMethod]
  349. public void SourceIsEnumerable ()
  350. {
  351. // Raw enumerables are not editable and have their own
  352. // ICollectionView implementation
  353. Source.Source = Items.Select (d => true);
  354. Assert.IsNotInstanceOfType<IEditableCollectionView> (View);
  355. }
  356. [TestMethod]
  357. public void SourceIsArray ()
  358. {
  359. Source.Source = Items.ToArray ();
  360. Assert.IsFalse (Editable.CanAddNew, "#1");
  361. Assert.IsFalse (Editable.CanCancelEdit, "#2");
  362. Assert.IsFalse (Editable.CanRemove, "#3");
  363. }
  364. [TestMethod]
  365. public void SourceIsList ()
  366. {
  367. Assert.IsTrue (Editable.CanAddNew, "#1");
  368. Assert.IsFalse (Editable.CanCancelEdit, "#2");
  369. Assert.IsTrue (Editable.CanRemove, "#3");
  370. }
  371. }
  372. public class MyList : IList {
  373. List<object> list = new List<object> ();
  374. public int Add (object value)
  375. {
  376. list.Add (value);
  377. return 1;
  378. }
  379. public void Clear ()
  380. {
  381. list.Clear ();
  382. }
  383. public bool Contains (object value)
  384. {
  385. return list.Contains (value);
  386. }
  387. public int IndexOf (object value)
  388. {
  389. return list.IndexOf (value);
  390. }
  391. public void Insert (int index, object value)
  392. {
  393. list.Insert (index, value);
  394. }
  395. public bool IsFixedSize
  396. {
  397. get { return ((IList) list).IsFixedSize; }
  398. }
  399. public bool IsReadOnly
  400. {
  401. get { return ((IList) list).IsReadOnly; }
  402. }
  403. public void Remove (object value)
  404. {
  405. list.Remove (value);
  406. }
  407. public void RemoveAt (int index)
  408. {
  409. list.RemoveAt (index);
  410. }
  411. public object this [int index]
  412. {
  413. get
  414. {
  415. return list [index];
  416. }
  417. set
  418. {
  419. list [index] = value;
  420. }
  421. }
  422. public void CopyTo (Array array, int index)
  423. {
  424. ((IList)list).CopyTo (array, index);
  425. }
  426. public int Count
  427. {
  428. get { return list.Count; }
  429. }
  430. public bool IsSynchronized
  431. {
  432. get { return ((IList) list).IsSynchronized; }
  433. }
  434. public object SyncRoot
  435. {
  436. get { return ((IList) list).SyncRoot; }
  437. }
  438. public IEnumerator GetEnumerator ()
  439. {
  440. return list.GetEnumerator ();
  441. }
  442. }
  443. }