ComboBoxTests.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. ((Control)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. ((Control)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. ((Control)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. }.RegisterInNameScope(scope)
  187. }.RegisterInNameScope(scope)
  188. }
  189. };
  190. });
  191. }
  192. [Fact]
  193. public void Detaching_Closed_ComboBox_Keeps_Current_Focus()
  194. {
  195. using (UnitTestApplication.Start(TestServices.RealFocus))
  196. {
  197. var target = new ComboBox
  198. {
  199. Items = new[] { new Canvas() },
  200. SelectedIndex = 0,
  201. Template = GetTemplate(),
  202. };
  203. var other = new Control { Focusable = true };
  204. StackPanel panel;
  205. var root = new TestRoot { Child = panel = new StackPanel { Children = { target, other } } };
  206. target.ApplyTemplate();
  207. target.Presenter.ApplyTemplate();
  208. other.Focus();
  209. Assert.True(other.IsFocused);
  210. panel.Children.Remove(target);
  211. Assert.True(other.IsFocused);
  212. }
  213. }
  214. [Theory]
  215. [InlineData(-1, 2, "c", "A item", "B item", "C item")]
  216. [InlineData(0, 1, "b", "A item", "B item", "C item")]
  217. [InlineData(2, 2, "x", "A item", "B item", "C item")]
  218. public void TextSearch_Should_Have_Expected_SelectedIndex(
  219. int initialSelectedIndex,
  220. int expectedSelectedIndex,
  221. string searchTerm,
  222. params string[] items)
  223. {
  224. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  225. {
  226. var target = new ComboBox
  227. {
  228. Template = GetTemplate(),
  229. Items = items.Select(x => new ComboBoxItem { Content = x })
  230. };
  231. target.ApplyTemplate();
  232. target.Presenter.ApplyTemplate();
  233. target.SelectedIndex = initialSelectedIndex;
  234. var args = new TextInputEventArgs
  235. {
  236. Text = searchTerm,
  237. RoutedEvent = InputElement.TextInputEvent
  238. };
  239. target.RaiseEvent(args);
  240. Assert.Equal(expectedSelectedIndex, target.SelectedIndex);
  241. }
  242. }
  243. [Fact]
  244. public void SelectedItem_Validation()
  245. {
  246. using (UnitTestApplication.Start(TestServices.MockThreadingInterface))
  247. {
  248. var target = new ComboBox
  249. {
  250. Template = GetTemplate(),
  251. VirtualizationMode = ItemVirtualizationMode.None
  252. };
  253. target.ApplyTemplate();
  254. target.Presenter.ApplyTemplate();
  255. var exception = new System.InvalidCastException("failed validation");
  256. var textObservable = new BehaviorSubject<BindingNotification>(new BindingNotification(exception, BindingErrorType.DataValidationError));
  257. target.Bind(ComboBox.SelectedItemProperty, textObservable);
  258. Assert.True(DataValidationErrors.GetHasErrors(target));
  259. Assert.True(DataValidationErrors.GetErrors(target).SequenceEqual(new[] { exception }));
  260. }
  261. }
  262. [Fact]
  263. public void Close_Window_On_Alt_F4_When_ComboBox_Is_Focus()
  264. {
  265. var inputManagerMock = new Moq.Mock<IInputManager>();
  266. var services = TestServices.StyledWindow.With(inputManager: inputManagerMock.Object);
  267. using (UnitTestApplication.Start(TestServices.StyledWindow))
  268. {
  269. var window = new Window();
  270. window.KeyDown += (s, e) =>
  271. {
  272. if (e.Handled == false
  273. && e.KeyModifiers.HasAllFlags(KeyModifiers.Alt) == true
  274. && e.Key == Key.F4 )
  275. {
  276. e.Handled = true;
  277. window.Close();
  278. }
  279. };
  280. var count = 0;
  281. var target = new ComboBox
  282. {
  283. Items = new[] { new Canvas() },
  284. SelectedIndex = 0,
  285. Template = GetTemplate(),
  286. };
  287. window.Content = target;
  288. window.Closing +=
  289. (sender, e) =>
  290. {
  291. count++;
  292. };
  293. window.Show();
  294. target.Focus();
  295. _helper.Down(target);
  296. _helper.Up(target);
  297. Assert.True(target.IsDropDownOpen);
  298. target.RaiseEvent(new KeyEventArgs
  299. {
  300. RoutedEvent = InputElement.KeyDownEvent,
  301. KeyModifiers = KeyModifiers.Alt,
  302. Key = Key.F4
  303. });
  304. Assert.Equal(1, count);
  305. }
  306. }
  307. [Fact]
  308. public void FlowDirection_Of_RectangleContent_Shuold_Be_LeftToRight()
  309. {
  310. var items = new[]
  311. {
  312. new ComboBoxItem()
  313. {
  314. Content = new Control()
  315. }
  316. };
  317. var target = new ComboBox
  318. {
  319. FlowDirection = FlowDirection.RightToLeft,
  320. Items = items,
  321. Template = GetTemplate()
  322. };
  323. var root = new TestRoot(target);
  324. target.ApplyTemplate();
  325. target.SelectedIndex = 0;
  326. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  327. Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection);
  328. }
  329. [Fact]
  330. public void FlowDirection_Of_RectangleContent_Updated_After_InvalidateMirrorTransform()
  331. {
  332. var parentContent = new Decorator()
  333. {
  334. Child = new Control()
  335. };
  336. var items = new[]
  337. {
  338. new ComboBoxItem()
  339. {
  340. Content = parentContent.Child
  341. }
  342. };
  343. var target = new ComboBox
  344. {
  345. Items = items,
  346. Template = GetTemplate()
  347. };
  348. var root = new TestRoot(target);
  349. target.ApplyTemplate();
  350. target.SelectedIndex = 0;
  351. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  352. Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection);
  353. parentContent.FlowDirection = FlowDirection.RightToLeft;
  354. target.FlowDirection = FlowDirection.RightToLeft;
  355. Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection);
  356. }
  357. [Fact]
  358. public void FlowDirection_Of_RectangleContent_Updated_After_OpenPopup()
  359. {
  360. using (UnitTestApplication.Start(TestServices.StyledWindow))
  361. {
  362. var parentContent = new Decorator()
  363. {
  364. Child = new Control()
  365. };
  366. var items = new[]
  367. {
  368. new ComboBoxItem()
  369. {
  370. Content = parentContent.Child
  371. }
  372. };
  373. var target = new ComboBox
  374. {
  375. FlowDirection = FlowDirection.RightToLeft,
  376. Items = items,
  377. Template = GetTemplate()
  378. };
  379. var root = new TestRoot(target);
  380. target.ApplyTemplate();
  381. target.SelectedIndex = 0;
  382. var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle;
  383. Assert.Equal(FlowDirection.LeftToRight, rectangle.FlowDirection);
  384. parentContent.FlowDirection = FlowDirection.RightToLeft;
  385. var popup = target.GetVisualDescendants().OfType<Popup>().First();
  386. popup.PlacementTarget = new Window();
  387. popup.Open();
  388. Assert.Equal(FlowDirection.RightToLeft, rectangle.FlowDirection);
  389. }
  390. }
  391. }
  392. }