-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add regression test for #34713: Binding with Converter and x:DataType is compiled #34717
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
| xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
| xmlns:local="clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests" | ||
| x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Maui34713"> | ||
| <!-- NOTE: The converter is NOT defined in page resources. | ||
| It is expected to be in Application.Resources at runtime. | ||
| This tests the scenario from issue #34713. --> | ||
|
|
||
| <VerticalStackLayout x:DataType="local:Maui34713ViewModel"> | ||
| <Label x:Name="label0" Text="{Binding IsActive, Converter={StaticResource BoolToTextConverter}}" /> | ||
| <Label x:Name="label1" Text="{Binding Name}" /> | ||
| </VerticalStackLayout> | ||
| </ContentPage> |
203 changes: 203 additions & 0 deletions
203
src/Controls/tests/Xaml.UnitTests/Issues/Maui34713.xaml.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| using System; | ||
| using System.Globalization; | ||
| using Microsoft.Maui.ApplicationModel; | ||
| using Microsoft.Maui.Controls.Core.UnitTests; | ||
| using Microsoft.Maui.Dispatching; | ||
| using Microsoft.Maui.UnitTests; | ||
| using Xunit; | ||
|
|
||
| using static Microsoft.Maui.Controls.Xaml.UnitTests.MockSourceGenerator; | ||
|
|
||
| namespace Microsoft.Maui.Controls.Xaml.UnitTests; | ||
|
|
||
| public partial class Maui34713 : ContentPage | ||
| { | ||
| public Maui34713() | ||
| { | ||
| InitializeComponent(); | ||
| } | ||
|
|
||
| [Collection("Issue")] | ||
| public class Tests : IDisposable | ||
| { | ||
| public Tests() | ||
| { | ||
| DispatcherProvider.SetCurrent(new DispatcherProviderStub()); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| AppInfo.SetCurrent(null); | ||
| Application.SetCurrentApplication(null); | ||
| DispatcherProvider.SetCurrent(null); | ||
| } | ||
|
|
||
| const string SharedCs = @" | ||
| using System; | ||
| using System.Globalization; | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
| namespace Microsoft.Maui.Controls.Xaml.UnitTests | ||
| { | ||
| public class Maui34713ViewModel { public bool IsActive { get; set; } public string Name { get; set; } = """"; } | ||
| public class Maui34713BoolToTextConverter : IValueConverter { | ||
| public object Convert(object v, Type t, object p, CultureInfo c) => v is true ? ""Active"" : ""Inactive""; | ||
| public object ConvertBack(object v, Type t, object p, CultureInfo c) => throw new NotImplementedException(); | ||
| } | ||
| }"; | ||
|
|
||
| [Fact] | ||
| internal void SourceGenResolvesConverterAtCompileTime_ImplicitResources() | ||
| { | ||
| // When converter IS in page resources (implicit), source gen should | ||
| // resolve it at compile time - no runtime ProvideValue needed. | ||
| var xaml = @"<?xml version=""1.0"" encoding=""UTF-8""?> | ||
| <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" | ||
| xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" | ||
| xmlns:local=""clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"" | ||
| x:Class=""Microsoft.Maui.Controls.Xaml.UnitTests.Maui34713Test1""> | ||
| <ContentPage.Resources> | ||
| <local:Maui34713BoolToTextConverter x:Key=""BoolToTextConverter"" /> | ||
| </ContentPage.Resources> | ||
| <VerticalStackLayout x:DataType=""local:Maui34713ViewModel""> | ||
| <Label x:Name=""label0"" Text=""{Binding IsActive, Converter={StaticResource BoolToTextConverter}}"" /> | ||
| </VerticalStackLayout> | ||
| </ContentPage>"; | ||
|
|
||
| var cs = @" | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
| namespace Microsoft.Maui.Controls.Xaml.UnitTests | ||
| { | ||
| [XamlProcessing(XamlInflator.Runtime, true)] | ||
| public partial class Maui34713Test1 : ContentPage { public Maui34713Test1() { InitializeComponent(); } } | ||
| }" + SharedCs; | ||
|
|
||
| var result = CreateMauiCompilation() | ||
| .WithAdditionalSource(cs, hintName: "Maui34713Test1.xaml.cs") | ||
| .RunMauiSourceGenerator(new AdditionalXamlFile("Issues/Maui34713Test1.xaml", xaml, TargetFramework: "net10.0")); | ||
|
|
||
| var generated = result.GeneratedInitializeComponent(); | ||
|
|
||
| Assert.Contains("TypedBinding", generated, StringComparison.Ordinal); | ||
| // Converter should be resolved at compile time - no ProvideValue call | ||
| Assert.DoesNotContain(".ProvideValue(", generated, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| [Fact] | ||
| internal void SourceGenResolvesConverterAtCompileTime_ExplicitResourceDictionary() | ||
| { | ||
| // When converter IS in page resources (explicit RD), source gen should | ||
| // also resolve it at compile time. | ||
| var xaml = @"<?xml version=""1.0"" encoding=""UTF-8""?> | ||
| <ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui"" | ||
| xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" | ||
| xmlns:local=""clr-namespace:Microsoft.Maui.Controls.Xaml.UnitTests"" | ||
| x:Class=""Microsoft.Maui.Controls.Xaml.UnitTests.Maui34713Test2""> | ||
| <ContentPage.Resources> | ||
| <ResourceDictionary> | ||
| <local:Maui34713BoolToTextConverter x:Key=""BoolToTextConverter"" /> | ||
| </ResourceDictionary> | ||
| </ContentPage.Resources> | ||
| <VerticalStackLayout x:DataType=""local:Maui34713ViewModel""> | ||
| <Label x:Name=""label0"" Text=""{Binding IsActive, Converter={StaticResource BoolToTextConverter}}"" /> | ||
| </VerticalStackLayout> | ||
| </ContentPage>"; | ||
|
|
||
| var cs = @" | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
| namespace Microsoft.Maui.Controls.Xaml.UnitTests | ||
| { | ||
| [XamlProcessing(XamlInflator.Runtime, true)] | ||
| public partial class Maui34713Test2 : ContentPage { public Maui34713Test2() { InitializeComponent(); } } | ||
| }" + SharedCs; | ||
|
|
||
| var result = CreateMauiCompilation() | ||
| .WithAdditionalSource(cs, hintName: "Maui34713Test2.xaml.cs") | ||
| .RunMauiSourceGenerator(new AdditionalXamlFile("Issues/Maui34713Test2.xaml", xaml, TargetFramework: "net10.0")); | ||
|
|
||
| var generated = result.GeneratedInitializeComponent(); | ||
|
|
||
| Assert.Contains("TypedBinding", generated, StringComparison.Ordinal); | ||
| // Converter should be resolved at compile time - no ProvideValue call | ||
| Assert.DoesNotContain(".ProvideValue(", generated, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| [Fact] | ||
| internal void SourceGenCompilesBindingWithConverterToTypedBinding() | ||
| { | ||
| // When the converter is NOT in page resources, the binding should | ||
| // still be compiled into a TypedBinding. | ||
| var result = CreateMauiCompilation() | ||
| .WithAdditionalSource( | ||
| @"using System; | ||
| using System.Globalization; | ||
| using Microsoft.Maui.Controls; | ||
| using Microsoft.Maui.Controls.Xaml; | ||
|
|
||
| namespace Microsoft.Maui.Controls.Xaml.UnitTests; | ||
|
|
||
| [XamlProcessing(XamlInflator.Runtime, true)] | ||
| public partial class Maui34713 : ContentPage | ||
| { | ||
| public Maui34713() => InitializeComponent(); | ||
| } | ||
|
|
||
| public class Maui34713ViewModel | ||
| { | ||
| public bool IsActive { get; set; } | ||
| public string Name { get; set; } = string.Empty; | ||
| } | ||
|
|
||
| public class Maui34713BoolToTextConverter : IValueConverter | ||
| { | ||
| public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
| => value is true ? ""Active"" : ""Inactive""; | ||
|
|
||
| public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
| => throw new NotImplementedException(); | ||
| } | ||
| ") | ||
| .RunMauiSourceGenerator(typeof(Maui34713)); | ||
|
|
||
| var generated = result.GeneratedInitializeComponent(); | ||
|
|
||
| Assert.Contains("TypedBinding", generated, StringComparison.Ordinal); | ||
| Assert.Contains("Converter = extension.Converter", generated, StringComparison.Ordinal); | ||
| Assert.DoesNotContain("new global::Microsoft.Maui.Controls.Binding(", generated, StringComparison.Ordinal); | ||
| } | ||
|
|
||
| [Theory] | ||
| [XamlInflatorData] | ||
| internal void BindingWithConverterFromAppResourcesWorksCorrectly(XamlInflator inflator) | ||
| { | ||
| var mockApp = new MockApplication(); | ||
| mockApp.Resources.Add("BoolToTextConverter", new Maui34713BoolToTextConverter()); | ||
| Application.SetCurrentApplication(mockApp); | ||
|
|
||
| var page = new Maui34713(inflator); | ||
| page.BindingContext = new Maui34713ViewModel { IsActive = true, Name = "Test" }; | ||
|
|
||
| Assert.Equal("Active", page.label0.Text); | ||
| Assert.Equal("Test", page.label1.Text); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #nullable enable | ||
|
|
||
| public class Maui34713ViewModel | ||
| { | ||
| public bool IsActive { get; set; } | ||
| public string Name { get; set; } = string.Empty; | ||
| } | ||
|
|
||
| public class Maui34713BoolToTextConverter : IValueConverter | ||
| { | ||
| public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
| => value is true ? "Active" : "Inactive"; | ||
|
|
||
| public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
| => throw new NotImplementedException(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using Microsoft.Maui.Controls.Internals;appears to be unused in this file (no symbols from that namespace are referenced). With analyzers enabled, unused usings can fail the build; please remove it (or reference the intended type explicitly if it was meant to be used).