From 1ec5c63b6767c45b7765d06065f0e96b96fa49b1 Mon Sep 17 00:00:00 2001 From: Oliver Brown Date: Mon, 25 Oct 2021 22:28:53 +0100 Subject: [PATCH] Add tests for #9151 --- .../BindableObjectUnitTests.cs | 38 +++++++++++ .../AttachedProperties.xaml | 22 ++++++ .../AttachedProperties.xaml.cs | 68 +++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml create mode 100644 Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml.cs diff --git a/Xamarin.Forms.Core.UnitTests/BindableObjectUnitTests.cs b/Xamarin.Forms.Core.UnitTests/BindableObjectUnitTests.cs index cb61072dbaf..ed1f1e3401c 100644 --- a/Xamarin.Forms.Core.UnitTests/BindableObjectUnitTests.cs +++ b/Xamarin.Forms.Core.UnitTests/BindableObjectUnitTests.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Globalization; using NUnit.Framework; using Xamarin.Forms.Internals; @@ -61,6 +62,15 @@ public Baz Qux } } + internal static class AttachedPropertyHolder + { + public static readonly BindableProperty AttachedCollectionProperty = + BindableProperty.CreateAttached("AttachedCollection", typeof(IList), typeof(AttachedPropertyHolder), new List()); + + public static IList GetAttachedCollection(BindableObject bindable) => (IList)bindable.GetValue(AttachedCollectionProperty); + public static void SetAttachedCollection(BindableObject bindable, IList value) => bindable.SetValue(AttachedCollectionProperty, value); + } + internal class ToBarConverter : TypeConverter { public override object ConvertFrom(System.Globalization.CultureInfo culture, object value) @@ -1593,5 +1603,33 @@ public void SetBindingToTextInvokesToString() Assert.That(bindable.GetValue(prop), Is.EqualTo("converted")); } + [Test] + //https://github.com/xamarin/Xamarin.Forms/issues/9151 + public void SetValueWorksWithAttachedCollectionValuedProperties() + { + var prop = BindableProperty.CreateAttached("AttachedCollection", typeof(IList), typeof(AttachedPropertyHolder), null, defaultValueCreator: _ => new List()); + var collection = new[] { "a", "b" }; + var bindable = new MockBindable() { BindingContext = new { info = collection } }; + bindable.SetBinding(prop, "info"); + + Assert.That(bindable.GetValue(prop), Is.EquivalentTo(collection)); + } + + [Test] + //https://github.com/xamarin/Xamarin.Forms/issues/9151 + public void SetValueWorksWithAttachedCollectionValuedPropertiesOnMultipleBindables() + { + var prop = BindableProperty.CreateAttached("AttachedCollection", typeof(IList), typeof(AttachedPropertyHolder), null, defaultValueCreator: _ => new List()); + var collection1 = new[] { "a", "b" }; + var collection2 = new[] { "c", "d" }; + + var bindable1 = new MockBindable() { BindingContext = new { info = collection1 } }; + bindable1.SetValue(prop, collection1); + var bindable2 = new MockBindable() { BindingContext = new { info = collection2 } }; + bindable2.SetValue(prop, collection2); + + Assert.That(bindable1.GetValue(prop), Is.EquivalentTo(collection1)); + Assert.That(bindable2.GetValue(prop), Is.EquivalentTo(collection2)); + } } } diff --git a/Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml b/Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml new file mode 100644 index 00000000000..6b549b99e21 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml.cs b/Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml.cs new file mode 100644 index 00000000000..7c9a547ea48 --- /dev/null +++ b/Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml.cs @@ -0,0 +1,68 @@ +using System.Collections.Generic; +using NUnit.Framework; +using Xamarin.Forms.Core.UnitTests; + +namespace Xamarin.Forms.Xaml.UnitTests +{ + public partial class AttachedProperties : ContentPage + { + + public AttachedProperties() + { + InitializeComponent(); + } + + public AttachedProperties(bool useCompiledXaml) + { + //this stub will be replaced at compile time + } + + [TestFixture] + public class Tests + { + [SetUp] + public void Setup() + { + Device.PlatformServices = new MockPlatformServices(); + } + + [TearDown] + public void TearDown() + { + Device.PlatformServices = null; + } + + [TestCase(true)] + [TestCase(false)] + public void BindProperties(bool useCompiledXaml) + { + var layout = new AttachedProperties(useCompiledXaml); + var collection1 = StackLayoutProperties.GetStackLayoutCollection(layout.StackLayout1); + var collection2 = StackLayoutProperties.GetStackLayoutCollection(layout.StackLayout2); + Assert.That(collection1[0].ExampleProperty1, Is.EqualTo("a")); + Assert.That(collection1[1].ExampleProperty1, Is.EqualTo("b")); + Assert.That(collection2[0].ExampleProperty1, Is.EqualTo("c")); + Assert.That(collection2[1].ExampleProperty1, Is.EqualTo("d")); + } + } + } + + + public class MyCustomClass + { + public string ExampleProperty1 { get; set; } + } + + public class StackLayoutProperties + { + public static readonly BindableProperty StackLayoutCollectionProperty = + BindableProperty.CreateAttached("StackLayoutCollection", + typeof(IList), + typeof(StackLayoutProperties), + null, + defaultValueCreator: _ => new List()); + + public static IList GetStackLayoutCollection(BindableObject view) => (IList)view.GetValue(StackLayoutCollectionProperty); + public static void SetStackLayoutCollection(BindableObject view, IList value) => view.SetValue(StackLayoutCollectionProperty, value); + } +} \ No newline at end of file