ListBoxTests_Single.cs 11 KB

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