Skip to content

Commit d0f70aa

Browse files
[XSG] make sure RD are properly inflated, and loaded
this fixes the latest known failing test with XSG. 🎉
1 parent 58944a1 commit d0f70aa

File tree

8 files changed

+129
-10
lines changed

8 files changed

+129
-10
lines changed

src/Controls/src/SourceGen/CodeBehindCodeWriter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ public static string GenerateXamlCodeBehind(XamlProjectItemForCB? xamlItem, Comp
7171
var rootSymbol = compilation.GetTypeByMetadataName($"{rootClrNamespace}.{rootType}");
7272
bool alreadyHasXamlCompilationAttribute = rootSymbol?.GetAttributes().Any(a => a.AttributeClass != null && a.AttributeClass.Equals(compilation.GetTypeByMetadataName("Microsoft.Maui.Controls.Xaml.XamlCompilationAttribute")!, SymbolEqualityComparer.Default)) ?? false;
7373

74-
var generateInflatorSwitch = compilation.AssemblyName == "Microsoft.Maui.Controls.Xaml.UnitTests";
74+
var generateInflatorSwitch = compilation.AssemblyName == "Microsoft.Maui.Controls.Xaml.UnitTests" && !generateDefaultCtor;
7575
var xamlInflators = projItem.Inflator;
76-
76+
7777
//if there's only the XamlC inflator, prevent non-assigned errors
7878
if (xamlInflators == XamlInflator.XamlC)
7979
sb.AppendLine("#pragma warning disable CS0649");

src/Controls/src/SourceGen/InitializeComponentCodeWriter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,9 @@ PrePost newblock() =>
6868
if (rootType == null)
6969
goto exit;
7070

71-
var genSwitch = compilation.AssemblyName == "Microsoft.Maui.Controls.Xaml.UnitTests";
71+
var generatedDefaultCtor = rootType.Constructors.Any(c => c.Parameters.Length == 0 && c.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, compilation.GetTypeByMetadataName("System.CodeDom.Compiler.GeneratedCodeAttribute"))));
72+
73+
var genSwitch = compilation.AssemblyName == "Microsoft.Maui.Controls.Xaml.UnitTests" && !generatedDefaultCtor;
7274
var xamlInflators = xamlItem.ProjectItem.Inflator;
7375

7476
var generate = (xamlInflators & XamlInflator.SourceGen) == XamlInflator.SourceGen;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ResourceDictionary
3+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
xmlns:local="using:Microsoft.Maui.Controls.Xaml.UnitTests">
6+
<Color x:Key="AccentColor">#FF4B14</Color>
7+
<Color x:Key="BlackOpacityColor">#99253748</Color>
8+
<Color x:Key="BlackTextColor">#253748</Color>
9+
<Color x:Key="BackgroundColor">#F8F8F8</Color>
10+
<Color x:Key="PinkColor">#ED0241</Color>
11+
<Color x:Key="GrayColor">#ACB1B4</Color>
12+
<Color x:Key="DarkColor">#203446</Color>
13+
<Color x:Key="WhiteColor">#FFFFFF</Color>
14+
<Color x:Key="GreenColor">#368F95</Color>
15+
16+
<Color x:Key="Primary">#FF96F3</Color>
17+
<Color x:Key="PrimaryDark">#1976D2</Color>
18+
<Color x:Key="Accent">#96d1ff</Color>
19+
<Color x:Key="LightBackgroundColor">#FAFAFA</Color>
20+
<Color x:Key="DarkBackgroundColor">#C0C0C0</Color>
21+
<Color x:Key="MediumGrayTextColor">#4d4d4d</Color>
22+
<Color x:Key="LightTextColor">#999999</Color>
23+
24+
<Style TargetType="local:Gh7531" x:Key="style">
25+
<Setter Property="BackgroundColor" Value="HotPink"/>
26+
</Style>
27+
</ResourceDictionary>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ResourceDictionary
3+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
xmlns:local="using:Microsoft.Maui.Controls.Xaml.UnitTests">
6+
<Color x:Key="AccentColor">#FF4B14</Color>
7+
<Color x:Key="BlackOpacityColor">#99253748</Color>
8+
<Color x:Key="BlackTextColor">#253748</Color>
9+
<Color x:Key="BackgroundColor">#F8F8F8</Color>
10+
<Color x:Key="PinkColor">#ED0241</Color>
11+
<Color x:Key="GrayColor">#ACB1B4</Color>
12+
<Color x:Key="DarkColor">#203446</Color>
13+
<Color x:Key="WhiteColor">#FFFFFF</Color>
14+
<Color x:Key="GreenColor">#368F95</Color>
15+
16+
<Color x:Key="Primary">#FF96F3</Color>
17+
<Color x:Key="PrimaryDark">#1976D2</Color>
18+
<Color x:Key="Accent">#96d1ff</Color>
19+
<Color x:Key="LightBackgroundColor">#FAFAFA</Color>
20+
<Color x:Key="DarkBackgroundColor">#C0C0C0</Color>
21+
<Color x:Key="MediumGrayTextColor">#4d4d4d</Color>
22+
<Color x:Key="LightTextColor">#999999</Color>
23+
24+
<Style TargetType="local:Gh7531" x:Key="style">
25+
<Setter Property="BackgroundColor" Value="HotPink"/>
26+
</Style>
27+
</ResourceDictionary>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ResourceDictionary
3+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
xmlns:local="using:Microsoft.Maui.Controls.Xaml.UnitTests">
6+
<Color x:Key="AccentColor">#FF4B14</Color>
7+
<Color x:Key="BlackOpacityColor">#99253748</Color>
8+
<Color x:Key="BlackTextColor">#253748</Color>
9+
<Color x:Key="BackgroundColor">#F8F8F8</Color>
10+
<Color x:Key="PinkColor">#ED0241</Color>
11+
<Color x:Key="GrayColor">#ACB1B4</Color>
12+
<Color x:Key="DarkColor">#203446</Color>
13+
<Color x:Key="WhiteColor">#FFFFFF</Color>
14+
<Color x:Key="GreenColor">#368F95</Color>
15+
16+
<Color x:Key="Primary">#FF96F3</Color>
17+
<Color x:Key="PrimaryDark">#1976D2</Color>
18+
<Color x:Key="Accent">#96d1ff</Color>
19+
<Color x:Key="LightBackgroundColor">#FAFAFA</Color>
20+
<Color x:Key="DarkBackgroundColor">#C0C0C0</Color>
21+
<Color x:Key="MediumGrayTextColor">#4d4d4d</Color>
22+
<Color x:Key="LightTextColor">#999999</Color>
23+
24+
<Style TargetType="local:Gh7531" x:Key="style">
25+
<Setter Property="BackgroundColor" Value="HotPink"/>
26+
</Style>
27+
</ResourceDictionary>

