ControlTests_NameScope.cs 3.4 KB

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