Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

[core] Add tests for #9151 #14793

Merged
merged 1 commit into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions Xamarin.Forms.Core.UnitTests/BindableObjectUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using NUnit.Framework;
using Xamarin.Forms.Internals;
Expand Down Expand Up @@ -61,6 +62,15 @@ public Baz Qux
}
}

internal static class AttachedPropertyHolder
{
public static readonly BindableProperty AttachedCollectionProperty =
BindableProperty.CreateAttached("AttachedCollection", typeof(IList<string>), typeof(AttachedPropertyHolder), new List<string>());

public static IList<string> GetAttachedCollection(BindableObject bindable) => (IList<string>)bindable.GetValue(AttachedCollectionProperty);
public static void SetAttachedCollection(BindableObject bindable, IList<string> value) => bindable.SetValue(AttachedCollectionProperty, value);
}

internal class ToBarConverter : TypeConverter
{
public override object ConvertFrom(System.Globalization.CultureInfo culture, object value)
Expand Down Expand Up @@ -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<string>), typeof(AttachedPropertyHolder), null, defaultValueCreator: _ => new List<string>());
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<string>), typeof(AttachedPropertyHolder), null, defaultValueCreator: _ => new List<string>());
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));
}
}
}
22 changes: 22 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Xamarin.Forms.Xaml.UnitTests"
x:Class="Xamarin.Forms.Xaml.UnitTests.AttachedProperties">
<ContentPage.Content>
<StackLayout>
<StackLayout x:Name="StackLayout1">
<local:StackLayoutProperties.StackLayoutCollection>
<local:MyCustomClass ExampleProperty1="a"/>
<local:MyCustomClass ExampleProperty1="b"/>
</local:StackLayoutProperties.StackLayoutCollection>
</StackLayout>
<StackLayout x:Name="StackLayout2">
<local:StackLayoutProperties.StackLayoutCollection>
<local:MyCustomClass ExampleProperty1="c"/>
<local:MyCustomClass ExampleProperty1="d"/>
</local:StackLayoutProperties.StackLayoutCollection>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
68 changes: 68 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/AttachedProperties.xaml.cs
Original file line number Diff line number Diff line change
@@ -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<MyCustomClass>),
typeof(StackLayoutProperties),
null,
defaultValueCreator: _ => new List<MyCustomClass>());

public static IList<MyCustomClass> GetStackLayoutCollection(BindableObject view) => (IList<MyCustomClass>)view.GetValue(StackLayoutCollectionProperty);
public static void SetStackLayoutCollection(BindableObject view, IList<MyCustomClass> value) => view.SetValue(StackLayoutCollectionProperty, value);
}
}