| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using System;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Templates;
- using Avalonia.Styling;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Controls.UnitTests
- {
- public class ControlTests_Resources
- {
- [Fact]
- public void FindResource_Should_Find_Control_Resource()
- {
- var target = new Control
- {
- Resources =
- {
- { "foo", "foo-value" },
- }
- };
- Assert.Equal("foo-value", target.FindResource("foo"));
- }
- [Fact]
- public void FindResource_Should_Find_Control_Resource_In_Parent()
- {
- Control target;
- var root = new Decorator
- {
- Resources =
- {
- { "foo", "foo-value" },
- },
- Child = target = new Control(),
- };
- Assert.Equal("foo-value", target.FindResource("foo"));
- }
- [Fact]
- public void FindResource_Should_Find_Application_Resource()
- {
- Control target;
- var app = new Application
- {
- Resources =
- {
- { "foo", "foo-value" },
- },
- };
- var root = new TestRoot
- {
- Child = target = new Control(),
- StylingParent = app,
- };
- Assert.Equal("foo-value", target.FindResource("foo"));
- }
- [Fact]
- public void FindResource_Should_Find_Style_Resource()
- {
- var target = new Control
- {
- Styles =
- {
- new Style
- {
- Resources =
- {
- { "foo", "foo-value" },
- }
- }
- },
- Resources =
- {
- { "bar", "bar-value" },
- },
- };
- Assert.Equal("foo-value", target.FindResource("foo"));
- }
- [Fact]
- public void FindResource_Should_Find_Styles_Resource()
- {
- var target = new Control
- {
- Styles =
- {
- new Styles
- {
- Resources =
- {
- { "foo", "foo-value" },
- }
- }
- },
- Resources =
- {
- { "bar", "bar-value" },
- },
- };
- Assert.Equal("foo-value", target.FindResource("foo"));
- }
- [Fact]
- public void FindResource_Should_Find_Application_Style_Resource()
- {
- Control target;
- var app = new Application
- {
- Styles =
- {
- new Style
- {
- Resources =
- {
- { "foo", "foo-value" },
- },
- }
- },
- Resources =
- {
- { "bar", "bar-value" },
- },
- };
- var root = new TestRoot
- {
- Child = target = new Control(),
- StylingParent = app,
- };
- Assert.Equal("foo-value", target.FindResource("foo"));
- }
- [Fact]
- public void Adding_Resource_Should_Call_Raise_ResourceChanged_On_Logical_Children()
- {
- Border child;
- var target = new ContentControl
- {
- Content = child = new Border(),
- Template = ContentControlTemplate(),
- };
- var raisedOnTarget = false;
- var raisedOnPresenter = false;
- var raisedOnChild = false;
- target.Measure(Size.Infinity);
- target.ResourcesChanged += (_, __) => raisedOnTarget = true;
- target.Presenter.ResourcesChanged += (_, __) => raisedOnPresenter = true;
- child.ResourcesChanged += (_, __) => raisedOnChild = true;
- target.Resources.Add("foo", "bar");
- Assert.True(raisedOnTarget);
- Assert.False(raisedOnPresenter);
- Assert.True(raisedOnChild);
- }
- [Fact]
- public void Adding_Resource_To_Styles_Should_Raise_ResourceChanged()
- {
- var target = new Decorator();
- var raised = false;
- target.ResourcesChanged += (_, __) => raised = true;
- target.Styles.Resources.Add("foo", "bar");
- Assert.True(raised);
- }
- [Fact]
- public void Adding_Resource_To_Nested_Style_Should_Raise_ResourceChanged()
- {
- Style style;
- var target = new Decorator
- {
- Styles =
- {
- (style = new Style()),
- }
- };
- var raised = false;
- target.ResourcesChanged += (_, __) => raised = true;
- style.Resources.Add("foo", "bar");
- Assert.True(raised);
- }
- private IControlTemplate ContentControlTemplate()
- {
- return new FuncControlTemplate<ContentControl>(x =>
- new ContentPresenter
- {
- Name = "PART_ContentPresenter",
- [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
- });
- }
- }
- }
|