ListBoxTests_Single.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Linq;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Primitives;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Data;
  8. using Avalonia.Input;
  9. using Avalonia.Input.Platform;
  10. using Avalonia.LogicalTree;
  11. using Avalonia.Styling;
  12. using Avalonia.UnitTests;
  13. using Avalonia.VisualTree;
  14. using Moq;
  15. using Xunit;
  16. namespace Avalonia.Controls.UnitTests
  17. {
  18. public class ListBoxTests_Single
  19. {
  20. MouseTestHelper _mouse = new MouseTestHelper();
  21. [Fact]
  22. public void Focusing_Item_With_Tab_Should_Not_Select_It()
  23. {
  24. var target = new ListBox
  25. {
  26. Template = new FuncControlTemplate(CreateListBoxTemplate),
  27. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  28. };
  29. ApplyTemplate(target);
  30. target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
  31. {
  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. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  43. };
  44. ApplyTemplate(target);
  45. target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
  46. {
  47. NavigationMethod = NavigationMethod.Directional,
  48. });
  49. Assert.Equal(0, target.SelectedIndex);
  50. }
  51. [Fact]
  52. public void Focusing_Item_With_Arrow_Key_And_Ctrl_Pressed_Should_Not_Select_It()
  53. {
  54. var target = new ListBox
  55. {
  56. Template = new FuncControlTemplate(CreateListBoxTemplate),
  57. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  58. };
  59. ApplyTemplate(target);
  60. target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
  61. {
  62. NavigationMethod = NavigationMethod.Directional,
  63. KeyModifiers = KeyModifiers.Control
  64. });
  65. Assert.Equal(-1, target.SelectedIndex);
  66. }
  67. [Fact]
  68. public void Pressing_Space_On_Focused_Item_With_Ctrl_Pressed_Should_Select_It()
  69. {
  70. using (UnitTestApplication.Start())
  71. {
  72. var target = new ListBox
  73. {
  74. Template = new FuncControlTemplate(CreateListBoxTemplate),
  75. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  76. };
  77. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  78. ApplyTemplate(target);
  79. target.Presenter.Panel.Children[0].RaiseEvent(new GotFocusEventArgs
  80. {
  81. NavigationMethod = NavigationMethod.Directional,
  82. KeyModifiers = KeyModifiers.Control
  83. });
  84. target.Presenter.Panel.Children[0].RaiseEvent(new KeyEventArgs
  85. {
  86. RoutedEvent = InputElement.KeyDownEvent,
  87. Key = Key.Space,
  88. KeyModifiers = KeyModifiers.Control
  89. });
  90. Assert.Equal(0, target.SelectedIndex);
  91. }
  92. }
  93. [Fact]
  94. public void Clicking_Item_Should_Select_It()
  95. {
  96. using (UnitTestApplication.Start())
  97. {
  98. var target = new ListBox
  99. {
  100. Template = new FuncControlTemplate(CreateListBoxTemplate),
  101. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  102. };
  103. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  104. ApplyTemplate(target);
  105. _mouse.Click(target.Presenter.Panel.Children[0]);
  106. Assert.Equal(0, target.SelectedIndex);
  107. }
  108. }
  109. [Fact]
  110. public void Clicking_Selected_Item_Should_Not_Deselect_It()
  111. {
  112. using (UnitTestApplication.Start())
  113. {
  114. var target = new ListBox
  115. {
  116. Template = new FuncControlTemplate(CreateListBoxTemplate),
  117. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  118. };
  119. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  120. ApplyTemplate(target);
  121. target.SelectedIndex = 0;
  122. _mouse.Click(target.Presenter.Panel.Children[0]);
  123. Assert.Equal(0, target.SelectedIndex);
  124. }
  125. }
  126. [Fact]
  127. public void Clicking_Item_Should_Select_It_When_SelectionMode_Toggle()
  128. {
  129. using (UnitTestApplication.Start())
  130. {
  131. var target = new ListBox
  132. {
  133. Template = new FuncControlTemplate(CreateListBoxTemplate),
  134. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  135. SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
  136. };
  137. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  138. ApplyTemplate(target);
  139. _mouse.Click(target.Presenter.Panel.Children[0]);
  140. Assert.Equal(0, target.SelectedIndex);
  141. }
  142. }
  143. [Fact]
  144. public void Clicking_Selected_Item_Should_Deselect_It_When_SelectionMode_Toggle()
  145. {
  146. using (UnitTestApplication.Start())
  147. {
  148. var target = new ListBox
  149. {
  150. Template = new FuncControlTemplate(CreateListBoxTemplate),
  151. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  152. SelectionMode = SelectionMode.Toggle,
  153. };
  154. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  155. ApplyTemplate(target);
  156. target.SelectedIndex = 0;
  157. _mouse.Click(target.Presenter.Panel.Children[0]);
  158. Assert.Equal(-1, target.SelectedIndex);
  159. }
  160. }
  161. [Fact]
  162. public void Clicking_Selected_Item_Should_Not_Deselect_It_When_SelectionMode_ToggleAlwaysSelected()
  163. {
  164. using (UnitTestApplication.Start())
  165. {
  166. var target = new ListBox
  167. {
  168. Template = new FuncControlTemplate(CreateListBoxTemplate),
  169. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  170. SelectionMode = SelectionMode.Toggle | SelectionMode.AlwaysSelected,
  171. };
  172. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  173. ApplyTemplate(target);
  174. target.SelectedIndex = 0;
  175. _mouse.Click(target.Presenter.Panel.Children[0]);
  176. Assert.Equal(0, target.SelectedIndex);
  177. }
  178. }
  179. [Fact]
  180. public void Clicking_Another_Item_Should_Select_It_When_SelectionMode_Toggle()
  181. {
  182. using (UnitTestApplication.Start())
  183. {
  184. var target = new ListBox
  185. {
  186. Template = new FuncControlTemplate(CreateListBoxTemplate),
  187. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  188. SelectionMode = SelectionMode.Single | SelectionMode.Toggle,
  189. };
  190. AvaloniaLocator.CurrentMutable.Bind<PlatformHotkeyConfiguration>().ToConstant(new Mock<PlatformHotkeyConfiguration>().Object);
  191. ApplyTemplate(target);
  192. target.SelectedIndex = 1;
  193. _mouse.Click(target.Presenter.Panel.Children[0]);
  194. Assert.Equal(0, target.SelectedIndex);
  195. }
  196. }
  197. [Fact]
  198. public void Setting_Item_IsSelected_Sets_ListBox_Selection()
  199. {
  200. var target = new ListBox
  201. {
  202. Template = new FuncControlTemplate(CreateListBoxTemplate),
  203. ItemsSource = new[] { "Foo", "Bar", "Baz " },
  204. };
  205. ApplyTemplate(target);
  206. ((ListBoxItem)target.GetLogicalChildren().ElementAt(1)).IsSelected = true;
  207. Assert.Equal("Bar", target.SelectedItem);
  208. Assert.Equal(1, target.SelectedIndex);
  209. }
  210. [Fact]
  211. public void SelectedItem_Should_Not_Cause_StackOverflow()
  212. {
  213. var viewModel = new TestStackOverflowViewModel()
  214. {
  215. Items = new List<string> { "foo", "bar", "baz" }
  216. };
  217. var target = new ListBox
  218. {
  219. Template = new FuncControlTemplate(CreateListBoxTemplate),
  220. DataContext = viewModel,
  221. ItemsSource = viewModel.Items
  222. };
  223. target.Bind(ListBox.SelectedItemProperty,
  224. new Binding("SelectedItem") { Mode = BindingMode.TwoWay });
  225. Assert.Equal(0, viewModel.SetterInvokedCount);
  226. // In Issue #855, a Stackoverflow occured here.
  227. target.SelectedItem = viewModel.Items[2];
  228. Assert.Equal(viewModel.Items[1], target.SelectedItem);
  229. Assert.Equal(1, viewModel.SetterInvokedCount);
  230. }
  231. private class TestStackOverflowViewModel : INotifyPropertyChanged
  232. {
  233. public List<string> Items { get; set; }
  234. public int SetterInvokedCount { get; private set; }
  235. public const int MaxInvokedCount = 1000;
  236. private string _selectedItem;
  237. public event PropertyChangedEventHandler PropertyChanged;
  238. public string SelectedItem
  239. {
  240. get { return _selectedItem; }
  241. set
  242. {
  243. if (_selectedItem != value)
  244. {
  245. SetterInvokedCount++;
  246. int index = Items.IndexOf(value);
  247. if (MaxInvokedCount > SetterInvokedCount && index > 0)
  248. {
  249. _selectedItem = Items[index - 1];
  250. }
  251. else
  252. {
  253. _selectedItem = value;
  254. }
  255. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedItem)));
  256. }
  257. }
  258. }
  259. }
  260. private Control CreateListBoxTemplate(TemplatedControl parent, INameScope scope)
  261. {
  262. return new ScrollViewer
  263. {
  264. Template = new FuncControlTemplate(CreateScrollViewerTemplate),
  265. Content = new ItemsPresenter
  266. {
  267. Name = "PART_ItemsPresenter",
  268. }.RegisterInNameScope(scope)
  269. };
  270. }
  271. private Control CreateScrollViewerTemplate(TemplatedControl parent, INameScope scope)
  272. {
  273. return new ScrollContentPresenter
  274. {
  275. Name = "PART_ContentPresenter",
  276. [~ContentPresenter.ContentProperty] =
  277. parent.GetObservable(ContentControl.ContentProperty).ToBinding(),
  278. }.RegisterInNameScope(scope);
  279. }
  280. private static void ApplyTemplate(ListBox target)
  281. {
  282. // Apply the template to the ListBox itself.
  283. target.ApplyTemplate();
  284. // Then to its inner ScrollViewer.
  285. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single();
  286. scrollViewer.ApplyTemplate();
  287. // Then make the ScrollViewer create its child.
  288. ((ContentPresenter)scrollViewer.Presenter).UpdateChild();
  289. // Now the ItemsPresenter should be reigstered, so apply its template.
  290. target.Presenter.ApplyTemplate();
  291. }
  292. }
  293. }