ComboBoxTests.cs 15 KB

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