浏览代码

Modify/add tests for non-registered properties.

We should change to the behavior of other XAML frameworks where non-registered `AvaloniaProperty`s can be set on `AvaloniaObject`s via code.
Steven Kirk 7 年之前
父节点
当前提交
7b4cd4f96d

+ 4 - 5
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_Binding.cs

@@ -92,14 +92,13 @@ namespace Avalonia.Base.UnitTests
         }
 
         [Fact]
-        public void Bind_Throws_Exception_For_Unregistered_Property()
+        public void Bind_Does_Not_Throw_Exception_For_Unregistered_Property()
         {
             Class1 target = new Class1();
 
-            Assert.Throws<ArgumentException>(() =>
-            {
-                target.Bind(Class2.BarProperty, Observable.Return("foo"));
-            });
+            target.Bind(Class2.BarProperty, Observable.Never<string>().StartWith("foo"));
+
+            Assert.Equal("foo", target.GetValue(Class2.BarProperty));
         }
 
         [Fact]

+ 2 - 2
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_GetValue.cs

@@ -46,11 +46,11 @@ namespace Avalonia.Base.UnitTests
         }
 
         [Fact]
-        public void GetValue_Throws_Exception_For_Unregistered_Property()
+        public void GetValue_Doesnt_Throw_Exception_For_Unregistered_Property()
         {
             var target = new Class3();
 
-            Assert.Throws<ArgumentException>(() => target.GetValue(Class1.FooProperty));
+            Assert.Equal("foodefault", target.GetValue(Class1.FooProperty));
         }
 
         private class Class1 : AvaloniaObject

+ 34 - 5
tests/Avalonia.Base.UnitTests/AvaloniaObjectTests_SetValue.cs

@@ -30,6 +30,16 @@ namespace Avalonia.Base.UnitTests
             Assert.Equal("newvalue", target.GetValue(Class1.FooProperty));
         }
 
+        [Fact]
+        public void SetValue_Sets_Attached_Value()
+        {
+            Class2 target = new Class2();
+
+            target.SetValue(AttachedOwner.AttachedProperty, "newvalue");
+
+            Assert.Equal("newvalue", target.GetValue(AttachedOwner.AttachedProperty));
+        }
+
         [Fact]
         public void SetValue_Raises_PropertyChanged()
         {
@@ -84,14 +94,27 @@ namespace Avalonia.Base.UnitTests
         }
 
         [Fact]
-        public void SetValue_Throws_Exception_For_Unregistered_Property()
+        public void SetValue_Allows_Setting_Unregistered_Property()
         {
             Class1 target = new Class1();
 
-            Assert.Throws<ArgumentException>(() =>
-            {
-                target.SetValue(Class2.BarProperty, "invalid");
-            });
+            Assert.False(AvaloniaPropertyRegistry.Instance.IsRegistered(target, Class2.BarProperty));
+
+            target.SetValue(Class2.BarProperty, "bar");
+
+            Assert.Equal("bar", target.GetValue(Class2.BarProperty));
+        }
+
+        [Fact]
+        public void SetValue_Allows_Setting_Unregistered_Attached_Property()
+        {
+            Class1 target = new Class1();
+
+            Assert.False(AvaloniaPropertyRegistry.Instance.IsRegistered(target, AttachedOwner.AttachedProperty));
+
+            target.SetValue(AttachedOwner.AttachedProperty, "bar");
+
+            Assert.Equal("bar", target.GetValue(AttachedOwner.AttachedProperty));
         }
 
         [Fact]
@@ -189,6 +212,12 @@ namespace Avalonia.Base.UnitTests
             }
         }
 
+        private class AttachedOwner
+        {
+            public static readonly AttachedProperty<string> AttachedProperty =
+                AvaloniaProperty.RegisterAttached<AttachedOwner, Class2, string>("Attached");
+        }
+
         private class ImplictDouble
         {
             public ImplictDouble(double value)

+ 14 - 0
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/AttachedPropertyOwner.cs

@@ -0,0 +1,14 @@
+using System;
+using Avalonia.Controls;
+
+namespace Avalonia.Markup.Xaml.UnitTests.Xaml
+{
+    public class AttachedPropertyOwner
+    {
+        public static readonly AttachedProperty<double> DoubleProperty =
+            AvaloniaProperty.RegisterAttached<AttachedPropertyOwner, Control, double>("Double");
+
+        public static double GetDouble(Control control) => control.GetValue(DoubleProperty);
+        public static void SetDouble(Control control, double value) => control.SetValue(DoubleProperty, value);
+    }
+}

+ 19 - 0
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BasicTests.cs

@@ -10,6 +10,7 @@ using Avalonia.Markup.Xaml.Templates;
 using Avalonia.Media;
 using Avalonia.Styling;
 using Avalonia.UnitTests;
+using Portable.Xaml;
 using System.Collections;
 using System.ComponentModel;
 using System.Linq;
@@ -124,6 +125,24 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
             Assert.Equal("Foo", ToolTip.GetTip(target));
         }
 
