diff --git a/src/CommunityToolkit.Maui.UnitTests/Behaviors/EventToCommandBehaviorGenericTests.cs b/src/CommunityToolkit.Maui.UnitTests/Behaviors/EventToCommandBehaviorGenericTests.cs index 443eaf3c9..9dd4a3b68 100644 --- a/src/CommunityToolkit.Maui.UnitTests/Behaviors/EventToCommandBehaviorGenericTests.cs +++ b/src/CommunityToolkit.Maui.UnitTests/Behaviors/EventToCommandBehaviorGenericTests.cs @@ -1,6 +1,7 @@ using System.Reflection; using CommunityToolkit.Maui.Behaviors; using CommunityToolkit.Maui.Converters; +using CommunityToolkit.Maui.UnitTests.Mocks; using Xunit; namespace CommunityToolkit.Maui.UnitTests.Behaviors; @@ -43,7 +44,7 @@ public void NoExceptionIfAttachedToPage() [Fact] public void NoExceptionWhenTheEventArgsAreNotNull() { - var vm = new ViewModelCoffe(); + var vm = new ViewModelCoffee(); var behavior = new EventToCommandBehavior { EventName = nameof(ListView.ItemTapped), @@ -65,7 +66,7 @@ public void NoExceptionWhenTheEventArgsAreNotNull() [Fact] public void NoExceptionWhenTheEventArgsAreNotNull_InheritedType() { - var vm = new ViewModelCoffe(); + var vm = new ViewModelCoffee(); var behavior = new EventToCommandBehavior { EventName = nameof(ListView.ItemTapped), @@ -87,7 +88,7 @@ public void NoExceptionWhenTheEventArgsAreNotNull_InheritedType() [Fact] public void ParameterOfTypeInt() { - var vm = new ViewModelCoffe(); + var vm = new ViewModelCoffee(); var behavior = new EventToCommandBehavior { EventName = nameof(ListView.ItemTapped), @@ -103,7 +104,7 @@ public void ParameterOfTypeInt() [Fact] public void NoExceptionWhenTheSelectedItemIsNull() { - var vm = new ViewModelCoffe(); + var vm = new ViewModelCoffee(); var behavior = new EventToCommandBehavior { EventName = nameof(ListView.ItemTapped), @@ -120,6 +121,27 @@ public void NoExceptionWhenTheSelectedItemIsNull() Assert.Null(vm.CoffeeName); } + [Fact] + public void HappilyUsesIValueConverterImplementation() + { + var vm = new ViewModelCoffee(); + var convertedValue = default(object); + var cappuccino = new Coffee(); + + var behavior = new EventToCommandBehavior + { + EventName = nameof(ListView.ItemTapped), + EventArgsConverter = new MockValueConverter((value) => cappuccino), + Command = new Command((parameter) => convertedValue = parameter) + }; + + var nullArgs = new object?[] { null, null }; + + TriggerEventToCommandBehavior(behavior, nullArgs); + + Assert.Equal(cappuccino, convertedValue); + } + static void TriggerEventToCommandBehavior(EventToCommandBehavior eventToCommand, object?[] args) { var method = eventToCommand.GetType().GetMethod("OnTriggerHandled", BindingFlags.Instance | BindingFlags.NonPublic); @@ -141,13 +163,13 @@ public class Coffee public string Image { get; set; } = string.Empty; } - public class ViewModelCoffe + public class ViewModelCoffee { public Command SelectedCommand { get; set; } public string? CoffeeName { get; set; } - public ViewModelCoffe() + public ViewModelCoffee() { SelectedCommand = new Command(Selected); } diff --git a/src/CommunityToolkit.Maui.UnitTests/Mocks/MockValueConverter.cs b/src/CommunityToolkit.Maui.UnitTests/Mocks/MockValueConverter.cs new file mode 100644 index 000000000..d55b23451 --- /dev/null +++ b/src/CommunityToolkit.Maui.UnitTests/Mocks/MockValueConverter.cs @@ -0,0 +1,23 @@ +using System.Globalization; + +namespace CommunityToolkit.Maui.UnitTests.Mocks; + +class MockValueConverter : IValueConverter +{ + readonly Func returnValue; + + public MockValueConverter(Func returnValue) + { + this.returnValue = returnValue; + } + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return returnValue.Invoke(value); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return returnValue.Invoke(value); + } +} \ No newline at end of file diff --git a/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehavior.shared.cs b/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehavior.shared.cs index 29c6acf42..37b9c99ad 100644 --- a/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehavior.shared.cs +++ b/src/CommunityToolkit.Maui/Behaviors/EventToCommandBehavior.shared.cs @@ -31,7 +31,7 @@ public class EventToCommandBehavior : BaseBehavior /// Backing BindableProperty for the property. /// public static readonly BindableProperty EventArgsConverterProperty = - BindableProperty.Create(nameof(EventArgsConverter), typeof(ICommunityToolkitValueConverter), typeof(EventToCommandBehavior)); + BindableProperty.Create(nameof(EventArgsConverter), typeof(IValueConverter), typeof(EventToCommandBehavior)); readonly MethodInfo eventHandlerMethodInfo = typeof(EventToCommandBehavior).GetTypeInfo()?.GetDeclaredMethod(nameof(OnTriggerHandled)) ?? throw new InvalidOperationException($"Cannot find method {nameof(OnTriggerHandled)}"); @@ -69,9 +69,9 @@ public class EventToCommandBehavior : BaseBehavior /// /// An optional that can be used to convert values, associated with the event configured with , to values passed into the . This is a bindable property. /// - public ICommunityToolkitValueConverter? EventArgsConverter + public IValueConverter? EventArgsConverter { - get => (ICommunityToolkitValueConverter?)GetValue(EventArgsConverterProperty); + get => (IValueConverter?)GetValue(EventArgsConverterProperty); set => SetValue(EventArgsConverterProperty, value); }