ListBoxTests.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.Linq;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Input;
  7. using Avalonia.LogicalTree;
  8. using Avalonia.Styling;
  9. using Avalonia.VisualTree;
  10. using Xunit;
  11. namespace Avalonia.Controls.UnitTests
  12. {
  13. public class ListBoxTests
  14. {
  15. [Fact]
  16. public void Should_Use_ItemTemplate_To_Create_Item_Content()
  17. {
  18. var target = new ListBox
  19. {
  20. Template = CreateListBoxTemplate(),
  21. ItemTemplate = new FuncDataTemplate<string>(_ => new Canvas()),
  22. };
  23. target.Items = new[] { "Foo" };
  24. target.ApplyTemplate();
  25. target.Presenter.ApplyTemplate();
  26. var container = (ListBoxItem)target.Presenter.Panel.Children[0];
  27. container.Template = ListBoxItemTemplate();
  28. container.ApplyTemplate();
  29. ((ContentPresenter)container.Presenter).UpdateChild();
  30. Assert.IsType<Canvas>(container.Presenter.Child);
  31. }
  32. [Fact]
  33. public void ListBox_Should_Find_ItemsPresenter_In_ScrollViewer()
  34. {
  35. var target = new ListBox
  36. {
  37. Template = CreateListBoxTemplate(),
  38. };
  39. ApplyTemplate(target);
  40. Assert.IsType<ItemsPresenter>(target.Presenter);
  41. }
  42. [Fact]
  43. public void ListBoxItem_Containers_Should_Be_Generated()
  44. {
  45. var items = new[] { "Foo", "Bar", "Baz " };
  46. var target = new ListBox
  47. {
  48. Template = CreateListBoxTemplate(),
  49. Items = items,
  50. };
  51. ApplyTemplate(target);
  52. var text = target.Presenter.Panel.Children
  53. .OfType<ListBoxItem>()
  54. .Do(x => x.Template = ListBoxItemTemplate())
  55. .Do(x => { x.ApplyTemplate(); ((ContentPresenter)x.Presenter).UpdateChild(); })
  56. .Select(x => x.Presenter.Child)
  57. .OfType<TextBlock>()
  58. .Select(x => x.Text)
  59. .ToList();
  60. Assert.Equal(items, text);
  61. }
  62. [Fact]
  63. public void LogicalChildren_Should_Be_Set_For_DataTemplate_Generated_Items()
  64. {
  65. var target = new ListBox
  66. {
  67. Template = CreateListBoxTemplate(),
  68. Items = new[] { "Foo", "Bar", "Baz " },
  69. };
  70. ApplyTemplate(target);
  71. Assert.Equal(3, target.GetLogicalChildren().Count());
  72. foreach (var child in target.GetLogicalChildren())
  73. {
  74. Assert.IsType<ListBoxItem>(child);
  75. }
  76. }
  77. [Fact]
  78. public void DataContexts_Should_Be_Correctly_Set()
  79. {
  80. var items = new object[]
  81. {
  82. "Foo",
  83. new Item("Bar"),
  84. new TextBlock { Text = "Baz" },
  85. new ListBoxItem { Content = "Qux" },
  86. };
  87. var target = new ListBox
  88. {
  89. Template = CreateListBoxTemplate(),
  90. DataContext = "Base",
  91. DataTemplates = new DataTemplates
  92. {
  93. new FuncDataTemplate<Item>(x => new Button { Content = x })
  94. },
  95. Items = items,
  96. };
  97. ApplyTemplate(target);
  98. var dataContexts = target.Presenter.Panel.Children
  99. .Cast<Control>()
  100. .Select(x => x.DataContext)
  101. .ToList();
  102. Assert.Equal(
  103. new object[] { items[0], items[1], "Base", "Base" },
  104. dataContexts);
  105. }
  106. private FuncControlTemplate CreateListBoxTemplate()
  107. {
  108. return new FuncControlTemplate<ListBox>(parent =>
  109. new ScrollViewer
  110. {
  111. Name = "PART_ScrollViewer",
  112. Template = new FuncControlTemplate(CreateScrollViewerTemplate),
  113. Content = new ItemsPresenter
  114. {
  115. Name = "PART_ItemsPresenter",
  116. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty),
  117. }
  118. });
  119. }
  120. private FuncControlTemplate ListBoxItemTemplate()
  121. {
  122. return new FuncControlTemplate<ListBoxItem>(parent => new ContentPresenter
  123. {
  124. Name = "PART_ContentPresenter",
  125. [!ContentPresenter.ContentProperty] = parent[!ListBoxItem.ContentProperty],
  126. [!ContentPresenter.ContentTemplateProperty] = parent[!ListBoxItem.ContentTemplateProperty],
  127. });
  128. }
  129. private Control CreateScrollViewerTemplate(ITemplatedControl parent)
  130. {
  131. return new ScrollContentPresenter
  132. {
  133. Name = "PART_ContentPresenter",
  134. [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty),
  135. };
  136. }
  137. private void ApplyTemplate(ListBox target)
  138. {
  139. // Apply the template to the ListBox itself.
  140. target.ApplyTemplate();
  141. // Then to its inner ScrollViewer.
  142. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  143. scrollViewer.ApplyTemplate();
  144. // Then make the ScrollViewer create its child.
  145. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  146. // Now the ItemsPresenter should be reigstered, so apply its template.
  147. target.Presenter.ApplyTemplate();
  148. }
  149. private class Item
  150. {
  151. public Item(string value)
  152. {
  153. Value = value;
  154. }
  155. public string Value { get; }
  156. }
  157. }
  158. }