AvaloniaListTests.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.Specialized;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using Avalonia.Collections;
  9. using Xunit;
  10. namespace Avalonia.Base.UnitTests.Collections
  11. {
  12. public class AvaloniaListTests
  13. {
  14. [Fact]
  15. public void Items_Passed_To_Constructor_Should_Appear_In_List()
  16. {
  17. var items = new[] { 1, 2, 3 };
  18. var target = new AvaloniaList<int>(items);
  19. Assert.Equal(items, target);
  20. }
  21. [Fact]
  22. public void AddRange_With_Null_Should_Throw_Exception()
  23. {
  24. var target = new AvaloniaList<int>();
  25. Assert.Throws<ArgumentNullException>(() => target.AddRange(null));
  26. }
  27. [Fact]
  28. public void RemoveAll_With_Null_Should_Throw_Exception()
  29. {
  30. var target = new AvaloniaList<int>();
  31. Assert.Throws<ArgumentNullException>(() => target.RemoveAll(null));
  32. }
  33. [Fact]
  34. public void InsertRange_With_Null_Should_Throw_Exception()
  35. {
  36. var target = new AvaloniaList<int>();
  37. Assert.Throws<ArgumentNullException>(() => target.InsertRange(1, null));
  38. }
  39. [Fact]
  40. public void InsertRange_Past_End_Should_Throw_Exception()
  41. {
  42. var target = new AvaloniaList<int>();
  43. Assert.Throws<ArgumentOutOfRangeException>(() => target.InsertRange(1, new List<int>() { 1 }));
  44. }
  45. [Fact]
  46. public void Move_Should_Update_Collection()
  47. {
  48. var target = new AvaloniaList<int>(new[] { 1, 2, 3 });
  49. target.Move(2, 0);
  50. Assert.Equal(new[] { 3, 1, 2 }, target);
  51. }
  52. [Fact]
  53. public void MoveRange_Should_Update_Collection()
  54. {
  55. var target = new AvaloniaList<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  56. target.MoveRange(4, 3, 0);
  57. Assert.Equal(new[] { 5, 6, 7, 1, 2, 3, 4, 8, 9, 10 }, target);
  58. }
  59. [Fact]
  60. public void MoveRange_Can_Move_To_End()
  61. {
  62. var target = new AvaloniaList<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  63. target.MoveRange(0, 5, 10);
  64. Assert.Equal(new[] { 6, 7, 8, 9, 10, 1, 2, 3, 4, 5 }, target);
  65. }
  66. [Fact]
  67. public void MoveRange_Raises_Correct_CollectionChanged_Event()
  68. {
  69. var target = new AvaloniaList<int>(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
  70. var raised = false;
  71. target.CollectionChanged += (s, e) =>
  72. {
  73. Assert.Equal(NotifyCollectionChangedAction.Move, e.Action);
  74. Assert.Equal(0, e.OldStartingIndex);
  75. Assert.Equal(10, e.NewStartingIndex);
  76. Assert.Equal(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, e.OldItems);
  77. Assert.Equal(new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, e.NewItems);
  78. raised = true;
  79. };
  80. target.MoveRange(0, 9, 10);
  81. Assert.True(raised);
  82. Assert.Equal(new[] { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, target);
  83. }
  84. [Fact]
  85. public void Adding_Item_Should_Raise_CollectionChanged()
  86. {
  87. var target = new AvaloniaList<int>(new[] { 1, 2 });
  88. var raised = false;
  89. target.CollectionChanged += (s, e) =>
  90. {
  91. Assert.Equal(target, s);
  92. Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
  93. Assert.Equal(new[] { 3 }, e.NewItems.Cast<int>());
  94. Assert.Equal(2, e.NewStartingIndex);
  95. raised = true;
  96. };
  97. target.Add(3);
  98. Assert.True(raised);
  99. }
  100. [Fact]
  101. public void Adding_Items_Should_Raise_CollectionChanged()
  102. {
  103. var target = new AvaloniaList<int>(new[] { 1, 2 });
  104. var raised = false;
  105. target.CollectionChanged += (s, e) =>
  106. {
  107. Assert.Equal(target, s);
  108. Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
  109. Assert.Equal(new[] { 3, 4 }, e.NewItems.Cast<int>());
  110. Assert.Equal(2, e.NewStartingIndex);
  111. raised = true;
  112. };
  113. target.AddRange(new[] { 3, 4 });
  114. Assert.True(raised);
  115. }
  116. [Fact]
  117. public void Replacing_Item_Should_Raise_CollectionChanged()
  118. {
  119. var target = new AvaloniaList<int>(new[] { 1, 2 });
  120. var raised = false;
  121. target.CollectionChanged += (s, e) =>
  122. {
  123. Assert.Equal(target, s);
  124. Assert.Equal(NotifyCollectionChangedAction.Replace, e.Action);
  125. Assert.Equal(new[] { 2 }, e.OldItems.Cast<int>());
  126. Assert.Equal(new[] { 3 }, e.NewItems.Cast<int>());
  127. Assert.Equal(1, e.OldStartingIndex);
  128. Assert.Equal(1, e.NewStartingIndex);
  129. raised = true;
  130. };
  131. target[1] = 3;
  132. Assert.True(raised);
  133. }
  134. [Fact]
  135. public void Inserting_Item_Should_Raise_CollectionChanged()
  136. {
  137. var target = new AvaloniaList<int>(new[] { 1, 2 });
  138. var raised = false;
  139. target.CollectionChanged += (s, e) =>
  140. {
  141. Assert.Equal(target, s);
  142. Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
  143. Assert.Equal(new[] { 3 }, e.NewItems.Cast<int>());
  144. Assert.Equal(1, e.NewStartingIndex);
  145. raised = true;
  146. };
  147. target.Insert(1, 3);
  148. Assert.True(raised);
  149. }
  150. [Fact]
  151. public void Inserting_Items_Should_Raise_CollectionChanged()
  152. {
  153. var target = new AvaloniaList<int>(new[] { 1, 2 });
  154. var raised = false;
  155. target.CollectionChanged += (s, e) =>
  156. {
  157. Assert.Equal(target, s);
  158. Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
  159. Assert.Equal(new[] { 3, 4 }, e.NewItems.Cast<int>());
  160. Assert.Equal(1, e.NewStartingIndex);
  161. raised = true;
  162. };
  163. target.InsertRange(1, new[] { 3, 4 });
  164. Assert.True(raised);
  165. }
  166. [Fact]
  167. public void Removing_Item_Should_Raise_CollectionChanged()
  168. {
  169. var target = new AvaloniaList<int>(new[] { 1, 2, 3 });
  170. var raised = false;
  171. target.CollectionChanged += (s, e) =>
  172. {
  173. Assert.Equal(target, s);
  174. Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action);
  175. Assert.Equal(new[] { 3 }, e.OldItems.Cast<int>());
  176. Assert.Equal(2, e.OldStartingIndex);
  177. raised = true;
  178. };
  179. target.Remove(3);
  180. Assert.True(raised);
  181. }
  182. [Fact]
  183. public void Moving_Item_Should_Raise_CollectionChanged()
  184. {
  185. var target = new AvaloniaList<int>(new[] { 1, 2, 3 });
  186. var raised = false;
  187. target.CollectionChanged += (s, e) =>
  188. {
  189. Assert.Equal(target, s);
  190. Assert.Equal(NotifyCollectionChangedAction.Move, e.Action);
  191. Assert.Equal(new[] { 3 }, e.OldItems.Cast<int>());
  192. Assert.Equal(new[] { 3 }, e.NewItems.Cast<int>());
  193. Assert.Equal(2, e.OldStartingIndex);
  194. Assert.Equal(0, e.NewStartingIndex);
  195. raised = true;
  196. };
  197. target.Move(2, 0);
  198. Assert.True(raised);
  199. }
  200. [Fact]
  201. public void Moving_Items_Should_Raise_CollectionChanged()
  202. {
  203. var target = new AvaloniaList<int>(new[] { 1, 2, 3 });
  204. var raised = false;
  205. target.CollectionChanged += (s, e) =>
  206. {
  207. Assert.Equal(target, s);
  208. Assert.Equal(NotifyCollectionChangedAction.Move, e.Action);
  209. Assert.Equal(new[] { 2, 3 }, e.OldItems.Cast<int>());
  210. Assert.Equal(new[] { 2, 3 }, e.NewItems.Cast<int>());
  211. Assert.Equal(1, e.OldStartingIndex);
  212. Assert.Equal(0, e.NewStartingIndex);
  213. raised = true;
  214. };
  215. target.MoveRange(1, 2, 0);
  216. Assert.True(raised);
  217. }
  218. [Fact]
  219. public void Clearing_Items_Should_Raise_CollectionChanged_Reset()
  220. {
  221. var target = new AvaloniaList<int>(new[] { 1, 2, 3 });
  222. var raised = false;
  223. target.CollectionChanged += (s, e) =>
  224. {
  225. Assert.Equal(target, s);
  226. Assert.Equal(NotifyCollectionChangedAction.Reset, e.Action);
  227. raised = true;
  228. };
  229. target.Clear();
  230. Assert.True(raised);
  231. }
  232. [Fact]
  233. public void Clearing_Items_Should_Raise_CollectionChanged_Remove()
  234. {
  235. var target = new AvaloniaList<int>(new[] { 1, 2, 3 });
  236. var raised = false;
  237. target.ResetBehavior = ResetBehavior.Remove;
  238. target.CollectionChanged += (s, e) =>
  239. {
  240. Assert.Equal(target, s);
  241. Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action);
  242. Assert.Equal(new[] { 1, 2, 3 }, e.OldItems.Cast<int>());
  243. Assert.Equal(0, e.OldStartingIndex);
  244. raised = true;
  245. };
  246. target.Clear();
  247. Assert.True(raised);
  248. }
  249. }
  250. }