Skip to content

Commit ebc39f2

Browse files
Fix tests
1 parent 3188f57 commit ebc39f2

20 files changed

+266
-184
lines changed

samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementPage.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ async void DisplayPopup(object sender, EventArgs e)
266266
}
267267
};
268268

269-
await Navigation.ShowPopup(popup, new PopupOptions());
269+
await Navigation.ShowPopup(popup, new PopupOptions<Popup>());
270270
popupMediaElement.Stop();
271271
popupMediaElement.Handler?.DisconnectHandler();
272272
}

samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/MultiplePopupPage.xaml.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,48 @@ public MultiplePopupPage(MultiplePopupViewModel multiplePopupViewModel, IPopupSe
1919

2020
async void HandleSimplePopupButtonClicked(object sender, EventArgs e)
2121
{
22-
await popupService.ShowPopupAsync<SimplePopup>(new PopupOptions(), CancellationToken.None);
22+
await popupService.ShowPopupAsync(new PopupOptions<SimplePopup>(), CancellationToken.None);
2323
}
2424

2525
async void HandleButtonPopupButtonClicked(object sender, EventArgs e)
2626
{
27-
await popupService.ShowPopupAsync< ButtonPopup>(new PopupOptions(), CancellationToken.None);
27+
await popupService.ShowPopupAsync(new PopupOptions<ButtonPopup>(), CancellationToken.None);
2828
}
2929

3030
async void HandleMultipleButtonPopupButtonClicked(object sender, EventArgs e)
3131
{
32-
await popupService.ShowPopupAsync<MultipleButtonPopup>(new PopupOptions(), CancellationToken.None);
32+
await popupService.ShowPopupAsync(new PopupOptions<MultipleButtonPopup>(), CancellationToken.None);
3333
}
3434

3535
async void HandleNoOutsideTapDismissPopupClicked(object sender, EventArgs e)
3636
{
37-
await popupService.ShowPopupAsync<NoOutsideTapDismissPopup>(new PopupOptions(){CanBeDismissedByTappingOutsideOfPopup = false}, CancellationToken.None);
37+
await popupService.ShowPopupAsync(new PopupOptions<NoOutsideTapDismissPopup>(){CanBeDismissedByTappingOutsideOfPopup = false}, CancellationToken.None);
3838
}
3939

4040
async void HandleToggleSizePopupButtonClicked(object sender, EventArgs e)
4141
{
42-
await popupService.ShowPopupAsync<ToggleSizePopup>(new PopupOptions(), CancellationToken.None);
42+
await popupService.ShowPopupAsync(new PopupOptions<ToggleSizePopup>(), CancellationToken.None);
4343
}
4444

4545
async void HandleTransparentPopupButtonClicked(object sender, EventArgs e)
4646
{
47-
await popupService.ShowPopupAsync<TransparentPopup>(new PopupOptions(), CancellationToken.None);
47+
await popupService.ShowPopupAsync(new PopupOptions<TransparentPopup>(), CancellationToken.None);
4848
}
4949

5050
async void HandleOpenedEventSimplePopupButtonClicked(object sender, EventArgs e)
5151
{
52-
await popupService.ShowPopupAsync<OpenedEventSimplePopup>(new PopupOptions(), CancellationToken.None);
52+
await popupService.ShowPopupAsync(new PopupOptions<OpenedEventSimplePopup>(), CancellationToken.None);
5353
}
5454

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

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

6262
async void HandleXamlBindingPopupPopupButtonClicked(object sender, EventArgs e)
6363
{
64-
await popupService.ShowPopupAsync<XamlBindingPopup>(new PopupOptions(), CancellationToken.None);
64+
await popupService.ShowPopupAsync(new PopupOptions<XamlBindingPopup>(), CancellationToken.None);
6565
}
6666
}

samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/PopupLayoutAlignmentPage.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ async void ShowPopupButtonClicked(object sender, EventArgs e)
2020
HeightRequest = double.Parse(heightEntry.Text)
2121
};
2222

23-
await Navigation.ShowPopup(redBlueBoxPopup, new PopupOptions());
23+
await Navigation.ShowPopup(redBlueBoxPopup, new PopupOptions<RedBlueBoxPopup>());
2424
}
2525
}

