ItemsPresenterTests_Virtualization_Simple.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Linq;
  8. using Avalonia.Collections;
  9. using Avalonia.Controls.Generators;
  10. using Avalonia.Controls.Presenters;
  11. using Avalonia.Controls.Primitives;
  12. using Avalonia.Controls.Templates;
  13. using Avalonia.Input;
  14. using Avalonia.Rendering;
  15. using Avalonia.UnitTests;
  16. using Xunit;
  17. namespace Avalonia.Controls.UnitTests.Presenters
  18. {
  19. public class ItemsPresenterTests_Virtualization_Simple
  20. {
  21. [Fact]
  22. public void Should_Return_Items_Count_For_Extent_Vertical()
  23. {
  24. var target = CreateTarget();
  25. target.ApplyTemplate();
  26. Assert.Equal(new Size(0, 20), ((ILogicalScrollable)target).Extent);
  27. }
  28. [Fact]
  29. public void Should_Return_Items_Count_For_Extent_Horizontal()
  30. {
  31. var target = CreateTarget(orientation: Orientation.Horizontal);
  32. target.ApplyTemplate();
  33. Assert.Equal(new Size(20, 0), ((ILogicalScrollable)target).Extent);
  34. }
  35. [Fact]
  36. public void Should_Have_Number_Of_Visible_Items_As_Viewport_Vertical()
  37. {
  38. var target = CreateTarget();
  39. target.ApplyTemplate();
  40. target.Measure(new Size(100, 100));
  41. target.Arrange(new Rect(0, 0, 100, 100));
  42. Assert.Equal(new Size(0, 10), ((ILogicalScrollable)target).Viewport);
  43. }
  44. [Fact]
  45. public void Should_Have_Number_Of_Visible_Items_As_Viewport_Horizontal()
  46. {
  47. var target = CreateTarget(orientation: Orientation.Horizontal);
  48. target.ApplyTemplate();
  49. target.Measure(new Size(100, 100));
  50. target.Arrange(new Rect(0, 0, 100, 100));
  51. Assert.Equal(new Size(10, 0), ((ILogicalScrollable)target).Viewport);
  52. }
  53. [Fact]
  54. public void Should_Add_Containers_When_Panel_Is_Not_Full()
  55. {
  56. var target = CreateTarget(itemCount: 5);
  57. var items = (IList<string>)target.Items;
  58. target.ApplyTemplate();
  59. target.Measure(new Size(100, 100));
  60. target.Arrange(new Rect(0, 0, 100, 100));
  61. Assert.Equal(5, target.Panel.Children.Count);
  62. items.Add("New Item");
  63. Assert.Equal(6, target.Panel.Children.Count);
  64. }
  65. [Fact]
  66. public void Should_Remove_Items_When_Control_Is_Shrank()
  67. {
  68. var target = CreateTarget();
  69. var items = (IList<string>)target.Items;
  70. target.ApplyTemplate();
  71. target.Measure(new Size(100, 100));
  72. target.Arrange(new Rect(0, 0, 100, 100));
  73. Assert.Equal(10, target.Panel.Children.Count);
  74. target.Measure(new Size(100, 80));
  75. target.Arrange(new Rect(0, 0, 100, 80));
  76. Assert.Equal(8, target.Panel.Children.Count);
  77. }
  78. [Fact]
  79. public void Should_Add_New_Containers_At_Top_When_Control_Is_Scrolled_To_Bottom_And_Enlarged()
  80. {
  81. var target = CreateTarget();
  82. var items = (IList<string>)target.Items;
  83. target.ApplyTemplate();
  84. target.Measure(new Size(100, 100));
  85. target.Arrange(new Rect(0, 0, 100, 100));
  86. Assert.Equal(10, target.Panel.Children.Count);
  87. ((IScrollable)target).Offset = new Vector(0, 10);
  88. target.Measure(new Size(120, 120));
  89. target.Arrange(new Rect(0, 0, 100, 120));
  90. Assert.Equal(12, target.Panel.Children.Count);
  91. for (var i = 0; i < target.Panel.Children.Count; ++i)
  92. {
  93. Assert.Equal(items[i + 8], target.Panel.Children[i].DataContext);
  94. }
  95. }
  96. [Fact]
  97. public void Should_Update_Containers_When_Items_Changes()
  98. {
  99. var target = CreateTarget();
  100. target.ApplyTemplate();
  101. target.Measure(new Size(100, 100));
  102. target.Arrange(new Rect(0, 0, 100, 100));
  103. target.Items = new[] { "foo", "bar", "baz" };
  104. Assert.Equal(3, target.Panel.Children.Count);
  105. }
  106. [Fact]
  107. public void Should_Decrease_The_Viewport_Size_By_One_If_There_Is_A_Partial_Item()
  108. {
  109. var target = CreateTarget();
  110. target.ApplyTemplate();
  111. target.Measure(new Size(100, 95));
  112. target.Arrange(new Rect(0, 0, 100, 95));
  113. Assert.Equal(new Size(0, 9), ((ILogicalScrollable)target).Viewport);
  114. }
  115. [Fact]
  116. public void Moving_To_And_From_The_End_With_Partial_Item_Should_Set_Panel_PixelOffset()
  117. {
  118. var target = CreateTarget();
  119. target.ApplyTemplate();
  120. target.Measure(new Size(100, 95));
  121. target.Arrange(new Rect(0, 0, 100, 95));
  122. ((ILogicalScrollable)target).Offset = new Vector(0, 11);
  123. var minIndex = target.ItemContainerGenerator.Containers.Min(x => x.Index);
  124. Assert.Equal(new Vector(0, 11), ((ILogicalScrollable)target).Offset);
  125. Assert.Equal(10, minIndex);
  126. Assert.Equal(10, ((IVirtualizingPanel)target.Panel).PixelOffset);
  127. ((ILogicalScrollable)target).Offset = new Vector(0, 10);
  128. minIndex = target.ItemContainerGenerator.Containers.Min(x => x.Index);
  129. Assert.Equal(new Vector(0, 10), ((ILogicalScrollable)target).Offset);
  130. Assert.Equal(10, minIndex);
  131. Assert.Equal(0, ((IVirtualizingPanel)target.Panel).PixelOffset);
  132. ((ILogicalScrollable)target).Offset = new Vector(0, 11);
  133. minIndex = target.ItemContainerGenerator.Containers.Min(x => x.Index);
  134. Assert.Equal(new Vector(0, 11), ((ILogicalScrollable)target).Offset);
  135. Assert.Equal(10, minIndex);
  136. Assert.Equal(10, ((IVirtualizingPanel)target.Panel).PixelOffset);
  137. }
  138. [Fact]
  139. public void Inserting_Items_Should_Update_Containers()
  140. {
  141. var target = CreateTarget();
  142. target.ApplyTemplate();
  143. target.Measure(new Size(100, 100));
  144. target.Arrange(new Rect(0, 0, 100, 100));
  145. ((ILogicalScrollable)target).Offset = new Vector(0, 5);
  146. var expected = Enumerable.Range(5, 10).Select(x => $"Item {x}").ToList();
  147. var items = (ObservableCollection<string>)target.Items;
  148. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  149. Assert.Equal(expected, actual);
  150. items.Insert(6, "Inserted");
  151. expected.Insert(1, "Inserted");
  152. expected.RemoveAt(expected.Count - 1);
  153. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  154. Assert.Equal(expected, actual);
  155. }
  156. [Fact]
  157. public void Removing_First_Materialized_Item_Should_Update_Containers()
  158. {
  159. var target = CreateTarget();
  160. target.ApplyTemplate();
  161. target.Measure(new Size(100, 100));
  162. target.Arrange(new Rect(0, 0, 100, 100));
  163. var expected = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList();
  164. var items = (ObservableCollection<string>)target.Items;
  165. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  166. Assert.Equal(expected, actual);
  167. items.RemoveAt(0);
  168. expected = Enumerable.Range(1, 10).Select(x => $"Item {x}").ToList();
  169. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  170. Assert.Equal(expected, actual);
  171. }
  172. [Fact]
  173. public void Removing_Items_From_Middle_Should_Update_Containers_When_All_Items_Visible()
  174. {
  175. var target = CreateTarget();
  176. target.ApplyTemplate();
  177. target.Measure(new Size(100, 200));
  178. target.Arrange(new Rect(0, 0, 100, 200));
  179. var items = (ObservableCollection<string>)target.Items;
  180. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  181. Assert.Equal(items, actual);
  182. items.RemoveAt(2);
  183. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  184. Assert.Equal(items, actual);
  185. items.RemoveAt(items.Count - 2);
  186. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  187. Assert.Equal(items, actual);
  188. }
  189. [Fact]
  190. public void Removing_Last_Item_Should_Update_Containers_When_All_Items_Visible()
  191. {
  192. var target = CreateTarget();
  193. target.ApplyTemplate();
  194. target.Measure(new Size(100, 200));
  195. target.Arrange(new Rect(0, 0, 100, 200));
  196. ((ILogicalScrollable)target).Offset = new Vector(0, 5);
  197. var expected = Enumerable.Range(0, 20).Select(x => $"Item {x}").ToList();
  198. var items = (ObservableCollection<string>)target.Items;
  199. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  200. Assert.Equal(expected, actual);
  201. items.Remove(items.Last());
  202. expected.Remove(expected.Last());
  203. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  204. Assert.Equal(expected, actual);
  205. }
  206. [Fact]
  207. public void Removing_Items_When_Scrolled_To_End_Should_Recyle_Containers_At_Top()
  208. {
  209. var target = CreateTarget(useAvaloniaList: true);
  210. target.ApplyTemplate();
  211. target.Measure(new Size(100, 100));
  212. target.Arrange(new Rect(0, 0, 100, 100));
  213. ((ILogicalScrollable)target).Offset = new Vector(0, 10);
  214. var expected = Enumerable.Range(10, 10).Select(x => $"Item {x}").ToList();
  215. var items = (AvaloniaList<string>)target.Items;
  216. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  217. Assert.Equal(expected, actual);
  218. items.RemoveRange(18, 2);
  219. expected = Enumerable.Range(8, 10).Select(x => $"Item {x}").ToList();
  220. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  221. Assert.Equal(expected, actual);
  222. }
  223. [Fact]
  224. public void Removing_Items_When_Scrolled_To_Near_End_Should_Recycle_Containers_At_Bottom_And_Top()
  225. {
  226. var target = CreateTarget(useAvaloniaList: true);
  227. target.ApplyTemplate();
  228. target.Measure(new Size(100, 100));
  229. target.Arrange(new Rect(0, 0, 100, 100));
  230. ((ILogicalScrollable)target).Offset = new Vector(0, 9);
  231. var expected = Enumerable.Range(9, 10).Select(x => $"Item {x}").ToList();
  232. var items = (AvaloniaList<string>)target.Items;
  233. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  234. Assert.Equal(expected, actual);
  235. items.RemoveRange(15, 3);
  236. expected = Enumerable.Range(7, 8).Select(x => $"Item {x}")
  237. .Concat(Enumerable.Range(18, 2).Select(x => $"Item {x}"))
  238. .ToList();
  239. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  240. Assert.Equal(expected, actual);
  241. }
  242. [Fact]
  243. public void Measuring_To_Infinity_When_Scrolled_To_End_Should_Not_Throw()
  244. {
  245. var target = CreateTarget(useAvaloniaList: true);
  246. target.ApplyTemplate();
  247. target.Measure(new Size(100, 100));
  248. target.Arrange(new Rect(0, 0, 100, 100));
  249. ((ILogicalScrollable)target).Offset = new Vector(0, 10);
  250. // Check for issue #589: this should not throw.
  251. target.Measure(Size.Infinity);
  252. var expected = Enumerable.Range(0, 20).Select(x => $"Item {x}").ToList();
  253. var items = (AvaloniaList<string>)target.Items;
  254. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  255. Assert.Equal(expected, actual);
  256. }
  257. [Fact]
  258. public void Replacing_Items_Should_Update_Containers()
  259. {
  260. var target = CreateTarget();
  261. target.ApplyTemplate();
  262. target.Measure(new Size(100, 100));
  263. target.Arrange(new Rect(0, 0, 100, 100));
  264. var expected = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList();
  265. var items = (ObservableCollection<string>)target.Items;
  266. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  267. Assert.Equal(expected, actual);
  268. items[4] = expected[4] = "Replaced";
  269. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  270. Assert.Equal(expected, actual);
  271. }
  272. [Fact]
  273. public void Moving_Items_Should_Update_Containers()
  274. {
  275. var target = CreateTarget();
  276. target.ApplyTemplate();
  277. target.Measure(new Size(100, 100));
  278. target.Arrange(new Rect(0, 0, 100, 100));
  279. var expected = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList();
  280. var items = (ObservableCollection<string>)target.Items;
  281. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  282. Assert.Equal(expected, actual);
  283. items.Move(4, 8);
  284. var i = expected[4];
  285. expected.RemoveAt(4);
  286. expected.Insert(8, i);
  287. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  288. Assert.Equal(expected, actual);
  289. }
  290. [Fact]
  291. public void Setting_Items_To_Null_Should_Remove_Containers()
  292. {
  293. var target = CreateTarget();
  294. target.ApplyTemplate();
  295. target.Measure(new Size(100, 100));
  296. target.Arrange(new Rect(0, 0, 100, 100));
  297. var expected = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList();
  298. var items = (ObservableCollection<string>)target.Items;
  299. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  300. Assert.Equal(expected, actual);
  301. target.Items = null;
  302. Assert.Empty(target.Panel.Children);
  303. }
  304. [Fact]
  305. public void Reassigning_Items_Should_Create_Containers()
  306. {
  307. var target = CreateTarget(itemCount: 5);
  308. target.ApplyTemplate();
  309. target.Measure(new Size(100, 100));
  310. target.Arrange(new Rect(0, 0, 100, 100));
  311. var expected = Enumerable.Range(0, 5).Select(x => $"Item {x}").ToList();
  312. var items = (ObservableCollection<string>)target.Items;
  313. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  314. Assert.Equal(expected, actual);
  315. expected = Enumerable.Range(0, 6).Select(x => $"Item {x}").ToList();
  316. target.Items = expected;
  317. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  318. Assert.Equal(expected, actual);
  319. }
  320. [Fact]
  321. public void Inserting_Then_Removing_Should_Add_Remove_Containers()
  322. {
  323. var items = new AvaloniaList<string>(Enumerable.Range(0, 5).Select(x => $"Item {x}"));
  324. var toAdd = Enumerable.Range(0, 3).Select(x => $"Added Item {x}").ToArray();
  325. var target = new ItemsPresenter
  326. {
  327. VirtualizationMode = ItemVirtualizationMode.None,
  328. Items = items,
  329. ItemTemplate = new FuncDataTemplate<string>(x => new TextBlock { Height = 10 }),
  330. };
  331. target.ApplyTemplate();
  332. Assert.Equal(items.Count, target.Panel.Children.Count);
  333. int addIndex = 1;
  334. foreach (var item in toAdd)
  335. {
  336. items.Insert(addIndex++, item);
  337. }
  338. Assert.Equal(items.Count, target.Panel.Children.Count);
  339. foreach (var item in toAdd)
  340. {
  341. items.Remove(item);
  342. }
  343. Assert.Equal(items.Count, target.Panel.Children.Count);
  344. }
  345. [Fact]
  346. public void Reassigning_Items_Should_Remove_Containers()
  347. {
  348. var target = CreateTarget(itemCount: 6);
  349. target.ApplyTemplate();
  350. target.Measure(new Size(100, 100));
  351. target.Arrange(new Rect(0, 0, 100, 100));
  352. var expected = Enumerable.Range(0, 6).Select(x => $"Item {x}").ToList();
  353. var items = (ObservableCollection<string>)target.Items;
  354. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  355. Assert.Equal(expected, actual);
  356. expected = Enumerable.Range(0, 5).Select(x => $"Item {x}").ToList();
  357. target.Items = expected;
  358. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  359. Assert.Equal(expected, actual);
  360. }
  361. [Fact]
  362. public void Clearing_Items_And_ReAdding_Should_Remove_Containers()
  363. {
  364. var target = CreateTarget(itemCount: 6);
  365. target.ApplyTemplate();
  366. target.Measure(new Size(100, 100));
  367. target.Arrange(new Rect(0, 0, 100, 100));
  368. var expected = Enumerable.Range(0, 6).Select(x => $"Item {x}").ToList();
  369. var items = (ObservableCollection<string>)target.Items;
  370. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  371. Assert.Equal(expected, actual);
  372. expected = Enumerable.Range(0, 5).Select(x => $"Item {x}").ToList();
  373. target.Items = null;
  374. target.Items = expected;
  375. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  376. Assert.Equal(expected, actual);
  377. }
  378. [Fact]
  379. public void Scrolling_To_Partial_Last_Item_Then_Adding_Item_Updates_Containers()
  380. {
  381. var target = CreateTarget(itemCount: 10);
  382. var items = (IList<string>)target.Items;
  383. target.ApplyTemplate();
  384. target.Measure(new Size(100, 95));
  385. target.Arrange(new Rect(0, 0, 100, 95));
  386. ((ILogicalScrollable)target).Offset = new Vector(0, 1);
  387. Assert.Equal(new Vector(0, 1), ((ILogicalScrollable)target).Offset);
  388. var expected = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList();
  389. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  390. Assert.Equal(expected, actual);
  391. Assert.Equal(10, ((IVirtualizingPanel)target.Panel).PixelOffset);
  392. items.Add("Item 10");
  393. expected = Enumerable.Range(1, 10).Select(x => $"Item {x}").ToList();
  394. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  395. Assert.Equal(expected, actual);
  396. Assert.Equal(0, ((IVirtualizingPanel)target.Panel).PixelOffset);
  397. }
  398. [Fact]
  399. public void Scrolling_To_Item_In_Zero_Sized_Presenter_Doesnt_Throw()
  400. {
  401. using (UnitTestApplication.Start(TestServices.RealLayoutManager))
  402. {
  403. var target = CreateTarget(itemCount: 10);
  404. var items = (IList<string>)target.Items;
  405. target.ApplyTemplate();
  406. target.Measure(Size.Empty);
  407. target.Arrange(Rect.Empty);
  408. // Check for issue #591: this should not throw.
  409. target.ScrollIntoView(items[0]);
  410. }
  411. }
  412. [Fact]
  413. public void InsertRange_Items_Should_Update_Containers()
  414. {
  415. var target = CreateTarget(useAvaloniaList: true);
  416. target.ApplyTemplate();
  417. target.Measure(new Size(100, 100));
  418. target.Arrange(new Rect(0, 0, 100, 100));
  419. var expected = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList();
  420. var items = (AvaloniaList<string>)target.Items;
  421. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  422. Assert.Equal(expected, actual);
  423. var toAdd = Enumerable.Range(0, 3).Select(x => $"New Item {x}").ToList();
  424. int index = 1;
  425. items.InsertRange(index, toAdd);
  426. expected.InsertRange(index, toAdd);
  427. expected.RemoveRange(10, toAdd.Count);
  428. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  429. Assert.Equal(expected, actual);
  430. }
  431. [Fact]
  432. public void InsertRange_Items_Before_Last_Should_Update_Containers()
  433. {
  434. var target = CreateTarget(useAvaloniaList: true);
  435. target.ApplyTemplate();
  436. target.Measure(new Size(100, 100));
  437. target.Arrange(new Rect(0, 0, 100, 100));
  438. var expected = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList();
  439. var items = (AvaloniaList<string>)target.Items;
  440. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  441. Assert.Equal(expected, actual);
  442. var toAdd = Enumerable.Range(0, 3).Select(x => $"New Item {x}").ToList();
  443. int index = 8;
  444. items.InsertRange(index, toAdd);
  445. expected.InsertRange(index, toAdd);
  446. expected.RemoveRange(10, toAdd.Count);
  447. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  448. Assert.Equal(expected, actual);
  449. }
  450. [Fact]
  451. public void RemoveRange_Items_Should_Update_Containers()
  452. {
  453. var target = CreateTarget(useAvaloniaList: true);
  454. target.ApplyTemplate();
  455. target.Measure(new Size(100, 100));
  456. target.Arrange(new Rect(0, 0, 100, 100));
  457. var expected = Enumerable.Range(0, 13).Select(x => $"Item {x}").ToList();
  458. var items = (AvaloniaList<string>)target.Items;
  459. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  460. Assert.Equal(expected.Take(10), actual);
  461. int index = 5;
  462. int count = 3;
  463. items.RemoveRange(index, count);
  464. expected.RemoveRange(index, count);
  465. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  466. Assert.Equal(expected, actual);
  467. }
  468. [Fact]
  469. public void RemoveRange_Items_Before_Last_Should_Update_Containers()
  470. {
  471. var target = CreateTarget(useAvaloniaList: true);
  472. target.ApplyTemplate();
  473. target.Measure(new Size(100, 100));
  474. target.Arrange(new Rect(0, 0, 100, 100));
  475. var expected = Enumerable.Range(0, 13).Select(x => $"Item {x}").ToList();
  476. var items = (AvaloniaList<string>)target.Items;
  477. var actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  478. Assert.Equal(expected.Take(10), actual);
  479. int index = 8;
  480. int count = 3;
  481. items.RemoveRange(index, count);
  482. expected.RemoveRange(index, count);
  483. actual = target.Panel.Children.Select(x => x.DataContext).ToList();
  484. Assert.Equal(expected, actual);
  485. }
  486. public class Vertical
  487. {
  488. [Fact]
  489. public void GetControlInDirection_Down_Should_Return_Existing_Container_If_Materialized()
  490. {
  491. var target = CreateTarget();
  492. target.ApplyTemplate();
  493. target.Measure(new Size(100, 100));
  494. target.Arrange(new Rect(0, 0, 100, 100));
  495. var from = target.Panel.Children[5];
  496. var result = ((ILogicalScrollable)target).GetControlInDirection(
  497. NavigationDirection.Down,
  498. from);
  499. Assert.Same(target.Panel.Children[6], result);
  500. }
  501. [Fact]
  502. public void GetControlInDirection_Down_Should_Scroll_If_Necessary()
  503. {
  504. var target = CreateTarget();
  505. target.ApplyTemplate();
  506. target.Measure(new Size(100, 100));
  507. target.Arrange(new Rect(0, 0, 100, 100));
  508. var from = target.Panel.Children[9];
  509. var result = ((ILogicalScrollable)target).GetControlInDirection(
  510. NavigationDirection.Down,
  511. from);
  512. Assert.Equal(new Vector(0, 1), ((ILogicalScrollable)target).Offset);
  513. Assert.Same(target.Panel.Children[9], result);
  514. }
  515. [Fact]
  516. public void GetControlInDirection_Down_Should_Scroll_If_Partially_Visible()
  517. {
  518. using (UnitTestApplication.Start(TestServices.RealLayoutManager))
  519. {
  520. var target = CreateTarget();
  521. var scroller = (ScrollContentPresenter)target.Parent;
  522. scroller.Measure(new Size(100, 95));
  523. scroller.Arrange(new Rect(0, 0, 100, 95));
  524. var from = target.Panel.Children[8];
  525. var result = ((ILogicalScrollable)target).GetControlInDirection(
  526. NavigationDirection.Down,
  527. from);
  528. Assert.Equal(new Vector(0, 1), ((ILogicalScrollable)target).Offset);
  529. Assert.Same(target.Panel.Children[8], result);
  530. }
  531. }
  532. [Fact]
  533. public void GetControlInDirection_Up_Should_Scroll_If_Partially_Visible_Item_Is_Currently_Shown()
  534. {
  535. using (UnitTestApplication.Start(TestServices.RealLayoutManager))
  536. {
  537. var target = CreateTarget();
  538. var scroller = (ScrollContentPresenter)target.Parent;
  539. scroller.Measure(new Size(100, 95));
  540. scroller.Arrange(new Rect(0, 0, 100, 95));
  541. ((ILogicalScrollable)target).Offset = new Vector(0, 11);
  542. var from = target.Panel.Children[1];
  543. var result = ((ILogicalScrollable)target).GetControlInDirection(
  544. NavigationDirection.Up,
  545. from);
  546. Assert.Equal(new Vector(0, 10), ((ILogicalScrollable)target).Offset);
  547. Assert.Same(target.Panel.Children[0], result);
  548. }
  549. }
  550. }
  551. public class Horizontal
  552. {
  553. [Fact]
  554. public void GetControlInDirection_Right_Should_Return_Existing_Container_If_Materialized()
  555. {
  556. var target = CreateTarget(orientation: Orientation.Horizontal);
  557. target.ApplyTemplate();
  558. target.Measure(new Size(100, 100));
  559. target.Arrange(new Rect(0, 0, 100, 100));
  560. var from = target.Panel.Children[5];
  561. var result = ((ILogicalScrollable)target).GetControlInDirection(
  562. NavigationDirection.Right,
  563. from);
  564. Assert.Same(target.Panel.Children[6], result);
  565. }
  566. [Fact]
  567. public void GetControlInDirection_Right_Should_Scroll_If_Necessary()
  568. {
  569. var target = CreateTarget(orientation: Orientation.Horizontal);
  570. target.ApplyTemplate();
  571. target.Measure(new Size(100, 100));
  572. target.Arrange(new Rect(0, 0, 100, 100));
  573. var from = target.Panel.Children[9];
  574. var result = ((ILogicalScrollable)target).GetControlInDirection(
  575. NavigationDirection.Right,
  576. from);
  577. Assert.Equal(new Vector(1, 0), ((ILogicalScrollable)target).Offset);
  578. Assert.Same(target.Panel.Children[9], result);
  579. }
  580. [Fact]
  581. public void GetControlInDirection_Right_Should_Scroll_If_Partially_Visible()
  582. {
  583. using (UnitTestApplication.Start(TestServices.RealLayoutManager))
  584. {
  585. var target = CreateTarget(orientation: Orientation.Horizontal);
  586. var scroller = (ScrollContentPresenter)target.Parent;
  587. scroller.Measure(new Size(95, 100));
  588. scroller.Arrange(new Rect(0, 0, 95, 100));
  589. var from = target.Panel.Children[8];
  590. var result = ((ILogicalScrollable)target).GetControlInDirection(
  591. NavigationDirection.Right,
  592. from);
  593. Assert.Equal(new Vector(1, 0), ((ILogicalScrollable)target).Offset);
  594. Assert.Same(target.Panel.Children[8], result);
  595. }
  596. }
  597. [Fact]
  598. public void GetControlInDirection_Left_Should_Scroll_If_Partially_Visible_Item_Is_Currently_Shown()
  599. {
  600. var target = CreateTarget(orientation: Orientation.Horizontal);
  601. target.ApplyTemplate();
  602. target.Measure(new Size(95, 100));
  603. target.Arrange(new Rect(0, 0, 95, 100));
  604. ((ILogicalScrollable)target).Offset = new Vector(11, 0);
  605. var from = target.Panel.Children[1];
  606. var result = ((ILogicalScrollable)target).GetControlInDirection(
  607. NavigationDirection.Left,
  608. from);
  609. Assert.Equal(new Vector(10, 0), ((ILogicalScrollable)target).Offset);
  610. Assert.Same(target.Panel.Children[0], result);
  611. }
  612. }
  613. public class WithContainers
  614. {
  615. [Fact]
  616. public void Scrolling_Less_Than_A_Page_Should_Move_Recycled_Items()
  617. {
  618. var target = CreateTarget();
  619. var items = (IList<string>)target.Items;
  620. target.ApplyTemplate();
  621. target.Measure(new Size(100, 100));
  622. target.Arrange(new Rect(0, 0, 100, 100));
  623. var containers = target.Panel.Children.ToList();
  624. var scroller = (ScrollContentPresenter)target.Parent;
  625. scroller.Offset = new Vector(0, 5);
  626. var scrolledContainers = containers
  627. .Skip(5)
  628. .Take(5)
  629. .Concat(containers.Take(5)).ToList();
  630. Assert.Equal(new Vector(0, 5), ((ILogicalScrollable)target).Offset);
  631. Assert.Equal(scrolledContainers, target.Panel.Children);
  632. for (var i = 0; i < target.Panel.Children.Count; ++i)
  633. {
  634. Assert.Equal(items[i + 5], target.Panel.Children[i].DataContext);
  635. }
  636. scroller.Offset = new Vector(0, 0);
  637. Assert.Equal(new Vector(0, 0), ((ILogicalScrollable)target).Offset);
  638. Assert.Equal(containers, target.Panel.Children);
  639. var dcs = target.Panel.Children.Select(x => x.DataContext).ToList();
  640. for (var i = 0; i < target.Panel.Children.Count; ++i)
  641. {
  642. Assert.Equal(items[i], target.Panel.Children[i].DataContext);
  643. }
  644. }
  645. [Fact]
  646. public void Scrolling_More_Than_A_Page_Should_Recycle_Items()
  647. {
  648. var target = CreateTarget(itemCount: 50);
  649. var items = (IList<string>)target.Items;
  650. target.ApplyTemplate();
  651. target.Measure(new Size(100, 100));
  652. target.Arrange(new Rect(0, 0, 100, 100));
  653. var containers = target.Panel.Children.ToList();
  654. var scroller = (ScrollContentPresenter)target.Parent;
  655. scroller.Offset = new Vector(0, 20);
  656. Assert.Equal(new Vector(0, 20), ((ILogicalScrollable)target).Offset);
  657. Assert.Equal(containers, target.Panel.Children);
  658. for (var i = 0; i < target.Panel.Children.Count; ++i)
  659. {
  660. Assert.Equal(items[i + 20], target.Panel.Children[i].DataContext);
  661. }
  662. scroller.Offset = new Vector(0, 0);
  663. Assert.Equal(new Vector(0, 0), ((ILogicalScrollable)target).Offset);
  664. Assert.Equal(containers, target.Panel.Children);
  665. for (var i = 0; i < target.Panel.Children.Count; ++i)
  666. {
  667. Assert.Equal(items[i], target.Panel.Children[i].DataContext);
  668. }
  669. }
  670. }
  671. private static ItemsPresenter CreateTarget(
  672. Orientation orientation = Orientation.Vertical,
  673. bool useContainers = true,
  674. int itemCount = 20,
  675. bool useAvaloniaList = false)
  676. {
  677. ItemsPresenter result;
  678. var itemsSource = Enumerable.Range(0, itemCount).Select(x => $"Item {x}");
  679. var items = useAvaloniaList ?
  680. (IEnumerable)new AvaloniaList<string>(itemsSource) :
  681. (IEnumerable)new ObservableCollection<string>(itemsSource);
  682. var scroller = new TestScroller
  683. {
  684. Content = result = new TestItemsPresenter(useContainers)
  685. {
  686. Items = items,
  687. ItemsPanel = VirtualizingPanelTemplate(orientation),
  688. ItemTemplate = ItemTemplate(),
  689. VirtualizationMode = ItemVirtualizationMode.Simple,
  690. }
  691. };
  692. scroller.UpdateChild();
  693. return result;
  694. }
  695. private static IDataTemplate ItemTemplate()
  696. {
  697. return new FuncDataTemplate<string>(x => new Canvas
  698. {
  699. Width = 10,
  700. Height = 10,
  701. });
  702. }
  703. private static ITemplate<IPanel> VirtualizingPanelTemplate(
  704. Orientation orientation = Orientation.Vertical)
  705. {
  706. return new FuncTemplate<IPanel>(() => new VirtualizingStackPanel
  707. {
  708. Orientation = orientation,
  709. });
  710. }
  711. private class TestScroller : ScrollContentPresenter, IRenderRoot
  712. {
  713. public IRenderQueueManager RenderQueueManager { get; }
  714. public Point PointToClient(Point point)
  715. {
  716. throw new NotImplementedException();
  717. }
  718. public Point PointToScreen(Point point)
  719. {
  720. throw new NotImplementedException();
  721. }
  722. }
  723. private class TestItemsPresenter : ItemsPresenter
  724. {
  725. private bool _useContainers;
  726. public TestItemsPresenter(bool useContainers)
  727. {
  728. _useContainers = useContainers;
  729. }
  730. protected override IItemContainerGenerator CreateItemContainerGenerator()
  731. {
  732. return _useContainers ?
  733. new ItemContainerGenerator<TestContainer>(this, TestContainer.ContentProperty, null) :
  734. new ItemContainerGenerator(this);
  735. }
  736. }
  737. private class TestContainer : ContentControl
  738. {
  739. public TestContainer()
  740. {
  741. Width = 10;
  742. Height = 10;
  743. }
  744. }
  745. }
  746. }