Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavAntonyuk committed Jan 15, 2025
1 parent 3188f57 commit c1488d0
Show file tree
Hide file tree
Showing 20 changed files with 277 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ async void DisplayPopup(object sender, EventArgs e)
}
};

await Navigation.ShowPopup(popup, new PopupOptions());
await Navigation.ShowPopup(popup, new PopupOptions<Popup>());
popupMediaElement.Stop();
popupMediaElement.Handler?.DisconnectHandler();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,48 @@ public MultiplePopupPage(MultiplePopupViewModel multiplePopupViewModel, IPopupSe

async void HandleSimplePopupButtonClicked(object sender, EventArgs e)
{
await popupService.ShowPopupAsync<SimplePopup>(new PopupOptions(), CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<SimplePopup>(), CancellationToken.None);
}

async void HandleButtonPopupButtonClicked(object sender, EventArgs e)
{
await popupService.ShowPopupAsync< ButtonPopup>(new PopupOptions(), CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<ButtonPopup>(), CancellationToken.None);
}

async void HandleMultipleButtonPopupButtonClicked(object sender, EventArgs e)
{
await popupService.ShowPopupAsync<MultipleButtonPopup>(new PopupOptions(), CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<MultipleButtonPopup>(), CancellationToken.None);
}

async void HandleNoOutsideTapDismissPopupClicked(object sender, EventArgs e)
{
await popupService.ShowPopupAsync<NoOutsideTapDismissPopup>(new PopupOptions(){CanBeDismissedByTappingOutsideOfPopup = false}, CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<NoOutsideTapDismissPopup>(){CanBeDismissedByTappingOutsideOfPopup = false}, CancellationToken.None);
}

async void HandleToggleSizePopupButtonClicked(object sender, EventArgs e)
{
await popupService.ShowPopupAsync<ToggleSizePopup>(new PopupOptions(), CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<ToggleSizePopup>(), CancellationToken.None);
}

async void HandleTransparentPopupButtonClicked(object sender, EventArgs e)
{
await popupService.ShowPopupAsync<TransparentPopup>(new PopupOptions(), CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<TransparentPopup>(), CancellationToken.None);
}

async void HandleOpenedEventSimplePopupButtonClicked(object sender, EventArgs e)
{
await popupService.ShowPopupAsync<OpenedEventSimplePopup>(new PopupOptions(), CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<OpenedEventSimplePopup>(), CancellationToken.None);
}

async void HandleReturnResultPopupButtonClicked(object sender, EventArgs e)
{
var result = await popupService.ShowPopupAsync< ReturnResultPopup, bool>(new PopupOptions(), CancellationToken.None);
var result = await popupService.ShowPopupAsync<ReturnResultPopup, bool>(new PopupOptions<ReturnResultPopup>(), CancellationToken.None);

await DisplayAlert("Pop Result Returned", $"Result: {result.Result}", "OK");
}

async void HandleXamlBindingPopupPopupButtonClicked(object sender, EventArgs e)
{
await popupService.ShowPopupAsync<XamlBindingPopup>(new PopupOptions(), CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<XamlBindingPopup>(), CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ async void ShowPopupButtonClicked(object sender, EventArgs e)
HeightRequest = double.Parse(heightEntry.Text)
};

await Navigation.ShowPopup(redBlueBoxPopup, new PopupOptions());
await Navigation.ShowPopup(redBlueBoxPopup, new PopupOptions<RedBlueBoxPopup>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ protected override async void OnAppearing()
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
// Proves that we now support showing a popup before the platform is even ready.
await popupService.ShowPopupAsync<ReturnResultPopup>(new PopupOptions(), cts.Token);
await popupService.ShowPopupAsync(new PopupOptions<ReturnResultPopup>(), cts.Token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public Task ExecuteShowButton(CancellationToken token)
HeightRequest = Height
};

return Shell.Current.Navigation.ShowPopup(popup, new PopupOptions());
return Shell.Current.Navigation.ShowPopup(popup, new PopupOptions<FlowDirectionPopup>());
}

static bool IsFlowDirectionSelectionValid(int flowDirectionSelection, int flowDirectionOptionsCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ static async Task ShowPopup(View anchor)
{

};
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<TransparentPopup>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ static async Task DisplayPopup(PopupPosition position)
{
var popup = new TransparentPopup();

await Page.Navigation.ShowPopup(popup, new PopupOptions());
await Page.Navigation.ShowPopup(popup, new PopupOptions<TransparentPopup>());
}

public enum PopupPosition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async Task OnShowPopup(Page page)

popup.Content = container;

await page.Navigation.ShowPopup(popup, new PopupOptions());
await page.Navigation.ShowPopup(popup, new PopupOptions<Popup>());
}

static Label GetContentLabel(in string text) => new()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,48 @@ public partial class StylePopupViewModel : BaseViewModel
static async Task DisplayImplicitStylePopup()
{
var popup = new ImplicitStylePopup();
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<ImplicitStylePopup>());
}

[RelayCommand]
static async Task DisplayExplicitStylePopup()
{
var popup = new ExplicitStylePopup();
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<ExplicitStylePopup>());
}

