Skip to content

Commit

Permalink
Remove Reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
brminnick committed Jun 14, 2024
1 parent 6f5103e commit 9bcb6e7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 50 deletions.
24 changes: 2 additions & 22 deletions src/CommunityToolkit.Maui.UnitTests/Mocks/MockApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,8 @@ public MockApplication(IServiceProvider serviceProvider)
#pragma warning restore CS0612 // Type or member is obsolete
{
Services = serviceProvider;

InitializeSystemResources(new MockResourcesProvider());
}
#pragma warning disable CS0612 // Type or member is obsolete
void InitializeSystemResources(ISystemResourcesProvider resourcesProvider)
#pragma warning restore CS0612 // Type or member is obsolete
{
const string privateFieldName = "_systemResources";

if (typeof(Application).GetField(privateFieldName, BindingFlags.NonPublic | BindingFlags.Instance) is not FieldInfo systemResourcesFieldInfo)
{
throw new InvalidOperationException($"Unable to access {privateFieldName}");
}

// .NET MAUI's SystemResources initialization: https://github.com/dotnet/maui/blob/79695fbb7ba6517a334c795ecf0a1d6358ef309a/src/Controls/src/Core/Application/Application.cs#L42-L49
systemResourcesFieldInfo.SetValue(this, new Lazy<IResourceDictionary>(() =>
{
var systemResources = resourcesProvider.GetSystemResources();
systemResources.ValuesChanged += OnParentResourcesChanged;
return systemResources;
}));


DependencyService.Register<ISystemResourcesProvider, MockResourcesProvider>();
}
}

Expand Down
30 changes: 2 additions & 28 deletions src/CommunityToolkit.Maui.UnitTests/Mocks/MockResourcesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,7 @@ namespace CommunityToolkit.Maui.UnitTests.Mocks;
class MockResourcesProvider : ISystemResourcesProvider
#pragma warning restore CS0612 // Type or member is obsolete
{
public IResourceDictionary GetSystemResources()
{
var dictionary = new ResourceDictionary();
readonly ResourceDictionary dictionary = new();

Style style = new Style(typeof(Label));
dictionary[Device.Styles.BodyStyleKey] = style;

style = new Style(typeof(Label));
style.Setters.Add(Label.FontSizeProperty, 50);
dictionary[Device.Styles.TitleStyleKey] = style;

style = new Style(typeof(Label));
style.Setters.Add(Label.FontSizeProperty, 40);
dictionary[Device.Styles.SubtitleStyleKey] = style;

style = new Style(typeof(Label));
style.Setters.Add(Label.FontSizeProperty, 30);
dictionary[Device.Styles.CaptionStyleKey] = style;

style = new Style(typeof(Label));
style.Setters.Add(Label.FontSizeProperty, 20);
dictionary[Device.Styles.ListItemTextStyleKey] = style;

style = new Style(typeof(Label));
style.Setters.Add(Label.FontSizeProperty, 10);
dictionary[Device.Styles.ListItemDetailTextStyleKey] = style;

return dictionary;
}
public IResourceDictionary GetSystemResources() => dictionary;
}

0 comments on commit 9bcb6e7

Please sign in to comment.