TreeViewTests.cs 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using Avalonia.Collections;
  8. using Avalonia.Controls.Presenters;
  9. using Avalonia.Controls.Templates;
  10. using Avalonia.Data;
  11. using Avalonia.Data.Core;
  12. using Avalonia.Input;
  13. using Avalonia.Input.Platform;
  14. using Avalonia.Interactivity;
  15. using Avalonia.LogicalTree;
  16. using Avalonia.UnitTests;
  17. using Xunit;
  18. namespace Avalonia.Controls.UnitTests
  19. {
  20. public class TreeViewTests
  21. {
  22. MouseTestHelper _mouse = new MouseTestHelper();
  23. [Fact]
  24. public void Items_Should_Be_Created()
  25. {
  26. var target = new TreeView
  27. {
  28. Template = CreateTreeViewTemplate(),
  29. Items = CreateTestTreeData(),
  30. };
  31. CreateNodeDataTemplate(target);
  32. ApplyTemplates(target);
  33. Assert.Equal(new[] { "Root" }, ExtractItemHeader(target, 0));
  34. Assert.Equal(new[] { "Child1", "Child2", "Child3" }, ExtractItemHeader(target, 1));
  35. Assert.Equal(new[] { "Grandchild2a" }, ExtractItemHeader(target, 2));
  36. }
  37. [Fact]
  38. public void Items_Should_Be_Created_Using_ItemTemplate_If_Present()
  39. {
  40. TreeView target;
  41. var root = new TestRoot
  42. {
  43. Child = target = new TreeView
  44. {
  45. Template = CreateTreeViewTemplate(),
  46. Items = CreateTestTreeData(),
  47. ItemTemplate = new FuncTreeDataTemplate<Node>(
  48. (_, __) => new Canvas(),
  49. x => x.Children),
  50. }
  51. };
  52. ApplyTemplates(target);
  53. var items = target.ItemContainerGenerator.Index.Containers
  54. .OfType<TreeViewItem>()
  55. .ToList();
  56. Assert.Equal(5, items.Count);
  57. Assert.All(items, x => Assert.IsType<Canvas>(x.HeaderPresenter.Child));
  58. }
  59. [Fact]
  60. public void Root_ItemContainerGenerator_Containers_Should_Be_Root_Containers()
  61. {
  62. var target = new TreeView
  63. {
  64. Template = CreateTreeViewTemplate(),
  65. Items = CreateTestTreeData(),
  66. };
  67. CreateNodeDataTemplate(target);
  68. ApplyTemplates(target);
  69. var container = (TreeViewItem)target.ItemContainerGenerator.Containers.Single().ContainerControl;
  70. var header = (TextBlock)container.Header;
  71. Assert.Equal("Root", header.Text);
  72. }
  73. [Fact]
  74. public void Root_TreeContainerFromItem_Should_Return_Descendant_Item()
  75. {
  76. var tree = CreateTestTreeData();
  77. var target = new TreeView
  78. {
  79. Template = CreateTreeViewTemplate(),
  80. Items = tree,
  81. };
  82. // For TreeViewItem to find its parent TreeView, OnAttachedToLogicalTree needs
  83. // to be called, which requires an IStyleRoot.
  84. var root = new TestRoot();
  85. root.Child = target;
  86. CreateNodeDataTemplate(target);
  87. ApplyTemplates(target);
  88. var container = target.ItemContainerGenerator.Index.ContainerFromItem(
  89. tree[0].Children[1].Children[0]);
  90. Assert.NotNull(container);
  91. var header = ((TreeViewItem)container).Header;
  92. var headerContent = ((TextBlock)header).Text;
  93. Assert.Equal("Grandchild2a", headerContent);
  94. }
  95. [Fact]
  96. public void Clicking_Item_Should_Select_It()
  97. {
  98. var tree = CreateTestTreeData();
  99. var target = new TreeView
  100. {
  101. Template = CreateTreeViewTemplate(),
  102. Items = tree,
  103. };
  104. var visualRoot = new TestRoot();
  105. visualRoot.Child = target;
  106. CreateNodeDataTemplate(target);
  107. ApplyTemplates(target);
  108. var item = tree[0].Children[1].Children[0];
  109. var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item);
  110. Assert.NotNull(container);
  111. _mouse.Click(container);
  112. Assert.Equal(item, target.SelectedItem);
  113. Assert.True(container.IsSelected);
  114. }
  115. [Fact]
  116. public void Clicking_WithControlModifier_Selected_Item_Should_Deselect_It()
  117. {
  118. var tree = CreateTestTreeData();
  119. var target = new TreeView
  120. {
  121. Template = CreateTreeViewTemplate(),
  122. Items = tree
  123. };
  124. var visualRoot = new TestRoot();
  125. visualRoot.Child = target;
  126. CreateNodeDataTemplate(target);
  127. ApplyTemplates(target);
  128. var item = tree[0].Children[1].Children[0];
  129. var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item);
  130. Assert.NotNull(container);
  131. target.SelectedItem = item;
  132. Assert.True(container.IsSelected);
  133. _mouse.Click(container, modifiers: InputModifiers.Control);
  134. Assert.Null(target.SelectedItem);
  135. Assert.False(container.IsSelected);
  136. }
  137. [Fact]
  138. public void Clicking_WithControlModifier_Not_Selected_Item_Should_Select_It()
  139. {
  140. var tree = CreateTestTreeData();
  141. var target = new TreeView
  142. {
  143. Template = CreateTreeViewTemplate(),
  144. Items = tree
  145. };
  146. var visualRoot = new TestRoot();
  147. visualRoot.Child = target;
  148. CreateNodeDataTemplate(target);
  149. ApplyTemplates(target);
  150. var item1 = tree[0].Children[1].Children[0];
  151. var container1 = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item1);
  152. var item2 = tree[0].Children[1];
  153. var container2 = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item2);
  154. Assert.NotNull(container1);
  155. Assert.NotNull(container2);
  156. target.SelectedItem = item1;
  157. Assert.True(container1.IsSelected);
  158. _mouse.Click(container2, modifiers: InputModifiers.Control);
  159. Assert.Equal(item2, target.SelectedItem);
  160. Assert.False(container1.IsSelected);
  161. Assert.True(container2.IsSelected);
  162. }
  163. [Fact]
  164. public void Clicking_WithControlModifier_Selected_Item_Should_Deselect_And_Remove_From_SelectedItems()
  165. {
  166. var tree = CreateTestTreeData();
  167. var target = new TreeView
  168. {
  169. Template = CreateTreeViewTemplate(),
  170. Items = tree,
  171. SelectionMode = SelectionMode.Multiple
  172. };
  173. var visualRoot = new TestRoot();
  174. visualRoot.Child = target;
  175. CreateNodeDataTemplate(target);
  176. ApplyTemplates(target);
  177. var rootNode = tree[0];
  178. var item1 = rootNode.Children[0];
  179. var item2 = rootNode.Children.Last();
  180. var item1Container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item1);
  181. var item2Container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item2);
  182. ClickContainer(item1Container, InputModifiers.Control);
  183. Assert.True(item1Container.IsSelected);
  184. ClickContainer(item2Container, InputModifiers.Control);
  185. Assert.True(item2Container.IsSelected);
  186. Assert.Equal(new[] {item1, item2}, target.SelectedItems.OfType<Node>());
  187. ClickContainer(item1Container, InputModifiers.Control);
  188. Assert.False(item1Container.IsSelected);
  189. Assert.DoesNotContain(item1, target.SelectedItems.OfType<Node>());
  190. }
  191. [Fact]
  192. public void Clicking_WithShiftModifier_DownDirection_Should_Select_Range_Of_Items()
  193. {
  194. var tree = CreateTestTreeData();
  195. var target = new TreeView
  196. {
  197. Template = CreateTreeViewTemplate(),
  198. Items = tree,
  199. SelectionMode = SelectionMode.Multiple
  200. };
  201. var visualRoot = new TestRoot();
  202. visualRoot.Child = target;
  203. CreateNodeDataTemplate(target);
  204. ApplyTemplates(target);
  205. var rootNode = tree[0];
  206. var from = rootNode.Children[0];
  207. var to = rootNode.Children.Last();
  208. var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
  209. var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
  210. ClickContainer(fromContainer, InputModifiers.None);
  211. Assert.True(fromContainer.IsSelected);
  212. ClickContainer(toContainer, InputModifiers.Shift);
  213. AssertChildrenSelected(target, rootNode);
  214. }
  215. [Fact]
  216. public void Clicking_WithShiftModifier_UpDirection_Should_Select_Range_Of_Items()
  217. {
  218. var tree = CreateTestTreeData();
  219. var target = new TreeView
  220. {
  221. Template = CreateTreeViewTemplate(),
  222. Items = tree,
  223. SelectionMode = SelectionMode.Multiple
  224. };
  225. var visualRoot = new TestRoot();
  226. visualRoot.Child = target;
  227. CreateNodeDataTemplate(target);
  228. ApplyTemplates(target);
  229. var rootNode = tree[0];
  230. var from = rootNode.Children.Last();
  231. var to = rootNode.Children[0];
  232. var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
  233. var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
  234. ClickContainer(fromContainer, InputModifiers.None);
  235. Assert.True(fromContainer.IsSelected);
  236. ClickContainer(toContainer, InputModifiers.Shift);
  237. AssertChildrenSelected(target, rootNode);
  238. }
  239. [Fact]
  240. public void Clicking_First_Item_Of_SelectedItems_Should_Select_Only_It()
  241. {
  242. var tree = CreateTestTreeData();
  243. var target = new TreeView
  244. {
  245. Template = CreateTreeViewTemplate(),
  246. Items = tree,
  247. SelectionMode = SelectionMode.Multiple
  248. };
  249. var visualRoot = new TestRoot();
  250. visualRoot.Child = target;
  251. CreateNodeDataTemplate(target);
  252. ApplyTemplates(target);
  253. var rootNode = tree[0];
  254. var from = rootNode.Children.Last();
  255. var to = rootNode.Children[0];
  256. var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
  257. var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
  258. ClickContainer(fromContainer, InputModifiers.None);
  259. ClickContainer(toContainer, InputModifiers.Shift);
  260. AssertChildrenSelected(target, rootNode);
  261. ClickContainer(fromContainer, InputModifiers.None);
  262. Assert.True(fromContainer.IsSelected);
  263. foreach (var child in rootNode.Children)
  264. {
  265. if (child == from)
  266. {
  267. continue;
  268. }
  269. var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(child);
  270. Assert.False(container.IsSelected);
  271. }
  272. }
  273. [Fact]
  274. public void Setting_SelectedItem_Should_Set_Container_Selected()
  275. {
  276. var tree = CreateTestTreeData();
  277. var target = new TreeView
  278. {
  279. Template = CreateTreeViewTemplate(),
  280. Items = tree,
  281. };
  282. var visualRoot = new TestRoot();
  283. visualRoot.Child = target;
  284. CreateNodeDataTemplate(target);
  285. ApplyTemplates(target);
  286. var item = tree[0].Children[1].Children[0];
  287. var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item);
  288. Assert.NotNull(container);
  289. target.SelectedItem = item;
  290. Assert.True(container.IsSelected);
  291. }
  292. [Fact]
  293. public void Setting_SelectedItem_Should_Raise_SelectedItemChanged_Event()
  294. {
  295. var tree = CreateTestTreeData();
  296. var target = new TreeView
  297. {
  298. Template = CreateTreeViewTemplate(),
  299. Items = tree,
  300. };
  301. var visualRoot = new TestRoot();
  302. visualRoot.Child = target;
  303. CreateNodeDataTemplate(target);
  304. ApplyTemplates(target);
  305. var item = tree[0].Children[1].Children[0];
  306. var called = false;
  307. target.SelectionChanged += (s, e) =>
  308. {
  309. Assert.Empty(e.RemovedItems);
  310. Assert.Equal(1, e.AddedItems.Count);
  311. Assert.Same(item, e.AddedItems[0]);
  312. called = true;
  313. };
  314. target.SelectedItem = item;
  315. Assert.True(called);
  316. }
  317. [Fact]
  318. public void LogicalChildren_Should_Be_Set()
  319. {
  320. var target = new TreeView
  321. {
  322. Template = CreateTreeViewTemplate(),
  323. Items = new[] { "Foo", "Bar", "Baz " },
  324. };
  325. ApplyTemplates(target);
  326. var result = target.GetLogicalChildren()
  327. .OfType<TreeViewItem>()
  328. .Select(x => x.Header)
  329. .OfType<TextBlock>()
  330. .Select(x => x.Text)
  331. .ToList();
  332. Assert.Equal(new[] { "Foo", "Bar", "Baz " }, result);
  333. }
  334. [Fact]
  335. public void Removing_Item_Should_Remove_Itself_And_Children_From_Index()
  336. {
  337. var tree = CreateTestTreeData();
  338. var target = new TreeView
  339. {
  340. Template = CreateTreeViewTemplate(),
  341. Items = tree,
  342. };
  343. var root = new TestRoot();
  344. root.Child = target;
  345. CreateNodeDataTemplate(target);
  346. ApplyTemplates(target);
  347. Assert.Equal(5, target.ItemContainerGenerator.Index.Containers.Count());
  348. tree[0].Children.RemoveAt(1);
  349. Assert.Equal(3, target.ItemContainerGenerator.Index.Containers.Count());
  350. }
  351. [Fact]
  352. public void DataContexts_Should_Be_Correctly_Set()
  353. {
  354. var items = new object[]
  355. {
  356. "Foo",
  357. new Node { Value = "Bar" },
  358. new TextBlock { Text = "Baz" },
  359. new TreeViewItem { Header = "Qux" },
  360. };
  361. var target = new TreeView
  362. {
  363. Template = CreateTreeViewTemplate(),
  364. DataContext = "Base",
  365. DataTemplates =
  366. {
  367. new FuncDataTemplate<Node>((x, _) => new Button { Content = x })
  368. },
  369. Items = items,
  370. };
  371. ApplyTemplates(target);
  372. var dataContexts = target.Presenter.Panel.Children
  373. .Cast<Control>()
  374. .Select(x => x.DataContext)
  375. .ToList();
  376. Assert.Equal(
  377. new object[] { items[0], items[1], "Base", "Base" },
  378. dataContexts);
  379. }
  380. [Fact]
  381. public void Control_Item_Should_Not_Be_NameScope()
  382. {
  383. var items = new object[]
  384. {
  385. new TreeViewItem(),
  386. };
  387. var target = new TreeView
  388. {
  389. Template = CreateTreeViewTemplate(),
  390. Items = items,
  391. };
  392. target.ApplyTemplate();
  393. target.Presenter.ApplyTemplate();
  394. var item = target.Presenter.Panel.LogicalChildren[0];
  395. Assert.Null(NameScope.GetNameScope((TreeViewItem)item));
  396. }
  397. [Fact]
  398. public void Should_React_To_Children_Changing()
  399. {
  400. var data = CreateTestTreeData();
  401. var target = new TreeView
  402. {
  403. Template = CreateTreeViewTemplate(),
  404. Items = data,
  405. };
  406. CreateNodeDataTemplate(target);
  407. ApplyTemplates(target);
  408. Assert.Equal(new[] { "Root" }, ExtractItemHeader(target, 0));
  409. Assert.Equal(new[] { "Child1", "Child2", "Child3" }, ExtractItemHeader(target, 1));
  410. Assert.Equal(new[] { "Grandchild2a" }, ExtractItemHeader(target, 2));
  411. // Make sure that the binding to Node.Children does not get collected.
  412. GC.Collect();
  413. data[0].Children = new AvaloniaList<Node>
  414. {
  415. new Node
  416. {
  417. Value = "NewChild1",
  418. }
  419. };
  420. Assert.Equal(new[] { "Root" }, ExtractItemHeader(target, 0));
  421. Assert.Equal(new[] { "NewChild1" }, ExtractItemHeader(target, 1));
  422. }
  423. [Fact]
  424. public void Keyboard_Navigation_Should_Move_To_Last_Selected_Node()
  425. {
  426. using (UnitTestApplication.Start(TestServices.RealFocus))
  427. {
  428. var focus = FocusManager.Instance;
  429. var navigation = AvaloniaLocator.Current.GetService<IKeyboardNavigationHandler>();
  430. var data = CreateTestTreeData();
  431. var target = new TreeView
  432. {
  433. Template = CreateTreeViewTemplate(),
  434. Items = data,
  435. };
  436. var button = new Button();
  437. var root = new TestRoot
  438. {
  439. Child = new StackPanel
  440. {
  441. Children = { target, button },
  442. }
  443. };
  444. CreateNodeDataTemplate(target);
  445. ApplyTemplates(target);
  446. var item = data[0].Children[0];
  447. var node = target.ItemContainerGenerator.Index.ContainerFromItem(item);
  448. Assert.NotNull(node);
  449. target.SelectedItem = item;
  450. node.Focus();
  451. Assert.Same(node, focus.Current);
  452. navigation.Move(focus.Current, NavigationDirection.Next);
  453. Assert.Same(button, focus.Current);
  454. navigation.Move(focus.Current, NavigationDirection.Next);
  455. Assert.Same(node, focus.Current);
  456. }
  457. }
  458. [Fact]
  459. public void Pressing_SelectAll_Gesture_Should_Select_All_Nodes()
  460. {
  461. using (UnitTestApplication.Start())
  462. {
  463. var tree = CreateTestTreeData();
  464. var target = new TreeView
  465. {
  466. Template = CreateTreeViewTemplate(),
  467. Items = tree,
  468. SelectionMode = SelectionMode.Multiple
  469. };
  470. var visualRoot = new TestRoot();
  471. visualRoot.Child = target;
  472. CreateNodeDataTemplate(target);
  473. ApplyTemplates(target);
  474. var rootNode = tree[0];
  475. var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
  476. var selectAllGesture = keymap.SelectAll.First();
  477. var keyEvent = new KeyEventArgs
  478. {
  479. RoutedEvent = InputElement.KeyDownEvent,
  480. Key = selectAllGesture.Key,
  481. KeyModifiers = selectAllGesture.KeyModifiers
  482. };
  483. target.RaiseEvent(keyEvent);
  484. AssertChildrenSelected(target, rootNode);
  485. }
  486. }
  487. [Fact]
  488. public void Pressing_SelectAll_Gesture_With_Downward_Range_Selected_Should_Select_All_Nodes()
  489. {
  490. using (UnitTestApplication.Start())
  491. {
  492. var tree = CreateTestTreeData();
  493. var target = new TreeView
  494. {
  495. Template = CreateTreeViewTemplate(),
  496. Items = tree,
  497. SelectionMode = SelectionMode.Multiple
  498. };
  499. var visualRoot = new TestRoot();
  500. visualRoot.Child = target;
  501. CreateNodeDataTemplate(target);
  502. ApplyTemplates(target);
  503. var rootNode = tree[0];
  504. var from = rootNode.Children[0];
  505. var to = rootNode.Children.Last();
  506. var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
  507. var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
  508. ClickContainer(fromContainer, InputModifiers.None);
  509. ClickContainer(toContainer, InputModifiers.Shift);
  510. var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
  511. var selectAllGesture = keymap.SelectAll.First();
  512. var keyEvent = new KeyEventArgs
  513. {
  514. RoutedEvent = InputElement.KeyDownEvent,
  515. Key = selectAllGesture.Key,
  516. KeyModifiers = selectAllGesture.KeyModifiers
  517. };
  518. target.RaiseEvent(keyEvent);
  519. AssertChildrenSelected(target, rootNode);
  520. }
  521. }
  522. [Fact]
  523. public void Pressing_SelectAll_Gesture_With_Upward_Range_Selected_Should_Select_All_Nodes()
  524. {
  525. using (UnitTestApplication.Start())
  526. {
  527. var tree = CreateTestTreeData();
  528. var target = new TreeView
  529. {
  530. Template = CreateTreeViewTemplate(),
  531. Items = tree,
  532. SelectionMode = SelectionMode.Multiple
  533. };
  534. var visualRoot = new TestRoot();
  535. visualRoot.Child = target;
  536. CreateNodeDataTemplate(target);
  537. ApplyTemplates(target);
  538. var rootNode = tree[0];
  539. var from = rootNode.Children.Last();
  540. var to = rootNode.Children[0];
  541. var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
  542. var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
  543. ClickContainer(fromContainer, InputModifiers.None);
  544. ClickContainer(toContainer, InputModifiers.Shift);
  545. var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
  546. var selectAllGesture = keymap.SelectAll.First();
  547. var keyEvent = new KeyEventArgs
  548. {
  549. RoutedEvent = InputElement.KeyDownEvent,
  550. Key = selectAllGesture.Key,
  551. KeyModifiers = selectAllGesture.KeyModifiers
  552. };
  553. target.RaiseEvent(keyEvent);
  554. AssertChildrenSelected(target, rootNode);
  555. }
  556. }
  557. [Fact]
  558. public void Right_Click_On_SelectedItem_Should_Not_Clear_Existing_Selection()
  559. {
  560. var tree = CreateTestTreeData();
  561. var target = new TreeView
  562. {
  563. Template = CreateTreeViewTemplate(),
  564. Items = tree,
  565. SelectionMode = SelectionMode.Multiple,
  566. };
  567. var visualRoot = new TestRoot();
  568. visualRoot.Child = target;
  569. CreateNodeDataTemplate(target);
  570. ApplyTemplates(target);
  571. target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]);
  572. target.SelectAll();
  573. AssertChildrenSelected(target, tree[0]);
  574. Assert.Equal(5, target.SelectedItems.Count);
  575. _mouse.Click((Interactive)target.Presenter.Panel.Children[0], MouseButton.Right);
  576. Assert.Equal(5, target.SelectedItems.Count);
  577. }
  578. [Fact]
  579. public void Right_Click_On_UnselectedItem_Should_Clear_Existing_Selection()
  580. {
  581. var tree = CreateTestTreeData();
  582. var target = new TreeView
  583. {
  584. Template = CreateTreeViewTemplate(),
  585. Items = tree,
  586. SelectionMode = SelectionMode.Multiple,
  587. };
  588. var visualRoot = new TestRoot();
  589. visualRoot.Child = target;
  590. CreateNodeDataTemplate(target);
  591. ApplyTemplates(target);
  592. target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]);
  593. var rootNode = tree[0];
  594. var to = rootNode.Children[0];
  595. var then = rootNode.Children[1];
  596. var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(rootNode);
  597. var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
  598. var thenContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(then);
  599. ClickContainer(fromContainer, InputModifiers.None);
  600. ClickContainer(toContainer, InputModifiers.Shift);
  601. Assert.Equal(2, target.SelectedItems.Count);
  602. _mouse.Click(thenContainer, MouseButton.Right);
  603. Assert.Equal(1, target.SelectedItems.Count);
  604. }
  605. [Fact]
  606. public void Shift_Right_Click_Should_Not_Select_Multiple()
  607. {
  608. var tree = CreateTestTreeData();
  609. var target = new TreeView
  610. {
  611. Template = CreateTreeViewTemplate(),
  612. Items = tree,
  613. SelectionMode = SelectionMode.Multiple,
  614. };
  615. var visualRoot = new TestRoot();
  616. visualRoot.Child = target;
  617. CreateNodeDataTemplate(target);
  618. ApplyTemplates(target);
  619. target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]);
  620. var rootNode = tree[0];
  621. var from = rootNode.Children[0];
  622. var to = rootNode.Children[1];
  623. var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
  624. var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
  625. _mouse.Click(fromContainer);
  626. _mouse.Click(toContainer, MouseButton.Right, modifiers: InputModifiers.Shift);
  627. Assert.Equal(1, target.SelectedItems.Count);
  628. }
  629. [Fact]
  630. public void Ctrl_Right_Click_Should_Not_Select_Multiple()
  631. {
  632. var tree = CreateTestTreeData();
  633. var target = new TreeView
  634. {
  635. Template = CreateTreeViewTemplate(),
  636. Items = tree,
  637. SelectionMode = SelectionMode.Multiple,
  638. };
  639. var visualRoot = new TestRoot();
  640. visualRoot.Child = target;
  641. CreateNodeDataTemplate(target);
  642. ApplyTemplates(target);
  643. target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]);
  644. var rootNode = tree[0];
  645. var from = rootNode.Children[0];
  646. var to = rootNode.Children[1];
  647. var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
  648. var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
  649. _mouse.Click(fromContainer);
  650. _mouse.Click(toContainer, MouseButton.Right, modifiers: InputModifiers.Control);
  651. Assert.Equal(1, target.SelectedItems.Count);
  652. }
  653. [Fact]
  654. public void TreeViewItems_Level_Should_Be_Set()
  655. {
  656. var tree = CreateTestTreeData();
  657. var target = new TreeView
  658. {
  659. Template = CreateTreeViewTemplate(),
  660. Items = tree,
  661. };
  662. var visualRoot = new TestRoot();
  663. visualRoot.Child = target;
  664. CreateNodeDataTemplate(target);
  665. ApplyTemplates(target);
  666. ExpandAll(target);
  667. Assert.Equal(0, GetItem(target, 0).Level);
  668. Assert.Equal(1, GetItem(target, 0, 0).Level);
  669. Assert.Equal(1, GetItem(target, 0, 1).Level);
  670. Assert.Equal(1, GetItem(target, 0, 2).Level);
  671. Assert.Equal(2, GetItem(target, 0, 1, 0).Level);
  672. }
  673. [Fact]
  674. public void TreeViewItems_Level_Should_Be_Set_For_Derived_TreeView()
  675. {
  676. var tree = CreateTestTreeData();
  677. var target = new DerivedTreeView
  678. {
  679. Template = CreateTreeViewTemplate(),
  680. Items = tree,
  681. };
  682. var visualRoot = new TestRoot();
  683. visualRoot.Child = target;
  684. CreateNodeDataTemplate(target);
  685. ApplyTemplates(target);
  686. ExpandAll(target);
  687. Assert.Equal(0, GetItem(target, 0).Level);
  688. Assert.Equal(1, GetItem(target, 0, 0).Level);
  689. Assert.Equal(1, GetItem(target, 0, 1).Level);
  690. Assert.Equal(1, GetItem(target, 0, 2).Level);
  691. Assert.Equal(2, GetItem(target, 0, 1, 0).Level);
  692. }
  693. private void ApplyTemplates(TreeView tree)
  694. {
  695. tree.ApplyTemplate();
  696. tree.Presenter.ApplyTemplate();
  697. ApplyTemplates(tree.Presenter.Panel.Children);
  698. }
  699. private void ApplyTemplates(IEnumerable<IControl> controls)
  700. {
  701. foreach (TreeViewItem control in controls)
  702. {
  703. control.Template = CreateTreeViewItemTemplate();
  704. control.ApplyTemplate();
  705. control.Presenter.ApplyTemplate();
  706. control.HeaderPresenter.ApplyTemplate();
  707. ApplyTemplates(control.Presenter.Panel.Children);
  708. }
  709. }
  710. private TreeViewItem GetItem(TreeView target, params int[] indexes)
  711. {
  712. var c = (ItemsControl)target;
  713. foreach (var index in indexes)
  714. {
  715. var item = ((IList)c.Items)[index];
  716. c = (ItemsControl)target.ItemContainerGenerator.Index.ContainerFromItem(item);
  717. }
  718. return (TreeViewItem)c;
  719. }
  720. private IList<Node> CreateTestTreeData()
  721. {
  722. return new AvaloniaList<Node>
  723. {
  724. new Node
  725. {
  726. Value = "Root",
  727. Children = new AvaloniaList<Node>
  728. {
  729. new Node
  730. {
  731. Value = "Child1",
  732. },
  733. new Node
  734. {
  735. Value = "Child2",
  736. Children = new AvaloniaList<Node>
  737. {
  738. new Node
  739. {
  740. Value = "Grandchild2a",
  741. },
  742. },
  743. },
  744. new Node
  745. {
  746. Value = "Child3",
  747. }
  748. }
  749. }
  750. };
  751. }
  752. private void CreateNodeDataTemplate(IControl control)
  753. {
  754. control.DataTemplates.Add(new TestTreeDataTemplate());
  755. }
  756. private IControlTemplate CreateTreeViewTemplate()
  757. {
  758. return new FuncControlTemplate<TreeView>((parent, scope) => new ItemsPresenter
  759. {
  760. Name = "PART_ItemsPresenter",
  761. [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
  762. }.RegisterInNameScope(scope));
  763. }
  764. private IControlTemplate CreateTreeViewItemTemplate()
  765. {
  766. return new FuncControlTemplate<TreeViewItem>((parent, scope) => new Panel
  767. {
  768. Children =
  769. {
  770. new ContentPresenter
  771. {
  772. Name = "PART_HeaderPresenter",
  773. [~ContentPresenter.ContentProperty] = parent[~TreeViewItem.HeaderProperty],
  774. }.RegisterInNameScope(scope),
  775. new ItemsPresenter
  776. {
  777. Name = "PART_ItemsPresenter",
  778. [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
  779. }.RegisterInNameScope(scope)
  780. }
  781. });
  782. }
  783. private void ExpandAll(TreeView tree)
  784. {
  785. foreach (var i in tree.ItemContainerGenerator.Containers)
  786. {
  787. tree.ExpandSubTree((TreeViewItem)i.ContainerControl);
  788. }
  789. }
  790. private List<string> ExtractItemHeader(TreeView tree, int level)
  791. {
  792. return ExtractItemContent(tree.Presenter.Panel, 0, level)
  793. .Select(x => x.Header)
  794. .OfType<TextBlock>()
  795. .Select(x => x.Text)
  796. .ToList();
  797. }
  798. private IEnumerable<TreeViewItem> ExtractItemContent(IPanel panel, int currentLevel, int level)
  799. {
  800. foreach (TreeViewItem container in panel.Children)
  801. {
  802. if (container.Template == null)
  803. {
  804. container.Template = CreateTreeViewItemTemplate();
  805. container.ApplyTemplate();
  806. }
  807. if (currentLevel == level)
  808. {
  809. yield return container;
  810. }
  811. else
  812. {
  813. foreach (var child in ExtractItemContent(container.Presenter.Panel, currentLevel + 1, level))
  814. {
  815. yield return child;
  816. }
  817. }
  818. }
  819. }
  820. void ClickContainer(IControl container, InputModifiers modifiers)
  821. {
  822. _mouse.Click(container, modifiers: modifiers);
  823. }
  824. void AssertChildrenSelected(TreeView treeView, Node rootNode)
  825. {
  826. foreach (var child in rootNode.Children)
  827. {
  828. var container = (TreeViewItem)treeView.ItemContainerGenerator.Index.ContainerFromItem(child);
  829. Assert.True(container.IsSelected);
  830. }
  831. }
  832. private class Node : NotifyingBase
  833. {
  834. private IAvaloniaList<Node> _children;
  835. public string Value { get; set; }
  836. public IAvaloniaList<Node> Children
  837. {
  838. get
  839. {
  840. return _children;
  841. }
  842. set
  843. {
  844. _children = value;
  845. RaisePropertyChanged(nameof(Children));
  846. }
  847. }
  848. }
  849. private class TestTreeDataTemplate : ITreeDataTemplate
  850. {
  851. public IControl Build(object param)
  852. {
  853. var node = (Node)param;
  854. return new TextBlock { Text = node.Value };
  855. }
  856. public bool SupportsRecycling => false;
  857. public InstancedBinding ItemsSelector(object item)
  858. {
  859. var obs = ExpressionObserver.Create(item, o => (o as Node).Children);
  860. return InstancedBinding.OneWay(obs);
  861. }
  862. public bool Match(object data)
  863. {
  864. return data is Node;
  865. }
  866. }
  867. private class DerivedTreeView : TreeView
  868. {
  869. }
  870. }
  871. }