BindingTests_Self.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using Moq;
  3. using Avalonia.Controls;
  4. using Avalonia.Data;
  5. using Avalonia.Styling;
  6. using Xunit;
  7. using System.Reactive.Disposables;
  8. using Avalonia.Markup.Data;
  9. namespace Avalonia.Markup.UnitTests.Data
  10. {
  11. public class BindingTests_Self
  12. {
  13. [Fact]
  14. public void Binding_To_Property_On_Self_Should_Work()
  15. {
  16. var target = new TextBlock
  17. {
  18. Tag = "Hello World!",
  19. [!TextBlock.TextProperty] = new Binding("Tag")
  20. {
  21. RelativeSource = new RelativeSource(RelativeSourceMode.Self)
  22. },
  23. };
  24. Assert.Equal("Hello World!", target.Text);
  25. }
  26. [Fact]
  27. public void TwoWay_Binding_To_Property_On_Self_Should_Work()
  28. {
  29. var target = new TextBlock
  30. {
  31. Tag = "Hello World!",
  32. [!TextBlock.TextProperty] = new Binding("Tag", BindingMode.TwoWay)
  33. {
  34. RelativeSource = new RelativeSource(RelativeSourceMode.Self)
  35. },
  36. };
  37. Assert.Equal("Hello World!", target.Text);
  38. target.Text = "Goodbye cruel world :(";
  39. Assert.Equal("Goodbye cruel world :(", target.Text);
  40. }
  41. private static Mock<IControl> CreateTarget(
  42. ITemplatedControl templatedParent = null,
  43. string text = null)
  44. {
  45. var result = new Mock<IControl>();
  46. result.Setup(x => x.GetValue(Control.TemplatedParentProperty)).Returns(templatedParent);
  47. result.Setup(x => x.GetValue((AvaloniaProperty)Control.TemplatedParentProperty)).Returns(templatedParent);
  48. result.Setup(x => x.GetValue((AvaloniaProperty)TextBox.TextProperty)).Returns(text);
  49. result.Setup(x => x.Bind(It.IsAny<AvaloniaProperty>(), It.IsAny<IObservable<object>>(), It.IsAny<BindingPriority>()))
  50. .Returns(Disposable.Empty);
  51. return result;
  52. }
  53. }
  54. }