ListBoxTests.cs 5.8 KB

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