Skip to content

Commit 3188f57

Browse files
Update samples
1 parent cb50431 commit 3188f57

17 files changed

+88
-496
lines changed

samples/CommunityToolkit.Maui.Sample/MauiProgram.cs

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ static void RegisterViewsAndViewModels(in IServiceCollection services)
269269
services.AddPopup<CsharpBindingPopup, CsharpBindingPopupViewModel>();
270270
services.AddPopup<UpdatingPopup, UpdatingPopupViewModel>();
271271
services.AddPopup<XamlBindingPopup, XamlBindingPopupViewModel>();
272+
services.AddPopup<ReturnResultPopup>();
272273

273274
services.AddPopup<PopupContentView, PopupContentViewModel>();
274275
}

samples/CommunityToolkit.Maui.Sample/Views/Popups/ButtonPopup.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
<Setter Property="VerticalTextAlignment" Value="Center" />
2626
</Style>
2727
<Style x:Key="ConfirmButton" TargetType="Button">
28-
<Setter Property="VerticalOptions" Value="EndAndExpand" />
28+
<Setter Property="VerticalOptions" Value="End" />
2929
</Style>
3030
</ResourceDictionary>
3131
</VerticalStackLayout.Resources>

samples/CommunityToolkit.Maui.Sample/Views/Popups/MultipleButtonPopup.xaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
<Setter Property="TextColor" Value="Blue" />
3232
</Style>
3333
<Style x:Key="ButtonGroup" TargetType="HorizontalStackLayout">
34-
<Setter Property="VerticalOptions" Value="EndAndExpand" />
35-
<Setter Property="HorizontalOptions" Value="CenterAndExpand" />
34+
<Setter Property="VerticalOptions" Value="End" />
35+
<Setter Property="HorizontalOptions" Value="Center" />
3636
<Setter Property="Spacing" Value="20" />
3737
</Style>
3838
</ResourceDictionary>

samples/CommunityToolkit.Maui.Sample/Views/Popups/NoOutsideTapDismissPopup.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
<Button
3434
Clicked="Button_Clicked"
3535
Text="Close"
36-
VerticalOptions="EndAndExpand" />
36+
VerticalOptions="End" />
3737
</VerticalStackLayout>
3838
</mct:Popup>

samples/CommunityToolkit.Maui.Sample/Views/Popups/ReturnResultPopup.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<Setter Property="VerticalTextAlignment" Value="Center" />
2525
</Style>
2626
<Style x:Key="ConfirmButton" TargetType="Button">
27-
<Setter Property="VerticalOptions" Value="EndAndExpand" />
27+
<Setter Property="VerticalOptions" Value="End" />
2828
</Style>
2929
</ResourceDictionary>
3030
</VerticalStackLayout.Resources>

samples/CommunityToolkit.Maui.Sample/Views/Popups/ToggleSizePopup.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<Setter Property="VerticalTextAlignment" Value="Center" />
2727
</Style>
2828
<Style x:Key="ConfirmButton" TargetType="Button">
29-
<Setter Property="VerticalOptions" Value="EndAndExpand" />
29+
<Setter Property="VerticalOptions" Value="End" />
3030
</Style>
3131
</ResourceDictionary>
3232
</VerticalStackLayout.Resources>

samples/CommunityToolkit.Maui.Sample/Views/Popups/UpdatingPopup.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<Setter Property="VerticalTextAlignment" Value="Center" />
2929
</Style>
3030
<Style x:Key="ConfirmButton" TargetType="Button">
31-
<Setter Property="VerticalOptions" Value="EndAndExpand" />
31+
<Setter Property="VerticalOptions" Value="End" />
3232
<Setter Property="HorizontalOptions" Value="Center" />
3333
</Style>
3434

src/CommunityToolkit.Maui.Analyzers.UnitTests/CommunityToolkit.Maui.Analyzers.UnitTests.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="FluentAssertions" Version="7.0.0" />
13+
<PackageReference Include="FluentAssertions" Version="8.0.0" />
1414
<PackageReference Include="FluentAssertions.Analyzers" Version="0.34.1" />
1515
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.2" />
1616
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.1.2" />

src/CommunityToolkit.Maui.UnitTests/BaseHandlerTest.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CommunityToolkit.Maui.Core;
22
using CommunityToolkit.Maui.UnitTests.Mocks;
3+
using CommunityToolkit.Maui.Views;
34

45
namespace CommunityToolkit.Maui.UnitTests;
56

