| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Avalonia.Controls;
- using Avalonia.Markup.Xaml.Styling;
- using Avalonia.Markup.Xaml.XamlIl.Runtime;
- using Avalonia.Media;
- using Avalonia.Platform;
- using Avalonia.Styling;
- using Avalonia.Themes.Simple;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Markup.Xaml.UnitTests.Xaml;
- public class StyleIncludeTests
- {
- [Fact]
- public void StyleInclude_Is_Built()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow
- .With(theme: () => new Styles())))
- {
- var xaml = @"
- <ContentControl xmlns='https://github.com/avaloniaui'>
- <ContentControl.Styles>
- <StyleInclude Source='avares://Avalonia.Markup.Xaml.UnitTests/Xaml/Style1.xaml'/>
- </ContentControl.Styles>
- </ContentControl>";
- var window = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
-
- Assert.IsType<Style>(window.Styles[0]);
- }
- }
-
- [Fact]
- public void StyleInclude_Is_Built_Resources()
- {
- using (UnitTestApplication.Start(TestServices.StyledWindow
- .With(theme: () => new Styles())))
- {
- var xaml = @"
- <ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <ContentControl.Resources>
- <StyleInclude x:Key='Include' Source='avares://Avalonia.Markup.Xaml.UnitTests/Xaml/Style1.xaml'/>
- </ContentControl.Resources>
- </ContentControl>";
- var contentControl = AvaloniaRuntimeXamlLoader.Parse<ContentControl>(xaml);
- Assert.IsType<Style>(contentControl.Resources["Include"]);
- }
- }
- [Fact]
- public void StyleInclude_Is_Resolved_With_Two_Files()
- {
- var documents = new[]
- {
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Style.xaml"), @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style.Resources>
- <Color x:Key='Red'>Red</Color>
- </Style.Resources>
- </Style>"),
- new RuntimeXamlLoaderDocument(@"
- <ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <ContentControl.Resources>
- <StyleInclude x:Key='Include' Source='avares://Tests/Style.xaml'/>
- </ContentControl.Resources>
- </ContentControl>")
- };
-
- var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
- var style = Assert.IsType<Style>(objects[0]);
- var contentControl = Assert.IsType<ContentControl>(objects[1]);
- Assert.IsType<Style>(contentControl.Resources["Include"]);
- }
-
- [Fact]
- public void Relative_Back_StyleInclude_Is_Resolved_With_Two_Files()
- {
- var documents = new[]
- {
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Subfolder/Style.xaml"), @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style.Resources>
- <Color x:Key='Red'>Red</Color>
- </Style.Resources>
- </Style>"),
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Subfolder/Folder/Root.xaml"), @"
- <ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <ContentControl.Resources>
- <StyleInclude x:Key='Include' Source='../Style.xaml'/>
- </ContentControl.Resources>
- </ContentControl>")
- };
-
- var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
- var style = Assert.IsType<Style>(objects[0]);
- var contentControl = Assert.IsType<ContentControl>(objects[1]);
- Assert.IsType<Style>(contentControl.Resources["Include"]);
- }
-
- [Fact]
- public void Relative_Root_StyleInclude_Is_Resolved_With_Two_Files()
- {
- var documents = new[]
- {
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Style.xaml"), @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style.Resources>
- <Color x:Key='Red'>Red</Color>
- </Style.Resources>
- </Style>"),
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Root.xaml"), @"
- <ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <ContentControl.Resources>
- <StyleInclude x:Key='Include' Source='/Style.xaml'/>
- </ContentControl.Resources>
- </ContentControl>")
- };
-
- var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
- var style = Assert.IsType<Style>(objects[0]);
- var contentControl = Assert.IsType<ContentControl>(objects[1]);
- Assert.IsType<Style>(contentControl.Resources["Include"]);
- }
-
- [Fact]
- public void Relative_StyleInclude_Is_Resolved_With_Two_Files()
- {
- var documents = new[]
- {
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Style.xaml"), @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style.Resources>
- <Color x:Key='Red'>Red</Color>
- </Style.Resources>
- </Style>"),
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Root.xaml"), @"
- <ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <ContentControl.Resources>
- <StyleInclude x:Key='Include' Source='Style.xaml'/>
- </ContentControl.Resources>
- </ContentControl>")
- };
-
- var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
- var style = Assert.IsType<Style>(objects[0]);
- var contentControl = Assert.IsType<ContentControl>(objects[1]);
- Assert.IsType<Style>(contentControl.Resources["Include"]);
- }
-
- [Fact]
- public void Relative_Dot_Syntax__StyleInclude_Is_Resolved_With_Two_Files()
- {
- var documents = new[]
- {
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Style.xaml"), @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style.Resources>
- <Color x:Key='Red'>Red</Color>
- </Style.Resources>
- </Style>"),
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Folder/Root.xaml"), @"
- <ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <ContentControl.Resources>
- <StyleInclude x:Key='Include' Source='./Style.xaml'/>
- </ContentControl.Resources>
- </ContentControl>")
- };
-
- var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
- var style = Assert.IsType<Style>(objects[0]);
- var contentControl = Assert.IsType<ContentControl>(objects[1]);
- Assert.IsType<Style>(contentControl.Resources["Include"]);
- }
-
- [Fact]
- public void NonLatin_StyleInclude_Is_Resolved_With_Two_Files()
- {
- var documents = new[]
- {
- new RuntimeXamlLoaderDocument(new Uri("avares://アセンブリ/スタイル.xaml"), @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style.Resources>
- <Color x:Key='Red'>Red</Color>
- </Style.Resources>
- </Style>"),
- new RuntimeXamlLoaderDocument(@"
- <ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <ContentControl.Resources>
- <StyleInclude x:Key='Include' Source='avares://アセンブリ/スタイル.xaml'/>
- </ContentControl.Resources>
- </ContentControl>")
- };
-
- var objects = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
- var style = Assert.IsType<Style>(objects[0]);
- var contentControl = Assert.IsType<ContentControl>(objects[1]);
- Assert.IsType<Style>(contentControl.Resources["Include"]);
- }
-
- [Fact]
- public void Missing_ResourceKey_In_StyleInclude_Does_Not_Cause_StackOverflow()
- {
- var documents = new[]
- {
- new RuntimeXamlLoaderDocument(new Uri("avares://Tests/Style.xaml"), @"
- <Style xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <Style.Resources>
- <StaticResource x:Key='brush' ResourceKey='missing' />
- </Style.Resources>
- </Style>"),
- new RuntimeXamlLoaderDocument(@"
- <ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
- <ContentControl.Styles>
- <StyleInclude Source='avares://Tests/Style.xaml'/>
- </ContentControl.Styles>
- </ContentControl>")
- };
- try
- {
- _ = AvaloniaRuntimeXamlLoader.LoadGroup(documents);
- }
- catch (KeyNotFoundException)
- {
- }
- }
-
- [Fact]
- public void StyleInclude_Should_Be_Replaced_With_Direct_Call()
- {
- var control = (ContentControl)AvaloniaRuntimeXamlLoader.Load(@"
- <ContentControl xmlns='https://github.com/avaloniaui'
- xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
- xmlns:themes='clr-namespace:Avalonia.Themes.Simple;assembly=Avalonia.Themes.Simple'>
- <ContentControl.Styles>
- <themes:SimpleTheme />
- <StyleInclude Source='avares://Avalonia.Themes.Simple/SimpleTheme.xaml'/>
- </ContentControl.Styles>
- </ContentControl>");
- Assert.IsType<SimpleTheme>(control.Styles[0]);
- Assert.IsType<SimpleTheme>(control.Styles[1]);
- }
- [Fact]
- public void StyleInclude_From_CodeBehind_Resolves_Compiled()
- {
- using var locatorScope = AvaloniaLocator.EnterScope();
- AvaloniaLocator.CurrentMutable.BindToSelf<IAssetLoader>(new AssetLoader(GetType().Assembly));
-
- var sp = new TestServiceProvider();
- var styleInclude = new StyleInclude(sp)
- {
- Source = new Uri("avares://Avalonia.Markup.Xaml.UnitTests/Xaml/StyleWithServiceLocator.xaml")
- };
- var loaded = Assert.IsType<StyleWithServiceLocator>(styleInclude.Loaded);
-
- Assert.Equal(
- sp.GetService<IAvaloniaXamlIlParentStackProvider>().Parents,
- loaded.ServiceProvider.GetService<IAvaloniaXamlIlParentStackProvider>().Parents);
- }
- private class TestServiceProvider : IServiceProvider, IUriContext, IAvaloniaXamlIlParentStackProvider
- {
- private IServiceProvider _root = XamlIlRuntimeHelpers.CreateRootServiceProviderV2();
- public object GetService(Type serviceType)
- {
- if (serviceType == typeof(IUriContext))
- {
- return this;
- }
- if (serviceType == typeof(IAvaloniaXamlIlParentStackProvider))
- {
- return this;
- }
- return _root.GetService(serviceType);
- }
- public Uri BaseUri { get; set; }
- public IEnumerable<object> Parents { get; } = new[] { new ContentControl() };
- }
- }
|