|
| 1 | +// Copyright (c) The Avalonia Project. All rights reserved. |
| 2 | +// Licensed under the MIT license. See licence.md file in the project root for full license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using Avalonia.Controls; |
| 6 | +using Avalonia.Controls.Presenters; |
| 7 | +using Avalonia.Controls.Templates; |
| 8 | +using Avalonia.Media; |
| 9 | +using Avalonia.Styling; |
| 10 | +using Avalonia.UnitTests; |
| 11 | +using Xunit; |
| 12 | + |
| 13 | +namespace Avalonia.Markup.Xaml.UnitTests.Xaml |
| 14 | +{ |
| 15 | + public class ResourceDictionaryTests : XamlTestBase |
| 16 | + { |
| 17 | + [Fact] |
| 18 | + public void StaticResource_Works_In_ResourceDictionary() |
| 19 | + { |
| 20 | + using (StyledWindow()) |
| 21 | + { |
| 22 | + var xaml = @" |
| 23 | +<ResourceDictionary xmlns='https://github.com/avaloniaui' |
| 24 | + xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> |
| 25 | + <Color x:Key='Red'>Red</Color> |
| 26 | + <SolidColorBrush x:Key='RedBrush' Color='{StaticResource Red}'/> |
| 27 | +</ResourceDictionary>"; |
| 28 | + var loader = new AvaloniaXamlLoader(); |
| 29 | + var resources = (ResourceDictionary)loader.Load(xaml); |
| 30 | + var brush = (SolidColorBrush)resources["RedBrush"]; |
| 31 | + |
| 32 | + Assert.Equal(Colors.Red, brush.Color); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void DynamicResource_Works_In_ResourceDictionary() |
| 38 | + { |
| 39 | + using (StyledWindow()) |
| 40 | + { |
| 41 | + var xaml = @" |
| 42 | +<ResourceDictionary xmlns='https://github.com/avaloniaui' |
| 43 | + xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> |
| 44 | + <Color x:Key='Red'>Red</Color> |
| 45 | + <SolidColorBrush x:Key='RedBrush' Color='{DynamicResource Red}'/> |
| 46 | +</ResourceDictionary>"; |
| 47 | + var loader = new AvaloniaXamlLoader(); |
| 48 | + var resources = (ResourceDictionary)loader.Load(xaml); |
| 49 | + var brush = (SolidColorBrush)resources["RedBrush"]; |
| 50 | + |
| 51 | + Assert.Equal(Colors.Red, brush.Color); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void DynamicResource_Finds_Resource_In_Parent_Dictionary() |
| 57 | + { |
| 58 | + var dictionaryXaml = @" |
| 59 | +<ResourceDictionary xmlns='https://github.com/avaloniaui' |
| 60 | + xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> |
| 61 | + <SolidColorBrush x:Key='RedBrush' Color='{DynamicResource Red}'/> |
| 62 | +</ResourceDictionary>"; |
| 63 | + |
| 64 | + using (StyledWindow(assets: ("test:dict.xaml", dictionaryXaml))) |
| 65 | + { |
| 66 | + var xaml = @" |
| 67 | +<Window xmlns='https://github.com/avaloniaui' |
| 68 | + xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'> |
| 69 | + <Window.Resources> |
| 70 | + <ResourceDictionary> |
| 71 | + <ResourceDictionary.MergedDictionaries> |
| 72 | + <ResourceInclude Source='test:dict.xaml'/> |
| 73 | + </ResourceDictionary.MergedDictionaries> |
| 74 | + </ResourceDictionary> |
| 75 | + <Color x:Key='Red'>Red</Color> |
| 76 | + </Window.Resources> |
| 77 | + <Button Name='button' Background='{DynamicResource RedBrush}'/> |
| 78 | +</Window>"; |
| 79 | + |
| 80 | + var loader = new AvaloniaXamlLoader(); |
| 81 | + var window = (Window)loader.Load(xaml); |
| 82 | + var button = window.FindControl<Button>("button"); |
| 83 | + |
| 84 | + var brush = Assert.IsType<SolidColorBrush>(button.Background); |
| 85 | + Assert.Equal(Colors.Red, brush.Color); |
| 86 | + |
| 87 | + window.Resources["Red"] = Colors.Green; |
| 88 | + |
| 89 | + Assert.Equal(Colors.Green, brush.Color); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private IDisposable StyledWindow(params (string, string)[] assets) |
| 94 | + { |
| 95 | + var services = TestServices.StyledWindow.With( |
| 96 | + assetLoader: new MockAssetLoader(assets), |
| 97 | + theme: () => new Styles |
| 98 | + { |
| 99 | + WindowStyle(), |
| 100 | + }); |
| 101 | + |
| 102 | + return UnitTestApplication.Start(services); |
| 103 | + } |
| 104 | + |
| 105 | + private Style WindowStyle() |
| 106 | + { |
| 107 | + return new Style(x => x.OfType<Window>()) |
| 108 | + { |
| 109 | + Setters = |
| 110 | + { |
| 111 | + new Setter( |
| 112 | + Window.TemplateProperty, |
| 113 | + new FuncControlTemplate<Window>((x, scope) => |
| 114 | + new ContentPresenter |
| 115 | + { |
| 116 | + Name = "PART_ContentPresenter", |
| 117 | + [!ContentPresenter.ContentProperty] = x[!Window.ContentProperty], |
| 118 | + }.RegisterInNameScope(scope))) |
| 119 | + } |
| 120 | + }; |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments