| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- // Copyright (c) The Perspex 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.Generic;
- using System.Linq;
- using Moq;
- using Perspex.Controls.Presenters;
- using Perspex.Controls.Primitives;
- using Perspex.Controls.Templates;
- using Perspex.LogicalTree;
- using Perspex.Styling;
- using Perspex.UnitTests;
- using Perspex.VisualTree;
- using Xunit;
- namespace Perspex.Controls.UnitTests.Primitives
- {
- public class TemplatedControlTests
- {
- [Fact]
- public void Template_Doesnt_Get_Executed_On_Set()
- {
- bool executed = false;
- var template = new FuncControlTemplate(_ =>
- {
- executed = true;
- return new Control();
- });
- var target = new TemplatedControl
- {
- Template = template,
- };
- Assert.False(executed);
- }
- [Fact]
- public void Template_Gets_Executed_On_Measure()
- {
- bool executed = false;
- var template = new FuncControlTemplate(_ =>
- {
- executed = true;
- return new Control();
- });
- var target = new TemplatedControl
- {
- Template = template,
- };
- target.Measure(new Size(100, 100));
- Assert.True(executed);
- }
- [Fact]
- public void ApplyTemplate_Should_Create_Visual_Children()
- {
- var target = new TemplatedControl
- {
- Template = new FuncControlTemplate(_ => new Decorator
- {
- Child = new Panel
- {
- Children = new Controls
- {
- new TextBlock(),
- new Border(),
- }
- }
- }),
- };
- target.ApplyTemplate();
- var types = target.GetVisualDescendents().Select(x => x.GetType()).ToList();
- Assert.Equal(
- new[]
- {
- typeof(Decorator),
- typeof(Panel),
- typeof(TextBlock),
- typeof(Border)
- },
- types);
- Assert.Empty(target.GetLogicalChildren());
- }
- [Fact]
- public void Templated_Child_Should_Be_NameScope()
- {
- var target = new TemplatedControl
- {
- Template = new FuncControlTemplate(_ => new Decorator
- {
- Child = new Panel
- {
- Children = new Controls
- {
- new TextBlock(),
- new Border(),
- }
- }
- }),
- };
- target.ApplyTemplate();
- Assert.NotNull(NameScope.GetNameScope((Control)target.GetVisualChildren().Single()));
- }
- [Fact]
- public void Templated_Children_Should_Have_TemplatedParent_Set()
- {
- var target = new TemplatedControl
- {
- Template = new FuncControlTemplate(_ => new Decorator
- {
- Child = new Panel
- {
- Children = new Controls
- {
- new TextBlock(),
- new Border(),
- }
- }
- }),
- };
- target.ApplyTemplate();
- var templatedParents = target.GetVisualDescendents()
- .OfType<IControl>()
- .Select(x => x.TemplatedParent)
- .ToList();
- Assert.Equal(4, templatedParents.Count);
- Assert.True(templatedParents.All(x => x == target));
- }
- [Fact]
- public void Templated_Child_Should_Have_Parent_Set()
- {
- var target = new TemplatedControl
- {
- Template = new FuncControlTemplate(_ => new Decorator())
- };
- target.ApplyTemplate();
- var child = (Decorator)target.GetVisualChildren().Single();
- Assert.Equal(target, child.Parent);
- Assert.Equal(target, child.GetLogicalParent());
- }
- [Fact]
- public void Nested_Templated_Control_Should_Not_Have_Template_Applied()
- {
- var target = new TemplatedControl
- {
- Template = new FuncControlTemplate(_ => new ScrollViewer())
- };
- target.ApplyTemplate();
- var child = (ScrollViewer)target.GetVisualChildren().Single();
- Assert.Empty(child.GetVisualChildren());
- }
- [Fact]
- 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());
- }
- }
- [Fact]
- 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);
- }
- [Fact]
- 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"));
- }
- [Fact]
- 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);
- }
- private static IControl ScrollingContentControlTemplate(ContentControl control)
- {
- return new Border
- {
- Child = new ScrollViewer
- {
- Template = new FuncControlTemplate<ScrollViewer>(ScrollViewerTemplate),
- Name = "ScrollViewer",
- Content = new ContentPresenter
- {
- Name = "PART_ContentPresenter",
- [!ContentPresenter.ContentProperty] = control[!ContentControl.ContentProperty],
- }
- }
- };
- }
- private static Control ScrollViewerTemplate(ScrollViewer control)
- {
- var result = new ScrollContentPresenter
- {
- Name = "PART_ContentPresenter",
- [~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
- };
- return result;
- }
- }
- }
|