123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Moq;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Controls.Templates;
- using Avalonia.LogicalTree;
- using Avalonia.Styling;
- using Avalonia.UnitTests;
- using Avalonia.VisualTree;
- using Xunit;
- using Avalonia.Media;
- namespace Avalonia.Controls.UnitTests.Primitives
- {
- public class TemplatedControlTests : ScopedTestBase
- {
- [Fact]
- public void Template_Doesnt_Get_Executed_On_Set()
- {
- bool executed = false;
- var template = new FuncControlTemplate((_, __) =>
- {
- executed = true;
- return new Control();
- });
- var target = new Avalonia.Controls.Primitives.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 Avalonia.Controls.Primitives.TemplatedControl
- {
- Template = template,
- };
- target.Measure(new Size(100, 100));
- Assert.True(executed);
- }
- [Fact]
- public void ApplyTemplate_Should_Create_Visual_Children()
- {
- var target = new Avalonia.Controls.Primitives.TemplatedControl
- {
- Template = new FuncControlTemplate((_, __) => new Decorator
- {
- Child = new Panel
- {
- Children =
- {
- new TextBlock(),
- new Border(),
- }
- }
- }),
- };
- target.ApplyTemplate();
- var types = target.GetVisualDescendants().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_Children_Should_Have_TemplatedParent_Set()
- {
- var target = new Avalonia.Controls.Primitives.TemplatedControl
- {
- Template = new FuncControlTemplate((_, __) => new Decorator
- {
- Child = new Panel
- {
- Children =
- {
- new TextBlock(),
- new Border(),
- }
- }
- }),
- };
- target.ApplyTemplate();
- var templatedParents = target.GetVisualDescendants()
- .OfType<Control>()
- .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 Avalonia.Controls.Primitives.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 Changing_Template_Should_Clear_Old_Templated_Childs_Parent()
- {
- var target = new Avalonia.Controls.Primitives.TemplatedControl
- {
- Template = new FuncControlTemplate((_, __) => new Decorator())
- };
- target.ApplyTemplate();
- var child = (Decorator)target.GetVisualChildren().Single();
- target.Template = new FuncControlTemplate((_, __) => new Canvas());
- target.ApplyTemplate();
- Assert.Null(child.Parent);
- }
- [Fact]
- public void Nested_Templated_Control_Should_Not_Have_Template_Applied()
- {
- var target = new Avalonia.Controls.Primitives.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()
- {
- TestTemplatedControl target;
- var root = new TestRoot
- {
- Styles =
- {
- new Style(x => x.Is<Control>())
- {
- Setters =
- {
- new Setter(Control.TagProperty, "foo")
- }
- }
- },
- Child = target = new TestTemplatedControl
- {
- Template = new FuncControlTemplate((_, __) =>
- {
- return new StackPanel
- {
- Children =
- {
- new TextBlock
- {
- }
- }
- };
- }),
- }
- };
- target.ApplyTemplate();
- foreach (Control child in target.GetTemplateChildren())
- Assert.Equal("foo", child.Tag);
- }
- [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).ToBinding(),
- }
- };
- }),
- 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 ApplyTemplate_Should_Raise_TemplateApplied()
- {
- var target = new TestTemplatedControl
- {
- Template = new FuncControlTemplate((_, __) => new Decorator())
- };
- var raised = false;
- target.TemplateApplied += (s, e) =>
- {
- Assert.Equal(Avalonia.Controls.Primitives.TemplatedControl.TemplateAppliedEvent, e.RoutedEvent);
- Assert.Same(target, e.Source);
- Assert.NotNull(e.NameScope);
- raised = true;
- };
- target.ApplyTemplate();
- Assert.True(raised);
- }
- [Fact]
- 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);
- }
- [Fact]
- public void TemplateChild_AttachedToLogicalTree_Should_Be_Raised()
- {
- Border templateChild = new Border();
- var root = new TestRoot
- {
- Child = new TestTemplatedControl
- {
- Template = new FuncControlTemplate((_, __) => new Decorator
- {
- Child = templateChild,
- })
- }
- };
- var raised = false;
- templateChild.AttachedToLogicalTree += (s, e) => raised = true;
- root.Child.ApplyTemplate();
- Assert.True(raised);
- }
- [Fact]
- 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);
- }
- [Fact]
- public void Removing_From_LogicalTree_Should_Not_Remove_Child()
- {
- Border templateChild = new Border();
- TestTemplatedControl target;
- var root = new TestRoot
- {
- Styles =
- {
- new Style(x => x.OfType<TestTemplatedControl>())
- {
- Setters =
- {
- 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());
- }
- [Fact]
- public void Re_adding_To_Same_LogicalTree_Should_Not_Recreate_Template()
- {
- TestTemplatedControl target;
- var root = new TestRoot
- {
- Styles =
- {
- new Style(x => x.OfType<TestTemplatedControl>())
- {
- Setters =
- {
- new Setter(
- TemplatedControl.TemplateProperty,
- new FuncControlTemplate((_, __) => new Decorator
- {
- Child = new Border(),
- }))
- }
- }
- },
- Child = target = new TestTemplatedControl()
- };
- Assert.NotNull(target.Template);
- target.ApplyTemplate();
- var expected = (Decorator)target.GetVisualChildren().Single();
- root.Child = null;
- root.Child = target;
- target.ApplyTemplate();
- Assert.Same(expected, target.GetVisualChildren().Single());
- }
- [Fact]
- public void Re_adding_To_Different_LogicalTree_Should_Recreate_Template()
- {
- TestTemplatedControl target;
- var root = new TestRoot
- {
- Styles =
- {
- new Style(x => x.OfType<TestTemplatedControl>())
- {
- Setters =
- {
- new Setter(
- TemplatedControl.TemplateProperty,
- new FuncControlTemplate((_, __) => new Decorator
- {
- Child = new Border(),
- }))
- }
- }
- },
- Child = target = new TestTemplatedControl()
- };
- var root2 = new TestRoot
- {
- Styles =
- {
- new Style(x => x.OfType<TestTemplatedControl>())
- {
- Setters =
- {
- 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);
- }
- [Fact]
- public void Moving_To_New_LogicalTree_Should_Detach_Attach_Template_Child()
- {
- 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);
- }
- [Fact]
- public void Templated_Child_Should_Find_Resource_In_TemplatedParent()
- {
- var target = new ContentControl
- {
- Resources =
- {
- { "red", Brushes.Red },
- },
- Template = new FuncControlTemplate<ContentControl>((x, scope) =>
- {
- var result = new ContentPresenter
- {
- Name = "PART_ContentPresenter",
- [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
- }.RegisterInNameScope(scope);
- result.Bind(ContentPresenter.BackgroundProperty, result.GetResourceObservable("red"));
- return result;
- }),
- };
- var root = new TestRoot(target);
- target.ApplyTemplate();
- var contentPresenter = Assert.IsType<ContentPresenter>(target.GetVisualChildren().Single());
- Assert.Same(Brushes.Red, contentPresenter.Background);
- }
- [Fact]
- public void Changing_Resource_In_Templated_Parent_Should_Affect_Templated_Child()
- {
- var target = new ContentControl
- {
- Resources =
- {
- { "red", Brushes.Red },
- },
- Template = new FuncControlTemplate<ContentControl>((x, scope) =>
- {
- var result = new ContentPresenter
- {
- Name = "PART_ContentPresenter",
- [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
- }.RegisterInNameScope(scope);
- result.Bind(ContentPresenter.BackgroundProperty, result.GetResourceObservable("red"));
- return result;
- }),
- };
- var root = new TestRoot(target);
- target.ApplyTemplate();
- var contentPresenter = Assert.IsType<ContentPresenter>(target.GetVisualChildren().Single());
- Assert.Same(Brushes.Red, contentPresenter.Background);
- target.Resources["red"] = Brushes.Green;
- Assert.Same(Brushes.Green, contentPresenter.Background);
- }
- private static Control ScrollingContentControlTemplate(ContentControl control, INameScope scope)
- {
- return new Border
- {
- Child = new ScrollViewer
- {
- Template = new FuncControlTemplate<ScrollViewer>(ScrollViewerTemplate),
- Name = "ScrollViewer",
- Content = new ContentPresenter
- {
- Name = "PART_ContentPresenter",
- [!ContentPresenter.ContentProperty] = control[!ContentControl.ContentProperty],
- }.RegisterInNameScope(scope)
- }.RegisterInNameScope(scope)
- };
- }
- private static Control ScrollViewerTemplate(ScrollViewer control, INameScope scope)
- {
- var result = new ScrollContentPresenter
- {
- Name = "PART_ContentPresenter",
- [~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
- }.RegisterInNameScope(scope);
- return result;
- }
- }
- }
|