ResourceIncludeTests.cs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Controls;
  4. using Avalonia.Media;
  5. using Avalonia.UnitTests;
  6. using Xunit;
  7. namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions
  8. {
  9. public class ResourceIncludeTests : XamlTestBase
  10. {
  11. public class StaticResourceExtensionTests : XamlTestBase
  12. {
  13. [Fact]
  14. public void ResourceInclude_Loads_ResourceDictionary()
  15. {
  16. var documents = new[]
  17. {
  18. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @"
  19. <ResourceDictionary xmlns='https://github.com/avaloniaui'
  20. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  21. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  22. </ResourceDictionary>"),
  23. new RuntimeXamlLoaderDocument(@"
  24. <UserControl xmlns='https://github.com/avaloniaui'
  25. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  26. <UserControl.Resources>
  27. <ResourceDictionary>
  28. <ResourceDictionary.MergedDictionaries>
  29. <ResourceInclude Source='avares://Tests/Resource.xaml'/>
  30. </ResourceDictionary.MergedDictionaries>
  31. </ResourceDictionary>
  32. </UserControl.Resources>
  33. <Border Name='border' Background='{StaticResource brush}'/>
  34. </UserControl>")
  35. };
  36. using (StartWithResources())
  37. {
  38. var compiled = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
  39. var userControl = Assert.IsType<UserControl>(compiled[1]);
  40. var border = userControl.FindControl<Border>("border");
  41. var brush = (ISolidColorBrush)border.Background;
  42. Assert.Equal(0xff506070, brush.Color.ToUInt32());
  43. }
  44. }
  45. [Fact]
  46. public void Missing_ResourceKey_In_ResourceInclude_Does_Not_Cause_StackOverflow()
  47. {
  48. var app = Application.Current;
  49. var documents = new[]
  50. {
  51. new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Resource.xaml"), @"
  52. <ResourceDictionary xmlns='https://github.com/avaloniaui'
  53. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  54. <StaticResource x:Key='brush' ResourceKey='missing' />
  55. </ResourceDictionary>"),
  56. new RuntimeXamlLoaderDocument(app, @"
  57. <Application xmlns='https://github.com/avaloniaui'
  58. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  59. <Application.Resources>
  60. <ResourceDictionary>
  61. <ResourceDictionary.MergedDictionaries>
  62. <ResourceInclude Source='avares://Tests/Resource.xaml'/>
  63. </ResourceDictionary.MergedDictionaries>
  64. </ResourceDictionary>
  65. </Application.Resources>
  66. </Application>")
  67. };
  68. using (StartWithResources())
  69. {
  70. try
  71. {
  72. AvaloniaRuntimeXamlLoader.LoadGroup(documents);
  73. }
  74. catch (KeyNotFoundException)
  75. {
  76. }
  77. }
  78. }
  79. private IDisposable StartWithResources(params (string, string)[] assets)
  80. {
  81. var assetLoader = new MockAssetLoader(assets);
  82. var services = new TestServices(assetLoader: assetLoader);
  83. return UnitTestApplication.Start(services);
  84. }
  85. }
  86. }
  87. }