Skip to content

Commit 5513c8e

Browse files
committed
Added UITest capabilities
1 parent 69ec49d commit 5513c8e

File tree

8 files changed

+108
-46
lines changed

8 files changed

+108
-46
lines changed

Diff for: samples/NuGet.config

-9
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Xunit;
2+
3+
namespace CommunityToolkit.Maui.DeviceTests;
4+
[Collection("UITests")]
5+
public abstract class UITests<T> : IAsyncLifetime
6+
where T : Page
7+
{
8+
protected T CurrentPage { get; private set; } = default!;
9+
10+
protected IMauiContext MauiContext { get; private set; } = default!;
11+
12+
public async Task InitializeAsync()
13+
{
14+
Routing.RegisterRoute("uitests", typeof(T));
15+
16+
await Shell.Current.GoToAsync("uitests");
17+
18+
CurrentPage = (T)Shell.Current.CurrentPage;
19+
MauiContext = CurrentPage.Handler!.MauiContext!;
20+
if (CurrentPage.IsLoaded)
21+
{
22+
return;
23+
}
24+
25+
var tcs = new TaskCompletionSource();
26+
CurrentPage.Loaded += OnLoaded;
27+
28+
await Task.WhenAny(tcs.Task, Task.Delay(1000));
29+
30+
CurrentPage.Loaded -= OnLoaded;
31+
32+
Assert.True(CurrentPage.IsLoaded);
33+
34+
void OnLoaded(object? sender, EventArgs e)
35+
{
36+
CurrentPage.Loaded -= OnLoaded;
37+
tcs.SetResult();
38+
}
39+
}
40+
41+
public async Task DisposeAsync()
42+
{
43+
CurrentPage = null!;
44+
45+
await Shell.Current.GoToAsync("..");
46+
47+
Routing.UnRegisterRoute("uitests");
48+
}
49+
}

Diff for: src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,10 @@
6464
<ProjectReference Include="..\..\CommunityToolkit.Maui.Core\CommunityToolkit.Maui.Core.csproj" />
6565
<ProjectReference Include="..\..\CommunityToolkit.Maui\CommunityToolkit.Maui.csproj" />
6666
</ItemGroup>
67+
68+
<ItemGroup>
69+
<MauiXaml Update="Tests\UITests\StatusBarTestPage.xaml">
70+
<Generator>MSBuild:Compile</Generator>
71+
</MauiXaml>
72+
</ItemGroup>
6773
</Project>

Diff for: src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/BehaviorTest.cs

-28
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,4 @@ public void SomeBehavior()
2626

2727
Assert.Equal(expectedColor, appliedColor);
2828
}
29-
30-
[Fact]
31-
public async Task ChangeStatusBar()
32-
{
33-
var expectedColor = Colors.Fuchsia;
34-
var page = new ContentPage();
35-
var behavior = new IconTintColorBehavior();
36-
37-
var tcs = new TaskCompletionSource();
38-
39-
page.Loaded += (s,e) => tcs.SetResult();
40-
41-
var cts = new CancellationTokenSource();
42-
using var x = cts.Token.Register(() => tcs.SetCanceled());
43-
cts.CancelAfter(10_000);
44-
45-
await tcs.Task;
46-
47-
page.Behaviors.Add(behavior);
48-
49-
behavior.TintColor = expectedColor;
50-
51-
var appliedColor = behavior.TintColor;
52-
53-
Assert.Equal(expectedColor, appliedColor);
54-
55-
56-
}
5729
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage
3+
x:Class="CommunityToolkit.Maui.DeviceTests.Tests.UITests.StatusBarTestPage"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
7+
Title="StatusBarTestPage">
8+
<ContentPage.Behaviors>
9+
<toolkit:StatusBarBehavior StatusBarColor="Fuchsia" />
10+
</ContentPage.Behaviors>
11+
12+
<VerticalStackLayout>
13+
<Label
14+
HorizontalOptions="Center"
15+
Text="Welcome to .NET MAUI!"
16+
VerticalOptions="Center" />
17+
</VerticalStackLayout>
18+
</ContentPage>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace CommunityToolkit.Maui.DeviceTests.Tests.UITests;
2+
3+
public partial class StatusBarTestPage : ContentPage
4+
{
5+
public StatusBarTestPage()
6+
{
7+
InitializeComponent();
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using CommunityToolkit.Maui.Behaviors;
2+
using Xunit;
3+
4+
namespace CommunityToolkit.Maui.DeviceTests.Tests.UITests;
5+
public sealed class StatusBarUITest : UITests<StatusBarTestPage>
6+
{
7+
[UIFact]
8+
public void IsBehaviorAttached()
9+
{
10+
var behavior = CurrentPage.Behaviors.FirstOrDefault( x => x is StatusBarBehavior);
11+
12+
Assert.NotNull(behavior);
13+
}
14+
15+
[UIFact]
16+
public void GetTheColor()
17+
{
18+
var behavior = CurrentPage.Behaviors.FirstOrDefault(x => x is StatusBarBehavior) as StatusBarBehavior;
19+
20+
Assert.NotNull(behavior);
21+
22+
var color = behavior.StatusBarColor;
23+
24+
Assert.Equal(Colors.Fuchsia, color);
25+
}
26+
}

Diff for: src/NuGet.config

-9
This file was deleted.

0 commit comments

Comments
 (0)