ControlTests_NameScope.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright (c) The Perspex 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 Perspex.Controls.Presenters;
  5. using Perspex.Controls.Templates;
  6. using Perspex.Rendering;
  7. using Perspex.Styling;
  8. using Xunit;
  9. namespace Perspex.Controls.UnitTests
  10. {
  11. public class ControlTests_NameScope
  12. {
  13. [Fact]
  14. public void Controls_Should_Register_With_NameScope()
  15. {
  16. var root = new TestRoot
  17. {
  18. Content = new Border
  19. {
  20. Name = "foo",
  21. Child = new Border
  22. {
  23. Name = "bar",
  24. }
  25. }
  26. };
  27. root.ApplyTemplate();
  28. ((ContentPresenter)root.Presenter).UpdateChild();
  29. Assert.Same(root.Find("foo"), root.Content);
  30. Assert.Same(root.Find("bar"), ((Border)root.Content).Child);
  31. }
  32. [Fact]
  33. public void Control_Should_Unregister_With_NameScope()
  34. {
  35. var root = new TestRoot
  36. {
  37. Content = new Border
  38. {
  39. Name = "foo",
  40. Child = new Border
  41. {
  42. Name = "bar",
  43. }
  44. }
  45. };
  46. root.ApplyTemplate();
  47. root.Content = null;
  48. root.Presenter.ApplyTemplate();
  49. Assert.Null(root.Find("foo"));
  50. Assert.Null(root.Find("bar"));
  51. }
  52. [Fact]
  53. public void Control_Should_Not_Register_With_Template_NameScope()
  54. {
  55. var root = new TestRoot
  56. {
  57. Content = new Border
  58. {
  59. Name = "foo",
  60. }
  61. };
  62. root.ApplyTemplate();
  63. Assert.Null(NameScope.GetNameScope((Control)root.Presenter).Find("foo"));
  64. }
  65. private class TestRoot : ContentControl, IRenderRoot, INameScope, IStyleRoot
  66. {
  67. private readonly NameScope _nameScope = new NameScope();
  68. public TestRoot()
  69. {
  70. Template = new FuncControlTemplate<TestRoot>(x => new ContentPresenter
  71. {
  72. Name = "PART_ContentPresenter",
  73. [!ContentPresenter.ContentProperty] = x[!ContentControl.ContentProperty],
  74. });
  75. }
  76. public event EventHandler<NameScopeEventArgs> Registered
  77. {
  78. add { _nameScope.Registered += value; }
  79. remove { _nameScope.Registered -= value; }
  80. }
  81. public event EventHandler<NameScopeEventArgs> Unregistered
  82. {
  83. add { _nameScope.Unregistered += value; }
  84. remove { _nameScope.Unregistered -= value; }
  85. }
  86. public IRenderQueueManager RenderQueueManager
  87. {
  88. get { throw new NotImplementedException(); }
  89. }
  90. public Point TranslatePointToScreen(Point p)
  91. {
  92. throw new NotImplementedException();
  93. }
  94. public void Register(string name, object element)
  95. {
  96. _nameScope.Register(name, element);
  97. }
  98. public object Find(string name)
  99. {
  100. return _nameScope.Find(name);
  101. }
  102. public void Unregister(string name)
  103. {
  104. _nameScope.Unregister(name);
  105. }
  106. }
  107. }
  108. }