public void Track_By_Name_Should_Find_Control_Added_Later() { StackPanel panel; TextBlock relativeTo; var root = new TestRoot { Child = (panel = new StackPanel { Children = new Controls.Controls { (relativeTo = new TextBlock { Name = "start" }), } }) }; var locator = ControlLocator.Track(relativeTo, "target"); var target = new TextBlock { Name = "target" }; var result = new List<IControl>(); using (locator.Subscribe(x => result.Add(x))) { panel.Children.Add(target); } Assert.Equal(new[] { null, target }, result); Assert.Equal(0, root.NameScopeRegisteredSubscribers); Assert.Equal(0, root.NameScopeUnregisteredSubscribers); }
public void Clicking_Item_Should_Select_It() { var tree = CreateTestTreeData(); var target = new TreeView { Template = CreateTreeViewTemplate(), Items = tree, DataTemplates = CreateNodeDataTemplate(), }; var visualRoot = new TestRoot(); visualRoot.Child = target; ApplyTemplates(target); var item = tree[0].Children[1].Children[0]; var container = (TreeViewItem)target.ItemContainerGenerator.Index.ContainerFromItem(item); Assert.NotNull(container); container.RaiseEvent(new PointerPressedEventArgs { RoutedEvent = InputElement.PointerPressedEvent, MouseButton = MouseButton.Left, }); Assert.Equal(item, target.SelectedItem); Assert.True(container.IsSelected); }
public void AttachedToLogicalParent_Should_Be_Called_When_Added_To_Tree() { var root = new TestRoot(); var parent = new Border(); var child = new Border(); var grandchild = new Border(); var parentRaised = false; var childRaised = false; var grandchildRaised = false; parent.AttachedToLogicalTree += (s, e) => parentRaised = true; child.AttachedToLogicalTree += (s, e) => childRaised = true; grandchild.AttachedToLogicalTree += (s, e) => grandchildRaised = true; parent.Child = child; child.Child = grandchild; Assert.False(parentRaised); Assert.False(childRaised); Assert.False(grandchildRaised); root.Child = parent; Assert.True(parentRaised); Assert.True(childRaised); Assert.True(grandchildRaised); }
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.Items .OfType<TreeViewItem>() .ToList(); Assert.Equal(4, items.Count); Assert.All(items, x => Assert.IsType<Canvas>(x.HeaderPresenter.Child)); }
public void Should_Not_Create_Items_Before_Added_To_Visual_Tree() { var items = Enumerable.Range(0, 10).Select(x => $"Item {x}").ToList(); var target = new TestItemsPresenter(true) { Items = items, ItemsPanel = VirtualizingPanelTemplate(Orientation.Vertical), ItemTemplate = ItemTemplate(), VirtualizationMode = ItemVirtualizationMode.Simple, }; var scroller = new ScrollContentPresenter { Content = target, }; scroller.UpdateChild(); target.ApplyTemplate(); target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); Assert.Empty(target.Panel.Children); var root = new TestRoot { Child = scroller, }; target.InvalidateMeasure(); target.Panel.InvalidateMeasure(); target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); Assert.Equal(10, target.Panel.Children.Count); }
public void Should_Bind_To_Element() { TextBlock source; ContentControl target; var root = new TestRoot { Child = new StackPanel { Children = new Controls.Controls { (source = new TextBlock { Name = "source", Text = "foo", }), (target = new ContentControl { Name = "target", }) } } }; var binding = new Binding { ElementName = "source", }; target.Bind(ContentControl.ContentProperty, binding); Assert.Same(source, target.Content); }
public void Should_Bind_To_Element_Path() { TextBlock target; var root = new TestRoot { Child = new StackPanel { Children = new Controls.Controls { new TextBlock { Name = "source", Text = "foo", }, (target = new TextBlock { Name = "target", }) } } }; var binding = new Binding { ElementName = "source", Path = "Text", }; target.Bind(TextBox.TextProperty, binding); Assert.Equal("foo", target.Text); }
public void Vertical_Stays_Within_Constraints() { var cursorFactoryImpl = new Mock<IStandardCursorFactory>(); AvaloniaLocator.CurrentMutable.Bind<IStandardCursorFactory>().ToConstant(cursorFactoryImpl.Object); var control1 = new Border { [Grid.ColumnProperty] = 0 }; var splitter = new GridSplitter { Orientation = Orientation.Vertical, [Grid.ColumnProperty] = 1, }; var control2 = new Border { [Grid.ColumnProperty] = 2 }; var columnDefinitions = new ColumnDefinitions() { new ColumnDefinition(1, GridUnitType.Star) {MinWidth = 10, MaxWidth = 190}, new ColumnDefinition(GridLength.Auto), new ColumnDefinition(1, GridUnitType.Star) {MinWidth = 80, MaxWidth = 120}, }; var grid = new Grid() { ColumnDefinitions = columnDefinitions, Children = new Controls() { control1, splitter, control2 } }; var root = new TestRoot { Child = grid }; Assert.Equal(splitter.Orientation, Orientation.Vertical); root.Measure(new Size(200, 100)); root.Arrange(new Rect(0, 0, 200, 100)); splitter.RaiseEvent(new VectorEventArgs { RoutedEvent = Thumb.DragDeltaEvent, Vector = new Vector(-100,0) }); Assert.Equal(columnDefinitions[0].Width, new GridLength(80,GridUnitType.Star)); Assert.Equal(columnDefinitions[2].Width, new GridLength(120,GridUnitType.Star)); splitter.RaiseEvent(new VectorEventArgs { RoutedEvent = Thumb.DragDeltaEvent, Vector = new Vector(100, 0) }); Assert.Equal(columnDefinitions[0].Width, new GridLength(120, GridUnitType.Star)); Assert.Equal(columnDefinitions[2].Width, new GridLength(80, GridUnitType.Star)); }
public void Focus_Should_Set_FocusManager_Current() { Button target; using (UnitTestApplication.Start(TestServices.RealFocus)) { var root = new TestRoot { Child = target = new Button() }; target.Focus(); Assert.Same(target, FocusManager.Instance.Current); } }
public void Focus_Should_Be_Cleared_When_Control_Is_Removed_From_VisualTree() { Button target; using (UnitTestApplication.Start(TestServices.RealFocus)) { var root = new TestRoot { Child = target = new Button() }; target.Focus(); root.Child = null; Assert.Null(FocusManager.Instance.Current); } }
public void Templated_Children_Should_Be_Styled() { var root = new TestRoot(); var target = new ContentControl(); var styler = new Mock<IStyler>(); AvaloniaLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object); target.Content = "Foo"; target.Template = GetTemplate(); root.Child = target; target.ApplyTemplate(); styler.Verify(x => x.ApplyStyles(It.IsAny<ContentControl>()), Times.Once()); styler.Verify(x => x.ApplyStyles(It.IsAny<Border>()), Times.Once()); styler.Verify(x => x.ApplyStyles(It.IsAny<ContentPresenter>()), Times.Once()); styler.Verify(x => x.ApplyStyles(It.IsAny<TextBlock>()), Times.Once()); }
public void Detects_Horizontal_Orientation() { var grid = new Grid() { RowDefinitions = new RowDefinitions("*,Auto,*"), ColumnDefinitions = new ColumnDefinitions("*,*"), Children = new Controls() { new Border { [Grid.RowProperty] = 0 }, new GridSplitter { [Grid.RowProperty] = 1, Name = "splitter" }, new Border { [Grid.RowProperty] = 2 } } }; var root = new TestRoot { Child = grid }; root.Measure(new Size(100, 300)); root.Arrange(new Rect(0, 0, 100, 300)); Assert.Contains(grid.FindControl<GridSplitter>("splitter").Classes, ":horizontal".Equals); }
public void Detects_With_Both_Auto() { var grid = new Grid() { ColumnDefinitions = new ColumnDefinitions("Auto,Auto,Auto"), RowDefinitions = new RowDefinitions("Auto,Auto"), Children = new Controls() { new Border { [Grid.ColumnProperty] = 0 }, new GridSplitter { [Grid.ColumnProperty] = 1, Name = "splitter" }, new Border { [Grid.ColumnProperty] = 2 }, } }; var root = new TestRoot { Child = grid }; root.Measure(new Size(100, 300)); root.Arrange(new Rect(0, 0, 100, 300)); Assert.Contains(grid.FindControl<GridSplitter>("splitter").Classes, ":vertical".Equals); }
public void Control_Should_Unregister_With_NameScope() { var root = new TestRoot { Child = new Border { Name = "foo", Child = new Border { Name = "bar", } } }; root.Child = null; Assert.Null(root.FindControl<Border>("foo")); Assert.Null(root.FindControl<Border>("bar")); }
public void Controls_Should_Register_With_NameScope() { var root = new TestRoot { Child = new Border { Name = "foo", Child = new Border { Name = "bar", } } }; root.ApplyTemplate(); Assert.Same(root.FindControl<Border>("foo"), root.Child); Assert.Same(root.FindControl<Border>("bar"), ((Border)root.Child).Child); }
public void Should_Be_Styled_As_UserControl() { using (UnitTestApplication.Start(TestServices.RealStyler)) { var target = new UserControl(); var root = new TestRoot { Styles = new Styles { new Style(x => x.OfType<ContentControl>()) { Setters = new[] { new Setter(TemplatedControl.TemplateProperty, GetTemplate()) } } }, Child = target, }; Assert.NotNull(target.Template); } }
public async void Track_By_Name_Should_Find_Control_Added_Earlier() { TextBlock target; TextBlock relativeTo; var root = new TestRoot { Child = new StackPanel { Children = new Controls.Controls { (target = new TextBlock { Name = "target" }), (relativeTo = new TextBlock { Name = "start" }), } } }; var locator = ControlLocator.Track(relativeTo, "target"); var result = await locator.Take(1); Assert.Same(target, result); Assert.Equal(0, root.NameScopeRegisteredSubscribers); Assert.Equal(0, root.NameScopeUnregisteredSubscribers); }
public void SelectionBoxItem_Rectangle_Is_Removed_From_Logical_Tree() { var target = new DropDown { Items = new[] { new Canvas() }, SelectedIndex = 0, Template = GetTemplate(), }; var root = new TestRoot { Child = target }; target.ApplyTemplate(); target.Presenter.ApplyTemplate(); var rectangle = target.GetValue(DropDown.SelectionBoxItemProperty) as Rectangle; Assert.True(((ILogical)target).IsAttachedToLogicalTree); Assert.True(((ILogical)rectangle).IsAttachedToLogicalTree); rectangle.DetachedFromLogicalTree += (s, e) => { }; root.Child = null; Assert.False(((ILogical)target).IsAttachedToLogicalTree); Assert.False(((ILogical)rectangle).IsAttachedToLogicalTree); }
public void Adding_To_Logical_Tree_Should_Reevaluate_DataTemplates() { var target = new ContentPresenter { Content = "Foo", }; target.UpdateChild(); Assert.IsType<TextBlock>(target.Child); var root = new TestRoot { DataTemplates = new DataTemplates { new FuncDataTemplate<string>(x => new Decorator()), }, }; root.Child = target; target.ApplyTemplate(); Assert.IsType<Decorator>(target.Child); }
public void Re_adding_To_Different_LogicalTree_Should_Recreate_Template() { using (UnitTestApplication.Start(TestServices.RealStyler)) { TestTemplatedControl target; var root = new TestRoot { Styles = new Styles { new Style(x => x.OfType<TestTemplatedControl>()) { Setters = new[] { new Setter( TemplatedControl.TemplateProperty, new FuncControlTemplate(_ => new Decorator { Child = new Border(), })) } } }, Child = target = new TestTemplatedControl() }; var root2 = new TestRoot { Styles = new Styles { new Style(x => x.OfType<TestTemplatedControl>()) { Setters = new[] { new Setter( TemplatedControl.TemplateProperty, new FuncControlTemplate(_ => new Decorator { Child = new Border(), })) } } }, }; Assert.NotNull(target.Template); target.ApplyTemplate(); var expected = (Decorator)target.GetVisualChildren().Single(); root.Child = null; root2.Child = target; target.ApplyTemplate(); var child = target.GetVisualChildren().Single(); Assert.NotNull(target.Template); Assert.NotNull(child); Assert.NotSame(expected, child); } }
public void Removing_From_LogicalTree_Should_Not_Remove_Child() { using (UnitTestApplication.Start(TestServices.RealStyler)) { Border templateChild = new Border(); TestTemplatedControl target; var root = new TestRoot { Styles = new Styles { new Style(x => x.OfType<TestTemplatedControl>()) { Setters = new[] { new Setter( TemplatedControl.TemplateProperty, new FuncControlTemplate(_ => new Decorator { Child = new Border(), })) } } }, Child = target = new TestTemplatedControl() }; Assert.NotNull(target.Template); target.ApplyTemplate(); root.Child = null; Assert.Null(target.Template); Assert.IsType<Decorator>(target.GetVisualChildren().Single()); } }
public void TemplateChild_DetachedFromLogicalTree_Should_Be_Raised() { Border templateChild = new Border(); var root = new TestRoot { Child = new TestTemplatedControl { Template = new FuncControlTemplate(_ => new Decorator { Child = templateChild, }) } }; root.Child.ApplyTemplate(); var raised = false; templateChild.DetachedFromLogicalTree += (s, e) => raised = true; root.Child = null; Assert.True(raised); }
public void Nested_TemplatedControls_Should_Register_With_Correct_NameScope() { var target = new ContentControl { Template = new FuncControlTemplate<ContentControl>(ScrollingContentControlTemplate), Content = "foo" }; var root = new TestRoot { Child = target }; target.ApplyTemplate(); var border = target.GetVisualChildren().FirstOrDefault(); Assert.IsType<Border>(border); var scrollViewer = border.GetVisualChildren().FirstOrDefault(); Assert.IsType<ScrollViewer>(scrollViewer); ((ScrollViewer)scrollViewer).ApplyTemplate(); var scrollContentPresenter = scrollViewer.GetVisualChildren().FirstOrDefault(); Assert.IsType<ScrollContentPresenter>(scrollContentPresenter); ((ContentPresenter)scrollContentPresenter).UpdateChild(); var contentPresenter = scrollContentPresenter.GetVisualChildren().FirstOrDefault(); Assert.IsType<ContentPresenter>(contentPresenter); var borderNs = NameScope.GetNameScope((Control)border); var scrollContentPresenterNs = NameScope.GetNameScope((Control)scrollContentPresenter); Assert.NotNull(borderNs); Assert.Same(scrollViewer, borderNs.Find("ScrollViewer")); Assert.Same(contentPresenter, borderNs.Find("PART_ContentPresenter")); Assert.Same(scrollContentPresenter, scrollContentPresenterNs.Find("PART_ContentPresenter")); }
public void Templated_Children_Should_Be_Styled() { using (UnitTestApplication.Start(TestServices.MockStyler)) { TestTemplatedControl target; var root = new TestRoot { Child = target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => { return new StackPanel { Children = new Controls { new TextBlock { } } }; }), } }; target.ApplyTemplate(); var styler = Mock.Get(UnitTestApplication.Current.Services.Styler); styler.Verify(x => x.ApplyStyles(It.IsAny<TestTemplatedControl>()), Times.Once()); styler.Verify(x => x.ApplyStyles(It.IsAny<StackPanel>()), Times.Once()); styler.Verify(x => x.ApplyStyles(It.IsAny<TextBlock>()), Times.Once()); } }
public void Templated_Control_With_Popup_In_Template_Should_Set_TemplatedParent() { using (CreateServices()) { PopupContentControl target; var root = new TestRoot { Child = target = new PopupContentControl { Content = new Border(), Template = new FuncControlTemplate<PopupContentControl>(PopupContentControlTemplate), } }; target.ApplyTemplate(); var popup = (Popup)target.GetTemplateChildren().First(x => x.Name == "popup"); popup.Open(); var popupRoot = popup.PopupRoot; var children = popupRoot.GetVisualDescendents().ToList(); var types = children.Select(x => x.GetType().Name).ToList(); Assert.Equal( new[] { "ContentPresenter", "ContentPresenter", "Border", }, types); var templatedParents = children .OfType<IControl>() .Select(x => x.TemplatedParent).ToList(); Assert.Equal( new object[] { popupRoot, target, null, }, templatedParents); } }
public void PopupRoot_Should_Be_Detached_From_Logical_Tree_When_Popup_Is_Detached() { using (CreateServices()) { var target = new Popup(); var root = new TestRoot { Child = target }; target.Open(); var popupRoot = (ILogical)target.PopupRoot; Assert.True(popupRoot.IsAttachedToLogicalTree); root.Child = null; Assert.False(((ILogical)target).IsAttachedToLogicalTree); } }
private void Prepare(ListBox target) { // The ListBox needs to be part of a rooted visual tree. var root = new TestRoot(); root.Child = target; // Apply the template to the ListBox itself. target.ApplyTemplate(); // Then to its inner ScrollViewer. var scrollViewer = (ScrollViewer)target.GetVisualChildren().Single(); scrollViewer.ApplyTemplate(); // Then make the ScrollViewer create its child. ((ContentPresenter)scrollViewer.Presenter).UpdateChild(); // Now the ItemsPresenter should be reigstered, so apply its template. target.Presenter.ApplyTemplate(); // Because ListBox items are virtualized we need to do a layout to make them appear. target.Measure(new Size(100, 100)); target.Arrange(new Rect(0, 0, 100, 100)); // Now set and apply the item templates. foreach (ListBoxItem item in target.Presenter.Panel.Children) { item.Template = ListBoxItemTemplate(); item.ApplyTemplate(); item.Presenter.ApplyTemplate(); ((ContentPresenter)item.Presenter).UpdateChild(); } // The items were created before the template was applied, so now we need to go back // and re-arrange everything. foreach (IControl i in target.GetSelfAndVisualDescendents()) { i.InvalidateMeasure(); } target.Arrange(new Rect(0, 0, 100, 100)); }
public void In_First_Position_Doesnt_Throw_Exception() { var grid = new Grid() { ColumnDefinitions = new ColumnDefinitions("Auto,*,*"), RowDefinitions = new RowDefinitions("*,*"), Children = new Controls() { new GridSplitter { [Grid.ColumnProperty] = 0, Name = "splitter" }, new Border { [Grid.ColumnProperty] = 1 }, new Border { [Grid.ColumnProperty] = 2 }, } }; var root = new TestRoot { Child = grid }; root.Measure(new Size(100, 300)); root.Arrange(new Rect(0, 0, 100, 300)); var splitter = grid.FindControl<GridSplitter>("splitter"); splitter.RaiseEvent(new VectorEventArgs { RoutedEvent = Thumb.DragDeltaEvent, Vector = new Vector(100, 1000) }); }
public void Horizontal_Stays_Within_Constraints() { var control1 = new Border { [Grid.RowProperty] = 0 }; var splitter = new GridSplitter { [Grid.RowProperty] = 1, }; var control2 = new Border { [Grid.RowProperty] = 2 }; var rowDefinitions = new RowDefinitions() { new RowDefinition(1, GridUnitType.Star) { MinHeight = 70, MaxHeight = 110 }, new RowDefinition(GridLength.Auto), new RowDefinition(1, GridUnitType.Star) { MinHeight = 10, MaxHeight = 140 }, }; var grid = new Grid() { RowDefinitions = rowDefinitions, Children = new Controls() { control1, splitter, control2 } }; var root = new TestRoot { Child = grid }; root.Measure(new Size(100, 200)); root.Arrange(new Rect(0, 0, 100, 200)); splitter.RaiseEvent(new VectorEventArgs { RoutedEvent = Thumb.DragDeltaEvent, Vector = new Vector(0, -100) }); Assert.Equal(rowDefinitions[0].Height, new GridLength(70, GridUnitType.Star)); Assert.Equal(rowDefinitions[2].Height, new GridLength(130, GridUnitType.Star)); splitter.RaiseEvent(new VectorEventArgs { RoutedEvent = Thumb.DragDeltaEvent, Vector = new Vector(0, 100) }); Assert.Equal(rowDefinitions[0].Height, new GridLength(110, GridUnitType.Star)); Assert.Equal(rowDefinitions[2].Height, new GridLength(90, GridUnitType.Star)); }
public void Vertical_Stays_Within_Constraints() { var control1 = new Border { [Grid.ColumnProperty] = 0 }; var splitter = new GridSplitter { [Grid.ColumnProperty] = 1, }; var control2 = new Border { [Grid.ColumnProperty] = 2 }; var columnDefinitions = new ColumnDefinitions() { new ColumnDefinition(1, GridUnitType.Star) { MinWidth = 10, MaxWidth = 190 }, new ColumnDefinition(GridLength.Auto), new ColumnDefinition(1, GridUnitType.Star) { MinWidth = 80, MaxWidth = 120 }, }; var grid = new Grid() { ColumnDefinitions = columnDefinitions, Children = new Controls() { control1, splitter, control2 } }; var root = new TestRoot { Child = grid }; root.Measure(new Size(200, 100)); root.Arrange(new Rect(0, 0, 200, 100)); splitter.RaiseEvent(new VectorEventArgs { RoutedEvent = Thumb.DragDeltaEvent, Vector = new Vector(-100, 0) }); Assert.Equal(columnDefinitions[0].Width, new GridLength(80, GridUnitType.Star)); Assert.Equal(columnDefinitions[2].Width, new GridLength(120, GridUnitType.Star)); splitter.RaiseEvent(new VectorEventArgs { RoutedEvent = Thumb.DragDeltaEvent, Vector = new Vector(100, 0) }); Assert.Equal(columnDefinitions[0].Width, new GridLength(120, GridUnitType.Star)); Assert.Equal(columnDefinitions[2].Width, new GridLength(80, GridUnitType.Star)); }