ListBoxTests_Multiple.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Linq;
  2. using Avalonia.Controls.Presenters;
  3. using Avalonia.Controls.Templates;
  4. using Avalonia.Input;
  5. using Avalonia.Styling;
  6. using Avalonia.UnitTests;
  7. using Avalonia.VisualTree;
  8. using Xunit;
  9. namespace Avalonia.Controls.UnitTests
  10. {
  11. public class ListBoxTests_Multiple
  12. {
  13. [Fact]
  14. public void Focusing_Item_With_Shift_And_Arrow_Key_Should_Add_To_Selection()
  15. {
  16. var target = new ListBox
  17. {
  18. Template = new FuncControlTemplate(CreateListBoxTemplate),
  19. Items = new[] { "Foo", "Bar", "Baz " },
  20. SelectionMode = SelectionMode.Multiple
  21. };
  22. ApplyTemplate(target);
  23. target.SelectedItem = "Foo";
  24. target.Presenter.Panel.Children[1].RaiseEvent(new GotFocusEventArgs
  25. {
  26. RoutedEvent = InputElement.GotFocusEvent,
  27. NavigationMethod = NavigationMethod.Directional,
  28. KeyModifiers = KeyModifiers.Shift
  29. });
  30. Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
  31. }
  32. [Fact]
  33. public void Focusing_Item_With_Ctrl_And_Arrow_Key_Should_Add_To_Selection()
  34. {
  35. var target = new ListBox
  36. {
  37. Template = new FuncControlTemplate(CreateListBoxTemplate),
  38. Items = new[] { "Foo", "Bar", "Baz " },
  39. SelectionMode = SelectionMode.Multiple
  40. };
  41. ApplyTemplate(target);
  42. target.SelectedItem = "Foo";
  43. target.Presenter.Panel.Children[1].RaiseEvent(new GotFocusEventArgs
  44. {
  45. RoutedEvent = InputElement.GotFocusEvent,
  46. NavigationMethod = NavigationMethod.Directional,
  47. KeyModifiers = KeyModifiers.Control
  48. });
  49. Assert.Equal(new[] { "Foo", "Bar" }, target.SelectedItems);
  50. }
  51. [Fact]
  52. public void Focusing_Selected_Item_With_Ctrl_And_Arrow_Key_Should_Remove_From_Selection()
  53. {
  54. var target = new ListBox
  55. {
  56. Template = new FuncControlTemplate(CreateListBoxTemplate),
  57. Items = new[] { "Foo", "Bar", "Baz " },
  58. SelectionMode = SelectionMode.Multiple
  59. };
  60. ApplyTemplate(target);
  61. target.SelectedItems.Add("Foo");
  62. target.SelectedItems.Add("Bar");
  63. target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
  64. {
  65. RoutedEvent = InputElement.GotFocusEvent,
  66. NavigationMethod = NavigationMethod.Directional,
  67. KeyModifiers = KeyModifiers.Control
  68. });
  69. Assert.Equal(new[] { "Bar" }, target.SelectedItems);
  70. }
  71. private Control CreateListBoxTemplate(ITemplatedControl parent, INameScope scope)
  72. {
  73. return new ScrollViewer
  74. {
  75. Template = new FuncControlTemplate(CreateScrollViewerTemplate),
  76. Content = new ItemsPresenter
  77. {
  78. Name = "PART_ItemsPresenter",
  79. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty).ToBinding(),
  80. }.RegisterInNameScope(scope)
  81. };
  82. }
  83. private Control CreateScrollViewerTemplate(ITemplatedControl parent, INameScope scope)
  84. {
  85. return new ScrollContentPresenter
  86. {
  87. Name = "PART_ContentPresenter",
  88. [~ContentPresenter.ContentProperty] =
  89. parent.GetObservable(ContentControl.ContentProperty).ToBinding(),
  90. }.RegisterInNameScope(scope);
  91. }
  92. private void ApplyTemplate(ListBox target)
  93. {
  94. // Apply the template to the ListBox itself.
  95. target.ApplyTemplate();
  96. // Then to its inner ScrollViewer.
  97. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  98. scrollViewer.ApplyTemplate();
  99. // Then make the ScrollViewer create its child.
  100. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  101. // Now the ItemsPresenter should be reigstered, so apply its template.
  102. target.Presenter.ApplyTemplate();
  103. }
  104. }
  105. }