diff --git a/src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs b/src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs index ced7ff0cd5..2828d7d6dd 100644 --- a/src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs +++ b/src/CommunityToolkit.Maui.UnitTests/BaseViewTest.cs @@ -62,7 +62,8 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service appBuilder.Services.AddTransientPopup(); appBuilder.Services.AddTransientPopup(); appBuilder.Services.AddTransientPopup(); - + appBuilder.Services.AddTransientPopup(); + appBuilder.Services.AddTransientPopup(); #endregion @@ -82,4 +83,6 @@ static void InitializeServicesAndSetMockApplication(out IServiceProvider service CreateViewHandler(shell); } + + protected sealed class CustomButton : Button; } \ No newline at end of file diff --git a/src/CommunityToolkit.Maui.UnitTests/Views/Popup/DefaultPopupSettingsTests.cs b/src/CommunityToolkit.Maui.UnitTests/Views/Popup/DefaultPopupSettingsTests.cs index ff0e8d39d4..f7a5618ae0 100644 --- a/src/CommunityToolkit.Maui.UnitTests/Views/Popup/DefaultPopupSettingsTests.cs +++ b/src/CommunityToolkit.Maui.UnitTests/Views/Popup/DefaultPopupSettingsTests.cs @@ -1,10 +1,11 @@ +#pragma warning disable CA1416 +using CommunityToolkit.Maui.UnitTests.Services; using CommunityToolkit.Maui.Views; using Xunit; namespace CommunityToolkit.Maui.UnitTests.Views; -#pragma warning disable CA1416 -public class DefaultPopupSettingsTests : BaseTest +public class DefaultPopupSettingsTests : BaseViewTest { [Fact] public void Popup_SetPopupDefaultsNotCalled_UsesPopupDefaults() @@ -97,5 +98,103 @@ public void View_SetPopupDefaultsCalled_UsesDefaultPopupSettings() Assert.Equal(defaultPopupSettings.VerticalOptions, popupBorder.VerticalOptions); Assert.Equal(defaultPopupSettings.HorizontalOptions, popupBorder.HorizontalOptions); } + + [Fact(Timeout = (int)TestDuration.Medium)] + public void PopupService_View_SetPopupDefaultsCalled_UsesDefaultPopupSettings() + { + // Arrange + var defaultPopupSettings = new DefaultPopupSettings + { + CanBeDismissedByTappingOutsideOfPopup = false, + BackgroundColor = Colors.Orange, + HorizontalOptions = LayoutOptions.End, + VerticalOptions = LayoutOptions.Start, + Margin = 72, + Padding = 4 + }; + + if (Application.Current?.Windows[0].Page is not Page page) + { + throw new InvalidOperationException("Page cannot be null"); + } + + var builder = MauiApp.CreateBuilder(); + builder.UseMauiCommunityToolkit(options => { options.SetPopupDefaults(defaultPopupSettings); }); + + // Act + var popupService = ServiceProvider.GetRequiredService(); + popupService.ShowPopup(page.Navigation); + + if (Application.Current.Windows[0].Page is not Shell { CurrentPage: PopupPage popupPage }) + { + Assert.Fail("Popup page not found"); + throw new InvalidOperationException("Popup page not found"); + } + + var popupBorder = popupPage.Content.PopupBorder; + var popup = (Popup)(popupBorder.Content ?? throw new InvalidOperationException("PopupBorder Content cannot be null")); + var underlyingContentView = (ContentView)popup; + + // Assert + Assert.Equal(defaultPopupSettings.BackgroundColor, popup.BackgroundColor); + Assert.Equal(defaultPopupSettings.CanBeDismissedByTappingOutsideOfPopup, popup.CanBeDismissedByTappingOutsideOfPopup); + Assert.Equal(defaultPopupSettings.Margin, popupBorder.Margin); + Assert.Equal(defaultPopupSettings.VerticalOptions, popupBorder.VerticalOptions); + Assert.Equal(defaultPopupSettings.HorizontalOptions, popupBorder.HorizontalOptions); + Assert.Equal(defaultPopupSettings.Padding, popup.Padding); + Assert.Equal(defaultPopupSettings.Padding, underlyingContentView.Padding); + Assert.Equal(Thickness.Zero, underlyingContentView.Margin); + Assert.Equal(LayoutOptions.Fill, underlyingContentView.HorizontalOptions); + Assert.Equal(LayoutOptions.Fill, underlyingContentView.VerticalOptions); + } + + [Fact(Timeout = (int)TestDuration.Medium)] + public void PopupService_Popup_SetPopupDefaultsCalled_UsesDefaultPopupSettings() + { + // Arrange + var defaultPopupSettings = new DefaultPopupSettings + { + CanBeDismissedByTappingOutsideOfPopup = true, + BackgroundColor = Colors.Orange, + HorizontalOptions = LayoutOptions.End, + VerticalOptions = LayoutOptions.Start, + Margin = 72, + Padding = 4 + }; + + if (Application.Current?.Windows[0].Page is not Page page) + { + throw new InvalidOperationException("Page cannot be null"); + } + + var builder = MauiApp.CreateBuilder(); + builder.UseMauiCommunityToolkit(options => { options.SetPopupDefaults(defaultPopupSettings); }); + + // Act + var popupService = ServiceProvider.GetRequiredService(); + popupService.ShowPopup(page.Navigation); + + if (Application.Current.Windows[0].Page is not Shell { CurrentPage: PopupPage popupPage }) + { + Assert.Fail("Popup page not found"); + throw new InvalidOperationException("Popup page not found"); + } + + var popupBorder = popupPage.Content.PopupBorder; + var popup = (Popup)(popupBorder.Content ?? throw new InvalidOperationException("PopupBorder Content cannot be null")); + var underlyingContentView = (ContentView)popup; + + // Assert + Assert.Equal(defaultPopupSettings.BackgroundColor, popup.BackgroundColor); + Assert.Equal(defaultPopupSettings.CanBeDismissedByTappingOutsideOfPopup, popup.CanBeDismissedByTappingOutsideOfPopup); + Assert.Equal(defaultPopupSettings.Margin, popupBorder.Margin); + Assert.Equal(defaultPopupSettings.VerticalOptions, popupBorder.VerticalOptions); + Assert.Equal(defaultPopupSettings.HorizontalOptions, popupBorder.HorizontalOptions); + Assert.Equal(defaultPopupSettings.Padding, popup.Padding); + Assert.Equal(defaultPopupSettings.Padding, underlyingContentView.Padding); + Assert.Equal(Thickness.Zero, underlyingContentView.Margin); + Assert.Equal(LayoutOptions.Fill, underlyingContentView.HorizontalOptions); + Assert.Equal(LayoutOptions.Fill, underlyingContentView.VerticalOptions); + } } #pragma warning restore CA1416 \ No newline at end of file diff --git a/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs b/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs index 820d6751df..7fdecb4b5a 100644 --- a/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs +++ b/src/CommunityToolkit.Maui/Views/Popup/Popup.shared.cs @@ -18,6 +18,7 @@ public Popup() HorizontalOptions = Options.DefaultPopupSettings.HorizontalOptions; VerticalOptions = Options.DefaultPopupSettings.VerticalOptions; BackgroundColor = Options.DefaultPopupSettings.BackgroundColor; + CanBeDismissedByTappingOutsideOfPopup = Options.DefaultPopupSettings.CanBeDismissedByTappingOutsideOfPopup; } /// @@ -34,25 +35,19 @@ public Popup() /// Gets or sets the margin between the and the edge of the window. /// [BindableProperty] - public new partial Thickness Margin { get; set; } = Options.DefaultPopupSettings.Margin; - - /// - /// Gets or sets the padding between the border and the content. - /// - [BindableProperty] - public new partial Thickness Padding { get; set; } = Options.DefaultPopupSettings.Padding; + public new partial Thickness Margin { get; set; } /// /// Gets or sets the horizontal position of the when displayed on screen. /// [BindableProperty] - public new partial LayoutOptions HorizontalOptions { get; set; } = Options.DefaultPopupSettings.HorizontalOptions; + public new partial LayoutOptions HorizontalOptions { get; set; } /// /// Gets or sets the vertical position of the when displayed on screen. /// [BindableProperty] - public new partial LayoutOptions VerticalOptions { get; set; } = Options.DefaultPopupSettings.VerticalOptions; + public new partial LayoutOptions VerticalOptions { get; set; } /// /> /// @@ -60,7 +55,16 @@ public Popup() /// On Android - when false the hardware back button is disabled. /// [BindableProperty] - public partial bool CanBeDismissedByTappingOutsideOfPopup { get; set; } = Options.DefaultPopupSettings.CanBeDismissedByTappingOutsideOfPopup; + public partial bool CanBeDismissedByTappingOutsideOfPopup { get; set; } + + /// + /// Gets or sets the padding between the border and the content. + /// + public new Thickness Padding + { + get => base.Padding; + set => base.Padding = value; + } /// /// Close the Popup. diff --git a/src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs b/src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs index 8da2c64896..32f6f9714e 100644 --- a/src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs +++ b/src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs @@ -282,21 +282,21 @@ sealed partial class HorizontalOptionsConverter : BaseConverterOneWay value == LayoutOptions.Fill ? Options.DefaultPopupSettings.HorizontalOptions : value; + public override LayoutOptions ConvertFrom(LayoutOptions value, CultureInfo? culture) => value == LayoutOptions.Fill ? DefaultConvertReturnValue : value; } sealed partial class VerticalOptionsConverter : BaseConverterOneWay { public override LayoutOptions DefaultConvertReturnValue { get; set; } = Options.DefaultPopupSettings.VerticalOptions; - public override LayoutOptions ConvertFrom(LayoutOptions value, CultureInfo? culture) => value == LayoutOptions.Fill ? Options.DefaultPopupSettings.VerticalOptions : value; + public override LayoutOptions ConvertFrom(LayoutOptions value, CultureInfo? culture) => value == LayoutOptions.Fill ? DefaultConvertReturnValue : value; } sealed partial class BackgroundColorConverter : BaseConverterOneWay { public override Color DefaultConvertReturnValue { get; set; } = Options.DefaultPopupSettings.BackgroundColor; - public override Color ConvertFrom(Color? value, CultureInfo? culture) => value ?? Options.DefaultPopupSettings.BackgroundColor; + public override Color ConvertFrom(Color? value, CultureInfo? culture) => value ?? DefaultConvertReturnValue; } } @@ -304,13 +304,13 @@ sealed partial class PaddingConverter : BaseConverterOneWay value == default ? Options.DefaultPopupSettings.Padding : value; + public override Thickness ConvertFrom(Thickness value, CultureInfo? culture) => value.IsEmpty || value.IsNaN ? DefaultConvertReturnValue : value; } sealed partial class MarginConverter : BaseConverterOneWay { public override Thickness DefaultConvertReturnValue { get; set; } = Options.DefaultPopupSettings.Margin; - public override Thickness ConvertFrom(Thickness value, CultureInfo? culture) => value == default ? Options.DefaultPopupSettings.Margin : value; + public override Thickness ConvertFrom(Thickness value, CultureInfo? culture) => value.IsEmpty || value.IsNaN ? DefaultConvertReturnValue : value; } } \ No newline at end of file