ComboBoxTests.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. using System.Linq;
  2. using System.Reactive.Subjects;
  3. using Avalonia.Controls.Presenters;
  4. using Avalonia.Controls.Primitives;
  5. using Avalonia.Controls.Shapes;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.Data;
  8. using Avalonia.Input;
  9. using Avalonia.LogicalTree;
  10. using Avalonia.Media;
  11. using Avalonia.VisualTree;
  12. using Avalonia.UnitTests;
  13. using Xunit;
  14. namespace Avalonia.Controls.UnitTests
  15. {
  16. public class ComboBoxTests
  17. {
  18. MouseTestHelper _helper = new MouseTestHelper();
  19. [Fact]
  20. public void Clicking_On_Control_Toggles_IsDropDownOpen()
  21. {
  22. var target = new ComboBox
  23. {
  24. Items = new[] { "Foo", "Bar" },
  25. };
  26. _helper.Down(target);
  27. _helper.Up(target);
  28. Assert.True(target.IsDropDownOpen);
  29. _helper.Down(target);
  30. _helper.Up(target);
  31. Assert.False(target.IsDropDownOpen);
  32. }
  33. [Fact]
  34. public void WrapSelection_Should_Work()
  35. {
  36. using (UnitTestApplication.Start(TestServices.RealFocus))
  37. {
  38. var items = new[]
  39. {
  40. new ComboBoxItem() { Content = "bla" },
  41. new ComboBoxItem() { Content = "dd" },
  42. new ComboBoxItem() { Content = "sdf", IsEnabled = false }
  43. };
  44. var target = new ComboBox
  45. {
  46. Items = items,
  47. Template = GetTemplate(),
  48. WrapSelection = true
  49. };
  50. var root = new TestRoot(target);
  51. target.ApplyTemplate();
  52. target.Presenter.ApplyTemplate();
  53. target.Focus();
  54. Assert.Equal(target.SelectedIndex, -1);
  55. Assert.True(target.IsFocused);
  56. target.RaiseEvent(new KeyEventArgs
  57. {
  58. RoutedEvent = InputElement.KeyDownEvent,
  59. Key = Key.Up,
  60. });
  61. Assert.Equal(target.SelectedIndex, 1);
  62. target.RaiseEvent(new KeyEventArgs
  63. {
  64. RoutedEvent = InputElement.KeyDownEvent,
  65. Key = Key.Down,
  66. });
  67. Assert.Equal(target.SelectedIndex, 0);
  68. }
  69. }
  70. [Fact]
  71. public void Focuses_Next_Item_On_Key_Down()
  72. {
  73. using (UnitTestApplication.Start(TestServices.RealFocus))
  74. {
  75. var items = new[]
  76. {
  77. new ComboBoxItem() { Content = "bla" },
  78. new ComboBoxItem() { Content = "dd", IsEnabled = false },
  79. new ComboBoxItem() { Content = "sdf" }
  80. };
  81. var target = new ComboBox
  82. {
  83. Items = items,
  84. Template = GetTemplate()
  85. };
  86. var root = new TestRoot(target);
  87. target.ApplyTemplate();
  88. target.Presenter.ApplyTemplate();
  89. target.Focus();
  90. Assert.Equal(target.SelectedIndex, -1);
  91. Assert.True(target.IsFocused);
  92. target.RaiseEvent(new KeyEventArgs
  93. {
  94. RoutedEvent = InputElement.KeyDownEvent,
  95. Key = Key.Down,
  96. });
  97. Assert.Equal(target.SelectedIndex, 0);
  98. target.RaiseEvent(new KeyEventArgs
  99. {
  100. RoutedEvent = InputElement.KeyDownEvent,
  101. Key = Key.Down,
  102. });
  103. Assert.Equal(target.SelectedIndex, 2);
  104. }
  105. }
  106. [Fact]
  107. public void SelectionBoxItem_Is_Rectangle_With_VisualBrush_When_Selection_Is_Control()
  108. {
  109. var items = new[] { new Canvas() };
  110. var target = new ComboBox
  111. {
  112. Items = items,
  113. SelectedIndex = 0,
  114. };
  115. var root = new TestRoot(target);
  116. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  117. Assert.NotNull(rectangle);
  118. var brush = rectangle.Fill as VisualBrush;
  119. Assert.NotNull(brush);
  120. Assert.Same(items[0], brush.Visual);
  121. }
  122. [Fact]
  123. public void SelectionBoxItem_Rectangle_Is_Removed_From_Logical_Tree()
  124. {
  125. var target = new ComboBox
  126. {
  127. Items = new[] { new Canvas() },
  128. SelectedIndex = 0,
  129. Template = GetTemplate(),
  130. };
  131. var root = new TestRoot { Child = target };
  132. target.ApplyTemplate();
  133. target.Presenter.ApplyTemplate();
  134. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  135. Assert.True(((ILogical)target).IsAttachedToLogicalTree);
  136. Assert.True(((ILogical)rectangle).IsAttachedToLogicalTree);
  137. rectangle.DetachedFromLogicalTree += (s, e) => { };
  138. root.Child = null;
  139. Assert.False(((ILogical)target).IsAttachedToLogicalTree);
  140. Assert.False(((ILogical)rectangle).IsAttachedToLogicalTree);
  141. }
  142. private FuncControlTemplate GetTemplate()
  143. {
  144. return new FuncControlTemplate<ComboBox>((parent, scope) =>
  145. {
  146. return new Panel
  147. {
  148. Name = "container",
  149. Children =
  150. {
  151. new ContentControl
  152. {
  153. [!ContentControl.ContentProperty] = parent[!ComboBox.SelectionBoxItemProperty],
  154. },
  155. new ToggleButton
  156. {
  157. Name = "toggle",
  158. }.RegisterInNameScope(scope),
  159. new Popup
  160. {
  161. Name = "PART_Popup",
  162. Child = new ItemsPresenter
  163. {
  164. Name = "PART_ItemsPresenter",
  165. [!ItemsPresenter.ItemsProperty] = parent[!ComboBox.ItemsProperty],
  166. }.RegisterInNameScope(scope)
  167. }.RegisterInNameScope(scope)
  168. }
  169. };
  170. });
  171. }
  172. [Fact]
  173. public void Detaching_Closed_ComboBox_Keeps_Current_Focus()
  174. {
  175. using (UnitTestApplication.Start(TestServices.RealFocus))
  176. {
  177. var target = new ComboBox
  178. {
  179. Items = new[] { new Canvas() },
  180. SelectedIndex = 0,
  181. Template = GetTemplate(),
  182. };
  183. var other = new Control { Focusable = true };
  184. StackPanel panel;
  185. var root = new TestRoot { Child = panel = new StackPanel { Children = { target, other } } };
  186. target.ApplyTemplate();
  187. target.Presenter.ApplyTemplate();
  188. other.Focus();
  189. Assert.True(other.IsFocused);
  190. panel.Children.Remove(target);
  191. Assert.True(other.IsFocused);
  192. }
  193. }
  194. [Theory]
  195. [InlineData(-1, 2, "c", "A item", "B item", "C item")]
  196. [InlineData(0, 1, "b", "A item", "B item", "C item")]
  197. [InlineData(2, 2, "x", "A item", "B item", "C item")]
  198. public void TextSearch_Should_Have_Expected_SelectedIndex(
  199. int initialSelectedIndex,
  200. int expectedSelectedIndex,
  201. string searchTerm,
  202. params string[] items)
  203. {
  204. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  205. {
  206. var target = new ComboBox
  207. {
  208. Template = GetTemplate(),
  209. Items = items.Select(x => new ComboBoxItem { Content = x })
  210. };
  211. target.ApplyTemplate();
  212. target.Presenter.ApplyTemplate();
  213. target.SelectedIndex = initialSelectedIndex;
  214. var args = new TextInputEventArgs
  215. {
  216. Text = searchTerm,
  217. RoutedEvent = InputElement.TextInputEvent
  218. };
  219. target.RaiseEvent(args);
  220. Assert.Equal(expectedSelectedIndex, target.SelectedIndex);
  221. }
  222. }
  223. [Fact]
  224. public void SelectedItem_Validation()
  225. {
  226. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  227. {
  228. var target = new ComboBox
  229. {
  230. Template = GetTemplate(),
  231. VirtualizationMode = ItemVirtualizationMode.None
  232. };
  233. target.ApplyTemplate();
  234. target.Presenter.ApplyTemplate();
  235. var exception = new System.InvalidCastException("failed validation");
  236. var textObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
  237. target.Bind(ComboBox.SelectedItemProperty, textObservable);
  238. Assert.True(DataValidationErrors.GetHasErrors(target));
  239. Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception }));
  240. }
  241. }
  242. [Fact]
  243. public void Close_Window_On_Alt_F4_When_ComboBox_Is_Focus()
  244. {
  245. var inputManagerMock = new Moq.Mock<IInputManager>();
  246. var services = TestServices.StyledWindow.With(inputManager: inputManagerMock.Object);
  247. using (UnitTestApplication.Start(TestServices.StyledWindow))
  248. {
  249. var window = new Window();
  250. window.KeyDown += (s, e) =>
  251. {
  252. if (e.Handled == false
  253. && e.KeyModifiers.HasAllFlags(KeyModifiers.Alt) == true
  254. && e.Key == Key.F4 )
  255. {
  256. e.Handled = true;
  257. window.Close();
  258. }
  259. };
  260. var count = 0;
  261. var target = new ComboBox
  262. {
  263. Items = new[] { new Canvas() },
  264. SelectedIndex = 0,
  265. Template = GetTemplate(),
  266. };
  267. window.Content = target;
  268. window.Closing +=
  269. (sender, e) =>
  270. {
  271. count++;
  272. };
  273. window.Show();
  274. target.Focus();
  275. _helper.Down(target);
  276. _helper.Up(target);
  277. Assert.True(target.IsDropDownOpen);
  278. target.RaiseEvent(new KeyEventArgs
  279. {
  280. RoutedEvent = InputElement.KeyDownEvent,
  281. KeyModifiers = KeyModifiers.Alt,
  282. Key = Key.F4
  283. });
  284. Assert.Equal(1, count);
  285. }
  286. }
  287. [Fact]
  288. public void FlowDirection_Of_RectangleContent_Shuold_Be_LeftToRight()
  289. {
  290. var items = new[]
  291. {
  292. new ComboBoxItem()
  293. {
  294. Content = new Control()
  295. }
  296. };
  297. var target = new ComboBox
  298. {
  299. FlowDirection = FlowDirection.RightToLeft,
  300. Items = items,
  301. Template = GetTemplate()
  302. };
  303. var root = new TestRoot(target);
  304. target.ApplyTemplate();
  305. target.SelectedIndex = 0;
  306. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  307. Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection);
  308. }
  309. [Fact]
  310. public void FlowDirection_Of_RectangleContent_Updated_After_InvalidateMirrorTransform()
  311. {
  312. var parentContent = new Decorator()
  313. {
  314. Child = new Control()
  315. };
  316. var items = new[]
  317. {
  318. new ComboBoxItem()
  319. {
  320. Content = parentContent.Child
  321. }
  322. };
  323. var target = new ComboBox
  324. {
  325. Items = items,
  326. Template = GetTemplate()
  327. };
  328. var root = new TestRoot(target);
  329. target.ApplyTemplate();
  330. target.SelectedIndex = 0;
  331. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  332. Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection);
  333. parentContent.FlowDirection = FlowDirection.RightToLeft;
  334. target.FlowDirection = FlowDirection.RightToLeft;
  335. Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection);
  336. }
  337. [Fact]
  338. public void FlowDirection_Of_RectangleContent_Updated_After_OpenPopup()
  339. {
  340. using (UnitTestApplication.Start(TestServices.StyledWindow))
  341. {
  342. var parentContent = new Decorator()
  343. {
  344. Child = new Control()
  345. };
  346. var items = new[]
  347. {
  348. new ComboBoxItem()
  349. {
  350. Content = parentContent.Child
  351. }
  352. };
  353. var target = new ComboBox
  354. {
  355. FlowDirection = FlowDirection.RightToLeft,
  356. Items = items,
  357. Template = GetTemplate()
  358. };
  359. var root = new TestRoot(target);
  360. target.ApplyTemplate();
  361. target.SelectedIndex = 0;
  362. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  363. Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection);
  364. parentContent.FlowDirection = FlowDirection.RightToLeft;
  365. var popup = target.GetVisualDescendants().OfType<Popup>().First();
  366. popup.PlacementTarget = new Window();
  367. popup.Open();
  368. Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection);
  369. }
  370. }
  371. }
  372. }