+        [Fact]
+        public void NonExistent_Property_Throws()
+        {
+            var xaml =
+        @"<ContentControl xmlns='https://github.com/avaloniaui' DoesntExist='foo'/>";
+
+            Assert.Throws<XamlObjectWriterException>(() => AvaloniaXamlLoader.Parse<ContentControl>(xaml));
+        }
+
+        [Fact]
+        public void Non_Attached_Property_With_Attached_Property_Syntax_Throws()
+        {
+            var xaml =
+        @"<ContentControl xmlns='https://github.com/avaloniaui' TextBlock.Text='foo'/>";
+
+            Assert.Throws<XamlObjectWriterException>(() => AvaloniaXamlLoader.Parse<ContentControl>(xaml));
+        }
+
         [Fact]
         public void ContentControl_ContentTemplate_Is_Functional()
         {

+ 66 - 0
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/BindingTests.cs

@@ -215,5 +215,71 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
                 Assert.Equal("bar", textBlock.Text);
             }
         }
+
+        [Fact]
+        public void Binding_To_Namespaced_Attached_Property_Works()
+        {
+            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
+            {
+                var xaml = @"
+<Window xmlns='https://github.com/avaloniaui'
+        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
+        xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
+    <TextBlock local:AttachedPropertyOwner.Double='{Binding}'/>
+</Window>";
+                var loader = new AvaloniaXamlLoader();
+                var window = (Window)loader.Load(xaml);
+                var textBlock = (TextBlock)window.Content;
+
+                window.DataContext = 5.6;
+                window.ApplyTemplate();
+
+                Assert.Equal(5.6, AttachedPropertyOwner.GetDouble(textBlock));
+            }
+        }
+
+        [Fact]
+        public void Binding_To_AddOwnered_Attached_Property_Works()
+        {
+            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
+            {
+                var xaml = @"
+<Window xmlns='https://github.com/avaloniaui'
+        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
+        xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
+    <local:TestControl Double='{Binding}'/>
+</Window>";
+                var loader = new AvaloniaXamlLoader();
+                var window = (Window)loader.Load(xaml);
+                var testControl = (TestControl)window.Content;
+
+                window.DataContext = 5.6;
+                window.ApplyTemplate();
+
+                Assert.Equal(5.6, testControl.Double);
+            }
+        }
+
+        [Fact]
+        public void Binding_To_Attached_Property_Using_AddOwnered_Type_Works()
+        {
+            using (UnitTestApplication.Start(TestServices.MockWindowingPlatform))
+            {
+                var xaml = @"
+<Window xmlns='https://github.com/avaloniaui'
+        xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
+        xmlns:local='clr-namespace:Avalonia.Markup.Xaml.UnitTests.Xaml;assembly=Avalonia.Markup.Xaml.UnitTests'>
+    <TextBlock local:TestControl.Double='{Binding}'/>
+</Window>";
+                var loader = new AvaloniaXamlLoader();
+                var window = (Window)loader.Load(xaml);
+                var textBlock = (TextBlock)window.Content;
+
+                window.DataContext = 5.6;
+                window.ApplyTemplate();
+
+                Assert.Equal(5.6, AttachedPropertyOwner.GetDouble(textBlock));
+            }
+        }
     }
 }

+ 17 - 0
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/TestControl.cs

@@ -0,0 +1,17 @@
+using System;
+using Avalonia.Controls;
+
+namespace Avalonia.Markup.Xaml.UnitTests.Xaml
+{
+    public class TestControl : Control
+    {
+        public static readonly StyledProperty<double> DoubleProperty =
+            AttachedPropertyOwner.DoubleProperty.AddOwner<TestControl>();
+
+        public double Double
+        {
+            get => GetValue(DoubleProperty);
+            set => SetValue(DoubleProperty, value);
+        }
+    }
+}