ControlTests_NameScope.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Rendering;
  7. using Avalonia.Styling;
  8. using Avalonia.UnitTests;
  9. using Xunit;
  10. namespace Avalonia.Controls.UnitTests
  11. {
  12. public class ControlTests_NameScope
  13. {
  14. [Fact]
  15. public void Controls_Should_Register_With_NameScope()
  16. {
  17. var root = new TestRoot
  18. {
  19. Child = new Border
  20. {
  21. Name = "foo",
  22. Child = new Border
  23. {
  24. Name = "bar",
  25. }
  26. }
  27. };
  28. root.ApplyTemplate();
  29. Assert.Same(root.FindControl<Border>("foo"), root.Child);
  30. Assert.Same(root.FindControl<Border>("bar"), ((Border)root.Child).Child);
  31. }
  32. [Fact]
  33. public void Control_Should_Unregister_With_NameScope()
  34. {
  35. var root = new TestRoot
  36. {
  37. Child = new Border
  38. {
  39. Name = "foo",
  40. Child = new Border
  41. {
  42. Name = "bar",
  43. }
  44. }
  45. };
  46. root.Child = null;
  47. Assert.Null(root.FindControl<Border>("foo"));
  48. Assert.Null(root.FindControl<Border>("bar"));
  49. }
  50. [Fact]
  51. public void Control_Should_Not_Register_With_Template_NameScope()
  52. {
  53. var root = new TestTemplatedRoot
  54. {
  55. Content = new Border
  56. {
  57. Name = "foo",
  58. }
  59. };
  60. root.ApplyTemplate();
  61. Assert.Null(NameScope.GetNameScope((Control)root.Presenter).Find("foo"));
  62. }
  63. [Fact]
  64. public void Control_That_Is_NameScope_Should_Register_With_Parent_NameScope()
  65. {
  66. UserControl userControl;
  67. var root = new TestTemplatedRoot
  68. {
  69. Content = userControl = new UserControl
  70. {
  71. Name = "foo",
  72. }
  73. };
  74. root.ApplyTemplate();
  75. Assert.Same(userControl, root.FindControl<UserControl>("foo"));
  76. Assert.Same(userControl, userControl.FindControl<UserControl>("foo"));
  77. }
  78. }
  79. }