From b8415ae1120312c33f3b401d23202c4e46f20f83 Mon Sep 17 00:00:00 2001 From: Pedro Jesus Date: Thu, 14 Sep 2023 21:13:58 -0300 Subject: [PATCH] Added UITest capabilities --- .../BaseUITest.cs | 49 +++++++++++++++++++ .../CommunityToolkit.Maui.DeviceTests.csproj | 6 +++ .../Tests/BehaviorTest.cs | 28 ----------- .../Tests/UITests/StatusBarTestPage.xaml | 18 +++++++ .../Tests/UITests/StatusBarTestPage.xaml.cs | 9 ++++ .../Tests/UITests/StatusBarUITest.cs | 26 ++++++++++ 6 files changed, 108 insertions(+), 28 deletions(-) create mode 100644 src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/BaseUITest.cs create mode 100644 src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarTestPage.xaml create mode 100644 src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarTestPage.xaml.cs create mode 100644 src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarUITest.cs diff --git a/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/BaseUITest.cs b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/BaseUITest.cs new file mode 100644 index 0000000000..73451eff3e --- /dev/null +++ b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/BaseUITest.cs @@ -0,0 +1,49 @@ +using Xunit; + +namespace CommunityToolkit.Maui.DeviceTests; +[Collection("UITests")] +public abstract class UITests : 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"); + } +} diff --git a/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests.csproj b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests.csproj index bf6cf83bcf..32a6421633 100644 --- a/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests.csproj +++ b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests.csproj @@ -64,4 +64,10 @@ + + + + MSBuild:Compile + + diff --git a/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/BehaviorTest.cs b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/BehaviorTest.cs index dd20a01551..b9c687c5af 100644 --- a/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/BehaviorTest.cs +++ b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/BehaviorTest.cs @@ -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); - - - } } diff --git a/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarTestPage.xaml b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarTestPage.xaml new file mode 100644 index 0000000000..ba76a332bc --- /dev/null +++ b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarTestPage.xaml @@ -0,0 +1,18 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarTestPage.xaml.cs b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarTestPage.xaml.cs new file mode 100644 index 0000000000..5043734c95 --- /dev/null +++ b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarTestPage.xaml.cs @@ -0,0 +1,9 @@ +namespace CommunityToolkit.Maui.DeviceTests.Tests.UITests; + +public partial class StatusBarTestPage : ContentPage +{ + public StatusBarTestPage() + { + InitializeComponent(); + } +} \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarUITest.cs b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarUITest.cs new file mode 100644 index 0000000000..43ae831970 --- /dev/null +++ b/src/CommunityToolkit.Maui.DeviceTests/CommunityToolkit.Maui.DeviceTests/Tests/UITests/StatusBarUITest.cs @@ -0,0 +1,26 @@ +using CommunityToolkit.Maui.Behaviors; +using Xunit; + +namespace CommunityToolkit.Maui.DeviceTests.Tests.UITests; +public sealed class StatusBarUITest : UITests +{ + [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); + } +}