ContentControlMixinTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.Collections.Generic;
  4. using System.Linq;
  5. using Avalonia.Collections;
  6. using Avalonia.Controls.Mixins;
  7. using Avalonia.Controls.Presenters;
  8. using Avalonia.Controls.Primitives;
  9. using Avalonia.Controls.Templates;
  10. using Avalonia.LogicalTree;
  11. using Moq;
  12. using Xunit;
  13. namespace Avalonia.Controls.UnitTests.Mixins
  14. {
  15. public class ContentControlMixinTests
  16. {
  17. [Fact]
  18. public void Multiple_Mixin_Usages_Should_Not_Throw()
  19. {
  20. var target = new TestControl()
  21. {
  22. Template = new FuncControlTemplate((_, scope) => new Panel
  23. {
  24. Children =
  25. {
  26. new ContentPresenter {Name = "Content_1_Presenter"}.RegisterInNameScope(scope),
  27. new ContentPresenter {Name = "Content_2_Presenter"}.RegisterInNameScope(scope)
  28. }
  29. })
  30. };
  31. var ex = Record.Exception(() => target.ApplyTemplate());
  32. Assert.Null(ex);
  33. }
  34. [Fact]
  35. public void Replacing_Template_Releases_Events()
  36. {
  37. var p1 = new ContentPresenter { Name = "Content_1_Presenter" };
  38. var p2 = new ContentPresenter { Name = "Content_2_Presenter" };
  39. var target = new TestControl
  40. {
  41. Template = new FuncControlTemplate((_, scope) => new Panel
  42. {
  43. Children =
  44. {
  45. p1.RegisterInNameScope(scope),
  46. p2.RegisterInNameScope(scope)
  47. }
  48. })
  49. };
  50. target.ApplyTemplate();
  51. Control tc;
  52. p1.Content = tc = new Control();
  53. p1.UpdateChild();
  54. Assert.Contains(tc, target.GetLogicalChildren());
  55. p2.Content = tc = new Control();
  56. p2.UpdateChild();
  57. Assert.Contains(tc, target.GetLogicalChildren());
  58. target.Template = null;
  59. p1.Content = tc = new Control();
  60. p1.UpdateChild();
  61. Assert.DoesNotContain(tc, target.GetLogicalChildren());
  62. p2.Content = tc = new Control();
  63. p2.UpdateChild();
  64. Assert.DoesNotContain(tc, target.GetLogicalChildren());
  65. }
  66. private class TestControl : TemplatedControl
  67. {
  68. public static readonly StyledProperty<object> Content1Property =
  69. AvaloniaProperty.Register<TestControl, object>(nameof(Content1));
  70. public static readonly StyledProperty<object> Content2Property =
  71. AvaloniaProperty.Register<TestControl, object>(nameof(Content2));
  72. static TestControl()
  73. {
  74. ContentControlMixin.Attach<TestControl>(Content1Property, x => x.LogicalChildren, "Content_1_Presenter");
  75. ContentControlMixin.Attach<TestControl>(Content2Property, x => x.LogicalChildren, "Content_2_Presenter");
  76. }
  77. public object Content1
  78. {
  79. get { return GetValue(Content1Property); }
  80. set { SetValue(Content1Property, value); }
  81. }
  82. public object Content2
  83. {
  84. get { return GetValue(Content2Property); }
  85. set { SetValue(Content2Property, value); }
  86. }
  87. }
  88. }
  89. }