samples/CommunityToolkit.Maui.Sample/Pages/Views/Popup/ShowPopupInOnAppearingPage.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ protected override async void OnAppearing()
2323
{
2424
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
2525
// Proves that we now support showing a popup before the platform is even ready.
26-
await popupService.ShowPopupAsync<ReturnResultPopup>(new PopupOptions(), cts.Token);
26+
await popupService.ShowPopupAsync(new PopupOptions<ReturnResultPopup>(), cts.Token);
2727
}
2828
}

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/CustomSizeAndPositionPopupViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public Task ExecuteShowButton(CancellationToken token)
9898
HeightRequest = Height
9999
};
100100

101-
return Shell.Current.Navigation.ShowPopup(popup, new PopupOptions());
101+
return Shell.Current.Navigation.ShowPopup(popup, new PopupOptions<FlowDirectionPopup>());
102102
}
103103

104104
static bool IsFlowDirectionSelectionValid(int flowDirectionSelection, int flowDirectionOptionsCount)

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/PopupAnchorViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ static async Task ShowPopup(View anchor)
1616
{
1717

1818
};
19-
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
19+
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<TransparentPopup>());
2020
}
2121
}

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/PopupPositionViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static async Task DisplayPopup(PopupPosition position)
1515
{
1616
var popup = new TransparentPopup();
1717

18-
await Page.Navigation.ShowPopup(popup, new PopupOptions());
18+
await Page.Navigation.ShowPopup(popup, new PopupOptions<TransparentPopup>());
1919
}
2020

2121
public enum PopupPosition

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/PopupSizingIssuesViewModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async Task OnShowPopup(Page page)
6363

6464
popup.Content = container;
6565

66-
await page.Navigation.ShowPopup(popup, new PopupOptions());
66+
await page.Navigation.ShowPopup(popup, new PopupOptions<Popup>());
6767
}
6868

6969
static Label GetContentLabel(in string text) => new()

samples/CommunityToolkit.Maui.Sample/ViewModels/Views/Popup/StylePopupViewModel.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,48 @@ public partial class StylePopupViewModel : BaseViewModel
1313
static async Task DisplayImplicitStylePopup()
1414
{
1515
var popup = new ImplicitStylePopup();
16-
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
16+
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<ImplicitStylePopup>());
1717
}
1818

1919
[RelayCommand]
2020
static async Task DisplayExplicitStylePopup()
2121
{
2222
var popup = new ExplicitStylePopup();
23-
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
23+
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<ExplicitStylePopup>());
2424
}
2525

2626
[RelayCommand]
2727
static async Task DisplayDynamicStylePopup()
2828
{
2929
var popup = new DynamicStylePopup();
30-
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
30+
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<DynamicStylePopup>());
3131
}
3232

3333
[RelayCommand]
3434
static async Task DisplayApplyToDerivedTypesPopup()
3535
{
3636
var popup = new ApplyToDerivedTypesPopup();
37-
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
37+
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<ApplyToDerivedTypesPopup>());
3838
}
3939

4040
[RelayCommand]
4141
static async Task DisplayStyleInheritancePopup()
4242
{
4343
var popup = new StyleInheritancePopup();
44-
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
44+
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<StyleInheritancePopup>());
4545
}
4646

4747
[RelayCommand]
4848
static async Task DisplayDynamicStyleInheritancePopup()
4949
{
5050
var popup = new DynamicStyleInheritancePopup();
51-
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
51+
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<DynamicStyleInheritancePopup>());
5252
}
5353

5454
[RelayCommand]
5555
static async Task DisplayStyleClassPopup()
5656
{
5757
var popup = new StyleClassPopup();
58-
await MainPage.Navigation.ShowPopup(popup, new PopupOptions());
58+
await MainPage.Navigation.ShowPopup(popup, new PopupOptions<StyleClassPopup>());
5959
}
6060
}

