| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using Avalonia.Collections;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Templates;
- using Avalonia.Data;
- using Avalonia.Data.Core;
- using Avalonia.Input;
- using Avalonia.Input.Platform;
- using Avalonia.Interactivity;
- using Avalonia.LogicalTree;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Controls.UnitTests
- {
- public class TreeViewTests
- {
- MouseTestHelper _mouse = new MouseTestHelper();
-
- [Fact]
- public void Items_Should_Be_Created()
- {
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = CreateTestTreeData(),
- };
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- Assert.Equal(new[] { "Root" }, ExtractItemHeader(target, 0));
- Assert.Equal(new[] { "Child1", "Child2", "Child3" }, ExtractItemHeader(target, 1));
- Assert.Equal(new[] { "Grandchild2a" }, ExtractItemHeader(target, 2));
- }
- [Fact]
- public void Items_Should_Be_Created_Using_ItemTemplate_If_Present()
- {
- TreeView target;
- var root = new TestRoot
- {
- Child = target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = CreateTestTreeData(),
- ItemTemplate = new FuncTreeDataTemplate<Node>(
- (_, __) => new Canvas(),
- x => x.Children),
- }
- };
- ApplyTemplates(target);
- var items = target.ItemContainerGenerator.Index.Containers
- .OfType<TreeViewItem>()
- .ToList();
- Assert.Equal(5, items.Count);
- Assert.All(items, x => Assert.IsType<Canvas>(x.HeaderPresenter.Child));
- }
- [Fact]
- public void Root_ItemContainerGenerator_Containers_Should_Be_Root_Containers()
- {
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = CreateTestTreeData(),
- };
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var container = (TreeViewItem)target.ItemContainerGenerator.Containers.Single().ContainerControl;
- var header = (TextBlock)container.Header;
- Assert.Equal("Root", header.Text);
- }
- [Fact]
- public void Root_TreeContainerFromItem_Should_Return_Descendant_Item()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- };
- // For TreeViewItem to find its parent TreeView, OnAttachedToLogicalTree needs
- // to be called, which requires an IStyleRoot.
- var root = new TestRoot();
- root.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var container = target.ItemContainerGenerator.Index.ContainerFromItem(
- tree[0].Children[1].Children[0]);
- Assert.NotNull(container);
- var header = ((TreeViewItem)container).Header;
- var headerContent = ((TextBlock)header).Text;
- Assert.Equal("Grandchild2a", headerContent);
- }
- [Fact]
- public void Clicking_Item_Should_Select_It()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var item = tree[0].Children[1].Children[0];
- var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item);
- Assert.NotNull(container);
- _mouse.Click(container);
- Assert.Equal(item, target.SelectedItem);
- Assert.True(container.IsSelected);
- }
- [Fact]
- public void Clicking_WithControlModifier_Selected_Item_Should_Deselect_It()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var item = tree[0].Children[1].Children[0];
- var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item);
- Assert.NotNull(container);
- target.SelectedItem = item;
- Assert.True(container.IsSelected);
- _mouse.Click(container, modifiers: InputModifiers.Control);
- Assert.Null(target.SelectedItem);
- Assert.False(container.IsSelected);
- }
- [Fact]
- public void Clicking_WithControlModifier_Not_Selected_Item_Should_Select_It()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var item1 = tree[0].Children[1].Children[0];
- var container1 = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item1);
- var item2 = tree[0].Children[1];
- var container2 = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item2);
- Assert.NotNull(container1);
- Assert.NotNull(container2);
- target.SelectedItem = item1;
- Assert.True(container1.IsSelected);
- _mouse.Click(container2, modifiers: InputModifiers.Control);
-
- Assert.Equal(item2, target.SelectedItem);
- Assert.False(container1.IsSelected);
- Assert.True(container2.IsSelected);
- }
- [Fact]
- public void Clicking_WithControlModifier_Selected_Item_Should_Deselect_And_Remove_From_SelectedItems()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var rootNode = tree[0];
- var item1 = rootNode.Children[0];
- var item2 = rootNode.Children.Last();
- var item1Container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item1);
- var item2Container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item2);
- ClickContainer(item1Container, InputModifiers.Control);
- Assert.True(item1Container.IsSelected);
- ClickContainer(item2Container, InputModifiers.Control);
- Assert.True(item2Container.IsSelected);
- Assert.Equal(new[] {item1, item2}, target.SelectedItems.OfType<Node>());
- ClickContainer(item1Container, InputModifiers.Control);
- Assert.False(item1Container.IsSelected);
- Assert.DoesNotContain(item1, target.SelectedItems.OfType<Node>());
- }
- [Fact]
- public void Clicking_WithShiftModifier_DownDirection_Should_Select_Range_Of_Items()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var rootNode = tree[0];
- var from = rootNode.Children[0];
- var to = rootNode.Children.Last();
- var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
- var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
- ClickContainer(fromContainer, InputModifiers.None);
- Assert.True(fromContainer.IsSelected);
- ClickContainer(toContainer, InputModifiers.Shift);
- AssertChildrenSelected(target, rootNode);
- }
- [Fact]
- public void Clicking_WithShiftModifier_UpDirection_Should_Select_Range_Of_Items()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var rootNode = tree[0];
- var from = rootNode.Children.Last();
- var to = rootNode.Children[0];
- var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
- var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
- ClickContainer(fromContainer, InputModifiers.None);
- Assert.True(fromContainer.IsSelected);
- ClickContainer(toContainer, InputModifiers.Shift);
- AssertChildrenSelected(target, rootNode);
- }
- [Fact]
- public void Clicking_First_Item_Of_SelectedItems_Should_Select_Only_It()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var rootNode = tree[0];
- var from = rootNode.Children.Last();
- var to = rootNode.Children[0];
- var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
- var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
- ClickContainer(fromContainer, InputModifiers.None);
- ClickContainer(toContainer, InputModifiers.Shift);
- AssertChildrenSelected(target, rootNode);
- ClickContainer(fromContainer, InputModifiers.None);
- Assert.True(fromContainer.IsSelected);
- foreach (var child in rootNode.Children)
- {
- if (child == from)
- {
- continue;
- }
- var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(child);
- Assert.False(container.IsSelected);
- }
- }
- [Fact]
- public void Setting_SelectedItem_Should_Set_Container_Selected()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var item = tree[0].Children[1].Children[0];
- var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item);
- Assert.NotNull(container);
- target.SelectedItem = item;
- Assert.True(container.IsSelected);
- }
- [Fact]
- public void Setting_SelectedItem_Should_Raise_SelectedItemChanged_Event()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var item = tree[0].Children[1].Children[0];
- var called = false;
- target.SelectionChanged += (s, e) =>
- {
- Assert.Empty(e.RemovedItems);
- Assert.Equal(1, e.AddedItems.Count);
- Assert.Same(item, e.AddedItems[0]);
- called = true;
- };
- target.SelectedItem = item;
- Assert.True(called);
- }
- [Fact]
- public void LogicalChildren_Should_Be_Set()
- {
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = new[] { "Foo", "Bar", "Baz " },
- };
- ApplyTemplates(target);
- var result = target.GetLogicalChildren()
- .OfType<TreeViewItem>()
- .Select(x => x.Header)
- .OfType<TextBlock>()
- .Select(x => x.Text)
- .ToList();
- Assert.Equal(new[] { "Foo", "Bar", "Baz " }, result);
- }
- [Fact]
- public void Removing_Item_Should_Remove_Itself_And_Children_From_Index()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- };
- var root = new TestRoot();
- root.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- Assert.Equal(5, target.ItemContainerGenerator.Index.Containers.Count());
- tree[0].Children.RemoveAt(1);
- Assert.Equal(3, target.ItemContainerGenerator.Index.Containers.Count());
- }
- [Fact]
- public void DataContexts_Should_Be_Correctly_Set()
- {
- var items = new object[]
- {
- "Foo",
- new Node { Value = "Bar" },
- new TextBlock { Text = "Baz" },
- new TreeViewItem { Header = "Qux" },
- };
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- DataContext = "Base",
- DataTemplates =
- {
- new FuncDataTemplate<Node>((x, _) => new Button { Content = x })
- },
- Items = items,
- };
- ApplyTemplates(target);
- var dataContexts = target.Presenter.Panel.Children
- .Cast<Control>()
- .Select(x => x.DataContext)
- .ToList();
- Assert.Equal(
- new object[] { items[0], items[1], "Base", "Base" },
- dataContexts);
- }
- [Fact]
- public void Control_Item_Should_Not_Be_NameScope()
- {
- var items = new object[]
- {
- new TreeViewItem(),
- };
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = items,
- };
- target.ApplyTemplate();
- target.Presenter.ApplyTemplate();
- var item = target.Presenter.Panel.LogicalChildren[0];
- Assert.Null(NameScope.GetNameScope((TreeViewItem)item));
- }
- [Fact]
- public void Should_React_To_Children_Changing()
- {
- var data = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = data,
- };
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- Assert.Equal(new[] { "Root" }, ExtractItemHeader(target, 0));
- Assert.Equal(new[] { "Child1", "Child2", "Child3" }, ExtractItemHeader(target, 1));
- Assert.Equal(new[] { "Grandchild2a" }, ExtractItemHeader(target, 2));
- // Make sure that the binding to Node.Children does not get collected.
- GC.Collect();
- data[0].Children = new AvaloniaList<Node>
- {
- new Node
- {
- Value = "NewChild1",
- }
- };
- Assert.Equal(new[] { "Root" }, ExtractItemHeader(target, 0));
- Assert.Equal(new[] { "NewChild1" }, ExtractItemHeader(target, 1));
- }
- [Fact]
- public void Keyboard_Navigation_Should_Move_To_Last_Selected_Node()
- {
- using (UnitTestApplication.Start(TestServices.RealFocus))
- {
- var focus = FocusManager.Instance;
- var navigation = AvaloniaLocator.Current.GetService<IKeyboardNavigationHandler>();
- var data = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = data,
- };
- var button = new Button();
- var root = new TestRoot
- {
- Child = new StackPanel
- {
- Children = { target, button },
- }
- };
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var item = data[0].Children[0];
- var node = target.ItemContainerGenerator.Index.ContainerFromItem(item);
- Assert.NotNull(node);
- target.SelectedItem = item;
- node.Focus();
- Assert.Same(node, focus.Current);
- navigation.Move(focus.Current, NavigationDirection.Next);
- Assert.Same(button, focus.Current);
- navigation.Move(focus.Current, NavigationDirection.Next);
- Assert.Same(node, focus.Current);
- }
- }
- [Fact]
- public void Pressing_SelectAll_Gesture_Should_Select_All_Nodes()
- {
- using (UnitTestApplication.Start())
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var rootNode = tree[0];
- var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
- var selectAllGesture = keymap.SelectAll.First();
- var keyEvent = new KeyEventArgs
- {
- RoutedEvent = InputElement.KeyDownEvent,
- Key = selectAllGesture.Key,
- KeyModifiers = selectAllGesture.KeyModifiers
- };
- target.RaiseEvent(keyEvent);
- AssertChildrenSelected(target, rootNode);
- }
- }
- [Fact]
- public void Pressing_SelectAll_Gesture_With_Downward_Range_Selected_Should_Select_All_Nodes()
- {
- using (UnitTestApplication.Start())
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var rootNode = tree[0];
- var from = rootNode.Children[0];
- var to = rootNode.Children.Last();
- var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
- var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
- ClickContainer(fromContainer, InputModifiers.None);
- ClickContainer(toContainer, InputModifiers.Shift);
- var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
- var selectAllGesture = keymap.SelectAll.First();
- var keyEvent = new KeyEventArgs
- {
- RoutedEvent = InputElement.KeyDownEvent,
- Key = selectAllGesture.Key,
- KeyModifiers = selectAllGesture.KeyModifiers
- };
- target.RaiseEvent(keyEvent);
- AssertChildrenSelected(target, rootNode);
- }
- }
- [Fact]
- public void Pressing_SelectAll_Gesture_With_Upward_Range_Selected_Should_Select_All_Nodes()
- {
- using (UnitTestApplication.Start())
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- var rootNode = tree[0];
- var from = rootNode.Children.Last();
- var to = rootNode.Children[0];
- var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
- var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
- ClickContainer(fromContainer, InputModifiers.None);
- ClickContainer(toContainer, InputModifiers.Shift);
- var keymap = AvaloniaLocator.Current.GetService<PlatformHotkeyConfiguration>();
- var selectAllGesture = keymap.SelectAll.First();
- var keyEvent = new KeyEventArgs
- {
- RoutedEvent = InputElement.KeyDownEvent,
- Key = selectAllGesture.Key,
- KeyModifiers = selectAllGesture.KeyModifiers
- };
- target.RaiseEvent(keyEvent);
- AssertChildrenSelected(target, rootNode);
- }
- }
- [Fact]
- public void Right_Click_On_SelectedItem_Should_Not_Clear_Existing_Selection()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple,
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]);
- target.SelectAll();
- AssertChildrenSelected(target, tree[0]);
- Assert.Equal(5, target.SelectedItems.Count);
- _mouse.Click((Interactive)target.Presenter.Panel.Children[0], MouseButton.Right);
- Assert.Equal(5, target.SelectedItems.Count);
- }
- [Fact]
- public void Right_Click_On_UnselectedItem_Should_Clear_Existing_Selection()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple,
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]);
- var rootNode = tree[0];
- var to = rootNode.Children[0];
- var then = rootNode.Children[1];
- var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(rootNode);
- var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
- var thenContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(then);
- ClickContainer(fromContainer, InputModifiers.None);
- ClickContainer(toContainer, InputModifiers.Shift);
- Assert.Equal(2, target.SelectedItems.Count);
- _mouse.Click(thenContainer, MouseButton.Right);
- Assert.Equal(1, target.SelectedItems.Count);
- }
- [Fact]
- public void Shift_Right_Click_Should_Not_Select_Multiple()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple,
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]);
- var rootNode = tree[0];
- var from = rootNode.Children[0];
- var to = rootNode.Children[1];
- var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
- var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
- _mouse.Click(fromContainer);
- _mouse.Click(toContainer, MouseButton.Right, modifiers: InputModifiers.Shift);
- Assert.Equal(1, target.SelectedItems.Count);
- }
- [Fact]
- public void Ctrl_Right_Click_Should_Not_Select_Multiple()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- SelectionMode = SelectionMode.Multiple,
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- target.ExpandSubTree((TreeViewItem)target.Presenter.Panel.Children[0]);
- var rootNode = tree[0];
- var from = rootNode.Children[0];
- var to = rootNode.Children[1];
- var fromContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(from);
- var toContainer = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(to);
- _mouse.Click(fromContainer);
- _mouse.Click(toContainer, MouseButton.Right, modifiers: InputModifiers.Control);
- Assert.Equal(1, target.SelectedItems.Count);
- }
- [Fact]
- public void TreeViewItems_Level_Should_Be_Set()
- {
- var tree = CreateTestTreeData();
- var target = new TreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- ExpandAll(target);
- Assert.Equal(0, GetItem(target, 0).Level);
- Assert.Equal(1, GetItem(target, 0, 0).Level);
- Assert.Equal(1, GetItem(target, 0, 1).Level);
- Assert.Equal(1, GetItem(target, 0, 2).Level);
- Assert.Equal(2, GetItem(target, 0, 1, 0).Level);
- }
- [Fact]
- public void TreeViewItems_Level_Should_Be_Set_For_Derived_TreeView()
- {
- var tree = CreateTestTreeData();
- var target = new DerivedTreeView
- {
- Template = CreateTreeViewTemplate(),
- Items = tree,
- };
- var visualRoot = new TestRoot();
- visualRoot.Child = target;
- CreateNodeDataTemplate(target);
- ApplyTemplates(target);
- ExpandAll(target);
- Assert.Equal(0, GetItem(target, 0).Level);
- Assert.Equal(1, GetItem(target, 0, 0).Level);
- Assert.Equal(1, GetItem(target, 0, 1).Level);
- Assert.Equal(1, GetItem(target, 0, 2).Level);
- Assert.Equal(2, GetItem(target, 0, 1, 0).Level);
- }
- private void ApplyTemplates(TreeView tree)
- {
- tree.ApplyTemplate();
- tree.Presenter.ApplyTemplate();
- ApplyTemplates(tree.Presenter.Panel.Children);
- }
- private void ApplyTemplates(IEnumerable<IControl> controls)
- {
- foreach (TreeViewItem control in controls)
- {
- control.Template = CreateTreeViewItemTemplate();
- control.ApplyTemplate();
- control.Presenter.ApplyTemplate();
- control.HeaderPresenter.ApplyTemplate();
- ApplyTemplates(control.Presenter.Panel.Children);
- }
- }
- private TreeViewItem GetItem(TreeView target, params int[] indexes)
- {
- var c = (ItemsControl)target;
- foreach (var index in indexes)
- {
- var item = ((IList)c.Items)[index];
- c = (ItemsControl)target.ItemContainerGenerator.Index.ContainerFromItem(item);
- }
- return (TreeViewItem)c;
- }
- private IList<Node> CreateTestTreeData()
- {
- return new AvaloniaList<Node>
- {
- new Node
- {
- Value = "Root",
- Children = new AvaloniaList<Node>
- {
- new Node
- {
- Value = "Child1",
- },
- new Node
- {
- Value = "Child2",
- Children = new AvaloniaList<Node>
- {
- new Node
- {
- Value = "Grandchild2a",
- },
- },
- },
- new Node
- {
- Value = "Child3",
- }
- }
- }
- };
- }
- private void CreateNodeDataTemplate(IControl control)
- {
- control.DataTemplates.Add(new TestTreeDataTemplate());
- }
- private IControlTemplate CreateTreeViewTemplate()
- {
- return new FuncControlTemplate<TreeView>((parent, scope) => new ItemsPresenter
- {
- Name = "PART_ItemsPresenter",
- [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
- }.RegisterInNameScope(scope));
- }
- private IControlTemplate CreateTreeViewItemTemplate()
- {
- return new FuncControlTemplate<TreeViewItem>((parent, scope) => new Panel
- {
- Children =
- {
- new ContentPresenter
- {
- Name = "PART_HeaderPresenter",
- [~ContentPresenter.ContentProperty] = parent[~TreeViewItem.HeaderProperty],
- }.RegisterInNameScope(scope),
- new ItemsPresenter
- {
- Name = "PART_ItemsPresenter",
- [~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
- }.RegisterInNameScope(scope)
- }
- });
- }
- private void ExpandAll(TreeView tree)
- {
- foreach (var i in tree.ItemContainerGenerator.Containers)
- {
- tree.ExpandSubTree((TreeViewItem)i.ContainerControl);
- }
- }
- private List<string> ExtractItemHeader(TreeView tree, int level)
- {
- return ExtractItemContent(tree.Presenter.Panel, 0, level)
- .Select(x => x.Header)
- .OfType<TextBlock>()
- .Select(x => x.Text)
- .ToList();
- }
- private IEnumerable<TreeViewItem> ExtractItemContent(IPanel panel, int currentLevel, int level)
- {
- foreach (TreeViewItem container in panel.Children)
- {
- if (container.Template == null)
- {
- container.Template = CreateTreeViewItemTemplate();
- container.ApplyTemplate();
- }
- if (currentLevel == level)
- {
- yield return container;
- }
- else
- {
- foreach (var child in ExtractItemContent(container.Presenter.Panel, currentLevel + 1, level))
- {
- yield return child;
- }
- }
- }
- }
- void ClickContainer(IControl container, InputModifiers modifiers)
- {
- _mouse.Click(container, modifiers: modifiers);
- }
- void AssertChildrenSelected(TreeView treeView, Node rootNode)
- {
- foreach (var child in rootNode.Children)
- {
- var container = (TreeViewItem)treeView.ItemContainerGenerator.Index.ContainerFromItem(child);
- Assert.True(container.IsSelected);
- }
- }
- private class Node : NotifyingBase
- {
- private IAvaloniaList<Node> _children;
- public string Value { get; set; }
- public IAvaloniaList<Node> Children
- {
- get
- {
- return _children;
- }
- set
- {
- _children = value;
- RaisePropertyChanged(nameof(Children));
- }
- }
- }
- private class TestTreeDataTemplate : ITreeDataTemplate
- {
- public IControl Build(object param)
- {
- var node = (Node)param;
- return new TextBlock { Text = node.Value };
- }
- public bool SupportsRecycling => false;
- public InstancedBinding ItemsSelector(object item)
- {
- var obs = ExpressionObserver.Create(item, o => (o as Node).Children);
- return InstancedBinding.OneWay(obs);
- }
- public bool Match(object data)
- {
- return data is Node;
- }
- }
- private class DerivedTreeView : TreeView
- {
- }
- }
- }
|