| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- using System;
- using Avalonia.Controls;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Templates;
- using Avalonia.Media;
- using Avalonia.Media.Immutable;
- using Avalonia.Styling;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Markup.Xaml.UnitTests.Xaml
- {
- public class ResourceDictionaryTests : XamlTestBase
- {
- [Fact]
- public void StaticResource_Works_In_ResourceDictionary()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <ResourceDictionary xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Color x:Key='Red'>Red</Color>
- <SolidColorBrush x:Key='RedBrush' Color='{StaticResource Red}'/>
- </ResourceDictionary>";
- var resources = (ResourceDictionary)AvaloniaRuntimeXamlLoader.Load(xaml);
- var brush = (SolidColorBrush)resources["RedBrush"];
- Assert.Equal(Colors.Red, brush.Color);
- }
- }
- [Fact]
- public void DynamicResource_Finds_Resource_In_Parent_Dictionary()
- {
- var dictionaryXaml = @"
- <ResourceDictionary xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <SolidColorBrush x:Key='RedBrush' Color='{DynamicResource Red}'/>
- </ResourceDictionary>";
- using (StyledWindow(assets: ("test:dict.xaml", dictionaryXaml)))
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Window.Resources>
- <ResourceDictionary>
- <ResourceDictionary.MergedDictionaries>
- <ResourceInclude Source='test:dict.xaml'/>
- </ResourceDictionary.MergedDictionaries>
- </ResourceDictionary>
- <Color x:Key='Red'>Red</Color>
- </Window.Resources>
- <Button Name='button' Background='{DynamicResource RedBrush}'/>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var button = window.FindControl<Button>("button");
- var brush = Assert.IsType<SolidColorBrush>(button.Background);
- Assert.Equal(Colors.Red, brush.Color);
- window.Resources["Red"] = Colors.Green;
- Assert.Equal(Colors.Green, brush.Color);
- }
- }
- [Fact]
- public void Item_Is_Added_To_ResourceDictionary_As_Deferred()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <ResourceDictionary xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <SolidColorBrush x:Key='Red' Color='Red' />
- </ResourceDictionary>";
- var resources = (ResourceDictionary)AvaloniaRuntimeXamlLoader.Load(xaml);
- Assert.True(resources.ContainsDeferredKey("Red"));
- }
- }
-
- [Fact]
- public void Item_Added_To_ResourceDictionary_Is_UnDeferred_On_Read()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <ResourceDictionary xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <SolidColorBrush x:Key='Red' Color='Red' />
- </ResourceDictionary>";
- var resources = (ResourceDictionary)AvaloniaRuntimeXamlLoader.Load(xaml);
- Assert.True(resources.ContainsDeferredKey("Red"));
- Assert.IsType<SolidColorBrush>(resources["Red"]);
-
- Assert.False(resources.ContainsDeferredKey("Red"));
- }
- }
- [Fact]
- public void Item_Is_Added_To_Window_Resources_As_Deferred()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Window.Resources>
- <SolidColorBrush x:Key='Red' Color='Red' />
- </Window.Resources>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var resources = (ResourceDictionary)window.Resources;
- Assert.True(resources.ContainsDeferredKey("Red"));
- }
- }
- [Fact]
- public void Item_Is_Added_To_Window_MergedDictionaries_As_Deferred()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Window.Resources>
- <ResourceDictionary>
- <ResourceDictionary.MergedDictionaries>
- <ResourceDictionary>
- <SolidColorBrush x:Key='Red' Color='Red' />
- </ResourceDictionary>
- </ResourceDictionary.MergedDictionaries>
- </ResourceDictionary>
- </Window.Resources>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var resources = (ResourceDictionary)window.Resources.MergedDictionaries[0];
- Assert.True(resources.ContainsDeferredKey("Red"));
- }
- }
- [Fact]
- public void Item_Is_Added_To_Style_Resources_As_Deferred()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style.Resources>
- <SolidColorBrush x:Key='Red' Color='Red' />
- </Style.Resources>
- </Style>";
- var style = (Style)AvaloniaRuntimeXamlLoader.Load(xaml);
- var resources = (ResourceDictionary)style.Resources;
- Assert.True(resources.ContainsDeferredKey("Red"));
- }
- }
- [Fact]
- public void Item_Is_Added_To_Styles_Resources_As_Deferred()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <Styles xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Styles.Resources>
- <SolidColorBrush x:Key='Red' Color='Red' />
- </Styles.Resources>
- </Styles>";
- var style = (Styles)AvaloniaRuntimeXamlLoader.Load(xaml);
- var resources = (ResourceDictionary)style.Resources;
- Assert.True(resources.ContainsDeferredKey("Red"));
- }
- }
- [Fact]
- public void Item_Can_Be_StaticReferenced_As_Deferred()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Window.Resources>
- <SolidColorBrush x:Key='Red' Color='Red' />
- </Window.Resources>
- <Button>
- <Button.Resources>
- <StaticResource x:Key='Red2' ResourceKey='Red' />
- </Button.Resources>
- </Button>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var windowResources = (ResourceDictionary)window.Resources;
- var buttonResources = (ResourceDictionary)((Button)window.Content!).Resources;
-
- Assert.True(windowResources.ContainsDeferredKey("Red"));
- Assert.True(buttonResources.ContainsDeferredKey("Red2"));
- }
- }
-
- [Fact]
- public void Item_StaticReferenced_Is_UnDeferred_On_Read()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <Window xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Window.Resources>
- <SolidColorBrush x:Key='Red' Color='Red' />
- </Window.Resources>
- <Button>
- <Button.Resources>
- <StaticResource x:Key='Red2' ResourceKey='Red' />
- </Button.Resources>
- </Button>
- </Window>";
- var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml);
- var windowResources = (ResourceDictionary)window.Resources;
- var buttonResources = (ResourceDictionary)((Button)window.Content!).Resources;
-
- Assert.IsType<SolidColorBrush>(buttonResources["Red2"]);
-
- Assert.False(windowResources.ContainsDeferredKey("Red"));
- Assert.False(buttonResources.ContainsDeferredKey("Red2"));
- }
- }
-
- [Fact]
- public void Value_Type_With_Parse_Converter_Should_Not_Be_Deferred()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <ResourceDictionary xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Color x:Key='Red'>Red</Color>
- </ResourceDictionary>";
- var resources = (ResourceDictionary)AvaloniaRuntimeXamlLoader.Load(xaml);
- Assert.False(resources.ContainsDeferredKey("Red"));
- Assert.IsType<Color>(resources["Red"]);
- }
- }
-
- [Fact]
- public void Value_Type_With_Ctor_Converter_Should_Not_Be_Deferred()
- {
- using (StyledWindow())
- {
- var xaml = @"
- <ResourceDictionary xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Thickness x:Key='Margin'>1 1 1 1</Thickness>
- </ResourceDictionary>";
- var resources = (ResourceDictionary)AvaloniaRuntimeXamlLoader.Load(xaml);
- Assert.False(resources.ContainsDeferredKey("Margin"));
- Assert.IsType<Thickness>(resources["Margin"]);
- }
- }
- private IDisposable StyledWindow(params (string, string)[] assets)
- {
- var services = TestServices.StyledWindow.With(
- assetLoader: new MockAssetLoader(assets),
- theme: () => new Styles
- {
- ResourceDictionaryTests.WindowStyle(),
- });
- return UnitTestApplication.Start(services);
- }
- private static Style WindowStyle()
- {
- return new Style(x => x.OfType<Window>())
- {
- Setters =
- {
- new Setter(
- Window.TemplateProperty,
- new FuncControlTemplate<Window>((x, scope) =>
- new ContentPresenter
- {
- Name = "PART_ContentPresenter",
- [!ContentPresenter.ContentProperty] = x[!Window.ContentProperty],
- }.RegisterInNameScope(scope)))
- }
- };
- }
- }
- }
|