public void Applying_New_Template_Clears_TemplatedParent_Of_Old_Template_Children() { var target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => new Decorator { Child = new Border(), }) }; target.ApplyTemplate(); var decorator = (Decorator)target.GetVisualChildren().Single(); var border = (Border)decorator.Child; Assert.Equal(target, decorator.TemplatedParent); Assert.Equal(target, border.TemplatedParent); target.Template = new FuncControlTemplate(_ => new Canvas()); // Templated children should not be removed here: the control may be re-added // somewhere with the same template, so they could still be of use. Assert.Same(decorator, target.GetVisualChildren().Single()); Assert.Equal(target, decorator.TemplatedParent); Assert.Equal(target, border.TemplatedParent); target.ApplyTemplate(); Assert.Null(decorator.TemplatedParent); Assert.Null(border.TemplatedParent); }
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 Applying_New_Template_Clears_TemplatedParent_Of_Old_Template_Children() { var target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => new Decorator { Child = new Border(), }) }; target.ApplyTemplate(); var decorator = (Decorator)target.GetVisualChildren().Single(); var border = (Border)decorator.Child; Assert.Equal(target, decorator.TemplatedParent); Assert.Equal(target, border.TemplatedParent); target.Template = new FuncControlTemplate(_ => new Canvas()); // Templated children should not be removed here: the control may be re-added // somewhere with the same template, so they could still be of use. Assert.Same(decorator, target.GetVisualChildren().Single()); Assert.Equal(target, decorator.TemplatedParent); Assert.Equal(target, border.TemplatedParent); target.ApplyTemplate(); Assert.Null(decorator.TemplatedParent); Assert.Null(border.TemplatedParent); }
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 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 Moving_To_New_LogicalTree_Should_Detach_Attach_Template_Child() { using (UnitTestApplication.Start(TestServices.RealStyler)) { TestTemplatedControl target; var root = new TestRoot { Child = target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => new Decorator()), } }; Assert.NotNull(target.Template); target.ApplyTemplate(); var templateChild = (ILogical)target.GetVisualChildren().Single(); Assert.True(templateChild.IsAttachedToLogicalTree); root.Child = null; Assert.False(templateChild.IsAttachedToLogicalTree); var newRoot = new TestRoot { Child = target }; Assert.True(templateChild.IsAttachedToLogicalTree); } }
public void Nested_Templated_Controls_Have_Correct_TemplatedParent() { var target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => { return(new ContentControl { Template = new FuncControlTemplate(parent => { return new Border { Child = new ContentPresenter { [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty), } }; }), Content = new Decorator { Child = new TextBlock() } }); }), }; target.ApplyTemplate(); var contentControl = target.GetTemplateChildren().OfType <ContentControl>().Single(); contentControl.ApplyTemplate(); var border = contentControl.GetTemplateChildren().OfType <Border>().Single(); var presenter = contentControl.GetTemplateChildren().OfType <ContentPresenter>().Single(); var decorator = (Decorator)presenter.Content; var textBlock = (TextBlock)decorator.Child; Assert.Equal(target, contentControl.TemplatedParent); Assert.Equal(contentControl, border.TemplatedParent); Assert.Equal(contentControl, presenter.TemplatedParent); Assert.Equal(target, decorator.TemplatedParent); Assert.Equal(target, textBlock.TemplatedParent); }
public void ApplyTemplate_Should_Raise_TemplateApplied() { var target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => new Decorator()) }; var raised = false; target.TemplateApplied += (s, e) => { Assert.Equal(TemplatedControl.TemplateAppliedEvent, e.RoutedEvent); Assert.Same(target, e.Source); Assert.NotNull(e.NameScope); raised = true; }; target.ApplyTemplate(); Assert.True(raised); }
public void ApplyTemplate_Should_Raise_TemplateApplied() { var target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => new Decorator()) }; var raised = false; target.TemplateApplied += (s, e) => { Assert.Equal(TemplatedControl.TemplateAppliedEvent, e.RoutedEvent); Assert.Same(target, e.Source); Assert.NotNull(e.NameScope); raised = true; }; target.ApplyTemplate(); Assert.True(raised); }
public void Templated_Children_Should_Be_Styled() { using (PerspexLocator.EnterScope()) { var styler = new Mock <IStyler>(); PerspexLocator.CurrentMutable.Bind <IStyler>().ToConstant(styler.Object); TestTemplatedControl target; var root = new TestRoot { Child = target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => { return(new StackPanel { Children = new Controls { new TextBlock { } } }); }), } }; target.ApplyTemplate(); 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 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 Nested_Templated_Controls_Have_Correct_TemplatedParent() { var target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => { return new ContentControl { Template = new FuncControlTemplate(parent => { return new Border { Child = new ContentPresenter { [~ContentPresenter.ContentProperty] = parent.GetObservable(ContentControl.ContentProperty).AsBinding(), } }; }), Content = new Decorator { Child = new TextBlock() } }; }), }; target.ApplyTemplate(); var contentControl = target.GetTemplateChildren().OfType<ContentControl>().Single(); contentControl.ApplyTemplate(); var border = contentControl.GetTemplateChildren().OfType<Border>().Single(); var presenter = contentControl.GetTemplateChildren().OfType<ContentPresenter>().Single(); var decorator = (Decorator)presenter.Content; var textBlock = (TextBlock)decorator.Child; Assert.Equal(target, contentControl.TemplatedParent); Assert.Equal(contentControl, border.TemplatedParent); Assert.Equal(contentControl, presenter.TemplatedParent); Assert.Equal(target, decorator.TemplatedParent); Assert.Equal(target, textBlock.TemplatedParent); }
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_Children_Should_Be_Styled() { using (PerspexLocator.EnterScope()) { var styler = new Mock<IStyler>(); PerspexLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object); TestTemplatedControl target; var root = new TestRoot { Child = target = new TestTemplatedControl { Template = new FuncControlTemplate(_ => { return new StackPanel { Children = new Controls { new TextBlock { } } }; }), } }; target.ApplyTemplate(); 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()); } }