[RelayCommand]
static async Task DisplayDynamicStylePopup()
{
var popup = new DynamicStylePopup();
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<DynamicStylePopup>());
}

[RelayCommand]
static async Task DisplayApplyToDerivedTypesPopup()
{
var popup = new ApplyToDerivedTypesPopup();
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<ApplyToDerivedTypesPopup>());
}

[RelayCommand]
static async Task DisplayStyleInheritancePopup()
{
var popup = new StyleInheritancePopup();
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<StyleInheritancePopup>());
}

[RelayCommand]
static async Task DisplayDynamicStyleInheritancePopup()
{
var popup = new DynamicStyleInheritancePopup();
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<DynamicStyleInheritancePopup>());
}

[RelayCommand]
static async Task DisplayStyleClassPopup()
{
var popup = new StyleClassPopup();
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<StyleClassPopup>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class OpenedEventSimplePopup
public OpenedEventSimplePopup()
{
InitializeComponent();
OnOpened = async () =>
OnOpened += async (s,e) =>
{
await Task.Delay(TimeSpan.FromSeconds(1));

Expand Down
2 changes: 1 addition & 1 deletion src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static void InitializeServicesAndSetMockApplication(in IReadOnlyList<Type> trans
var mockPageViewModel = new MockPageViewModel();
var mockPopup = new MockSelfClosingPopup(mockPageViewModel, new());

PopupService.AddPopup(mockPopup, mockPageViewModel, appBuilder.Services, ServiceLifetime.Transient);
PopupService.AddPopup(mockPopup, mockPageViewModel, appBuilder.Services, ServiceLifetime.Singleton);
#endregion

foreach (var service in transientServicesToRegister)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace CommunityToolkit.Maui.UnitTests.Mocks;
using CommunityToolkit.Maui.Core;

namespace CommunityToolkit.Maui.UnitTests.Mocks;

public class MockPageViewModel : BindableObject
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using CommunityToolkit.Maui.UnitTests.Views;
using CommunityToolkit.Maui.Views;
using FluentAssertions;
using Microsoft.Maui.Dispatching;
using Xunit;

namespace CommunityToolkit.Maui.UnitTests;
Expand All @@ -13,23 +14,16 @@ public class PopupServiceTests : BaseHandlerTest
public PopupServiceTests()
{
var page = new MockPage(new MockPageViewModel());
var serviceCollection = new ServiceCollection();
PopupService.AddPopup<MockPopup, MockPageViewModel>(serviceCollection, ServiceLifetime.Transient);
CreateViewHandler<MockPageHandler>(page);

Assert.NotNull(Application.Current);
Application.Current.Windows[0].Page = page;
}

[Fact]
public async Task ShowPopupAsyncWithNullOnPresentingShouldThrowArgumentNullException()
public async Task ShowPopupAsyncWithNotRegisteredServiceShouldThrowInvalidOperationException()
{
var popupService = ServiceProvider.GetRequiredService<IPopupService>();

#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
await Assert.ThrowsAsync<ArgumentNullException>(() =>
popupService.ShowPopupAsync<INotifyPropertyChanged>(new PopupOptions(), CancellationToken.None));
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
await Assert.ThrowsAsync<InvalidOperationException>(() => popupService.ShowPopupAsync(new PopupOptions<INotifyPropertyChanged>(), CancellationToken.None));
}

[Fact(Timeout = (int)TestDuration.Short)]
Expand All @@ -42,7 +36,7 @@ public async Task ShowPopupAsync_CancellationTokenExpired()
// Ensure CancellationToken has expired
await Task.Delay(100, CancellationToken.None);

await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), cts.Token));
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>(), cts.Token));
}

