From 5d9560ce5d2ccdd175b7e883a2237346377dcd86 Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 26 Nov 2024 09:52:03 +0100 Subject: [PATCH 1/2] Update tests --- .../MarkupExpressionParserTests.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Controls/tests/Xaml.UnitTests/MarkupExpressionParserTests.cs b/src/Controls/tests/Xaml.UnitTests/MarkupExpressionParserTests.cs index f868b19b6934..5c55f7f5a462 100644 --- a/src/Controls/tests/Xaml.UnitTests/MarkupExpressionParserTests.cs +++ b/src/Controls/tests/Xaml.UnitTests/MarkupExpressionParserTests.cs @@ -386,7 +386,6 @@ public void OnPlatformExtension(string markup, string platform, int expected) [TestCase("{OnIdiom Phone=23, Tablet=25, Desktop=26, TV=30, Watch=10}", "Desktop", 26)] [TestCase("{OnIdiom Phone=23, Tablet=25, Desktop=26, TV=30, Watch=10}", "TV", 30)] [TestCase("{OnIdiom Phone=23, Tablet=25, Desktop=26, TV=30, Watch=10}", "Watch", 10)] - [TestCase("{OnIdiom Phone=23}", "Desktop", default(int))] public void OnIdiomExtension(string markup, string idiom, int expected) { mockDeviceInfo.Idiom = DeviceIdiom.Create(idiom); @@ -403,6 +402,40 @@ public void OnIdiomExtension(string markup, string idiom, int expected) Assert.AreEqual(expected, actual); } + [TestCase("{OnIdiom Phone=23}", "Desktop")] + public void OnIdiomExtensionMissingValue(string markup, string idiom) + { + mockDeviceInfo.Idiom = DeviceIdiom.Create(idiom); + + var serviceProvider = new Internals.XamlServiceProvider(null, null) + { + IXamlTypeResolver = typeResolver, + IProvideValueTarget = new MockValueProvider("foo", new object()) + { + TargetProperty = GetType().GetProperty(nameof(FontSize)) + } + }; + + Assert.Throws(() => (new MarkupExtensionParser()).ParseExpression(ref markup, serviceProvider)); + } + + [TestCase("{OnPlatform Android=23}", "iOS")] + public void OnPlatformExtensionMissingValue(string markup, string platform) + { + mockDeviceInfo.Platform = DevicePlatform.Create(platform); + + var serviceProvider = new Internals.XamlServiceProvider(null, null) + { + IXamlTypeResolver = typeResolver, + IProvideValueTarget = new MockValueProvider("foo", new object()) + { + TargetProperty = GetType().GetProperty(nameof(FontSize)) + } + }; + + Assert.Throws(() => (new MarkupExtensionParser()).ParseExpression(ref markup, serviceProvider)); + } + [TestCase("{Binding")] [TestCase("{Binding 'Foo}")] [TestCase("{Binding Foo, Converter={StaticResource Bar}")] From 1706a1102e07197f8dfd0e604caa0ca4c0b899ad Mon Sep 17 00:00:00 2001 From: Simon Rozsival Date: Tue, 26 Nov 2024 09:53:09 +0100 Subject: [PATCH 2/2] Throw when value is missing --- src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs | 5 +++-- .../src/Xaml/MarkupExtensions/OnPlatformExtension.cs | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs b/src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs index c92eef52a5c7..6dd27da64719 100644 --- a/src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs +++ b/src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs @@ -14,7 +14,6 @@ namespace Microsoft.Maui.Controls.Xaml typeof(IValueConverterProvider), typeof(IXmlLineInfoProvider), typeof(IConverterOptions)])] - [RequiresUnreferencedCode("The OnIdiomExtension is not trim safe. Use OnIdiom instead.")] public class OnIdiomExtension : IMarkupExtension { // See Device.Idiom @@ -61,7 +60,9 @@ public object ProvideValue(IServiceProvider serviceProvider) var value = GetValue(); if (value == null && propertyType.IsValueType) - return Activator.CreateInstance(propertyType); + { + throw new XamlParseException($"Missing value for idiom {DeviceInfo.Idiom} or Default", serviceProvider); + } if (Converter != null) return Converter.Convert(value, propertyType, ConverterParameter, CultureInfo.CurrentUICulture); diff --git a/src/Controls/src/Xaml/MarkupExtensions/OnPlatformExtension.cs b/src/Controls/src/Xaml/MarkupExtensions/OnPlatformExtension.cs index 2e3065b2cf4b..a9804092efdb 100644 --- a/src/Controls/src/Xaml/MarkupExtensions/OnPlatformExtension.cs +++ b/src/Controls/src/Xaml/MarkupExtensions/OnPlatformExtension.cs @@ -13,7 +13,6 @@ namespace Microsoft.Maui.Controls.Xaml typeof(IValueConverterProvider), typeof(IXmlLineInfoProvider), typeof(IConverterOptions)])] - [RequiresUnreferencedCode("The OnPlatformExtension is not trim safe. Use OnPlatform instead.")] public class OnPlatformExtension : IMarkupExtension { static object s_notset = new object(); @@ -81,7 +80,9 @@ public object ProvideValue(IServiceProvider serviceProvider) return bp.GetDefaultValue(targetObject as BindableObject); } if (propertyType.IsValueType) - return Activator.CreateInstance(propertyType); + { + throw new XamlParseException($"Missing value for platform {DeviceInfo.Platform} or Default", serviceProvider); + } return null; }