BindingTests_Self.cs 2.1 KB

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