ListBoxTests_Single.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using Avalonia.Controls.Presenters;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.Data;
  9. using Avalonia.Input;
  10. using Avalonia.LogicalTree;
  11. using Avalonia.Markup.Data;
  12. using Avalonia.Styling;
  13. using Avalonia.VisualTree;
  14. using Xunit;
  15. namespace Avalonia.Controls.UnitTests
  16. {
  17. public class ListBoxTests_Single
  18. {
  19. MouseTestHelper _mouse = new MouseTestHelper();
  20. [Fact]
  21. public void Focusing_Item_With_Tab_Should_Not_Select_It()
  22. {
  23. var target = new ListBox
  24. {
  25. Template = new FuncControlTemplate(CreateListBoxTemplate),
  26. Items = new[] { "Foo", "Bar", "Baz " },
  27. };
  28. ApplyTemplate(target);
  29. target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
  30. {
  31. RoutedEvent = InputElement.GotFocusEvent,
  32. NavigationMethod = NavigationMethod.Tab,
  33. });
  34. Assert.Equal(-1, target.SelectedIndex);
  35. }
  36. [Fact]
  37. public void Focusing_Item_With_Arrow_Key_Should_Select_It()
  38. {
  39. var target = new ListBox
  40. {
  41. Template = new FuncControlTemplate(CreateListBoxTemplate),
  42. Items = new[] { "Foo", "Bar", "Baz " },
  43. };
  44. ApplyTemplate(target);
  45. target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
  46. {
  47. RoutedEvent = InputElement.GotFocusEvent,
  48. NavigationMethod = NavigationMethod.Directional,
  49. });
  50. Assert.Equal(0, target.SelectedIndex);
  51. }
  52. [Fact]
  53. public void Clicking_Item_Should_Select_It()
  54. {
  55. var target = new ListBox
  56. {
  57. Template = new FuncControlTemplate(CreateListBoxTemplate),
  58. Items = new[] { "Foo", "Bar", "Baz " },
  59. };
  60. ApplyTemplate(target);
  61. _mouse.Click(target.Presenter.Panel.Children[0]);
  62. Assert.Equal(0, target.SelectedIndex);
  63. }
  64. [Fact]
  65. public void Clicking_Selected_Item_Should_Not_Deselect_It()
  66. {
  67. var target = new ListBox
  68. {
  69. Template = new FuncControlTemplate(CreateListBoxTemplate),
  70. Items = new[] { "Foo", "Bar", "Baz " },
  71. };
  72. ApplyTemplate(target);
  73. target.SelectedIndex = 0;
  74. _mouse.Click(target.Presenter.Panel.Children[0]);
  75. Assert.Equal(0, target.SelectedIndex);
  76. }
  77. [Fact]
  78. public void Clicking_Item_Should_Select_It_When_SelectionMode_Toggle()
  79. {
  80. var target = new ListBox
  81. {
  82. Template = new FuncControlTemplate(CreateListBoxTemplate),
  83. Items = new[] { "Foo", "Bar", "Baz " },
  84. SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
  85. };
  86. ApplyTemplate(target);
  87. _mouse.Click(target.Presenter.Panel.Children[0]);
  88. Assert.Equal(0, target.SelectedIndex);
  89. }
  90. [Fact]
  91. public void Clicking_Selected_Item_Should_Deselect_It_When_SelectionMode_Toggle()
  92. {
  93. var target = new ListBox
  94. {
  95. Template = new FuncControlTemplate(CreateListBoxTemplate),
  96. Items = new[] { "Foo", "Bar", "Baz " },
  97. SelectionMode = SelectionMode.Toggle,
  98. };
  99. ApplyTemplate(target);
  100. target.SelectedIndex = 0;
  101. _mouse.Click(target.Presenter.Panel.Children[0]);
  102. Assert.Equal(-1, target.SelectedIndex);
  103. }
  104. [Fact]
  105. public void Clicking_Selected_Item_Should_Not_Deselect_It_When_SelectionMode_ToggleAlwaysSelected()
  106. {
  107. var target = new ListBox
  108. {
  109. Template = new FuncControlTemplate(CreateListBoxTemplate),
  110. Items = new[] { "Foo", "Bar", "Baz " },
  111. SelectionMode = SelectionMode.Toggle | SelectionMode.AlwaysSelected,
  112. };
  113. ApplyTemplate(target);
  114. target.SelectedIndex = 0;
  115. _mouse.Click(target.Presenter.Panel.Children[0]);
  116. Assert.Equal(0, target.SelectedIndex);
  117. }
  118. [Fact]
  119. public void Clicking_Another_Item_Should_Select_It_When_SelectionMode_Toggle()
  120. {
  121. var target = new ListBox
  122. {
  123. Template = new FuncControlTemplate(CreateListBoxTemplate),
  124. Items = new[] { "Foo", "Bar", "Baz " },
  125. SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
  126. };
  127. ApplyTemplate(target);
  128. target.SelectedIndex = 1;
  129. _mouse.Click(target.Presenter.Panel.Children[0]);
  130. Assert.Equal(0, target.SelectedIndex);
  131. }
  132. [Fact]
  133. public void Setting_Item_IsSelected_Sets_ListBox_Selection()
  134. {
  135. var target = new ListBox
  136. {
  137. Template = new FuncControlTemplate(CreateListBoxTemplate),
  138. Items = new[] { "Foo", "Bar", "Baz " },
  139. };
  140. ApplyTemplate(target);
  141. ((ListBoxItem)target.GetLogicalChildren().ElementAt(1)).IsSelected = true;
  142. Assert.Equal("Bar", target.SelectedItem);
  143. Assert.Equal(1, target.SelectedIndex);
  144. }
  145. [Fact]
  146. public void SelectedItem_Should_Not_Cause_StackOverflow()
  147. {
  148. var viewModel = new TestStackOverflowViewModel()
  149. {
  150. Items = new List<string> { "foo", "bar", "baz" }
  151. };
  152. var target = new ListBox
  153. {
  154. Template = new FuncControlTemplate(CreateListBoxTemplate),
  155. DataContext = viewModel,
  156. Items = viewModel.Items
  157. };
  158. target.Bind(ListBox.SelectedItemProperty,
  159. new Binding("SelectedItem") { Mode = BindingMode.TwoWay });
  160. Assert.Equal(0, viewModel.SetterInvokedCount);
  161. // In Issue #855, a Stackoverflow occured here.
  162. target.SelectedItem = viewModel.Items[2];
  163. Assert.Equal(viewModel.Items[1], target.SelectedItem);
  164. Assert.Equal(1, viewModel.SetterInvokedCount);
  165. }
  166. private class TestStackOverflowViewModel : INotifyPropertyChanged
  167. {
  168. public List<string> Items { get; set; }
  169. public int SetterInvokedCount { get; private set; }
  170. public const int MaxInvokedCount = 1000;
  171. private string _selectedItem;
  172. public event PropertyChangedEventHandler PropertyChanged;
  173. public string SelectedItem
  174. {
  175. get { return _selectedItem; }
  176. set
  177. {
  178. if (_selectedItem != value)
  179. {
  180. SetterInvokedCount++;
  181. int index = Items.IndexOf(value);
  182. if (MaxInvokedCount > SetterInvokedCount && index > 0)
  183. {
  184. _selectedItem = Items[index - 1];
  185. }
  186. else
  187. {
  188. _selectedItem = value;
  189. }
  190. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));
  191. }
  192. }
  193. }
  194. }
  195. private Control CreateListBoxTemplate(ITemplatedControl parent)
  196. {
  197. return new ScrollViewer
  198. {
  199. Template = new FuncControlTemplate(CreateScrollViewerTemplate),
  200. Content = new ItemsPresenter
  201. {
  202. Name = "PART_ItemsPresenter",
  203. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty).ToBinding(),
  204. }
  205. };
  206. }
  207. private Control CreateScrollViewerTemplate(ITemplatedControl parent)
  208. {
  209. return new ScrollContentPresenter
  210. {
  211. Name = "PART_ContentPresenter",
  212. [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty).ToBinding(),
  213. };
  214. }
  215. private void ApplyTemplate(ListBox target)
  216. {
  217. // Apply the template to the ListBox itself.
  218. target.ApplyTemplate();
  219. // Then to its inner ScrollViewer.
  220. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  221. scrollViewer.ApplyTemplate();
  222. // Then make the ScrollViewer create its child.
  223. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  224. // Now the ItemsPresenter should be reigstered, so apply its template.
  225. target.Presenter.ApplyTemplate();
  226. }
  227. }
  228. }