ListBoxTests_Single.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Data;
  7. using Avalonia.Input;
  8. using Avalonia.Input.Platform;
  9. using Avalonia.LogicalTree;
  10. using Avalonia.Styling;
  11. using Avalonia.UnitTests;
  12. using Avalonia.VisualTree;
  13. using Moq;
  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. using (UnitTestApplication.Start())
  56. {
  57. var target = new ListBox
  58. {
  59. Template = new FuncControlTemplate(CreateListBoxTemplate),
  60. Items = new[] { "Foo", "Bar", "Baz " },
  61. };
  62. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  63. ApplyTemplate(target);
  64. _mouse.Click(target.Presenter.Panel.Children[0]);
  65. Assert.Equal(0, target.SelectedIndex);
  66. }
  67. }
  68. [Fact]
  69. public void Clicking_Selected_Item_Should_Not_Deselect_It()
  70. {
  71. using (UnitTestApplication.Start())
  72. {
  73. var target = new ListBox
  74. {
  75. Template = new FuncControlTemplate(CreateListBoxTemplate),
  76. Items = new[] { "Foo", "Bar", "Baz " },
  77. };
  78. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  79. ApplyTemplate(target);
  80. target.SelectedIndex = 0;
  81. _mouse.Click(target.Presenter.Panel.Children[0]);
  82. Assert.Equal(0, target.SelectedIndex);
  83. }
  84. }
  85. [Fact]
  86. public void Clicking_Item_Should_Select_It_When_SelectionMode_Toggle()
  87. {
  88. using (UnitTestApplication.Start())
  89. {
  90. var target = new ListBox
  91. {
  92. Template = new FuncControlTemplate(CreateListBoxTemplate),
  93. Items = new[] { "Foo", "Bar", "Baz " },
  94. SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
  95. };
  96. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  97. ApplyTemplate(target);
  98. _mouse.Click(target.Presenter.Panel.Children[0]);
  99. Assert.Equal(0, target.SelectedIndex);
  100. }
  101. }
  102. [Fact]
  103. public void Clicking_Selected_Item_Should_Deselect_It_When_SelectionMode_Toggle()
  104. {
  105. using (UnitTestApplication.Start())
  106. {
  107. var target = new ListBox
  108. {
  109. Template = new FuncControlTemplate(CreateListBoxTemplate),
  110. Items = new[] { "Foo", "Bar", "Baz " },
  111. SelectionMode = SelectionMode.Toggle,
  112. };
  113. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  114. ApplyTemplate(target);
  115. target.SelectedIndex = 0;
  116. _mouse.Click(target.Presenter.Panel.Children[0]);
  117. Assert.Equal(-1, target.SelectedIndex);
  118. }
  119. }
  120. [Fact]
  121. public void Clicking_Selected_Item_Should_Not_Deselect_It_When_SelectionMode_ToggleAlwaysSelected()
  122. {
  123. using (UnitTestApplication.Start())
  124. {
  125. var target = new ListBox
  126. {
  127. Template = new FuncControlTemplate(CreateListBoxTemplate),
  128. Items = new[] { "Foo", "Bar", "Baz " },
  129. SelectionMode = SelectionMode.Toggle | SelectionMode.AlwaysSelected,
  130. };
  131. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  132. ApplyTemplate(target);
  133. target.SelectedIndex = 0;
  134. _mouse.Click(target.Presenter.Panel.Children[0]);
  135. Assert.Equal(0, target.SelectedIndex);
  136. }
  137. }
  138. [Fact]
  139. public void Clicking_Another_Item_Should_Select_It_When_SelectionMode_Toggle()
  140. {
  141. using (UnitTestApplication.Start())
  142. {
  143. var target = new ListBox
  144. {
  145. Template = new FuncControlTemplate(CreateListBoxTemplate),
  146. Items = new[] { "Foo", "Bar", "Baz " },
  147. SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
  148. };
  149. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  150. ApplyTemplate(target);
  151. target.SelectedIndex = 1;
  152. _mouse.Click(target.Presenter.Panel.Children[0]);
  153. Assert.Equal(0, target.SelectedIndex);
  154. }
  155. }
  156. [Fact]
  157. public void Setting_Item_IsSelected_Sets_ListBox_Selection()
  158. {
  159. var target = new ListBox
  160. {
  161. Template = new FuncControlTemplate(CreateListBoxTemplate),
  162. Items = new[] { "Foo", "Bar", "Baz " },
  163. };
  164. ApplyTemplate(target);
  165. ((ListBoxItem)target.GetLogicalChildren().ElementAt(1)).IsSelected = true;
  166. Assert.Equal("Bar", target.SelectedItem);
  167. Assert.Equal(1, target.SelectedIndex);
  168. }
  169. [Fact]
  170. public void SelectedItem_Should_Not_Cause_StackOverflow()
  171. {
  172. var viewModel = new TestStackOverflowViewModel()
  173. {
  174. Items = new List<string> { "foo", "bar", "baz" }
  175. };
  176. var target = new ListBox
  177. {
  178. Template = new FuncControlTemplate(CreateListBoxTemplate),
  179. DataContext = viewModel,
  180. Items = viewModel.Items
  181. };
  182. target.Bind(ListBox.SelectedItemProperty,
  183. new Binding("SelectedItem") { Mode = BindingMode.TwoWay });
  184. Assert.Equal(0, viewModel.SetterInvokedCount);
  185. // In Issue #855, a Stackoverflow occured here.
  186. target.SelectedItem = viewModel.Items[2];
  187. Assert.Equal(viewModel.Items[1], target.SelectedItem);
  188. Assert.Equal(1, viewModel.SetterInvokedCount);
  189. }
  190. private class TestStackOverflowViewModel : INotifyPropertyChanged
  191. {
  192. public List<string> Items { get; set; }
  193. public int SetterInvokedCount { get; private set; }
  194. public const int MaxInvokedCount = 1000;
  195. private string _selectedItem;
  196. public event PropertyChangedEventHandler PropertyChanged;
  197. public string SelectedItem
  198. {
  199. get { return _selectedItem; }
  200. set
  201. {
  202. if (_selectedItem != value)
  203. {
  204. SetterInvokedCount++;
  205. int index = Items.IndexOf(value);
  206. if (MaxInvokedCount > SetterInvokedCount && index > 0)
  207. {
  208. _selectedItem = Items[index - 1];
  209. }
  210. else
  211. {
  212. _selectedItem = value;
  213. }
  214. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));
  215. }
  216. }
  217. }
  218. }
  219. private Control CreateListBoxTemplate(ITemplatedControl parent, INameScope scope)
  220. {
  221. return new ScrollViewer
  222. {
  223. Template = new FuncControlTemplate(CreateScrollViewerTemplate),
  224. Content = new ItemsPresenter
  225. {
  226. Name = "PART_ItemsPresenter",
  227. [~ItemsPresenter.ItemsProperty] = parent.GetObservable(ItemsControl.ItemsProperty).ToBinding(),
  228. }.RegisterInNameScope(scope)
  229. };
  230. }
  231. private Control CreateScrollViewerTemplate(ITemplatedControl parent, INameScope scope)
  232. {
  233. return new ScrollContentPresenter
  234. {
  235. Name = "PART_ContentPresenter",
  236. [~ContentPresenter.ContentProperty] =
  237. parent.GetObservable(ContentControl.ContentProperty).ToBinding(),
  238. }.RegisterInNameScope(scope);
  239. }
  240. private static void ApplyTemplate(ListBox target)
  241. {
  242. // Apply the template to the ListBox itself.
  243. target.ApplyTemplate();
  244. // Then to its inner ScrollViewer.
  245. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  246. scrollViewer.ApplyTemplate();
  247. // Then make the ScrollViewer create its child.
  248. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  249. // Now the ItemsPresenter should be reigstered, so apply its template.
  250. target.Presenter.ApplyTemplate();
  251. }
  252. }
  253. }