[Fact(Timeout = (int)TestDuration.Short)]
Expand All @@ -55,7 +49,7 @@ public async Task ShowPopupAsync_CancellationTokenCanceled()
// Ensure CancellationToken has expired
await cts.CancelAsync();

await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), cts.Token));
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>(), cts.Token));
}

[Fact(Timeout = (int)TestDuration.Medium)]
Expand All @@ -65,7 +59,7 @@ public async Task ShowPopupAsyncShouldValidateProperBindingContext()
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();

await popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>(), CancellationToken.None);

Assert.Same(popupInstance.BindingContext, popupViewModel);
}
Expand All @@ -86,7 +80,7 @@ public async Task ShowPopupAsyncWithOnPresenting_CancellationTokenExpired()
// Ensure CancellationToken has expired
await Task.Delay(100, CancellationToken.None);

await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions<MockPageViewModel>(){OnOpened = viewModel => viewModel.HasLoaded = true}, cts.Token));
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>(){OnOpened = viewModel => viewModel.HasLoaded = true}, cts.Token));
}

[Fact(Timeout = (int)TestDuration.Short)]
Expand All @@ -99,7 +93,7 @@ public async Task ShowPopupAsyncWithOnPresenting_CancellationTokenCanceled()
// Ensure CancellationToken has expired
await cts.CancelAsync();

await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions<MockPageViewModel>() { OnOpened = viewModel => viewModel.HasLoaded = true }, cts.Token));
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>() { OnOpened = viewModel => viewModel.HasLoaded = true }, cts.Token));
}

[Fact(Timeout = (int)TestDuration.Medium)]
Expand All @@ -108,7 +102,10 @@ public async Task ShowPopupAsyncWithOnPresentingShouldBeInvoked()
var popupService = ServiceProvider.GetRequiredService<IPopupService>();
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();

await popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions<MockPageViewModel>() { OnOpened = viewModel => viewModel.HasLoaded = true }, CancellationToken.None);
await popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>()
{
OnOpened = viewModel => viewModel.HasLoaded = true
}, CancellationToken.None);

Assert.True(popupViewModel.HasLoaded);
}
Expand All @@ -119,9 +116,9 @@ public async Task ShowPopupAsyncShouldReturnResultOnceClosed()
var mockPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
var popupService = ServiceProvider.GetRequiredService<IPopupService>();

var result = await popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), CancellationToken.None);
var result = await popupService.ShowPopupAsync<MockPageViewModel, object>(new PopupOptions<MockPageViewModel>(), CancellationToken.None);

Assert.Same(mockPopup.Result, result);
Assert.Same(mockPopup.Result, result.Result);
}

[Fact]
Expand All @@ -130,19 +127,50 @@ public void ShowPopupWithOnPresentingShouldBeInvoked()
var popupService = ServiceProvider.GetRequiredService<IPopupService>();
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();

popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions<MockPageViewModel>() { OnOpened = viewModel => viewModel.HasLoaded = true }, CancellationToken.None);
popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>() { OnOpened = viewModel => viewModel.HasLoaded = true }, CancellationToken.None);

Assert.True(popupViewModel.HasLoaded);
}
}

sealed class MockSelfClosingPopup : Popup
sealed class MockSelfClosingPopup : Popup<object?>
{
public MockSelfClosingPopup(MockPageViewModel viewModel, object? result = null)
{
BindingContext = viewModel;
Result = result;
OnOpened += MockSelfClosingPopup_OnOpened;
}

public new object? Result { get; }
void MockSelfClosingPopup_OnOpened(object? sender, EventArgs e)
{
var timer = Dispatcher.CreateTimer();
timer.Interval = TimeSpan.FromMilliseconds(500);
timer.Tick += (s, e) => Close(Result);
timer.Start();
}

public object? Result { get; }
}

public class MockPopup : Popup
{
}

class PopupViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;

public Color? Color
{
get;
set
{
if (!Equals(value, field))
{
field = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Color)));
}
}
} = new();
}
Loading

0 comments on commit c1488d0

Please sign in to comment.