SetterTests.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. using System.Reactive.Subjects;
  4. using Moq;
  5. using Avalonia.Controls;
  6. using Avalonia.Data;
  7. using Xunit;
  8. using System;
  9. using Avalonia.Controls.Templates;
  10. namespace Avalonia.Styling.UnitTests
  11. {
  12. public class SetterTests
  13. {
  14. [Fact]
  15. public void Cannot_Assign_Control_To_Value()
  16. {
  17. var target = new Setter();
  18. Assert.Throws<ArgumentException>(() => target.Value = new Border());
  19. }
  20. [Fact]
  21. public void Setter_Should_Apply_Binding_To_Property()
  22. {
  23. var control = new TextBlock();
  24. var subject = new BehaviorSubject<object>("foo");
  25. var descriptor = new InstancedBinding(subject);
  26. var binding = Mock.Of<IBinding>(x => x.Initiate(control, TextBlock.TextProperty, null, false) == descriptor);
  27. var style = Mock.Of<IStyle>();
  28. var setter = new Setter(TextBlock.TextProperty, binding);
  29. setter.Apply(style, control, null);
  30. Assert.Equal("foo", control.Text);
  31. }
  32. [Fact]
  33. public void Setter_Should_Materialize_Template_To_Property()
  34. {
  35. var control = new Decorator();
  36. var template = new FuncTemplate<Canvas>(() => new Canvas());
  37. var style = Mock.Of<IStyle>();
  38. var setter = new Setter(Decorator.ChildProperty, template);
  39. setter.Apply(style, control, null);
  40. Assert.IsType<Canvas>(control.Child);
  41. }
  42. [Fact]
  43. public void Materializes_Template_Should_Be_NameScope()
  44. {
  45. var control = new Decorator();
  46. var template = new FuncTemplate<Canvas>(() => new Canvas());
  47. var style = Mock.Of<IStyle>();
  48. var setter = new Setter(Decorator.ChildProperty, template);
  49. setter.Apply(style, control, null);
  50. Assert.NotNull(NameScope.GetNameScope((Control)control.Child));
  51. }
  52. }
  53. }