using NUnit.Framework;
using Xamarin.Forms.Core.UnitTests;
namespace Xamarin.Forms.Xaml.UnitTests
{
[TestFixture]
public class NameScopeTests : BaseTestFixture
{
[Test]
public void TopLevelObjectsHaveANameScope ()
{
var xaml = @"
";
var view = new CustomView ().LoadFromXaml (xaml);
Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (view));
Assert.That (Forms.Internals.NameScope.GetNameScope (view), Is.TypeOf ());
}
[Test]
public void NameScopeAreSharedWithChildren ()
{
var xaml = @"
";
var layout = new StackLayout ().LoadFromXaml (xaml);
Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (layout));
Assert.That (Forms.Internals.NameScope.GetNameScope (layout), Is.TypeOf ());
foreach (var child in layout.Children) {
Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (child));
Assert.That (Forms.Internals.NameScope.GetNameScope (child), Is.TypeOf ());
Assert.AreSame (Forms.Internals.NameScope.GetNameScope (layout), Forms.Internals.NameScope.GetNameScope (child));
}
}
[Test]
public void DataTemplateChildrenDoesNotParticipateToParentNameScope ()
{
var xaml = @"
";
var listview = new ListView ();
listview.LoadFromXaml (xaml);
Assert.AreSame (listview, ((Forms.Internals.INameScope)listview).FindByName ("listview"));
Assert.IsNull (((Forms.Internals.INameScope)listview).FindByName ("textcell"));
}
[Test]
public void ElementsCreatedFromDataTemplateHaveTheirOwnNameScope ()
{
var xaml = @"
";
var listview = new ListView ();
listview.LoadFromXaml (xaml);
Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (listview));
Assert.That (Forms.Internals.NameScope.GetNameScope (listview), Is.TypeOf ());
var cell0 = listview.ItemTemplate.CreateContent () as Element;
var cell1 = listview.ItemTemplate.CreateContent () as Element;
Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (cell0));
Assert.That (Forms.Internals.NameScope.GetNameScope (cell0), Is.TypeOf ());
Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (cell1));
Assert.That (Forms.Internals.NameScope.GetNameScope (cell1), Is.TypeOf ());
Assert.AreNotSame (Forms.Internals.NameScope.GetNameScope (listview), Forms.Internals.NameScope.GetNameScope (cell0));
Assert.AreNotSame (Forms.Internals.NameScope.GetNameScope (listview), Forms.Internals.NameScope.GetNameScope (cell1));
Assert.AreNotSame (Forms.Internals.NameScope.GetNameScope (cell0), Forms.Internals.NameScope.GetNameScope (cell1));
Assert.IsNull (((Forms.Internals.INameScope)listview).FindByName ("textcell"));
Assert.NotNull (((Forms.Internals.INameScope)cell0).FindByName ("textcell"));
Assert.NotNull (((Forms.Internals.INameScope)cell1).FindByName ("textcell"));
Assert.AreNotSame (((Forms.Internals.INameScope)cell0).FindByName ("textcell"), ((Forms.Internals.INameScope)cell1).FindByName ("textcell"));
}
}
}