ItemsPresenterTests_Virtualization.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 Avalonia.Controls.Presenters;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Controls.Templates;
  6. using Moq;
  7. using Xunit;
  8. namespace Avalonia.Controls.UnitTests.Presenters
  9. {
  10. public class ItemsPresenterTests_Virtualization
  11. {
  12. [Fact]
  13. public void Should_Return_IsLogicalScrollEnabled_False_When_Has_No_Virtualizing_Panel()
  14. {
  15. var target = new ItemsPresenter
  16. {
  17. };
  18. target.ApplyTemplate();
  19. Assert.False(((IScrollable)target).IsLogicalScrollEnabled);
  20. }
  21. [Fact]
  22. public void Should_Return_IsLogicalScrollEnabled_False_When_VirtualizationMode_None()
  23. {
  24. var target = new ItemsPresenter
  25. {
  26. ItemsPanel = VirtualizingPanelTemplate(),
  27. VirtualizationMode = ItemVirtualizationMode.None,
  28. };
  29. target.ApplyTemplate();
  30. Assert.False(((IScrollable)target).IsLogicalScrollEnabled);
  31. }
  32. [Fact]
  33. public void Should_Return_IsLogicalScrollEnabled_True_When_Has_Virtualizing_Panel()
  34. {
  35. var target = new ItemsPresenter
  36. {
  37. ItemsPanel = VirtualizingPanelTemplate(),
  38. };
  39. target.ApplyTemplate();
  40. Assert.True(((IScrollable)target).IsLogicalScrollEnabled);
  41. }
  42. public class Simple
  43. {
  44. [Fact]
  45. public void Should_Return_Items_Count_For_Extent()
  46. {
  47. var target = new ItemsPresenter
  48. {
  49. Items = new string[10],
  50. ItemsPanel = VirtualizingPanelTemplate(),
  51. VirtualizationMode = ItemVirtualizationMode.Simple,
  52. };
  53. target.ApplyTemplate();
  54. Assert.Equal(new Size(0, 10), ((IScrollable)target).Extent);
  55. }
  56. [Fact]
  57. public void Should_Have_Number_Of_Visible_Items_As_Viewport()
  58. {
  59. var target = new ItemsPresenter
  60. {
  61. Items = new string[20],
  62. ItemsPanel = VirtualizingPanelTemplate(),
  63. ItemTemplate = ItemTemplate(),
  64. VirtualizationMode = ItemVirtualizationMode.Simple,
  65. };
  66. target.ApplyTemplate();
  67. target.Measure(new Size(100, 100));
  68. target.Arrange(new Rect(0, 0, 100, 100));
  69. Assert.Equal(10, ((IScrollable)target).Viewport.Height);
  70. }
  71. }
  72. private static IDataTemplate ItemTemplate()
  73. {
  74. return new FuncDataTemplate<string>(x => new TextBlock
  75. {
  76. Text = x,
  77. Height = 10,
  78. });
  79. }
  80. private static ITemplate<IPanel> VirtualizingPanelTemplate()
  81. {
  82. return new FuncTemplate<IPanel>(() => new VirtualizingStackPanel());
  83. }
  84. }
  85. }