Browse Source

Added failing tests for #1099.

Steven Kirk 6 years ago
parent
commit
302bf55b8a

+ 16 - 0
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Inheritance.cs

@@ -1,6 +1,7 @@
 // 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.Collections.Generic;
 using Xunit;
 
 namespace Avalonia.Base.UnitTests
@@ -115,6 +116,21 @@ namespace Avalonia.Base.UnitTests
             Assert.True(raised);
         }
 
+        [Fact]
+        public void PropertyChanged_Is_Raised_In_Parent_Before_Child()
+        {
+            var parent = new Class1();
+            var child = new Class2 { Parent = parent };
+            var result = new List<object>();
+
+            parent.PropertyChanged += (s, e) => result.Add(parent);
+            child.PropertyChanged += (s, e) => result.Add(child);
+
+            parent.SetValue(Class1.BazProperty, "changed");
+
+            Assert.Equal(new[] { parent, child }, result);
+        }
+
         private class Class1 : AvaloniaObject
         {
             public static readonly StyledProperty<string> FooProperty =

+ 27 - 1
tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_InTemplate.cs

@@ -4,6 +4,7 @@
 using System.Linq;
 using Avalonia.Controls.Presenters;
 using Avalonia.Controls.Templates;
+using Avalonia.Data;
 using Avalonia.LogicalTree;
 using Avalonia.UnitTests;
 using Avalonia.VisualTree;
@@ -266,6 +267,31 @@ namespace Avalonia.Controls.UnitTests.Presenters
             Assert.IsType<Canvas>(target.Child);
         }
 
+
+        [Fact]
+        public void Should_Not_Bind_Old_Child_To_New_DataContext()
+        {
+            // Test for issue #1099.
+            var textBlock = new TextBlock
+            {
+                [!TextBlock.TextProperty] = new Binding(),
+            };
+
+            var (target, host) = CreateTarget();
+            host.DataTemplates.Add(new FuncDataTemplate<string>(x => textBlock));
+            host.DataTemplates.Add(new FuncDataTemplate<int>(x => new Canvas()));
+
+            target.Content = "foo";
+            Assert.Same(textBlock, target.Child);
+
+            textBlock.PropertyChanged += (s, e) =>
+            {
+                Assert.NotEqual(e.NewValue, "42");
+            };
+
+            target.Content = 42;
+        }
+
         (ContentPresenter presenter, ContentControl templatedParent) CreateTarget()
         {
             var templatedParent = new ContentControl
@@ -288,4 +314,4 @@ namespace Avalonia.Controls.UnitTests.Presenters
             public IControl Child { get; set; }
         }
     }
-}
+}

+ 31 - 1
tests/Avalonia.Controls.UnitTests/Presenters/ContentPresenterTests_Standalone.cs

@@ -14,6 +14,7 @@ using System.Linq;
 using Xunit;
 using Avalonia.Rendering;
 using Avalonia.Media;
+using Avalonia.Data;
 
 namespace Avalonia.Controls.UnitTests.Presenters
 {
@@ -204,7 +205,6 @@ namespace Avalonia.Controls.UnitTests.Presenters
             Assert.NotEqual(foo, logicalChildren.First());
         }
 
-
         [Fact]
         public void Changing_Background_Brush_Color_Should_Invalidate_Visual()
         {
@@ -221,5 +221,35 @@ namespace Avalonia.Controls.UnitTests.Presenters
 
             renderer.Verify(x => x.AddDirty(target), Times.Once);
         }
+
+        [Fact]
+        public void Should_Not_Bind_Old_Child_To_New_DataContext()
+        {
+            // Test for issue #1099.
+            var textBlock = new TextBlock
+            {
+                [!TextBlock.TextProperty] = new Binding(),
+            };
+
+            var target = new ContentPresenter()
+            {
+                DataTemplates =
+                {
+                    new FuncDataTemplate<string>(x => textBlock),
+                    new FuncDataTemplate<int>(x => new Canvas()),
+                },
+            };
+
+            var root = new TestRoot(target);
+            target.Content = "foo";
+            Assert.Same(textBlock, target.Child);
+
+            textBlock.PropertyChanged += (s, e) =>
+            {
+                Assert.NotEqual(e.NewValue, "42");
+            };
+
+            target.Content = 42;
+        }
     }
 }