| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- // 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.Generic;
- using System.Reactive.Linq;
- using Moq;
- using Avalonia.Styling;
- using Avalonia.UnitTests;
- using Xunit;
- using Avalonia.LogicalTree;
- using Avalonia.Controls;
- using System.ComponentModel;
- namespace Avalonia.Styling.UnitTests
- {
- public class StyledElementTests
- {
- [Fact]
- public void Classes_Should_Initially_Be_Empty()
- {
- var target = new StyledElement();
- Assert.Empty(target.Classes);
- }
- [Fact]
- public void Setting_Parent_Should_Also_Set_InheritanceParent()
- {
- var parent = new Decorator();
- var target = new TestControl();
- parent.Child = target;
- Assert.Equal(parent, target.Parent);
- Assert.Equal(parent, target.InheritanceParent);
- }
- [Fact]
- public void Setting_Parent_Should_Not_Set_InheritanceParent_If_Already_Set()
- {
- var parent = new Decorator();
- var inheritanceParent = new Decorator();
- var target = new TestControl();
- ((ISetInheritanceParent)target).SetParent(inheritanceParent);
- parent.Child = target;
- Assert.Equal(parent, target.Parent);
- Assert.Equal(inheritanceParent, target.InheritanceParent);
- }
- [Fact]
- public void InheritanceParent_Should_Be_Cleared_When_Removed_From_Parent()
- {
- var parent = new Decorator();
- var target = new TestControl();
- parent.Child = target;
- parent.Child = null;
- Assert.Null(target.InheritanceParent);
- }
- [Fact]
- public void InheritanceParent_Should_Be_Cleared_When_Removed_From_Parent_When_Has_Different_InheritanceParent()
- {
- var parent = new Decorator();
- var inheritanceParent = new Decorator();
- var target = new TestControl();
- ((ISetInheritanceParent)target).SetParent(inheritanceParent);
- parent.Child = target;
- parent.Child = null;
- Assert.Null(target.InheritanceParent);
- }
- [Fact]
- public void Adding_Element_With_Null_Parent_To_Logical_Tree_Should_Throw()
- {
- var target = new Border();
- var visualParent = new Panel();
- var logicalParent = new Panel();
- var root = new TestRoot();
- // Set the logical parent...
- ((ISetLogicalParent)target).SetParent(logicalParent);
- // ...so that when it's added to `visualParent`, the parent won't be set again.
- visualParent.Children.Add(target);
- // Clear the logical parent. It's now a logical child of `visualParent` but doesn't have
- // a logical parent itself.
- ((ISetLogicalParent)target).SetParent(null);
- // In this case, attaching the control to a logical tree should throw.
- logicalParent.Children.Add(visualParent);
- Assert.Throws<InvalidOperationException>(() => root.Child = logicalParent);
- }
- [Fact]
- 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);
- }
- [Fact]
- public void AttachedToLogicalParent_Should_Be_Called_Before_Parent_Change_Signalled()
- {
- var root = new TestRoot();
- var child = new Border();
- var raised = new List<string>();
- child.AttachedToLogicalTree += (s, e) =>
- {
- Assert.Equal(root, child.Parent);
- raised.Add("attached");
- };
- child.GetObservable(StyledElement.ParentProperty).Skip(1).Subscribe(_ => raised.Add("parent"));
- root.Child = child;
- Assert.Equal(new[] { "attached", "parent" }, raised);
- }
- [Fact]
- public void AttachedToLogicalParent_Should_Not_Be_Called_With_GlobalStyles_As_Root()
- {
- var globalStyles = Mock.Of<IGlobalStyles>();
- var root = new TestRoot { StylingParent = globalStyles };
- var child = new Border();
- var raised = false;
- child.AttachedToLogicalTree += (s, e) =>
- {
- Assert.Equal(root, e.Root);
- raised = true;
- };
- root.Child = child;
- Assert.True(raised);
- }
- [Fact]
- public void AttachedToLogicalParent_Should_Have_Source_Set()
- {
- var root = new TestRoot();
- var canvas = new Canvas();
- var border = new Border { Child = canvas };
- var raised = 0;
- void Attached(object sender, LogicalTreeAttachmentEventArgs e)
- {
- Assert.Same(border, e.Source);
- ++raised;
- }
- border.AttachedToLogicalTree += Attached;
- canvas.AttachedToLogicalTree += Attached;
- root.Child = border;
- Assert.Equal(2, raised);
- }
- [Fact]
- public void AttachedToLogicalParent_Should_Have_Parent_Set()
- {
- var root = new TestRoot();
- var canvas = new Canvas();
- var border = new Border { Child = canvas };
- var raised = 0;
- void Attached(object sender, LogicalTreeAttachmentEventArgs e)
- {
- Assert.Same(root, e.Parent);
- ++raised;
- }
- border.AttachedToLogicalTree += Attached;
- canvas.AttachedToLogicalTree += Attached;
- root.Child = border;
- Assert.Equal(2, raised);
- }
- [Fact]
- public void DetachedFromLogicalParent_Should_Be_Called_When_Removed_From_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.Child = child;
- child.Child = grandchild;
- root.Child = parent;
- parent.DetachedFromLogicalTree += (s, e) => parentRaised = true;
- child.DetachedFromLogicalTree += (s, e) => childRaised = true;
- grandchild.DetachedFromLogicalTree += (s, e) => grandchildRaised = true;
- root.Child = null;
- Assert.True(parentRaised);
- Assert.True(childRaised);
- Assert.True(grandchildRaised);
- }
- [Fact]
- public void DetachedFromLogicalParent_Should_Not_Be_Called_With_GlobalStyles_As_Root()
- {
- var globalStyles = Mock.Of<IGlobalStyles>();
- var root = new TestRoot { StylingParent = globalStyles };
- var child = new Border();
- var raised = false;
- child.DetachedFromLogicalTree += (s, e) =>
- {
- Assert.Equal(root, e.Root);
- raised = true;
- };
- root.Child = child;
- root.Child = null;
- Assert.True(raised);
- }
- [Fact]
- public void Parent_Should_Be_Null_When_DetachedFromLogicalParent_Called()
- {
- var target = new TestControl();
- var root = new TestRoot(target);
- var called = 0;
- target.DetachedFromLogicalTree += (s, e) =>
- {
- Assert.Null(target.Parent);
- Assert.Null(target.InheritanceParent);
- ++called;
- };
- root.Child = null;
- Assert.Equal(1, called);
- }
- [Fact]
- public void Adding_Tree_To_IStyleRoot_Should_Style_Controls()
- {
- using (AvaloniaLocator.EnterScope())
- {
- var root = new TestRoot();
- var parent = new Border();
- var child = new Border();
- var grandchild = new Control();
- var styler = new Mock<IStyler>();
- AvaloniaLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
- parent.Child = child;
- child.Child = grandchild;
- styler.Verify(x => x.ApplyStyles(It.IsAny<IStyleable>()), Times.Never());
- root.Child = parent;
- styler.Verify(x => x.ApplyStyles(parent), Times.Once());
- styler.Verify(x => x.ApplyStyles(child), Times.Once());
- styler.Verify(x => x.ApplyStyles(grandchild), Times.Once());
- }
- }
- [Fact]
- public void Styles_Not_Applied_Until_Initialization_Finished()
- {
- using (AvaloniaLocator.EnterScope())
- {
- var root = new TestRoot();
- var child = new Border();
- var styler = new Mock<IStyler>();
- AvaloniaLocator.CurrentMutable.Bind<IStyler>().ToConstant(styler.Object);
- ((ISupportInitialize)child).BeginInit();
- root.Child = child;
- styler.Verify(x => x.ApplyStyles(It.IsAny<IStyleable>()), Times.Never());
- ((ISupportInitialize)child).EndInit();
- styler.Verify(x => x.ApplyStyles(child), Times.Once());
- }
- }
- [Fact]
- public void Name_Cannot_Be_Set_After_Added_To_Logical_Tree()
- {
- using (AvaloniaLocator.EnterScope())
- {
- var root = new TestRoot();
- var child = new Border();
- root.Child = child;
- Assert.Throws<InvalidOperationException>(() => child.Name = "foo");
- }
- }
- [Fact]
- public void Name_Can_Be_Set_While_Initializing()
- {
- using (AvaloniaLocator.EnterScope())
- {
- var root = new TestRoot();
- var child = new Border();
- child.BeginInit();
- root.Child = child;
- child.Name = "foo";
- child.EndInit();
- }
- }
- [Fact]
- public void StyleInstance_Is_Disposed_When_Control_Removed_From_Logical_Tree()
- {
- using (AvaloniaLocator.EnterScope())
- {
- var root = new TestRoot();
- var child = new Border();
- root.Child = child;
- var styleInstance = new Mock<IStyleInstance>();
- ((IStyleable)child).StyleApplied(styleInstance.Object);
- root.Child = null;
- styleInstance.Verify(x => x.Dispose(), Times.Once);
- }
- }
- [Fact]
- public void EndInit_Should_Raise_Initialized()
- {
- var root = new TestRoot();
- var target = new Border();
- var called = false;
- target.Initialized += (s, e) => called = true;
- ((ISupportInitialize)target).BeginInit();
- root.Child = target;
- ((ISupportInitialize)target).EndInit();
- Assert.True(called);
- Assert.True(target.IsInitialized);
- }
- [Fact]
- public void Attaching_To_Visual_Tree_Should_Raise_Initialized()
- {
- var root = new TestRoot();
- var target = new Border();
- var called = false;
- target.Initialized += (s, e) => called = true;
- root.Child = target;
- Assert.True(called);
- Assert.True(target.IsInitialized);
- }
- [Fact]
- public void DataContextChanged_Should_Be_Called()
- {
- var root = new TestStackPanel
- {
- Name = "root",
- Children =
- {
- new TestControl
- {
- Name = "a1",
- Child = new TestControl
- {
- Name = "b1",
- }
- },
- new TestControl
- {
- Name = "a2",
- DataContext = "foo",
- },
- }
- };
- var called = new List<string>();
- void Record(object sender, EventArgs e) => called.Add(((StyledElement)sender).Name);
- root.DataContextChanged += Record;
- foreach (TestControl c in root.GetLogicalDescendants())
- {
- c.DataContextChanged += Record;
- }
- root.DataContext = "foo";
- Assert.Equal(new[] { "root", "a1", "b1", }, called);
- }
- [Fact]
- public void DataContext_Notifications_Should_Be_Called_In_Correct_Order()
- {
- var root = new TestStackPanel
- {
- Name = "root",
- Children =
- {
- new TestControl
- {
- Name = "a1",
- Child = new TestControl
- {
- Name = "b1",
- }
- },
- new TestControl
- {
- Name = "a2",
- DataContext = "foo",
- },
- }
- };
- var called = new List<string>();
- foreach (IDataContextEvents c in root.GetSelfAndLogicalDescendants())
- {
- c.DataContextBeginUpdate += (s, e) => called.Add("begin " + ((StyledElement)s).Name);
- c.DataContextChanged += (s, e) => called.Add("changed " + ((StyledElement)s).Name);
- c.DataContextEndUpdate += (s, e) => called.Add("end " + ((StyledElement)s).Name);
- }
- root.DataContext = "foo";
- Assert.Equal(
- new[]
- {
- "begin root",
- "begin a1",
- "begin b1",
- "changed root",
- "changed a1",
- "changed b1",
- "end b1",
- "end a1",
- "end root",
- },
- called);
- }
- private interface IDataContextEvents
- {
- event EventHandler DataContextBeginUpdate;
- event EventHandler DataContextChanged;
- event EventHandler DataContextEndUpdate;
- }
- private class TestControl : Decorator, IDataContextEvents
- {
- public event EventHandler DataContextBeginUpdate;
- public event EventHandler DataContextEndUpdate;
- public new IAvaloniaObject InheritanceParent => base.InheritanceParent;
- protected override void OnDataContextBeginUpdate()
- {
- DataContextBeginUpdate?.Invoke(this, EventArgs.Empty);
- base.OnDataContextBeginUpdate();
- }
- protected override void OnDataContextEndUpdate()
- {
- DataContextEndUpdate?.Invoke(this, EventArgs.Empty);
- base.OnDataContextEndUpdate();
- }
- }
- private class TestStackPanel : StackPanel, IDataContextEvents
- {
- public event EventHandler DataContextBeginUpdate;
- public event EventHandler DataContextEndUpdate;
- protected override void OnDataContextBeginUpdate()
- {
- DataContextBeginUpdate?.Invoke(this, EventArgs.Empty);
- base.OnDataContextBeginUpdate();
- }
- protected override void OnDataContextEndUpdate()
- {
- DataContextEndUpdate?.Invoke(this, EventArgs.Empty);
- base.OnDataContextEndUpdate();
- }
- }
- }
- }
|