using NUnit.Framework;
using System.Linq;
using System;
using System.Collections.Generic;
using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
[ContentProperty ("Content")]
public class CustomView : View
{
public string NotBindable { get; set; }
public View Content { get; set; }
public MockFlags MockFlags { get; set; }
}
[ContentProperty ("Children")]
public class ViewWithChildrenContent : View
{
public ViewWithChildrenContent ()
{
Children = DefaultChildren = new ViewList ();
}
public ViewList DefaultChildren;
public ViewList Children { get; set; }
}
public class ViewList : List
{
}
public class ReverseConverter : IValueConverter
{
public static ReverseConverter Instance = new ReverseConverter ();
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var s = value as string;
if (s == null)
return value;
return new string (s.Reverse ().ToArray ());
}
public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var s = value as string;
if (s == null)
return value;
return new string (s.Reverse ().ToArray ());
}
}
public class Catalog
{
public static readonly BindableProperty MessageProperty =
#pragma warning disable 618
BindableProperty.CreateAttached (bindable => GetMessage (bindable), default(string),
#pragma warning restore 618
propertyChanged: (bindable, oldvalue, newvalue) => {
var label = bindable as Label;
if (label != null)
label.SetValue (Label.TextProperty, new string (newvalue.Reverse ().ToArray ()));
});
public static string GetMessage (BindableObject bindable)
{
return (string)bindable.GetValue (MessageProperty);
}
public static void SetMessage (BindableObject bindable, string value)
{
bindable.SetValue (MessageProperty, value);
}
}
[Flags]
public enum MockFlags
{
Foo = 1<<0,
Bar = 1<<1,
Baz = 1<<2,
Qux = 1<<3,
}
[TestFixture]
public class LoaderTests : BaseTestFixture
{
[Test]
public void TestRootName ()
{
var xaml = @"
";
var view = new CustomView ();
view.LoadFromXaml (xaml);
Assert.AreSame (view, ((Forms.Internals.INameScope)view).FindByName("customView"));
}
[Test]
public void TestFindByXName ()
{
var xaml = @"
";
var stacklayout = new StackLayout ();
stacklayout.LoadFromXaml (xaml);
var label = stacklayout.FindByName";
var view = new CustView ();
Assert.Throws (new XamlParseExceptionConstraint (5, 53), () => view.LoadFromXaml (xaml));
}
[Test]
public void TestConnectingEventOnMethodWithWrongSignature ()
{
var xaml = @"
";
var view = new CustView ();
Assert.Throws (new XamlParseExceptionConstraint (5, 53), () => view.LoadFromXaml (xaml));
}
public class CustEntry : Entry
{
public bool fired = false;
public void onValueChanged (object sender, TextChangedEventArgs e)
{
fired = true;
}
}
[Test]
public void TestEventWithCustomEventArgs ()
{
var xaml = @"
";
new CustEntry ().LoadFromXaml (xaml);
}
[Test]
public void TestEmptyTemplate ()
{
var xaml = @"
";
var page = new ContentPage ();
page.LoadFromXaml (xaml);
var template = page.Resources["datatemplate"]as Forms.DataTemplate;
Assert.Throws(() => template.CreateContent());
}
[Test]
public void TestBoolValue ()
{
var xaml = @"
";
var image = new Image ();
Assert.AreEqual (Image.IsOpaqueProperty.DefaultValue, image.IsOpaque);
image.LoadFromXaml (xaml);
Assert.AreEqual (true, image.IsOpaque);
}
[Test]
public void TestAttachedBP ()
{
var xaml = @"
2
";
var view = new View ().LoadFromXaml (xaml);
Assert.AreEqual (1, Grid.GetColumn (view));
Assert.AreEqual (2, Grid.GetRow (view));
}
[Test]
public void TestAttachedBPWithDifferentNS ()
{
//If this looks very similar to Vernacular, well... it's on purpose :)
var xaml = @"
";
var label = new Label ().LoadFromXaml (xaml);
Assert.AreEqual ("raboof", label.Text);
}
[Test]
public void TestBindOnAttachedBP ()
{
var xaml = @"
";
var label = new Label ().LoadFromXaml (xaml);
label.BindingContext = "foobar";
Assert.AreEqual ("raboof", label.Text);
}
[Test]
public void TestContentProperties ()
{
var xaml = @"
";
CustomView customView = null;
Assert.DoesNotThrow(()=> customView = new CustomView ().LoadFromXaml (xaml));
Assert.NotNull (customView.Content);
Assert.AreSame (customView.Content, ((Forms.Internals.INameScope)customView).FindByName("contentview"));
}
[Test]
public void TestCollectionContentProperties ()
{
var xaml = @"
";
var layout = new StackLayout ().LoadFromXaml (xaml);
Assert.AreEqual (2, layout.Children.Count);
Assert.AreEqual ("Foo", ((Label)(layout.Children [0])).Text);
Assert.AreEqual ("Bar", ((Label)(layout.Children [1])).Text);
}
[Test]
public void TestCollectionContentPropertiesWithSingleElement ()
{
var xaml = @"
";
var layout = new StackLayout ().LoadFromXaml (xaml);
Assert.AreEqual (1, layout.Children.Count);
Assert.AreEqual ("Foo", ((Label)(layout.Children [0])).Text);
}
[Test]
public void TestPropertiesWithContentProperties ()
{
var xaml = @"
1
";
var contentPage = new ContentPage ().LoadFromXaml (xaml);
Assert.AreEqual (1, Grid.GetRow (contentPage));
Assert.NotNull (contentPage.Content);
}
[Test]
public void LoadFromXamlResource ()
{
ContentView view = null;
Assert.DoesNotThrow (() => view = new CustomXamlView ());
Assert.NotNull (view);
Assert.That (view.Content, Is.TypeOf ());
Assert.AreEqual ("foobar", ((Label)view.Content).Text);
}
[Test]
public void ThrowOnMissingXamlResource ()
{
var view = new CustomView ();
Assert.Throws (new XamlParseExceptionConstraint (), () => view.LoadFromXaml (typeof(CustomView)));
}
[Test]
public void CreateNewChildrenCollection ()
{
var xaml = @"
";
ViewWithChildrenContent layout = null;
Assert.DoesNotThrow (() => layout = new ViewWithChildrenContent ().LoadFromXaml (xaml));
Assert.IsNotNull (layout);
Assert.AreNotSame (layout.DefaultChildren, layout.Children);
Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child0"), layout.Children);
Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child1"), layout.Children);
}
[Test]
public void AddChildrenToCollectionContentProperty ()
{
var xaml = @"
";
ViewWithChildrenContent layout = null;
Assert.DoesNotThrow (() => layout = new ViewWithChildrenContent ().LoadFromXaml (xaml));
Assert.IsNotNull (layout);
Assert.AreSame (layout.DefaultChildren, layout.Children);
Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child0"), layout.Children);
Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child1"), layout.Children);
}
[Test]
public void AddChildrenToExistingCollection ()
{
var xaml = @"
";
ViewWithChildrenContent layout = null;
Assert.DoesNotThrow (() => layout = new ViewWithChildrenContent ().LoadFromXaml (xaml));
Assert.IsNotNull (layout);
Assert.AreSame (layout.DefaultChildren, layout.Children);
Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child0"), layout.Children);
Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child1"), layout.Children);
}
[Test]
public void AddSingleChildToCollectionContentProperty ()
{
var xaml = @"
";
ViewWithChildrenContent layout = null;
Assert.DoesNotThrow (() => layout = new ViewWithChildrenContent ().LoadFromXaml (xaml));
Assert.IsNotNull (layout);
Assert.AreSame (layout.DefaultChildren, layout.Children);
Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child0"), layout.Children);
}
[Test]
public void FindResourceByName ()
{
var xaml = @"
";
var layout = new ContentPage ().LoadFromXaml (xaml);
Assert.True (layout.Resources.ContainsKey ("buttonKey"));
var resource = layout.FindByName