summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/NameScopeTests.cs
blob: b622da27811ad632218b6d90dfb6cbdb46d6fa43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using NUnit.Framework;

using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{

	[TestFixture]
	public class NameScopeTests : BaseTestFixture
	{
		[Test]
		public void TopLevelObjectsHaveANameScope ()
		{
			var xaml = @"
				<View 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" />";

			var view = new CustomView ().LoadFromXaml (xaml);

			Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (view));
			Assert.That (Forms.Internals.NameScope.GetNameScope (view), Is.TypeOf<Forms.Internals.NameScope> ());
		}

		[Test]
		public void NameScopeAreSharedWithChildren ()
		{
			var xaml = @"
				<StackLayout 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
					<Label />
					<Label />
				</StackLayout>";

			var layout = new StackLayout ().LoadFromXaml (xaml);

			Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (layout));
			Assert.That (Forms.Internals.NameScope.GetNameScope (layout), Is.TypeOf<Forms.Internals.NameScope> ());

			foreach (var child in layout.Children) {
				Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (child));
				Assert.That (Forms.Internals.NameScope.GetNameScope (child), Is.TypeOf<Forms.Internals.NameScope> ());
				Assert.AreSame (Forms.Internals.NameScope.GetNameScope (layout), Forms.Internals.NameScope.GetNameScope (child));
			}
		}

		[Test]
		public void DataTemplateChildrenDoesNotParticipateToParentNameScope ()
		{
			var xaml = @"
				<ListView
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				x:Name=""listview"">
					<ListView.ItemTemplate>
						<DataTemplate>
						    <TextCell Text=""{Binding name}"" x:Name=""textcell""/>
						</DataTemplate>
					</ListView.ItemTemplate>
				</ListView>";

			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 = @"
				<ListView
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				x:Name=""listview"">
					<ListView.ItemTemplate>
						<DataTemplate>
						    <TextCell Text=""{Binding name}"" x:Name=""textcell""/>
						</DataTemplate>
					</ListView.ItemTemplate>
				</ListView>";

			var listview = new ListView ();
			listview.LoadFromXaml (xaml);	
			Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (listview));
			Assert.That (Forms.Internals.NameScope.GetNameScope (listview), Is.TypeOf<Forms.Internals.NameScope> ());

			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<Forms.Internals.NameScope> ());
			Assert.IsNotNull (Forms.Internals.NameScope.GetNameScope (cell1));
			Assert.That (Forms.Internals.NameScope.GetNameScope (cell1), Is.TypeOf<Forms.Internals.NameScope> ());

			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"));

		}
	}
}