maui-linux/Xamarin.Forms.Xaml.UnitTests/TypeConverterTestsLegacy.cs

250 строки
7.5 KiB
C#
Исходник Обычный вид История

2016-03-22 23:02:25 +03:00
using System;
using NUnit.Framework;
using System.Xml;
using System.Globalization;
using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
public class FooConverter : TypeConverter
{
public override object ConvertFromInvariantString (string value)
{
return new Foo { Value = value };
}
}
public class BarConverter : TypeConverter
{
public override object ConvertFromInvariantString (string value)
{
return new Bar { Value = (string)value };
}
}
public class QuxConverter : TypeConverter
{
public override object ConvertFromInvariantString (string value)
{
return new Qux { Value = (string)value };
}
}
public class FooBarConverter : TypeConverter
{
public override object ConvertFromInvariantString (string value)
{
return new FooBar { Value = (string)value };
}
}
public class Foo {
public string Value { get; set; }
}
public class Bar {
public string Value { get; set; }
}
public class Baz {
public string Value { get; set; }
}
public class Qux {
public string Value { get; set; }
}
[TypeConverter (typeof(FooBarConverter))]
public class FooBar {
public string Value { get; set; }
}
public class Bindable : BindableObject
{
[TypeConverter (typeof(FooConverter))]
public Foo Foo { get; set; }
public static readonly BindableProperty BarProperty =
#pragma warning disable 618
2016-03-22 23:02:25 +03:00
BindableProperty.Create<Bindable, Bar> (w => w.Bar, default(Bar));
#pragma warning restore 618
2016-03-22 23:02:25 +03:00
[TypeConverter (typeof(BarConverter))]
public Bar Bar {
get { return (Bar)GetValue (BarProperty); }
set { SetValue (BarProperty, value); }
}
public Baz Baz { get; set; }
public static readonly BindableProperty QuxProperty =
#pragma warning disable 618
2016-03-22 23:02:25 +03:00
BindableProperty.CreateAttached<Bindable, Qux> (bindable => GetQux (bindable), default(Qux));
#pragma warning restore 618
2016-03-22 23:02:25 +03:00
[TypeConverter (typeof(QuxConverter))]
public static Qux GetQux (BindableObject bindable)
{
return (Qux)bindable.GetValue (QuxProperty);
}
public static void SetQux (BindableObject bindable, Qux value)
{
bindable.SetValue (QuxProperty, value);
}
public FooBar FooBar { get; set; }
}
internal class MockNameSpaceResolver : IXmlNamespaceResolver
{
public System.Collections.Generic.IDictionary<string, string> GetNamespacesInScope (XmlNamespaceScope scope)
{
throw new NotImplementedException ();
}
public string LookupNamespace (string prefix)
{
return "";
}
public string LookupPrefix (string namespaceName)
{
return "";
}
}
[TestFixture]
public class TypeConverterTestsLegacy : BaseTestFixture
{
[Test]
public void TestSetPropertyWithoutConverter()
2016-03-22 23:02:25 +03:00
{
var baz = new Baz();
var node = new ValueNode(baz, new MockNameSpaceResolver());
var bindable = new Bindable();
2016-03-22 23:02:25 +03:00
Assert.IsNull(bindable.Baz);
var rootNode = new XamlLoader.RuntimeRootNode(new XmlType("clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests", "Bindable", null), bindable, null) {
2016-03-22 23:02:25 +03:00
Properties = {
{ new XmlName (null, "Baz"), node },
}
};
var context = new HydrationContext { RootElement = new Label() };
rootNode.Accept(new CreateValuesVisitor(context), null);
node.Accept(new ApplyPropertiesVisitor(context), rootNode);
Assert.AreEqual(baz, bindable.Baz);
2016-03-22 23:02:25 +03:00
}
[Test]
public void TestFailOnMissingOrWrongConverter()
2016-03-22 23:02:25 +03:00
{
var node = new ValueNode("baz", new MockNameSpaceResolver());
var bindable = new Bindable();
2016-03-22 23:02:25 +03:00
Assert.IsNull(bindable.Baz);
var rootNode = new XamlLoader.RuntimeRootNode(new XmlType("clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests", "Bindable", null), bindable, null) {
2016-03-22 23:02:25 +03:00
Properties = {
{ new XmlName (null, "Baz"), node },
}
};
var context = new HydrationContext { RootElement = new Label() };
rootNode.Accept(new CreateValuesVisitor(context), null);
Assert.Throws<XamlParseException>(() => node.Accept(new ApplyPropertiesVisitor(context), rootNode));
2016-03-22 23:02:25 +03:00
}
[Test]
public void TestConvertNonBindableProperty()
2016-03-22 23:02:25 +03:00
{
var node = new ValueNode("foo", new MockNameSpaceResolver());
var bindable = new Bindable();
2016-03-22 23:02:25 +03:00
Assert.IsNull(bindable.Foo);
var rootNode = new XamlLoader.RuntimeRootNode(new XmlType("clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests", "Bindable", null), bindable, null) {
2016-03-22 23:02:25 +03:00
Properties = {
{ new XmlName (null, "Foo"), node },
}
};
var context = new HydrationContext { RootElement = new Label() };
rootNode.Accept(new CreateValuesVisitor(context), null);
node.Accept(new ApplyPropertiesVisitor(context), rootNode);
Assert.IsNotNull(bindable.Foo);
Assert.That(bindable.Foo, Is.TypeOf<Foo>());
Assert.AreEqual("foo", bindable.Foo.Value);
2016-03-22 23:02:25 +03:00
}
[Test]
public void TestConvertBindableProperty()
2016-03-22 23:02:25 +03:00
{
var node = new ValueNode("bar", new MockNameSpaceResolver());
var bindable = new Bindable();
2016-03-22 23:02:25 +03:00
Assert.IsNull(bindable.Bar);
var rootNode = new XamlLoader.RuntimeRootNode(new XmlType("clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests", "Bindable", null), bindable, null) {
2016-03-22 23:02:25 +03:00
Properties = {
{ new XmlName (null, "Bar"), node },
}
};
var context = new HydrationContext { RootElement = new Label() };
rootNode.Accept(new CreateValuesVisitor(context), null);
node.Accept(new ApplyPropertiesVisitor(context), rootNode);
Assert.IsNotNull(bindable.Bar);
Assert.That(bindable.Bar, Is.TypeOf<Bar>());
Assert.AreEqual("bar", bindable.Bar.Value);
2016-03-22 23:02:25 +03:00
}
[Test]
public void TestConvertAttachedBindableProperty()
2016-03-22 23:02:25 +03:00
{
var node = new ValueNode("qux", new MockNameSpaceResolver());
var bindable = new Bindable();
2016-03-22 23:02:25 +03:00
Assert.IsNull(Bindable.GetQux(bindable));
var rootNode = new XamlLoader.RuntimeRootNode(new XmlType("clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests", "Bindable", null), bindable, null) {
2016-03-22 23:02:25 +03:00
Properties = {
{ new XmlName ("clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests", "Bindable.Qux"), node },
}
};
var context = new HydrationContext { RootElement = new Label() };
rootNode.Accept(new CreateValuesVisitor(context), null);
node.Accept(new ApplyPropertiesVisitor(context), rootNode);
Assert.IsNotNull(Bindable.GetQux(bindable));
Assert.That(Bindable.GetQux(bindable), Is.TypeOf<Qux>());
Assert.AreEqual("qux", Bindable.GetQux(bindable).Value);
2016-03-22 23:02:25 +03:00
}
[Test]
public void TestConvertWithAttributeOnType()
2016-03-22 23:02:25 +03:00
{
var node = new ValueNode("foobar", new MockNameSpaceResolver());
var bindable = new Bindable();
2016-03-22 23:02:25 +03:00
Assert.IsNull(bindable.FooBar);
var rootNode = new XamlLoader.RuntimeRootNode(new XmlType("clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests", "Bindable", null), bindable, null) {
2016-03-22 23:02:25 +03:00
Properties = {
{ new XmlName (null, "FooBar"), node },
}
};
var context = new HydrationContext { RootElement = new Label() };
rootNode.Accept(new CreateValuesVisitor(context), null);
node.Accept(new ApplyPropertiesVisitor(context), rootNode);
2016-03-22 23:02:25 +03:00
Assert.IsNotNull(bindable.FooBar);
Assert.That(bindable.FooBar, Is.TypeOf<FooBar>());
Assert.AreEqual("foobar", bindable.FooBar.Value);
2016-03-22 23:02:25 +03:00
}
#if !WINDOWS_PHONE
[TestCase("en-GB"), TestCase("fr-FR")]
public void TestCultureOnThickness(string culture)
2016-03-22 23:02:25 +03:00
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(culture);
2016-03-22 23:02:25 +03:00
var xaml = @"<Page Padding=""1.1, 2""/>";
var page = new Page().LoadFromXaml(xaml);
Assert.AreEqual(new Thickness(1.1, 2), page.Padding);
2016-03-22 23:02:25 +03:00
}
#endif
2016-03-22 23:02:25 +03:00
}
}