ResourceIncludeTests.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 includeXaml = @"
  17. <ResourceDictionary xmlns='https://github.com/avaloniaui'
  18. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  19. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  20. </ResourceDictionary>
  21. ";
  22. using (StaticResourceExtensionTests.StartWithResources(("test:include.xaml", includeXaml)))
  23. {
  24. var xaml = @"
  25. <UserControl xmlns='https://github.com/avaloniaui'
  26. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  27. <UserControl.Resources>
  28. <ResourceDictionary>
  29. <ResourceDictionary.MergedDictionaries>
  30. <ResourceInclude Source='test:include.xaml'/>
  31. </ResourceDictionary.MergedDictionaries>
  32. </ResourceDictionary>
  33. </UserControl.Resources>
  34. <Border Name='border' Background='{StaticResource brush}'/>
  35. </UserControl>";
  36. var userControl = (UserControl)AvaloniaRuntimeXamlLoader.Load(xaml);
  37. var border = userControl.FindControl<Border>("border");
  38. var brush = (ISolidColorBrush)border.Background;
  39. Assert.Equal(0xff506070, brush.Color.ToUint32());
  40. }
  41. }
  42. [Fact]
  43. public void Missing_ResourceKey_In_ResourceInclude_Does_Not_Cause_StackOverflow()
  44. {
  45. var styleXaml = @"
  46. <ResourceDictionary xmlns='https://github.com/avaloniaui'
  47. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  48. <StaticResource x:Key='brush' ResourceKey='missing' />
  49. </ResourceDictionary>";
  50. using (StaticResourceExtensionTests.StartWithResources(("test:style.xaml", styleXaml)))
  51. {
  52. var xaml = @"
  53. <Application xmlns='https://github.com/avaloniaui'
  54. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  55. <Application.Resources>
  56. <ResourceDictionary>
  57. <ResourceDictionary.MergedDictionaries>
  58. <ResourceInclude Source='test:style.xaml'/>
  59. </ResourceDictionary.MergedDictionaries>
  60. </ResourceDictionary>
  61. </Application.Resources>
  62. </Application>";
  63. var app = Application.Current;
  64. try
  65. {
  66. AvaloniaRuntimeXamlLoader.Load(xaml, null, app);
  67. }
  68. catch (KeyNotFoundException)
  69. {
  70. }
  71. }
  72. }
  73. private static IDisposable StartWithResources(params (string, string)[] assets)
  74. {
  75. var assetLoader = new MockAssetLoader(assets);
  76. var services = new TestServices(assetLoader: assetLoader);
  77. return UnitTestApplication.Start(services);
  78. }
  79. }
  80. }
  81. }