ListBoxTests_Multiple.cs 4.0 KB

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