Skip to content

Commit be64f0a

Browse files
committed
- added additional tests
1 parent 375f20d commit be64f0a

File tree

2 files changed

+108
-17
lines changed

2 files changed

+108
-17
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.Maui.Controls;
8+
using Xunit.Abstractions;
9+
10+
namespace Microsoft.Maui.DeviceTests
11+
{
12+
public class FindVisualTreeElementInsideTestCase : IXunitSerializable
13+
{
14+
VisualElement _visualElement;
15+
public VisualElement VisualElement => _visualElement ??= CreateVisualElement();
16+
public VisualElement ViewToLocate { get; private set; }
17+
18+
public string TestCaseName { get; private set; }
19+
20+
public FindVisualTreeElementInsideTestCase() { }
21+
22+
public FindVisualTreeElementInsideTestCase(string testCaseName)
23+
{
24+
TestCaseName = testCaseName;
25+
}
26+
27+
public void Deserialize(IXunitSerializationInfo info)
28+
{
29+
TestCaseName = info.GetValue<string>(nameof(TestCaseName));
30+
}
31+
32+
public void Serialize(IXunitSerializationInfo info)
33+
{
34+
info.AddValue(nameof(TestCaseName), TestCaseName, typeof(string));
35+
}
36+
37+
public override string ToString()
38+
{
39+
return TestCaseName;
40+
}
41+
42+
private VisualElement CreateVisualElement()
43+
{
44+
switch (TestCaseName)
45+
{
46+
case "CollectionView":
47+
{
48+
var cv = new CollectionView();
49+
NestingView view = new NestingView();
50+
ViewToLocate = view;
51+
cv.ItemTemplate = new DataTemplate(() =>
52+
{
53+
return view;
54+
});
55+
cv.ItemsSource = new[] { 0 };
56+
return cv;
57+
}
58+
case "ContentView":
59+
{
60+
var contentView = new ContentView();
61+
NestingView view = new NestingView();
62+
ViewToLocate = view;
63+
contentView.ControlTemplate = new ControlTemplate(() =>
64+
{
65+
return view;
66+
});
67+
return contentView;
68+
}
69+
}
70+
71+
throw new Exception(String.Concat(TestCaseName, " ", "Not Found"));
72+
}
73+
}
74+
75+
public class FindVisualTreeElementInsideTestCases : IEnumerable<object[]>
76+
{
77+
private readonly List<object[]> _data = new()
78+
{
79+
new object[] { new FindVisualTreeElementInsideTestCase("CollectionView") },
80+
new object[] { new FindVisualTreeElementInsideTestCase("ContentView") }
81+
};
82+
83+
public IEnumerator<object[]> GetEnumerator()
84+
=> _data.GetEnumerator();
85+
86+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
87+
}
88+
}

src/Controls/tests/DeviceTests/Elements/VisualElementTreeTests.cs renamed to src/Controls/tests/DeviceTests/Elements/VisualElementTree/VisualElementTreeTests.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.Maui.Platform;
88
using Xunit;
99
using System.Collections.Generic;
10+
using ContentView = Microsoft.Maui.Controls.ContentView;
11+
using Microsoft.Maui.Controls.Handlers.Items;
1012
#if ANDROID || IOS || MACCATALYST
1113
using ShellHandler = Microsoft.Maui.Controls.Handlers.Compatibility.ShellRenderer;
1214
#endif
@@ -23,31 +25,18 @@ void SetupBuilder()
2325
{
2426
EnsureHandlerCreated(builder =>
2527
{
28+
builder.SetupShellHandlers();
29+
2630
builder.ConfigureMauiHandlers(handlers =>
2731
{
28-
handlers.AddHandler(typeof(Controls.Shell), typeof(ShellHandler));
2932
#if IOS || MACCATALYST
3033
handlers.AddHandler(typeof(Controls.NavigationPage), typeof(Controls.Handlers.Compatibility.NavigationRenderer));
3134
#else
3235
handlers.AddHandler(typeof(Controls.NavigationPage), typeof(NavigationViewHandler));
3336
#endif
34-
handlers.AddHandler<Layout, LayoutHandler>();
35-
handlers.AddHandler<Image, ImageHandler>();
36-
handlers.AddHandler<Label, LabelHandler>();
37-
handlers.AddHandler<Page, PageHandler>();
38-
handlers.AddHandler<Toolbar, ToolbarHandler>();
39-
handlers.AddHandler<MenuBar, MenuBarHandler>();
40-
handlers.AddHandler<MenuBarItem, MenuBarItemHandler>();
41-
handlers.AddHandler<MenuFlyoutItem, MenuFlyoutItemHandler>();
42-
handlers.AddHandler<MenuFlyoutSubItem, MenuFlyoutSubItemHandler>();
43-
handlers.AddHandler<Button, ButtonHandler>();
44-
handlers.AddHandler<ScrollView, ScrollViewHandler>();
4537
handlers.AddHandler<NestingView, NestingViewHandler>();
46-
#if WINDOWS
47-
handlers.AddHandler<ShellItem, ShellItemHandler>();
48-
handlers.AddHandler<ShellSection, ShellSectionHandler>();
49-
handlers.AddHandler<ShellContent, ShellContentHandler>();
50-
#endif
38+
handlers.AddHandler<ContentView, ContentViewHandler>();
39+
handlers.AddHandler<CollectionView, CollectionViewHandler>();
5140
});
5241
});
5342
}
@@ -203,5 +192,19 @@ await CreateHandlerAndAddToWindow<NestingViewHandler>(view, (handler) =>
203192
Assert.Null(foundTreeElement);
204193
});
205194
}
195+
196+
[Theory]
197+
[ClassData(typeof(FindVisualTreeElementInsideTestCases))]
198+
public async Task FindPlatformViewInsideView(FindVisualTreeElementInsideTestCase testCase)
199+
{
200+
SetupBuilder();
201+
202+
await CreateHandlerAndAddToWindow(testCase.VisualElement, () =>
203+
{
204+
var platformView = testCase.ViewToLocate.ToPlatform();
205+
var foundTreeElement = platformView.GetVisualTreeElement();
206+
Assert.Equal(testCase.ViewToLocate, foundTreeElement);
207+
});
208+
}
206209
}
207210
}

0 commit comments

Comments
 (0)