-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
NameScopeTests.cs
115 lines (94 loc) · 4.3 KB
/
NameScopeTests.cs
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
111
112
113
114
115
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.UnitTests;
using NUnit.Framework;
using Microsoft.Maui.Dispatching;
namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
[TestFixture]
public class NameScopeTests : BaseTestFixture
{
[Test]
public void TopLevelObjectsHaveANameScope()
{
var xaml = @"
<View
xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" />";
var view = new CustomView().LoadFromXaml(xaml);
Assert.IsNotNull(Maui.Controls.Internals.NameScope.GetNameScope(view));
Assert.That(Maui.Controls.Internals.NameScope.GetNameScope(view), Is.TypeOf<Maui.Controls.Internals.NameScope>());
}
[Test]
public void NameScopeAreSharedWithChildren()
{
var xaml = @"
<StackLayout
xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" >
<Label />
<Label />
</StackLayout>";
var layout = new Controls.Compatibility.StackLayout().LoadFromXaml(xaml);
Assert.IsNotNull(Maui.Controls.Internals.NameScope.GetNameScope(layout));
Assert.That(Maui.Controls.Internals.NameScope.GetNameScope(layout), Is.TypeOf<Maui.Controls.Internals.NameScope>());
foreach (var child in layout.Children)
{
Assert.IsNull(Maui.Controls.Internals.NameScope.GetNameScope(child));
Assert.AreSame(Maui.Controls.Internals.NameScope.GetNameScope(layout), child.GetNameScope());
}
}
[Test]
public void DataTemplateChildrenDoesNotParticipateToParentNameScope()
{
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
var xaml = @"
<ListView
xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
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, ((Maui.Controls.Internals.INameScope)listview).FindByName("listview"));
Assert.IsNull(((Maui.Controls.Internals.INameScope)listview).FindByName("textcell"));
}
[Test]
public void ElementsCreatedFromDataTemplateHaveTheirOwnNameScope()
{
DispatcherProvider.SetCurrent(new DispatcherProviderStub());
var xaml = @"
<ListView
xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
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(Maui.Controls.Internals.NameScope.GetNameScope(listview));
Assert.That(Maui.Controls.Internals.NameScope.GetNameScope(listview), Is.TypeOf<Maui.Controls.Internals.NameScope>());
var cell0 = listview.ItemTemplate.CreateContent() as Element;
var cell1 = listview.ItemTemplate.CreateContent() as Element;
Assert.IsNotNull(Maui.Controls.Internals.NameScope.GetNameScope(cell0));
Assert.That(Maui.Controls.Internals.NameScope.GetNameScope(cell0), Is.TypeOf<Maui.Controls.Internals.NameScope>());
Assert.IsNotNull(Maui.Controls.Internals.NameScope.GetNameScope(cell1));
Assert.That(Maui.Controls.Internals.NameScope.GetNameScope(cell1), Is.TypeOf<Maui.Controls.Internals.NameScope>());
Assert.AreNotSame(Maui.Controls.Internals.NameScope.GetNameScope(listview), Maui.Controls.Internals.NameScope.GetNameScope(cell0));
Assert.AreNotSame(Maui.Controls.Internals.NameScope.GetNameScope(listview), Maui.Controls.Internals.NameScope.GetNameScope(cell1));
Assert.AreNotSame(Maui.Controls.Internals.NameScope.GetNameScope(cell0), Maui.Controls.Internals.NameScope.GetNameScope(cell1));
Assert.IsNull(((Maui.Controls.Internals.INameScope)listview).FindByName("textcell"));
Assert.NotNull(((Maui.Controls.Internals.INameScope)cell0).FindByName("textcell"));
Assert.NotNull(((Maui.Controls.Internals.INameScope)cell1).FindByName("textcell"));
Assert.AreNotSame(((Maui.Controls.Internals.INameScope)cell0).FindByName("textcell"), ((Maui.Controls.Internals.INameScope)cell1).FindByName("textcell"));
}
}
}