ResourceIncludeTests.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using Avalonia.Controls;
  3. using Avalonia.Media;
  4. using Avalonia.UnitTests;
  5. using Xunit;
  6. namespace Avalonia.Markup.Xaml.UnitTests.Data
  7. {
  8. public class ResourceIncludeTests
  9. {
  10. public class StaticResourceExtensionTests
  11. {
  12. [Fact]
  13. public void ResourceInclude_Loads_ResourceDictionary()
  14. {
  15. var includeXaml = @"
  16. <ResourceDictionary xmlns='https://github.com/avaloniaui'
  17. xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
  18. <SolidColorBrush x:Key='brush'>#ff506070</SolidColorBrush>
  19. </ResourceDictionary>
  20. ";
  21. using (StartWithResources(("test:include.xaml", includeXaml)))
  22. {
  23. var xaml = @"
  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='test:include.xaml'/>
  30. </ResourceDictionary.MergedDictionaries>
  31. </ResourceDictionary>
  32. </UserControl.Resources>
  33. <Border Name='border' Background='{StaticResource brush}'/>
  34. </UserControl>";
  35. var loader = new AvaloniaXamlLoader();
  36. var userControl = (UserControl)loader.Load(xaml);
  37. var border = userControl.FindControl<Border>("border");
  38. var brush = (SolidColorBrush)border.Background;
  39. Assert.Equal(0xff506070, brush.Color.ToUint32());
  40. }
  41. }
  42. private IDisposable StartWithResources(params (string, string)[] assets)
  43. {
  44. var assetLoader = new MockAssetLoader(assets);
  45. var services = new TestServices(assetLoader: assetLoader);
  46. return UnitTestApplication.Start(services);
  47. }
  48. }
  49. }
  50. }