samples/CommunityToolkit.Maui.Sample/Views/Popups/OpenedEventSimplePopup.xaml.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public partial class OpenedEventSimplePopup
99
public OpenedEventSimplePopup()
1010
{
1111
InitializeComponent();
12-
OnOpened = async () =>
12+
OnOpened += async (s,e) =>
1313
{
1414
await Task.Delay(TimeSpan.FromSeconds(1));
1515

src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static void InitializeServicesAndSetMockApplication(in IReadOnlyList<Type> trans
5959
var mockPageViewModel = new MockPageViewModel();
6060
var mockPopup = new MockSelfClosingPopup(mockPageViewModel, new());
6161

62-
PopupService.AddPopup(mockPopup, mockPageViewModel, appBuilder.Services, ServiceLifetime.Transient);
62+
PopupService.AddPopup(mockPopup, mockPageViewModel, appBuilder.Services, ServiceLifetime.Singleton);
6363
#endregion
6464

6565
foreach (var service in transientServicesToRegister)

src/CommunityToolkit.Maui.UnitTests/Mocks/MockPageViewModel.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace CommunityToolkit.Maui.UnitTests.Mocks;
1+
using CommunityToolkit.Maui.Core;
2+
3+
namespace CommunityToolkit.Maui.UnitTests.Mocks;
24

35
public class MockPageViewModel : BindableObject
46
{

src/CommunityToolkit.Maui.UnitTests/Views/Popup/PopupServiceTests.cs

+48-20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using CommunityToolkit.Maui.UnitTests.Views;
55
using CommunityToolkit.Maui.Views;
66
using FluentAssertions;
7+
using Microsoft.Maui.Dispatching;
78
using Xunit;
89

910
namespace CommunityToolkit.Maui.UnitTests;
@@ -13,23 +14,16 @@ public class PopupServiceTests : BaseHandlerTest
1314
public PopupServiceTests()
1415
{
1516
var page = new MockPage(new MockPageViewModel());
16-
var serviceCollection = new ServiceCollection();
17-
PopupService.AddPopup<MockPopup, MockPageViewModel>(serviceCollection, ServiceLifetime.Transient);
18-
CreateViewHandler<MockPageHandler>(page);
19-
2017
Assert.NotNull(Application.Current);
2118
Application.Current.Windows[0].Page = page;
2219
}
2320

2421
[Fact]
25-
public async Task ShowPopupAsyncWithNullOnPresentingShouldThrowArgumentNullException()
22+
public async Task ShowPopupAsyncWithNotRegisteredServiceShouldThrowInvalidOperationException()
2623
{
2724
var popupService = ServiceProvider.GetRequiredService<IPopupService>();
2825

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

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

45-
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), cts.Token));
39+
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>(), cts.Token));
4640
}
4741

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

58-
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), cts.Token));
52+
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>(), cts.Token));
5953
}
6054

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

68-
await popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), CancellationToken.None);
62+
await popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>(), CancellationToken.None);
6963

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

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

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

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

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

111-
await popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions<MockPageViewModel>() { OnOpened = viewModel => viewModel.HasLoaded = true }, CancellationToken.None);
105+
await popupService.ShowPopupAsync(new PopupOptions<MockPageViewModel>()
106+
{
107+
OnOpened = viewModel => viewModel.HasLoaded = true
108+
}, CancellationToken.None);
112109

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

122-
var result = await popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), CancellationToken.None);
119+
var result = await popupService.ShowPopupAsync<MockPageViewModel, object>(new PopupOptions<MockPageViewModel>(), CancellationToken.None);
123120

124-
Assert.Same(mockPopup.Result, result);
121+
Assert.Same(mockPopup.Result, result.Result);
125122
}
126123

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

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

135132
Assert.True(popupViewModel.HasLoaded);
136133
}
137134
}
138135

139-
sealed class MockSelfClosingPopup : Popup
136+
sealed class MockSelfClosingPopup : Popup<object?>
140137
{
141138
public MockSelfClosingPopup(MockPageViewModel viewModel, object? result = null)
142139
{
143140
BindingContext = viewModel;
144141
Result = result;
142+
OnOpened += MockSelfClosingPopup_OnOpened;
145143
}
146144

147-
public new object? Result { get; }
145+
void MockSelfClosingPopup_OnOpened(object? sender, EventArgs e)
146+
{
147+
var timer = Dispatcher.CreateTimer();
148+
timer.Interval = TimeSpan.FromMilliseconds(500);
149+
timer.Tick += (s, e) => Close(Result);
150+
timer.Start();
151+
}
152+
153+
public object? Result { get; }
154+
}
155+
156+
public class MockPopup : Popup
157+
{
158+
}
159+
160+
class PopupViewModel : INotifyPropertyChanged
161+
{
162+
public event PropertyChangedEventHandler? PropertyChanged;
163+
164+
public Color? Color
165+
{
166+
get;
167+
set
168+
{
169+
if (!Equals(value, field))
170+
{
171+
field = value;
172+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Color)));
173+
}
174+
}
175+
} = new();
148176
}

0 commit comments

Comments
 (0)