Skip to content

Commit

Permalink
Added UITest capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
pictos committed Oct 4, 2023
1 parent 69ec49d commit b8415ae
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Xunit;

namespace CommunityToolkit.Maui.DeviceTests;
[Collection("UITests")]
public abstract class UITests<T> : IAsyncLifetime
where T : Page
{
protected T CurrentPage { get; private set; } = default!;

protected IMauiContext MauiContext { get; private set; } = default!;

public async Task InitializeAsync()
{
Routing.RegisterRoute("uitests", typeof(T));

await Shell.Current.GoToAsync("uitests");

CurrentPage = (T)Shell.Current.CurrentPage;
MauiContext = CurrentPage.Handler!.MauiContext!;
if (CurrentPage.IsLoaded)
{
return;
}

var tcs = new TaskCompletionSource();
CurrentPage.Loaded += OnLoaded;

await Task.WhenAny(tcs.Task, Task.Delay(1000));

CurrentPage.Loaded -= OnLoaded;

Assert.True(CurrentPage.IsLoaded);

void OnLoaded(object? sender, EventArgs e)
{
CurrentPage.Loaded -= OnLoaded;
tcs.SetResult();
}
}

public async Task DisposeAsync()
{
CurrentPage = null!;

await Shell.Current.GoToAsync("..");

Routing.UnRegisterRoute("uitests");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,10 @@
<ProjectReference Include="..\..\CommunityToolkit.Maui.Core\CommunityToolkit.Maui.Core.csproj" />
<ProjectReference Include="..\..\CommunityToolkit.Maui\CommunityToolkit.Maui.csproj" />
</ItemGroup>

<ItemGroup>
<MauiXaml Update="Tests\UITests\StatusBarTestPage.xaml">
<Generator>MSBuild:Compile</Generator>
</MauiXaml>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,4 @@ public void SomeBehavior()

Assert.Equal(expectedColor, appliedColor);
}

[Fact]
public async Task ChangeStatusBar()
{
var expectedColor = Colors.Fuchsia;
var page = new ContentPage();
var behavior = new IconTintColorBehavior();

var tcs = new TaskCompletionSource();

page.Loaded += (s,e) => tcs.SetResult();

var cts = new CancellationTokenSource();
using var x = cts.Token.Register(() => tcs.SetCanceled());
cts.CancelAfter(10_000);

await tcs.Task;

page.Behaviors.Add(behavior);

behavior.TintColor = expectedColor;

var appliedColor = behavior.TintColor;

Assert.Equal(expectedColor, appliedColor);


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="CommunityToolkit.Maui.DeviceTests.Tests.UITests.StatusBarTestPage"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
Title="StatusBarTestPage">
<ContentPage.Behaviors>
<toolkit:StatusBarBehavior StatusBarColor="Fuchsia" />
</ContentPage.Behaviors>

<VerticalStackLayout>
<Label
HorizontalOptions="Center"
Text="Welcome to .NET MAUI!"
VerticalOptions="Center" />
</VerticalStackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CommunityToolkit.Maui.DeviceTests.Tests.UITests;

public partial class StatusBarTestPage : ContentPage
{
public StatusBarTestPage()
{
InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using CommunityToolkit.Maui.Behaviors;
using Xunit;

namespace CommunityToolkit.Maui.DeviceTests.Tests.UITests;
public sealed class StatusBarUITest : UITests<StatusBarTestPage>
{
[UIFact]
public void IsBehaviorAttached()
{
var behavior = CurrentPage.Behaviors.FirstOrDefault( x => x is StatusBarBehavior);

Assert.NotNull(behavior);
}

[UIFact]
public void GetTheColor()
{
var behavior = CurrentPage.Behaviors.FirstOrDefault(x => x is StatusBarBehavior) as StatusBarBehavior;

Assert.NotNull(behavior);

var color = behavior.StatusBarColor;

Assert.Equal(Colors.Fuchsia, color);
}
}

0 comments on commit b8415ae

Please sign in to comment.