@@ -58,8 +59,7 @@ static void InitializeServicesAndSetMockApplication(in IReadOnlyList<Type> trans
5859
var mockPageViewModel = new MockPageViewModel();
5960
var mockPopup = new MockSelfClosingPopup(mockPageViewModel, new());
6061

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

6565
foreach (var service in transientServicesToRegister)
@@ -77,7 +77,5 @@ static void InitializeServicesAndSetMockApplication(in IReadOnlyList<Type> trans
7777

7878
application.Handler = new ApplicationHandlerStub();
7979
application.Handler.SetMauiContext(new HandlersContextStub(mauiApp.Services));
80-
81-
CreateElementHandler<MockPopupHandler>(mockPopup);
8280
}
8381
}

src/CommunityToolkit.Maui.UnitTests/CommunityToolkit.Maui.UnitTests.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="FluentAssertions" Version="7.0.0" />
12+
<PackageReference Include="FluentAssertions" Version="8.0.0" />
1313
<PackageReference Include="FluentAssertions.Analyzers" Version="0.34.1" />
1414
<PackageReference Include="Nito.AsyncEx" Version="5.1.2" />
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
1616
<PackageReference Include="xunit.v3" Version="1.0.1" />
1717
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1" PrivateAssets="All" />
1818
<PackageReference Include="coverlet.collector" Version="6.0.3" PrivateAssets="All" />
19-
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiPackageVersion)"/>
19+
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiPackageVersion)" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

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

-41
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.ComponentModel;
22
using CommunityToolkit.Maui.Core;
33
using CommunityToolkit.Maui.UnitTests.Mocks;
4+
using CommunityToolkit.Maui.UnitTests.Views;
45
using CommunityToolkit.Maui.Views;
6+
using FluentAssertions;
57
using Xunit;
68

79
namespace CommunityToolkit.Maui.UnitTests;
@@ -10,12 +12,9 @@ public class PopupServiceTests : BaseHandlerTest
1012
{
1113
public PopupServiceTests()
1214
{
13-
var page = new ContentPage();
14-
15-
static PopupServiceTests()
16-
{
15+
var page = new MockPage(new MockPageViewModel());
1716
var serviceCollection = new ServiceCollection();
18-
PopupService.AddTransientPopup<MockPopup, MockPageViewModel>(serviceCollection);
17+
PopupService.AddPopup<MockPopup, MockPageViewModel>(serviceCollection, ServiceLifetime.Transient);
1918
CreateViewHandler<MockPageHandler>(page);
2019

2120
Assert.NotNull(Application.Current);
@@ -29,7 +28,7 @@ public async Task ShowPopupAsyncWithNullOnPresentingShouldThrowArgumentNullExcep
2928

3029
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
3130
await Assert.ThrowsAsync<ArgumentNullException>(() =>
32-
popupService.ShowPopupAsync<INotifyPropertyChanged>(onPresenting: null, token: CancellationToken.None));
31+
popupService.ShowPopupAsync<INotifyPropertyChanged>(new PopupOptions(), CancellationToken.None));
3332
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
3433
}
3534

@@ -43,7 +42,7 @@ public async Task ShowPopupAsync_CancellationTokenExpired()
4342
// Ensure CancellationToken has expired
4443
await Task.Delay(100, CancellationToken.None);
4544

46-
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(token: cts.Token));
45+
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), cts.Token));
4746
}
4847

4948
[Fact(Timeout = (int)TestDuration.Short)]
@@ -56,19 +55,7 @@ public async Task ShowPopupAsync_CancellationTokenCanceled()
5655
// Ensure CancellationToken has expired
5756
await cts.CancelAsync();
5857

59-
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(token: cts.Token));
60-
}
61-
62-
[Fact(Timeout = (int)TestDuration.Short)]
63-
public async Task ShowPopupAsyncShouldThrowInvalidOperationExceptionWhenNoViewModelIsRegistered()
64-
{
65-
var popupInstance = new MockMismatchedPopup();
66-
var popupViewModel = new MockPageViewModel();
67-
68-
SetupTest(popupInstance, () => popupViewModel, out var popupService);
69-
70-
await Assert.ThrowsAsync<InvalidOperationException>(() => popupService.ShowPopupAsync<MockPageViewModel>(token: CancellationToken.None));
71-
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(cts.Token));
58+
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), cts.Token));
7259
}
7360

7461
[Fact(Timeout = (int)TestDuration.Medium)]
@@ -78,7 +65,7 @@ public async Task ShowPopupAsyncShouldValidateProperBindingContext()
7865
var popupInstance = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
7966
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
8067

81-
await popupService.ShowPopupAsync<MockPageViewModel>(token: CancellationToken.None);
68+
await popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), CancellationToken.None);
8269

8370
Assert.Same(popupInstance.BindingContext, popupViewModel);
8471
}
@@ -99,7 +86,7 @@ public async Task ShowPopupAsyncWithOnPresenting_CancellationTokenExpired()
9986
// Ensure CancellationToken has expired
10087
await Task.Delay(100, CancellationToken.None);
10188

