ComboBoxTests.cs 15 KB

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