src/Controls/tests/Xaml.UnitTests/Controls.Xaml.UnitTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
<!-- We can't yet disable the namescope generation, ConstraintTypeConverter and ReferenceTypeConverter need to be ported to sourcegen
1515
<DefineConstants>$(DefineConstants);_MAUIXAML_SG_NAMESCOPE_DISABLE</DefineConstants>
1616
-->
17-
<DefineConstants>$(DefineConstants);FIXME_BEFORE_PUBLIC_RELEASE</DefineConstants>
1817

1918
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
2019
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>

src/Controls/tests/Xaml.UnitTests/ResourceDictionaryWithSource.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<ResourceDictionary Source="./SharedResourceDictionary.xaml" x:Key="relURI"/>
99
<ResourceDictionary Source="/SharedResourceDictionary.xaml" x:Key="absURI"/>
1010
<ResourceDictionary Source="SharedResourceDictionary.xaml" x:Key="shortURI"/>
11+
<ResourceDictionary Source="./AppResources/Colors.rt.xaml" x:Key="Colors.rt" />
12+
<ResourceDictionary Source="./AppResources/Colors.sgen.xaml" x:Key="Colors.sgen" />
13+
<ResourceDictionary Source="./AppResources/Colors.xc.xaml" x:Key="Colors.xc" />
1114
<ResourceDictionary Source="./AppResources/Colors.rtxc.xaml" x:Key="Colors" />
1215
<ResourceDictionary Source="./AppResources/CompiledColors.rtxc.xaml" x:Key="CompiledColors" />
1316
<ResourceDictionary Source="/AppResources/Colors.rtxc.xaml;assembly=Microsoft.Maui.Controls.Xaml.UnitTests" x:Key="inCurrentAssembly" />

src/Controls/tests/Xaml.UnitTests/ResourceDictionaryWithSource.xaml.cs

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,51 @@ public void RelativeAndAbsoluteURI([Values] XamlInflator inflator)
3232
}
3333

3434
[Test]
35-
public void XRIDIsGeneratedForRDWithoutCodeBehind()
35+
public void CanLoadInflatedResources([Values] XamlInflator from, [Values] XamlInflator rd)
3636
{
37+
var layout = new ResourceDictionaryWithSource(from);
38+
var key = "Colors";
39+
switch (rd)
40+
{
41+
case XamlInflator.Runtime:
42+
key = "Colors.rt";
43+
break;
44+
case XamlInflator.SourceGen:
45+
key = "Colors.sgen";
46+
break;
47+
case XamlInflator.XamlC:
48+
key = "Colors.xc";
49+
break;
50+
}
51+
var rdLoaded = layout.Resources[key] as ResourceDictionary;
52+
Assert.That(rdLoaded, Is.Not.Null);
53+
Assert.That(rdLoaded["MediumGrayTextColor"], Is.TypeOf<Color>());
54+
Assert.That(rdLoaded["MediumGrayTextColor"] as Color, Is.EqualTo(Color.Parse("#ff4d4d4d")));
55+
}
56+
57+
[Test]
58+
public void XRIDIsGeneratedForRDWithoutCodeBehind([Values] XamlInflator rd)
59+
{
60+
var path = "AppResources/Colors.xaml";
61+
switch (rd)
62+
{
63+
case XamlInflator.Runtime:
64+
path = "AppResources/Colors.rt.xaml";
65+
break;
66+
case XamlInflator.SourceGen:
67+
path = "AppResources/Colors.sgen.xaml";
68+
break;
69+
case XamlInflator.XamlC:
70+
path = "AppResources/Colors.xc.xaml";
71+
break;
72+
}
73+
3774
var asm = typeof(ResourceDictionaryWithSource).Assembly;
38-
var resourceId = XamlResourceIdAttribute.GetResourceIdForPath(asm, "AppResources/Colors.rtxc.xaml");
75+
var resourceId = XamlResourceIdAttribute.GetResourceIdForPath(asm, path);
3976
Assert.That(resourceId, Is.Not.Null);
4077
var type = XamlResourceIdAttribute.GetTypeForResourceId(asm, resourceId);
41-
Assert.That(type?.Name, Does.StartWith("__Type"), "xaml-comp default to true, this should have a type associated with it");
78+
Assert.That(type?.Name, Does.StartWith("__Type"), "We add a type for all RD without Class, this should have a type associated with it");
4279

43-
#if !FIXME_BEFORE_PUBLIC_RELEASE
44-
Assert.Fail(); //make sure to have the same default for sourcegen
45-
#endif
4680
}
4781

4882
[Test]

0 commit comments

Comments
 (0)