102-
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(viewModel => viewModel.HasLoaded = true, token: cts.Token));
89+
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions<MockPageViewModel>(){OnOpened = viewModel => viewModel.HasLoaded = true}, cts.Token));
10390
}
10491

10592
[Fact(Timeout = (int)TestDuration.Short)]
@@ -112,7 +99,7 @@ public async Task ShowPopupAsyncWithOnPresenting_CancellationTokenCanceled()
11299
// Ensure CancellationToken has expired
113100
await cts.CancelAsync();
114101

115-
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(viewModel => viewModel.HasLoaded = true, token: cts.Token));
102+
await Assert.ThrowsAsync<TaskCanceledException>(() => popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions<MockPageViewModel>() { OnOpened = viewModel => viewModel.HasLoaded = true }, cts.Token));
116103
}
117104

118105
[Fact(Timeout = (int)TestDuration.Medium)]
@@ -121,7 +108,7 @@ public async Task ShowPopupAsyncWithOnPresentingShouldBeInvoked()
121108
var popupService = ServiceProvider.GetRequiredService<IPopupService>();
122109
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
123110

124-
await popupService.ShowPopupAsync<MockPageViewModel>(onPresenting: viewModel => viewModel.HasLoaded = true, token: CancellationToken.None);
111+
await popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions<MockPageViewModel>() { OnOpened = viewModel => viewModel.HasLoaded = true }, CancellationToken.None);
125112

126113
Assert.True(popupViewModel.HasLoaded);
127114
}
@@ -132,51 +119,24 @@ public async Task ShowPopupAsyncShouldReturnResultOnceClosed()
132119
var mockPopup = ServiceProvider.GetRequiredService<MockSelfClosingPopup>();
133120
var popupService = ServiceProvider.GetRequiredService<IPopupService>();
134121

135-
var result = await popupService.ShowPopupAsync<MockPageViewModel>(token: CancellationToken.None);
122+
var result = await popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions(), CancellationToken.None);
136123

137-
Assert.Same(expectedResult, result);
138124
Assert.Same(mockPopup.Result, result);
139125
}
140126

141-
[Fact]
142-
public void ShowPopupWithNullOnPresentingShouldThrowArgumentNullException()
143-
{
144-
var popupService = new PopupService(ServiceProvider, new MockDispatcherProvider());
145-
146-
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
147-
Assert.Throws<ArgumentNullException>(() => popupService.ShowPopup<INotifyPropertyChanged>(onPresenting: null));
148-
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
149-
}
150-
151-
[Fact]
152-
public void ShowPopupShouldThrowInvalidOperationExceptionWhenNoViewModelIsRegistered()
153-
{
154-
var popupInstance = new MockMismatchedPopup();
155-
var popupViewModel = new MockPageViewModel();
156-
157-
SetupTest(popupInstance, () => popupViewModel, out var popupService);
158-
159-
Assert.Throws<InvalidOperationException>(() => popupService.ShowPopup<MockPageViewModel>());
160-
}
161-
162127
[Fact]
163128
public void ShowPopupWithOnPresentingShouldBeInvoked()
164129
{
165130
var popupService = ServiceProvider.GetRequiredService<IPopupService>();
166131
var popupViewModel = ServiceProvider.GetRequiredService<MockPageViewModel>();
167132

168-
popupService.ShowPopup<MockPageViewModel>(onPresenting: viewModel => viewModel.HasLoaded = true);
133+
popupService.ShowPopupAsync<MockPageViewModel>(new PopupOptions<MockPageViewModel>() { OnOpened = viewModel => viewModel.HasLoaded = true }, CancellationToken.None);
169134

170135
Assert.True(popupViewModel.HasLoaded);
171136
}
172-
173-
sealed class UnregisteredViewModel : INotifyPropertyChanged
174-
{
175-
public event PropertyChangedEventHandler? PropertyChanged;
176-
}
177137
}
178138

179-
sealed class MockSelfClosingPopup: ContentView
139+
sealed class MockSelfClosingPopup : Popup
180140
{
181141
public MockSelfClosingPopup(MockPageViewModel viewModel, object? result = null)
182142
{
@@ -185,13 +145,4 @@ public MockSelfClosingPopup(MockPageViewModel viewModel, object? result = null)
185145
}
186146

187147
public new object? Result { get; }
188-
189-
internal override async void OnOpened()
190-
{
191-
base.OnOpened();
192-
193-
await Task.Delay(TimeSpan.FromMilliseconds(500));
194-
195-
Close(Result);
196-
}
197148
}

0 commit comments

Comments
 (0)